Dieser Commit ist enthalten in:
Ursprung
36bb4db515
Commit
362eb8b6bb
@ -36,6 +36,7 @@ import javax.lang.model.element.VariableElement;
|
|||||||
import javax.lang.model.type.DeclaredType;
|
import javax.lang.model.type.DeclaredType;
|
||||||
import javax.lang.model.type.TypeMirror;
|
import javax.lang.model.type.TypeMirror;
|
||||||
import javax.tools.Diagnostic;
|
import javax.tools.Diagnostic;
|
||||||
|
import javax.tools.StandardLocation;
|
||||||
import java.io.*;
|
import java.io.*;
|
||||||
import java.lang.annotation.Annotation;
|
import java.lang.annotation.Annotation;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
@ -56,6 +57,8 @@ public class LinkageProcessor extends AbstractProcessor {
|
|||||||
private String name;
|
private String name;
|
||||||
private String className;
|
private String className;
|
||||||
|
|
||||||
|
private Set<String> disabledFeatures = new HashSet<>();
|
||||||
|
|
||||||
private Messager messager;
|
private Messager messager;
|
||||||
private boolean processed = false;
|
private boolean processed = false;
|
||||||
|
|
||||||
@ -75,6 +78,7 @@ public class LinkageProcessor extends AbstractProcessor {
|
|||||||
|
|
||||||
className = "LinkageUtils";
|
className = "LinkageUtils";
|
||||||
mainClass();
|
mainClass();
|
||||||
|
disabledFeatures();
|
||||||
}
|
}
|
||||||
|
|
||||||
@SneakyThrows
|
@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
|
@SneakyThrows
|
||||||
@Override
|
@Override
|
||||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||||
@ -111,9 +127,27 @@ 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("de.steamwar." + name + ".linkage", className);
|
BuildPlan buildPlan = new BuildPlan("de.steamwar." + name + ".linkage", className);
|
||||||
|
|
||||||
|
Set<TypeElement> disabledElements = new HashSet<>();
|
||||||
|
|
||||||
Set<TypeElement> elements = roundEnv.getElementsAnnotatedWith(Linked.class).stream()
|
Set<TypeElement> elements = roundEnv.getElementsAnnotatedWith(Linked.class).stream()
|
||||||
.filter(element -> element.getKind() == ElementKind.CLASS)
|
.filter(element -> element.getKind() == ElementKind.CLASS)
|
||||||
.map(TypeElement.class::cast)
|
.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()))
|
.peek(typeElement -> System.out.println("Found element: " + typeElement.getQualifiedName().toString()))
|
||||||
.collect(Collectors.toSet());
|
.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);
|
messager.printMessage(Diagnostic.Kind.ERROR, "Field " + variableElement.getSimpleName() + " must be non final", variableElement);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
neededFields.put(typeElement.getQualifiedName().toString(), typeElement);
|
|
||||||
TypeElement fieldType = (TypeElement) ((DeclaredType) variableElement.asType()).asElement();
|
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);
|
neededFields.put(fieldType.getQualifiedName().toString(), fieldType);
|
||||||
|
|
||||||
fieldInjections.add(() -> {
|
fieldInjections.add(() -> {
|
||||||
@ -159,6 +199,7 @@ public class LinkageProcessor extends AbstractProcessor {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
neededFields.forEach((s, typeElement) -> {
|
neededFields.forEach((s, typeElement) -> {
|
||||||
|
if (disabledElements.contains(typeElement)) return;
|
||||||
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);
|
||||||
@ -188,8 +229,13 @@ public class LinkageProcessor extends AbstractProcessor {
|
|||||||
return methodBuilder;
|
return methodBuilder;
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!entry.getKey().isEmpty()) method.addLine("if (" + String.join(" && ", entry.getKey()) + ") {");
|
boolean generated = false;
|
||||||
for (Map.Entry<TypeElement, List<LinkageType>> toGenerate : group.getValue().entrySet()) {
|
for (Map.Entry<TypeElement, List<LinkageType>> 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();
|
TypeElement typeElement = toGenerate.getKey();
|
||||||
String instance = getElement(typeElement, neededFields);
|
String instance = getElement(typeElement, neededFields);
|
||||||
if (toGenerate.getValue().size() > 1 && instance.startsWith("new ")) {
|
if (toGenerate.getValue().size() > 1 && instance.startsWith("new ")) {
|
||||||
@ -202,7 +248,7 @@ public class LinkageProcessor extends AbstractProcessor {
|
|||||||
linkageType.generateCode(buildPlan, method, finalInstance, typeElement);
|
linkageType.generateCode(buildPlan, method, finalInstance, typeElement);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
if (!entry.getKey().isEmpty()) method.addLine("}");
|
if (generated && !entry.getKey().isEmpty()) method.addLine("}");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -24,4 +24,5 @@ import java.lang.annotation.*;
|
|||||||
@Retention(RetentionPolicy.SOURCE)
|
@Retention(RetentionPolicy.SOURCE)
|
||||||
@Target({ElementType.TYPE})
|
@Target({ElementType.TYPE})
|
||||||
public @interface Linked {
|
public @interface Linked {
|
||||||
|
String feature() default "";
|
||||||
}
|
}
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren