Finalize Linkage
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2022-09-22 13:46:39 +02:00
Ursprung bbd4eeacd3
Commit 067acf5f62
7 geänderte Dateien mit 99 neuen und 86 gelöschten Zeilen

Datei anzeigen

@ -249,7 +249,7 @@ public class LinkageProcessor extends AbstractProcessor {
.filter(Objects::nonNull) .filter(Objects::nonNull)
.forEach(type -> { .forEach(type -> {
Class<? extends LinkageType> clazz = type.getClass(); 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()), typeElement)) {
return; return;
} }

Datei anzeigen

@ -23,8 +23,6 @@ import de.steamwar.linkage.plan.BuildPlan;
import de.steamwar.linkage.plan.MethodBuilder; import de.steamwar.linkage.plan.MethodBuilder;
import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeElement;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.Set; import java.util.Set;
public interface LinkageType { public interface LinkageType {
@ -39,9 +37,22 @@ public interface LinkageType {
SPIGOT SPIGOT
} }
default boolean requirements(String superClass, Set<String> interfaces) { default boolean requirements(String superClass, Set<String> interfaces, TypeElement typeElement) {
return true; return true;
} }
void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement); default void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) {
switch (context) {
case BUNGEE:
generateCodeBungee(buildPlan, linkageTypeMethod, instance, typeElement);
break;
case SPIGOT:
generateCodeSpigot(buildPlan, linkageTypeMethod, instance, typeElement);
break;
}
}
default void generateCodeBungee(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) {
}
default void generateCodeSpigot(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) {
}
} }

Datei anzeigen

@ -29,16 +29,17 @@ import java.util.Set;
public class Command implements LinkageType { public class Command implements LinkageType {
@Override @Override
public boolean requirements(String superClass, Set<String> interfaces) { public boolean requirements(String superClass, Set<String> interfaces, TypeElement typeElement) {
return superClass.equals("de.steamwar.command.SWCommand"); return superClass.equals("de.steamwar.command.SWCommand");
} }
@Override @Override
public void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { public void generateCodeBungee(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) {
if (context == Context.BUNGEE) { linkageTypeMethod.addLine(instance + ";");
linkageTypeMethod.addLine(instance + ";"); }
return;
} @Override
public void generateCodeSpigot(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) {
linkageTypeMethod.addLine(instance + ".setMessage(" + getPluginMain() + ".MESSAGE);"); linkageTypeMethod.addLine(instance + ".setMessage(" + getPluginMain() + ".MESSAGE);");
} }
} }

Datei anzeigen

