From 362eb8b6bbd2db10bec0a77bde42bb771bf45f6c Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 3 Mar 2023 20:37:37 +0100 Subject: [PATCH] Add feature disable --- src/de/steamwar/linkage/LinkageProcessor.java | 52 +++++++++++++++++-- src/de/steamwar/linkage/Linked.java | 1 + 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/src/de/steamwar/linkage/LinkageProcessor.java b/src/de/steamwar/linkage/LinkageProcessor.java index 488f9bf..29382a5 100644 --- a/src/de/steamwar/linkage/LinkageProcessor.java +++ b/src/de/steamwar/linkage/LinkageProcessor.java @@ -36,6 +36,7 @@ import javax.lang.model.element.VariableElement; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeMirror; import javax.tools.Diagnostic; +import javax.tools.StandardLocation; import java.io.*; import java.lang.annotation.Annotation; import java.nio.file.Files; @@ -56,6 +57,8 @@ public class LinkageProcessor extends AbstractProcessor { private String name; private String className; + private Set disabledFeatures = new HashSet<>(); + private Messager messager; private boolean processed = false; @@ -75,6 +78,7 @@ public class LinkageProcessor extends AbstractProcessor { className = "LinkageUtils"; mainClass(); + disabledFeatures(); } @SneakyThrows @@ -102,6 +106,18 @@ public class LinkageProcessor extends AbstractProcessor { } } + @SneakyThrows + private void disabledFeatures() { + File file = new File(System.getProperty("user.dir"), "disabled-features.txt"); + if (!file.exists()) return; + @Cleanup BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file))); + reader.lines() + .map(String::trim) + .filter(line -> !line.isEmpty()) + .filter(line -> !line.startsWith("#")) + .forEach(disabledFeatures::add); + } + @SneakyThrows @Override public boolean process(Set annotations, RoundEnvironment roundEnv) { @@ -111,9 +127,27 @@ public class LinkageProcessor extends AbstractProcessor { Writer writer = processingEnv.getFiler().createSourceFile("de.steamwar." + name + ".linkage.LinkageUtils").openWriter(); BuildPlan buildPlan = new BuildPlan("de.steamwar." + name + ".linkage", className); + Set disabledElements = new HashSet<>(); + Set elements = roundEnv.getElementsAnnotatedWith(Linked.class).stream() .filter(element -> element.getKind() == ElementKind.CLASS) .map(TypeElement.class::cast) + .peek(element -> { + String featureName = element.getAnnotation(Linked.class).feature(); + if (featureName.isEmpty()) { + String tempName = element.getQualifiedName().toString(); + if (tempName.contains(".features.")) { + tempName = tempName.substring(tempName.indexOf(".features.") + 10); + featureName = tempName.substring(0, tempName.indexOf('.')); + } else { + tempName = tempName.substring(0, tempName.lastIndexOf('.')); + featureName = tempName.substring(tempName.lastIndexOf('.') + 1); + } + } + if (disabledFeatures.contains(featureName) || disabledFeatures.contains("*")) { + disabledElements.add(element); + } + }) .peek(typeElement -> System.out.println("Found element: " + typeElement.getQualifiedName().toString())) .collect(Collectors.toSet()); @@ -147,8 +181,14 @@ public class LinkageProcessor extends AbstractProcessor { messager.printMessage(Diagnostic.Kind.ERROR, "Field " + variableElement.getSimpleName() + " must be non final", variableElement); continue; } - neededFields.put(typeElement.getQualifiedName().toString(), typeElement); TypeElement fieldType = (TypeElement) ((DeclaredType) variableElement.asType()).asElement(); + if (disabledElements.contains(fieldType)) { + continue; + } + if (disabledElements.contains(typeElement)) { + continue; + } + neededFields.put(typeElement.getQualifiedName().toString(), typeElement); neededFields.put(fieldType.getQualifiedName().toString(), fieldType); fieldInjections.add(() -> { @@ -159,6 +199,7 @@ public class LinkageProcessor extends AbstractProcessor { } } neededFields.forEach((s, typeElement) -> { + if (disabledElements.contains(typeElement)) return; buildPlan.addImport(typeElement.getQualifiedName().toString()); String t = typeElement.getSimpleName().toString(); t = t.substring(0, 1).toLowerCase() + t.substring(1); @@ -188,8 +229,13 @@ public class LinkageProcessor extends AbstractProcessor { return methodBuilder; }); - if (!entry.getKey().isEmpty()) method.addLine("if (" + String.join(" && ", entry.getKey()) + ") {"); + boolean generated = false; for (Map.Entry> toGenerate : group.getValue().entrySet()) { + if (disabledElements.contains(toGenerate.getKey())) continue; + if (!generated && !entry.getKey().isEmpty()) { + method.addLine("if (" + String.join(" && ", entry.getKey()) + ") {"); + generated = true; + } TypeElement typeElement = toGenerate.getKey(); String instance = getElement(typeElement, neededFields); if (toGenerate.getValue().size() > 1 && instance.startsWith("new ")) { @@ -202,7 +248,7 @@ public class LinkageProcessor extends AbstractProcessor { linkageType.generateCode(buildPlan, method, finalInstance, typeElement); }); } - if (!entry.getKey().isEmpty()) method.addLine("}"); + if (generated && !entry.getKey().isEmpty()) method.addLine("}"); } } diff --git a/src/de/steamwar/linkage/Linked.java b/src/de/steamwar/linkage/Linked.java index f3836e0..dbaf0eb 100644 --- a/src/de/steamwar/linkage/Linked.java +++ b/src/de/steamwar/linkage/Linked.java @@ -24,4 +24,5 @@ import java.lang.annotation.*; @Retention(RetentionPolicy.SOURCE) @Target({ElementType.TYPE}) public @interface Linked { + String feature() default ""; } -- 2.39.2