From f8e18ec79f8270d750b7ef51c0f07251537fc7b0 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 19 Sep 2022 09:01:05 +0200 Subject: [PATCH] Add MethodBuilder Add ParameterBuilder Add MinVersion --- src/de/steamwar/linkage/LinkageType.java | 4 +- src/de/steamwar/linkage/MinVersion.java | 27 +++++++++ src/de/steamwar/linkage/plan/BuildPlan.java | 25 +++++++- .../steamwar/linkage/plan/FieldBuilder.java | 6 +- .../steamwar/linkage/plan/MethodBuilder.java | 57 +++++++++++++++++++ .../linkage/plan/ParameterBuilder.java | 37 ++++++++++++ src/de/steamwar/linkage/types/Command.java | 4 +- 7 files changed, 155 insertions(+), 5 deletions(-) create mode 100644 src/de/steamwar/linkage/MinVersion.java create mode 100644 src/de/steamwar/linkage/plan/MethodBuilder.java create mode 100644 src/de/steamwar/linkage/plan/ParameterBuilder.java diff --git a/src/de/steamwar/linkage/LinkageType.java b/src/de/steamwar/linkage/LinkageType.java index 45baad6..98a4719 100644 --- a/src/de/steamwar/linkage/LinkageType.java +++ b/src/de/steamwar/linkage/LinkageType.java @@ -19,6 +19,8 @@ package de.steamwar.linkage; +import de.steamwar.linkage.plan.BuildPlan; + public interface LinkageType { // String name = new BufferedReader(new InputStreamReader(processingEnv.getFiler().getResource(StandardLocation.CLASS_OUTPUT, "", "plugin.yml").openInputStream())) @@ -36,5 +38,5 @@ public interface LinkageType { return null; } - String generateCode(String instance); + void generateCode(BuildPlan buildPlan, String instance); } diff --git a/src/de/steamwar/linkage/MinVersion.java b/src/de/steamwar/linkage/MinVersion.java new file mode 100644 index 0000000..6e396d7 --- /dev/null +++ b/src/de/steamwar/linkage/MinVersion.java @@ -0,0 +1,27 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.linkage; + +/** + * Only applicable in Spigot context. Will be ignored in other contexts. + */ +public @interface MinVersion { + int value(); +} diff --git a/src/de/steamwar/linkage/plan/BuildPlan.java b/src/de/steamwar/linkage/plan/BuildPlan.java index a1d8795..97c9aee 100644 --- a/src/de/steamwar/linkage/plan/BuildPlan.java +++ b/src/de/steamwar/linkage/plan/BuildPlan.java @@ -34,9 +34,26 @@ public class BuildPlan implements Writer { private final String className; private Map, FieldBuilder> fieldBuilderMap = new HashMap<>(); + private Map methodBuilderMap = new HashMap<>(); public void addField(FieldBuilder fieldBuilder) { - fieldBuilderMap.put(fieldBuilder.getType(), fieldBuilder); + fieldBuilderMap.putIfAbsent(fieldBuilder.getType(), fieldBuilder); + } + + public boolean hasField(Class type) { + return fieldBuilderMap.containsKey(type); + } + + public String getFieldName(Class type) { + return fieldBuilderMap.get(type).getFieldName(); + } + + public void addMethod(MethodBuilder methodBuilder) { + methodBuilderMap.put(methodBuilder.getMethodName(), methodBuilder); + } + + public boolean hasMethod(String methodName) { + return methodBuilderMap.containsKey(methodName); } @Override @@ -47,6 +64,12 @@ public class BuildPlan implements Writer { for (FieldBuilder fieldBuilder : fieldBuilderMap.values()) { fieldBuilder.write(writer); } + if (!fieldBuilderMap.isEmpty() && !methodBuilderMap.isEmpty()) { + writer.write("\n"); + } + for (MethodBuilder methodBuilder : methodBuilderMap.values()) { + methodBuilder.write(writer); + } writer.write("}\n"); } } diff --git a/src/de/steamwar/linkage/plan/FieldBuilder.java b/src/de/steamwar/linkage/plan/FieldBuilder.java index 8b2379a..cb67b50 100644 --- a/src/de/steamwar/linkage/plan/FieldBuilder.java +++ b/src/de/steamwar/linkage/plan/FieldBuilder.java @@ -31,8 +31,12 @@ public class FieldBuilder implements Writer { @Getter private Class type; + public String getFieldName() { + return type.getSimpleName(); + } + @Override public void write(BufferedWriter writer) throws IOException { - writer.write(" private static " + type.getTypeName() + " " + type.getSimpleName() + ";"); + writer.write(" private static " + type.getTypeName() + " " + getFieldName() + ";\n"); } } diff --git a/src/de/steamwar/linkage/plan/MethodBuilder.java b/src/de/steamwar/linkage/plan/MethodBuilder.java new file mode 100644 index 0000000..e1a0244 --- /dev/null +++ b/src/de/steamwar/linkage/plan/MethodBuilder.java @@ -0,0 +1,57 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.linkage.plan; + +import de.steamwar.linkage.Writer; +import lombok.RequiredArgsConstructor; + +import java.io.BufferedWriter; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +@RequiredArgsConstructor +public class MethodBuilder implements Writer { + + private final String name; + private final Class returnType; + private List parameters = new ArrayList<>(); + private List lines = new ArrayList<>(); + + public String getMethodName() { + return name; + } + + @Override + public void write(BufferedWriter writer) throws IOException { + writer.write(" public static " + returnType.getTypeName() + " " + getMethodName() + "("); + for (int i = 0; i < parameters.size(); i++) { + parameters.get(i).write(writer); + if (i < parameters.size() - 1) { + writer.write(", "); + } + } + writer.write(") {"); + for (String line : lines) { + writer.write(" " + line); + } + writer.write(" }\n"); + } +} diff --git a/src/de/steamwar/linkage/plan/ParameterBuilder.java b/src/de/steamwar/linkage/plan/ParameterBuilder.java new file mode 100644 index 0000000..d856726 --- /dev/null +++ b/src/de/steamwar/linkage/plan/ParameterBuilder.java @@ -0,0 +1,37 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.linkage.plan; + +import de.steamwar.linkage.Writer; +import lombok.AllArgsConstructor; + +import java.io.BufferedWriter; +import java.io.IOException; + +@AllArgsConstructor +public class ParameterBuilder implements Writer { + private Class type; + private String name; + + @Override + public void write(BufferedWriter writer) throws IOException { + writer.write(type.getTypeName() + " " + name); + } +} diff --git a/src/de/steamwar/linkage/types/Command.java b/src/de/steamwar/linkage/types/Command.java index e4eba15..c7ea544 100644 --- a/src/de/steamwar/linkage/types/Command.java +++ b/src/de/steamwar/linkage/types/Command.java @@ -21,6 +21,7 @@ package de.steamwar.linkage.types; import de.steamwar.command.AbstractSWCommand; import de.steamwar.linkage.LinkageType; +import de.steamwar.linkage.plan.BuildPlan; public class Command implements LinkageType { @@ -30,7 +31,6 @@ public class Command implements LinkageType { } @Override - public String generateCode(String instance) { - return null; + public void generateCode(BuildPlan buildPlan, String instance) { } }