Merge pull request 'Add feature disable' (#49) from LinkageFeatureDisable into master
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Reviewed-on: #49
Reviewed-by: Lixfel <lixfel@steamwar.de>
Dieser Commit ist enthalten in:
Lixfel 2023-04-11 11:18:53 +02:00
Commit e8d964d833
2 geänderte Dateien mit 50 neuen und 3 gelöschten Zeilen

Datei anzeigen

@ -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<String> 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<? 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();
BuildPlan buildPlan = new BuildPlan("de.steamwar." + name + ".linkage", className);
Set<TypeElement> disabledElements = new HashSet<>();
Set<TypeElement> 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<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();
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("}");
}
}

Datei anzeigen

@ -24,4 +24,5 @@ import java.lang.annotation.*;
@Retention(RetentionPolicy.SOURCE)
@Target({ElementType.TYPE})
public @interface Linked {
String feature() default "";
}