From 860d0827703f634c8dfcb1c43ec93b544c37d25b Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 26 Sep 2022 15:58:06 +0200 Subject: [PATCH] Simplify LinkageProcessor --- src/de/steamwar/linkage/LinkageProcessor.java | 40 +++++++++---------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/src/de/steamwar/linkage/LinkageProcessor.java b/src/de/steamwar/linkage/LinkageProcessor.java index 5e34d44..70253df 100644 --- a/src/de/steamwar/linkage/LinkageProcessor.java +++ b/src/de/steamwar/linkage/LinkageProcessor.java @@ -28,7 +28,10 @@ import lombok.SneakyThrows; import javax.annotation.processing.*; import javax.lang.model.SourceVersion; -import javax.lang.model.element.*; +import javax.lang.model.element.ElementKind; +import javax.lang.model.element.Modifier; +import javax.lang.model.element.TypeElement; +import javax.lang.model.element.VariableElement; import javax.lang.model.type.DeclaredType; import javax.lang.model.type.TypeMirror; import javax.tools.Diagnostic; @@ -116,25 +119,23 @@ public class LinkageProcessor extends AbstractProcessor { BuildPlan buildPlan = new BuildPlan(packageName, className); - Set elements = roundEnv.getElementsAnnotatedWith(Linked.class); + Set elements = roundEnv.getElementsAnnotatedWith(Linked.class).stream() + .filter(element -> element.getKind() == ElementKind.CLASS) + .map(TypeElement.class::cast) + .collect(Collectors.toSet()); + Map neededFields = new HashMap<>(); Set fieldInjections = new HashSet<>(); - for (Element element : elements) { - if (element.getKind() != ElementKind.CLASS) { - continue; - } - TypeElement typeElement = (TypeElement) element; - Linked linked = element.getAnnotation(Linked.class); - if (linked == null) { - continue; - } + for (TypeElement typeElement : elements) { if (getLinkagesOfType(typeElement).size() > 1) { neededFields.put(typeElement.getQualifiedName().toString(), typeElement); } - List variableElements = typeElement.getEnclosedElements().stream().filter(e -> e.getKind() == ElementKind.FIELD).map(VariableElement.class::cast).filter(e -> { - return e.getAnnotation(LinkedInstance.class) != null; - }).collect(Collectors.toList()); + List variableElements = typeElement.getEnclosedElements().stream() + .filter(e -> e.getKind() == ElementKind.FIELD) + .map(VariableElement.class::cast) + .filter(e -> e.getAnnotation(LinkedInstance.class) != null) + .collect(Collectors.toList()); if (variableElements.isEmpty()) { continue; } @@ -177,12 +178,7 @@ public class LinkageProcessor extends AbstractProcessor { fieldInjections.forEach(Runnable::run); Map methods = new HashMap<>(); - for (Element element : elements) { - if (element.getKind() != ElementKind.CLASS) { - continue; - } - TypeElement typeElement = (TypeElement) element; - + for (TypeElement typeElement : elements) { System.out.println("Found element: " + typeElement.getQualifiedName().toString()); Map> linkages = getLinkagesOfType(typeElement); if (linkages.isEmpty()) { @@ -211,11 +207,11 @@ public class LinkageProcessor extends AbstractProcessor { } private String getElement(TypeElement typeElement, Map neededFields) { + String s = typeElement.getSimpleName().toString(); if (neededFields.containsKey(typeElement.getQualifiedName().toString())) { - String s = typeElement.getSimpleName().toString(); return s.substring(0, 1).toLowerCase() + s.substring(1); } - return "new " + typeElement.getSimpleName().toString() + "()"; + return "new " + s + "()"; } private void specialElements(TypeElement typeElement, BuildPlan buildPlan, Consumer stringConsumer, Runnable inner) {