diff --git a/src/de/steamwar/linkage/LinkageProcessor.java b/src/de/steamwar/linkage/LinkageProcessor.java index bc93b3d..5e34d44 100644 --- a/src/de/steamwar/linkage/LinkageProcessor.java +++ b/src/de/steamwar/linkage/LinkageProcessor.java @@ -115,10 +115,10 @@ public class LinkageProcessor extends AbstractProcessor { Writer writer = processingEnv.getFiler().createSourceFile("de.steamwar." + name + ".linkage.LinkageUtils").openWriter(); BuildPlan buildPlan = new BuildPlan(packageName, className); - buildPlan.addImport("de.steamwar.linkage.LinkageType"); Set elements = roundEnv.getElementsAnnotatedWith(Linked.class); Map neededFields = new HashMap<>(); + Set fieldInjections = new HashSet<>(); for (Element element : elements) { if (element.getKind() != ElementKind.CLASS) { continue; @@ -156,8 +156,10 @@ public class LinkageProcessor extends AbstractProcessor { TypeElement fieldType = (TypeElement) ((DeclaredType) variableElement.asType()).asElement(); neededFields.put(fieldType.getQualifiedName().toString(), fieldType); - specialElements(typeElement, buildPlan, buildPlan::addStaticLine, () -> { - buildPlan.addStaticLine(getElement(typeElement, neededFields) + "." + variableElement.getSimpleName().toString() + " = " + getElement((TypeElement) ((DeclaredType) variableElement.asType()).asElement(), neededFields) + ";"); + fieldInjections.add(() -> { + specialElements(typeElement, buildPlan, buildPlan::addStaticLine, () -> { + buildPlan.addStaticLine(getElement(typeElement, neededFields) + "." + variableElement.getSimpleName().toString() + " = " + getElement((TypeElement) ((DeclaredType) variableElement.asType()).asElement(), neededFields) + ";"); + }); }); } } @@ -165,8 +167,14 @@ public class LinkageProcessor extends AbstractProcessor { buildPlan.addImport(typeElement.getQualifiedName().toString()); String t = typeElement.getSimpleName().toString(); t = t.substring(0, 1).toLowerCase() + t.substring(1); - buildPlan.addField(new FieldBuilder(typeElement.getSimpleName().toString(), t, "new " + typeElement.getSimpleName().toString() + "()")); + buildPlan.addField(new FieldBuilder(typeElement.getSimpleName().toString(), t)); + + String finalT = t; + specialElements(typeElement, buildPlan, buildPlan::addStaticLine, () -> { + buildPlan.addStaticLine(finalT + " = new " + typeElement.getSimpleName().toString() + "();"); + }); }); + fieldInjections.forEach(Runnable::run); Map methods = new HashMap<>(); for (Element element : elements) { diff --git a/src/de/steamwar/linkage/types/Listener_SPIGOT.java b/src/de/steamwar/linkage/types/Listener_SPIGOT.java index 9c235d7..3491d6d 100644 --- a/src/de/steamwar/linkage/types/Listener_SPIGOT.java +++ b/src/de/steamwar/linkage/types/Listener_SPIGOT.java @@ -79,7 +79,12 @@ public class Listener_SPIGOT implements LinkageType { }); String localInstance = "local" + typeElement.getSimpleName().toString(); - method.addLine(typeElement.getSimpleName() + " " + localInstance + " = " + instance + ";"); + if (!instance.startsWith("new ")) { + localInstance = instance; + } else { + method.addLine(typeElement.getSimpleName() + " " + localInstance + " = " + instance + ";"); + } + String finalLocalInstance = localInstance; eventMethods.forEach((type, executableElement) -> { AnnotationMirror eventHandler = executableElement.getAnnotationMirrors().stream().filter(annotationMirror -> annotationMirror.getAnnotationType().asElement().getSimpleName().toString().equals("EventHandler")).findFirst().orElse(null); if (eventHandler == null) { @@ -94,7 +99,7 @@ public class Listener_SPIGOT implements LinkageType { ignoreCancelled = entry.getValue().getValue().toString(); } } - method.addLine(type.getSimpleName().toString() + "(" + localInstance + ", " + localInstance + "::" + executableElement.getSimpleName().toString() + ", EventPriority." + priority + ", " + ignoreCancelled + ");"); + method.addLine(type.getSimpleName().toString() + "(" + finalLocalInstance + ", " + finalLocalInstance + "::" + executableElement.getSimpleName().toString() + ", EventPriority." + priority + ", " + ignoreCancelled + ");"); }); } }