Optimize generated java code
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Fix eager instantiation with special other annotation
Dieser Commit ist enthalten in:
yoyosource 2022-09-25 16:54:16 +02:00
Ursprung bc7ae5cf9a
Commit 0c68dfd19d
2 geänderte Dateien mit 19 neuen und 6 gelöschten Zeilen

Datei anzeigen

@ -115,10 +115,10 @@ public class LinkageProcessor extends AbstractProcessor {
Writer writer = processingEnv.getFiler().createSourceFile("de.steamwar." + name + ".linkage.LinkageUtils").openWriter(); Writer writer = processingEnv.getFiler().createSourceFile("de.steamwar." + name + ".linkage.LinkageUtils").openWriter();
BuildPlan buildPlan = new BuildPlan(packageName, className); BuildPlan buildPlan = new BuildPlan(packageName, className);
buildPlan.addImport("de.steamwar.linkage.LinkageType");
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(Linked.class); Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(Linked.class);
Map<String, TypeElement> neededFields = new HashMap<>(); Map<String, TypeElement> neededFields = new HashMap<>();
Set<Runnable> fieldInjections = new HashSet<>();
for (Element element : elements) { for (Element element : elements) {
if (element.getKind() != ElementKind.CLASS) { if (element.getKind() != ElementKind.CLASS) {
continue; continue;
@ -156,17 +156,25 @@ public class LinkageProcessor extends AbstractProcessor {
TypeElement fieldType = (TypeElement) ((DeclaredType) variableElement.asType()).asElement(); TypeElement fieldType = (TypeElement) ((DeclaredType) variableElement.asType()).asElement();
neededFields.put(fieldType.getQualifiedName().toString(), fieldType); neededFields.put(fieldType.getQualifiedName().toString(), fieldType);
fieldInjections.add(() -> {
specialElements(typeElement, buildPlan, buildPlan::addStaticLine, () -> { specialElements(typeElement, buildPlan, buildPlan::addStaticLine, () -> {
buildPlan.addStaticLine(getElement(typeElement, neededFields) + "." + variableElement.getSimpleName().toString() + " = " + getElement((TypeElement) ((DeclaredType) variableElement.asType()).asElement(), neededFields) + ";"); buildPlan.addStaticLine(getElement(typeElement, neededFields) + "." + variableElement.getSimpleName().toString() + " = " + getElement((TypeElement) ((DeclaredType) variableElement.asType()).asElement(), neededFields) + ";");
}); });
});
} }
} }
neededFields.forEach((s, typeElement) -> { neededFields.forEach((s, typeElement) -> {
buildPlan.addImport(typeElement.getQualifiedName().toString()); buildPlan.addImport(typeElement.getQualifiedName().toString());
String t = typeElement.getSimpleName().toString(); String t = typeElement.getSimpleName().toString();
t = t.substring(0, 1).toLowerCase() + t.substring(1); 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<String, MethodBuilder> methods = new HashMap<>(); Map<String, MethodBuilder> methods = new HashMap<>();
for (Element element : elements) { for (Element element : elements) {

Datei anzeigen

@ -79,7 +79,12 @@ public class Listener_SPIGOT implements LinkageType {
}); });
String localInstance = "local" + typeElement.getSimpleName().toString(); String localInstance = "local" + typeElement.getSimpleName().toString();
if (!instance.startsWith("new ")) {
localInstance = instance;
} else {
method.addLine(typeElement.getSimpleName() + " " + localInstance + " = " + instance + ";"); method.addLine(typeElement.getSimpleName() + " " + localInstance + " = " + instance + ";");
}
String finalLocalInstance = localInstance;
eventMethods.forEach((type, executableElement) -> { eventMethods.forEach((type, executableElement) -> {
AnnotationMirror eventHandler = executableElement.getAnnotationMirrors().stream().filter(annotationMirror -> annotationMirror.getAnnotationType().asElement().getSimpleName().toString().equals("EventHandler")).findFirst().orElse(null); AnnotationMirror eventHandler = executableElement.getAnnotationMirrors().stream().filter(annotationMirror -> annotationMirror.getAnnotationType().asElement().getSimpleName().toString().equals("EventHandler")).findFirst().orElse(null);
if (eventHandler == null) { if (eventHandler == null) {
@ -94,7 +99,7 @@ public class Listener_SPIGOT implements LinkageType {
ignoreCancelled = entry.getValue().getValue().toString(); 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 + ");");
}); });
} }
} }