Dieser Commit ist enthalten in:
Ursprung
1c7e9ad039
Commit
05f5d3f7b3
@ -23,11 +23,13 @@ import de.steamwar.linkage.plan.BuildPlan;
|
|||||||
import de.steamwar.linkage.plan.FieldBuilder;
|
import de.steamwar.linkage.plan.FieldBuilder;
|
||||||
import de.steamwar.linkage.plan.MethodBuilder;
|
import de.steamwar.linkage.plan.MethodBuilder;
|
||||||
import de.steamwar.linkage.plan.ParameterBuilder;
|
import de.steamwar.linkage.plan.ParameterBuilder;
|
||||||
import de.steamwar.linkage.types.ListenerLink;
|
|
||||||
import lombok.Getter;
|
import lombok.Getter;
|
||||||
import lombok.SneakyThrows;
|
import lombok.SneakyThrows;
|
||||||
|
|
||||||
import javax.annotation.processing.*;
|
import javax.annotation.processing.AbstractProcessor;
|
||||||
|
import javax.annotation.processing.Messager;
|
||||||
|
import javax.annotation.processing.ProcessingEnvironment;
|
||||||
|
import javax.annotation.processing.RoundEnvironment;
|
||||||
import javax.lang.model.SourceVersion;
|
import javax.lang.model.SourceVersion;
|
||||||
import javax.lang.model.element.*;
|
import javax.lang.model.element.*;
|
||||||
import javax.lang.model.type.DeclaredType;
|
import javax.lang.model.type.DeclaredType;
|
||||||
@ -40,17 +42,16 @@ import java.util.*;
|
|||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
@SupportedAnnotationTypes({"de.steamwar.linkage.Linked", "de.steamwar.linkage.Linked$Linkages"})
|
|
||||||
public class LinkageProcessor extends AbstractProcessor {
|
public class LinkageProcessor extends AbstractProcessor {
|
||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
private static LinkageType.Context context;
|
private static LinkageType.Context context;
|
||||||
|
|
||||||
|
private String name;
|
||||||
private String packageName;
|
private String packageName;
|
||||||
private String className;
|
private String className;
|
||||||
|
|
||||||
private Messager messager;
|
private Messager messager;
|
||||||
private Writer writer;
|
|
||||||
private boolean processed = false;
|
private boolean processed = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -58,6 +59,13 @@ public class LinkageProcessor extends AbstractProcessor {
|
|||||||
return SourceVersion.latestSupported();
|
return SourceVersion.latestSupported();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<String> getSupportedAnnotationTypes() {
|
||||||
|
return Stream.of(Linked.class, Linked.Linkages.class)
|
||||||
|
.map(Class::getCanonicalName)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
|
||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@Override
|
@Override
|
||||||
public synchronized void init(ProcessingEnvironment processingEnv) {
|
public synchronized void init(ProcessingEnvironment processingEnv) {
|
||||||
@ -68,9 +76,9 @@ public class LinkageProcessor extends AbstractProcessor {
|
|||||||
name = name.toLowerCase();
|
name = name.toLowerCase();
|
||||||
context = name.contains("bungee") ? LinkageType.Context.BUNGEE : LinkageType.Context.SPIGOT;
|
context = name.contains("bungee") ? LinkageType.Context.BUNGEE : LinkageType.Context.SPIGOT;
|
||||||
|
|
||||||
writer = processingEnv.getFiler().createSourceFile("de.steamwar." + name + ".linkage.LinkageUtils").openWriter();
|
|
||||||
messager = processingEnv.getMessager();
|
messager = processingEnv.getMessager();
|
||||||
|
|
||||||
|
this.name = name;
|
||||||
packageName = "de.steamwar." + name + ".linkage";
|
packageName = "de.steamwar." + name + ".linkage";
|
||||||
className = "LinkageUtils";
|
className = "LinkageUtils";
|
||||||
}
|
}
|
||||||
@ -78,9 +86,11 @@ public class LinkageProcessor extends AbstractProcessor {
|
|||||||
@SneakyThrows
|
@SneakyThrows
|
||||||
@Override
|
@Override
|
||||||
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
|
||||||
messager.printMessage(Diagnostic.Kind.NOTE, "Processing linkage types");
|
|
||||||
if (processed) return false;
|
if (processed) return false;
|
||||||
processed = true;
|
processed = true;
|
||||||
|
|
||||||
|
Writer writer = processingEnv.getFiler().createSourceFile("de.steamwar." + name + ".linkage.LinkageUtils").openWriter();
|
||||||
|
|
||||||
BuildPlan buildPlan = new BuildPlan(packageName, className);
|
BuildPlan buildPlan = new BuildPlan(packageName, className);
|
||||||
buildPlan.addImport("java.util.Set");
|
buildPlan.addImport("java.util.Set");
|
||||||
buildPlan.addImport("java.lang.Class");
|
buildPlan.addImport("java.lang.Class");
|
||||||
@ -160,40 +170,50 @@ public class LinkageProcessor extends AbstractProcessor {
|
|||||||
MinVersion minVersion = element.getAnnotation(MinVersion.class);
|
MinVersion minVersion = element.getAnnotation(MinVersion.class);
|
||||||
|
|
||||||
System.out.println("Found element: " + typeElement.getQualifiedName().toString());
|
System.out.println("Found element: " + typeElement.getQualifiedName().toString());
|
||||||
element.getAnnotationMirrors().forEach(annotationMirror -> {
|
element.getAnnotationMirrors().stream()
|
||||||
if (!annotationMirror.getAnnotationType().asElement().getSimpleName().toString().equals("Linked")) return;
|
.filter(annotationMirror -> {
|
||||||
Class<?> clazz = annotationMirror.getElementValues().entrySet().stream()
|
String annotationNames = annotationMirror.getAnnotationType().asElement().getSimpleName().toString();
|
||||||
.filter(entry -> entry.getKey().getSimpleName().toString().equals("value"))
|
return annotationNames.equals("Linked") || annotationNames.equals("Linkages");
|
||||||
.map(Map.Entry::getValue)
|
})
|
||||||
|
.flatMap(annotationMirror -> annotationMirror.getElementValues().values().stream())
|
||||||
.map(AnnotationValue::getValue)
|
.map(AnnotationValue::getValue)
|
||||||
|
.flatMap(o -> {
|
||||||
|
if (o instanceof List) {
|
||||||
|
return ((List<Object>) o).stream()
|
||||||
|
.map(AnnotationMirror.class::cast)
|
||||||
|
.map(AnnotationMirror::getElementValues)
|
||||||
|
.map(Map::values)
|
||||||
|
.flatMap(Collection::stream)
|
||||||
|
.map(AnnotationValue::getValue);
|
||||||
|
}
|
||||||
|
return Stream.of(o);
|
||||||
|
})
|
||||||
.map(Object::toString)
|
.map(Object::toString)
|
||||||
.map(s -> {
|
.map(s -> {
|
||||||
try {
|
try {
|
||||||
return Class.forName(s);
|
return Class.forName(s);
|
||||||
} catch (ClassNotFoundException e) {
|
} catch (Exception e) {
|
||||||
|
messager.printMessage(Diagnostic.Kind.ERROR, "Could not find class " + s, element);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.findFirst()
|
.filter(Objects::nonNull)
|
||||||
.orElse(null);
|
.map(clazz -> {
|
||||||
if (clazz == null) {
|
|
||||||
messager.printMessage(Diagnostic.Kind.ERROR, "No class specified", element);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
LinkageType type = null;
|
|
||||||
try {
|
try {
|
||||||
type = (LinkageType) clazz.getDeclaredConstructor().newInstance();
|
return (LinkageType) clazz.getDeclaredConstructor().newInstance();
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
messager.printMessage(Diagnostic.Kind.ERROR, "Could not instantiate class", element);
|
messager.printMessage(Diagnostic.Kind.ERROR, "Could not create instance of " + clazz.getName(), element);
|
||||||
return;
|
return null;
|
||||||
}
|
}
|
||||||
|
})
|
||||||
|
.filter(Objects::nonNull)
|
||||||
|
.forEach(type -> {
|
||||||
|
Class<? extends LinkageType> clazz = type.getClass();
|
||||||
if (!type.requirements(typeElement.getSuperclass().toString(), typeElement.getInterfaces().stream().map(TypeMirror::toString).collect(Collectors.toSet()))) {
|
if (!type.requirements(typeElement.getSuperclass().toString(), typeElement.getInterfaces().stream().map(TypeMirror::toString).collect(Collectors.toSet()))) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (alreadyGenerated.add((Class<? extends LinkageType>) clazz)) {
|
if (alreadyGenerated.add(clazz)) {
|
||||||
runMethod.addLine("if (type == " + clazz.getTypeName() + ".class) {");
|
runMethod.addLine("if (type == " + clazz.getTypeName() + ".class) {");
|
||||||
runMethod.addLine(" run" + clazz.getSimpleName() + "();");
|
runMethod.addLine(" run" + clazz.getSimpleName() + "();");
|
||||||
runMethod.addLine("}");
|
runMethod.addLine("}");
|
||||||
@ -215,9 +235,9 @@ public class LinkageProcessor extends AbstractProcessor {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
BufferedWriter writer = new BufferedWriter(this.writer);
|
BufferedWriter bufferedWriter = new BufferedWriter(writer);
|
||||||
buildPlan.write(writer);
|
buildPlan.write(bufferedWriter);
|
||||||
writer.close();
|
bufferedWriter.close();
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren