From f161379b620c9960cd1f636af809481afb7d8b98 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 22 Sep 2022 14:53:42 +0200 Subject: [PATCH] Finalize min version checks --- src/de/steamwar/linkage/LinkageProcessor.java | 38 +++++++++++++------ 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/src/de/steamwar/linkage/LinkageProcessor.java b/src/de/steamwar/linkage/LinkageProcessor.java index 37b4529..6b59e3b 100644 --- a/src/de/steamwar/linkage/LinkageProcessor.java +++ b/src/de/steamwar/linkage/LinkageProcessor.java @@ -38,6 +38,7 @@ import javax.tools.Diagnostic; import java.io.*; import java.io.Writer; import java.util.*; +import java.util.function.Consumer; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -185,7 +186,9 @@ public class LinkageProcessor extends AbstractProcessor { TypeElement fieldType = (TypeElement) ((DeclaredType) variableElement.asType()).asElement(); neededFields.put(fieldType.getQualifiedName().toString(), fieldType); - buildPlan.addStaticLine(getElement(typeElement, neededFields) + "." + variableElement.getSimpleName().toString() + " = " + getElement((TypeElement) ((DeclaredType) variableElement.asType()).asElement(), neededFields) + ";"); + specialElements(typeElement, buildPlan, buildPlan::addStaticLine, () -> { + buildPlan.addStaticLine(getElement(typeElement, neededFields) + "." + variableElement.getSimpleName().toString() + " = " + getElement((TypeElement) ((DeclaredType) variableElement.asType()).asElement(), neededFields) + ";"); + }); } } neededFields.forEach((s, typeElement) -> { @@ -194,9 +197,11 @@ public class LinkageProcessor extends AbstractProcessor { MethodBuilder initializer = new MethodBuilder(typeElement.getSimpleName().toString(), typeElement.getSimpleName().toString()); initializer.setPrivate(true); - initializer.addLine("if (" + typeElement.getSimpleName().toString() + " == null) {"); - initializer.addLine(" " + typeElement.getSimpleName().toString() + " = new " + typeElement.getSimpleName().toString() + "();"); - initializer.addLine("}"); + specialElements(typeElement, buildPlan, initializer::addLine, () -> { + initializer.addLine("if (" + typeElement.getSimpleName().toString() + " == null) {"); + initializer.addLine(" " + typeElement.getSimpleName().toString() + " = new " + typeElement.getSimpleName().toString() + "();"); + initializer.addLine("}"); + }); initializer.addLine("return " + typeElement.getSimpleName().toString() + ";"); buildPlan.addMethod(initializer); }); @@ -207,7 +212,6 @@ public class LinkageProcessor extends AbstractProcessor { continue; } TypeElement typeElement = (TypeElement) element; - MinVersion minVersion = element.getAnnotation(MinVersion.class); System.out.println("Found element: " + typeElement.getQualifiedName().toString()); element.getAnnotationMirrors().stream() @@ -265,13 +269,9 @@ public class LinkageProcessor extends AbstractProcessor { buildPlan.addMethod(methodBuilder); return methodBuilder; }); - if (context == LinkageType.Context.SPIGOT && minVersion != null) { - method.addLine("if (de.steamwar.core.Core.getVersion() >= " + minVersion.value() + ") {"); - } - type.generateCode(buildPlan, method, getElement(typeElement, neededFields), typeElement); - if (context == LinkageType.Context.SPIGOT && minVersion != null) { - method.addLine("}"); - } + specialElements(typeElement, buildPlan, method::addLine, () -> { + type.generateCode(buildPlan, method, getElement(typeElement, neededFields), typeElement); + }); }); } @@ -287,4 +287,18 @@ public class LinkageProcessor extends AbstractProcessor { } return "new " + typeElement.getQualifiedName().toString() + "()"; } + + private void specialElements(TypeElement typeElement, BuildPlan buildPlan, Consumer stringConsumer, Runnable inner) { + if (context == LinkageType.Context.SPIGOT) { + MinVersion minVersion = typeElement.getAnnotation(MinVersion.class); + if (minVersion != null) { + buildPlan.addImport("de.steamwar.core.Core"); + stringConsumer.accept("if (Core.getVersion() >= " + minVersion.value() + ") {"); + } + inner.run(); + if (minVersion != null) stringConsumer.accept("}"); + } else { + inner.run(); + } + } }