@ -29,7 +29,7 @@ import java.util.Set;
public class DisableLink implements LinkageType { public class DisableLink implements LinkageType {
@Override @Override
public boolean requirements(String superClass, Set<String> interfaces) { public boolean requirements(String superClass, Set<String> interfaces, TypeElement typeElement) {
return interfaces.contains("de.steamwar.linkage.api.Disable"); return interfaces.contains("de.steamwar.linkage.api.Disable");
} }

Datei anzeigen

@ -20,7 +20,6 @@
package de.steamwar.linkage.types; package de.steamwar.linkage.types;
import de.steamwar.linkage.LinkageType; import de.steamwar.linkage.LinkageType;
import de.steamwar.linkage.api.Enable;
import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.BuildPlan;
import de.steamwar.linkage.plan.MethodBuilder; import de.steamwar.linkage.plan.MethodBuilder;
@ -30,7 +29,7 @@ import java.util.Set;
public class EnableLink implements LinkageType { public class EnableLink implements LinkageType {
@Override @Override
public boolean requirements(String superClass, Set<String> interfaces) { public boolean requirements(String superClass, Set<String> interfaces, TypeElement typeElement) {
return interfaces.contains("de.steamwar.linkage.api.Enable"); return interfaces.contains("de.steamwar.linkage.api.Enable");
} }

Datei anzeigen

@ -24,7 +24,6 @@ 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 lombok.SneakyThrows;
import javax.lang.model.element.*; import javax.lang.model.element.*;
import javax.lang.model.type.DeclaredType; import javax.lang.model.type.DeclaredType;
@ -35,7 +34,7 @@ import java.util.Set;
public class ListenerLink implements LinkageType { public class ListenerLink implements LinkageType {
@Override @Override
public boolean requirements(String superClass, Set<String> interfaces) { public boolean requirements(String superClass, Set<String> interfaces, TypeElement typeElement) {
if (context == Context.BUNGEE) { if (context == Context.BUNGEE) {
return interfaces.contains("net.md_5.bungee.api.plugin.Listener"); return interfaces.contains("net.md_5.bungee.api.plugin.Listener");
} else { } else {
@ -44,70 +43,71 @@ public class ListenerLink implements LinkageType {
} }
@Override @Override
public void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { public void generateCodeBungee(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) {
if (context == Context.BUNGEE) { buildPlan.addImport("net.md_5.bungee.api.ProxyServer");
buildPlan.addImport("net.md_5.bungee.api.ProxyServer"); buildPlan.addImport("de.steamwar.bungeecore.BungeeCore");
buildPlan.addImport("de.steamwar.bungeecore.BungeeCore"); linkageTypeMethod.addLine("ProxyServer.getInstance().getPluginManager().registerListener(BungeeCore.get(), " + instance + ");");
linkageTypeMethod.addLine("ProxyServer.getInstance().getPluginManager().registerListener(BungeeCore.get(), " + instance + ");"); }
} else {
Map<String, TypeElement> eventClasses = new HashMap<>();
Map<TypeElement, ExecutableElement> eventMethods = new HashMap<>();
typeElement.getEnclosedElements().stream().filter(e -> e.getKind() == ElementKind.METHOD).map(ExecutableElement.class::cast).filter(e -> { @Override
return e.getAnnotationMirrors().stream().anyMatch(annotationMirror -> { public void generateCodeSpigot(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) {
return annotationMirror.getAnnotationType().asElement().getSimpleName().toString().equals("EventHandler"); Map<String, TypeElement> eventClasses = new HashMap<>();
}); Map<TypeElement, ExecutableElement> eventMethods = new HashMap<>();
}).forEach(e -> {
TypeElement current = ((TypeElement)((DeclaredType) e.getParameters().get(0).asType()).asElement()); typeElement.getEnclosedElements().stream().filter(e -> e.getKind() == ElementKind.METHOD).map(ExecutableElement.class::cast).filter(e -> {
eventClasses.put(current.getQualifiedName().toString(), current); return e.getAnnotationMirrors().stream().anyMatch(annotationMirror -> {
eventMethods.put(current, e); return annotationMirror.getAnnotationType().asElement().getSimpleName().toString().equals("EventHandler");
}); });
}).forEach(e -> {
TypeElement current = ((TypeElement)((DeclaredType) e.getParameters().get(0).asType()).asElement());
eventClasses.put(current.getQualifiedName().toString(), current);
eventMethods.put(current, e);
});
eventClasses.forEach((s, eventType) -> { eventClasses.forEach((s, eventType) -> {
if (buildPlan.hasMethod(s)) return; if (buildPlan.hasMethod(s)) return;
buildPlan.addImport("org.bukkit.event.HandlerList"); buildPlan.addImport("org.bukkit.event.HandlerList");
buildPlan.addImport("org.bukkit.event.Listener"); buildPlan.addImport("org.bukkit.event.Listener");
buildPlan.addImport("java.util.function.Consumer"); buildPlan.addImport("java.util.function.Consumer");
buildPlan.addImport("org.bukkit.event.EventPriority"); buildPlan.addImport("org.bukkit.event.EventPriority");
buildPlan.addImport("org.bukkit.plugin.RegisteredListener"); buildPlan.addImport("org.bukkit.plugin.RegisteredListener");
buildPlan.addImport("org.bukkit.plugin.EventExecutor"); buildPlan.addImport("org.bukkit.plugin.EventExecutor");
buildPlan.addImport(s); buildPlan.addImport(s);
buildPlan.addField(new FieldBuilder("HandlerList", "handlerList" + eventType.getSimpleName())); buildPlan.addField(new FieldBuilder("HandlerList", "handlerList" + eventType.getSimpleName()));
MethodBuilder methodBuilder = new MethodBuilder(eventType.getSimpleName().toString(), "void"); MethodBuilder methodBuilder = new MethodBuilder(eventType.getSimpleName().toString(), "void");
methodBuilder.addParameter(new ParameterBuilder("Listener", "listener")); methodBuilder.addParameter(new ParameterBuilder("Listener", "listener"));
methodBuilder.addParameter(new ParameterBuilder("Consumer<" + eventType.getSimpleName() + ">", "consumer")); methodBuilder.addParameter(new ParameterBuilder("Consumer<" + eventType.getSimpleName() + ">", "consumer"));
methodBuilder.addParameter(new ParameterBuilder("EventPriority", "eventPriority")); methodBuilder.addParameter(new ParameterBuilder("EventPriority", "eventPriority"));
methodBuilder.addParameter(new ParameterBuilder("boolean", "ignoreCancelled")); methodBuilder.addParameter(new ParameterBuilder("boolean", "ignoreCancelled"));
methodBuilder.setPrivate(true); methodBuilder.setPrivate(true);
methodBuilder.addLine("EventExecutor eventExecutor = (l, event) -> {"); methodBuilder.addLine("EventExecutor eventExecutor = (l, event) -> {");
methodBuilder.addLine(" if (event instanceof " + eventType.getSimpleName() + ") {"); methodBuilder.addLine(" if (event instanceof " + eventType.getSimpleName() + ") {");
methodBuilder.addLine(" consumer.accept((" + eventType.getSimpleName() + ") event);"); methodBuilder.addLine(" consumer.accept((" + eventType.getSimpleName() + ") event);");
methodBuilder.addLine(" }"); methodBuilder.addLine(" }");
methodBuilder.addLine("};"); methodBuilder.addLine("};");
methodBuilder.addLine("handlerList" + eventType.getSimpleName() + ".register(new RegisteredListener(listener, eventExecutor, eventPriority, " + getPluginMain() + ".getInstance(), ignoreCancelled));"); methodBuilder.addLine("handlerList" + eventType.getSimpleName() + ".register(new RegisteredListener(listener, eventExecutor, eventPriority, " + getPluginMain() + ".getInstance(), ignoreCancelled));");
buildPlan.addMethod(methodBuilder); buildPlan.addMethod(methodBuilder);
linkageTypeMethod.addLine("handlerList" + eventType.getSimpleName() + " = " + eventType.getSimpleName() + ".getHandlerList();"); linkageTypeMethod.addLine("handlerList" + eventType.getSimpleName() + " = " + eventType.getSimpleName() + ".getHandlerList();");
}); });
buildPlan.addImport(typeElement.getQualifiedName().toString()); buildPlan.addImport(typeElement.getQualifiedName().toString());
String localInstance = "local" + typeElement.getSimpleName().toString(); String localInstance = "local" + typeElement.getSimpleName().toString();
linkageTypeMethod.addLine(typeElement.getSimpleName() + " " + localInstance + " = " + instance + ";"); linkageTypeMethod.addLine(typeElement.getSimpleName() + " " + localInstance + " = " + instance + ";");
eventMethods.forEach((type, executableElement) -> { eventMethods.forEach((type, executableElement) -> {
AnnotationMirror eventHandler = executableElement.getAnnotationMirrors().stream().filter(annotationMirror -> annotationMirror.getAnnotationType().asElement().getSimpleName().toString().equals("EventHandler")).findFirst().orElse(null); AnnotationMirror eventHandler = executableElement.getAnnotationMirrors().stream().filter(annotationMirror -> annotationMirror.getAnnotationType().asElement().getSimpleName().toString().equals("EventHandler")).findFirst().orElse(null);
if (eventHandler == null) { if (eventHandler == null) {
return; return;
}
String priority = "NORMAL";
String ignoreCancelled = "false";
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : eventHandler.getElementValues().entrySet()) {
if (entry.getKey().getSimpleName().toString().equals("priority")) {
priority = entry.getValue().getValue().toString();
} else if (entry.getKey().getSimpleName().toString().equals("ignoreCancelled")) {
ignoreCancelled = entry.getValue().getValue().toString();
} }
String priority = "NORMAL"; }
String ignoreCancelled = "false"; linkageTypeMethod.addLine(type.getSimpleName().toString() + "(" + localInstance + ", " + localInstance + "::" + executableElement.getSimpleName().toString() + ", EventPriority." + priority + ", " + ignoreCancelled + ");");
for (Map.Entry<? extends ExecutableElement, ? extends AnnotationValue> entry : eventHandler.getElementValues().entrySet()) { });
if (entry.getKey().getSimpleName().toString().equals("priority")) {
priority = entry.getValue().getValue().toString();
} else if (entry.getKey().getSimpleName().toString().equals("ignoreCancelled")) {
ignoreCancelled = entry.getValue().getValue().toString();
}
}
linkageTypeMethod.addLine(type.getSimpleName().toString() + "(" + localInstance + ", " + localInstance + "::" + executableElement.getSimpleName().toString() + ", EventPriority." + priority + ", " + ignoreCancelled + ");");
});
}
} }
} }

Datei anzeigen

@ -24,12 +24,13 @@ import de.steamwar.linkage.plan.BuildPlan;
import de.steamwar.linkage.plan.MethodBuilder; import de.steamwar.linkage.plan.MethodBuilder;
import javax.lang.model.element.TypeElement; import javax.lang.model.element.TypeElement;
import java.lang.reflect.Type;
import java.util.Set; import java.util.Set;
public class UnlinkListener implements LinkageType { public class UnlinkListener implements LinkageType {
@Override @Override
public boolean requirements(String superClass, Set<String> interfaces) { public boolean requirements(String superClass, Set<String> interfaces, TypeElement typeElement) {
if (context == Context.BUNGEE) { if (context == Context.BUNGEE) {
return interfaces.contains("net.md_5.bungee.api.plugin.Listener"); return interfaces.contains("net.md_5.bungee.api.plugin.Listener");
} else { } else {
@ -38,14 +39,15 @@ public class UnlinkListener implements LinkageType {
} }
@Override @Override
public void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { public void generateCodeBungee(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) {
if (context == Context.BUNGEE) { buildPlan.addImport("net.md_5.bungee.api.ProxyServer");
buildPlan.addImport("net.md_5.bungee.api.ProxyServer"); buildPlan.addImport("de.steamwar.bungeecore.BungeeCore");
buildPlan.addImport("de.steamwar.bungeecore.BungeeCore"); linkageTypeMethod.addLine("ProxyServer.getInstance().getPluginManager().registerListener(" + instance + ");");
linkageTypeMethod.addLine("ProxyServer.getInstance().getPluginManager().registerListener(" + instance + ");"); }
} else {
buildPlan.addImport("org.bukkit.event.HandlerList"); @Override
linkageTypeMethod.addLine("HandlerList.unregisterAll(" + instance + ");"); public void generateCodeSpigot(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) {
} buildPlan.addImport("org.bukkit.event.HandlerList");
linkageTypeMethod.addLine("HandlerList.unregisterAll(" + instance + ");");
} }
} }