From 067acf5f6200a8f165150585013ac53e4ecf2fc2 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 22 Sep 2022 13:46:39 +0200 Subject: [PATCH] Finalize Linkage --- src/de/steamwar/linkage/LinkageProcessor.java | 2 +- src/de/steamwar/linkage/LinkageType.java | 19 ++- src/de/steamwar/linkage/types/Command.java | 13 +- .../steamwar/linkage/types/DisableLink.java | 2 +- src/de/steamwar/linkage/types/EnableLink.java | 3 +- .../steamwar/linkage/types/ListenerLink.java | 124 +++++++++--------- .../linkage/types/UnlinkListener.java | 22 ++-- 7 files changed, 99 insertions(+), 86 deletions(-) diff --git a/src/de/steamwar/linkage/LinkageProcessor.java b/src/de/steamwar/linkage/LinkageProcessor.java index 25eb4bd..37b4529 100644 --- a/src/de/steamwar/linkage/LinkageProcessor.java +++ b/src/de/steamwar/linkage/LinkageProcessor.java @@ -249,7 +249,7 @@ public class LinkageProcessor extends AbstractProcessor { .filter(Objects::nonNull) .forEach(type -> { Class 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; } diff --git a/src/de/steamwar/linkage/LinkageType.java b/src/de/steamwar/linkage/LinkageType.java index 5ae6693..afc37cc 100644 --- a/src/de/steamwar/linkage/LinkageType.java +++ b/src/de/steamwar/linkage/LinkageType.java @@ -23,8 +23,6 @@ import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.MethodBuilder; import javax.lang.model.element.TypeElement; -import java.util.Optional; -import java.util.OptionalInt; import java.util.Set; public interface LinkageType { @@ -39,9 +37,22 @@ public interface LinkageType { SPIGOT } - default boolean requirements(String superClass, Set interfaces) { + default boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { 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) { + } } diff --git a/src/de/steamwar/linkage/types/Command.java b/src/de/steamwar/linkage/types/Command.java index 68c4607..6d6948c 100644 --- a/src/de/steamwar/linkage/types/Command.java +++ b/src/de/steamwar/linkage/types/Command.java @@ -29,16 +29,17 @@ import java.util.Set; public class Command implements LinkageType { @Override - public boolean requirements(String superClass, Set interfaces) { + public boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { return superClass.equals("de.steamwar.command.SWCommand"); } @Override - public void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - if (context == Context.BUNGEE) { - linkageTypeMethod.addLine(instance + ";"); - return; - } + public void generateCodeBungee(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { + linkageTypeMethod.addLine(instance + ";"); + } + + @Override + public void generateCodeSpigot(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { linkageTypeMethod.addLine(instance + ".setMessage(" + getPluginMain() + ".MESSAGE);"); } } diff --git a/src/de/steamwar/linkage/types/DisableLink.java b/src/de/steamwar/linkage/types/DisableLink.java index c3cc54a..72f816d 100644 --- a/src/de/steamwar/linkage/types/DisableLink.java +++ b/src/de/steamwar/linkage/types/DisableLink.java @@ -29,7 +29,7 @@ import java.util.Set; public class DisableLink implements LinkageType { @Override - public boolean requirements(String superClass, Set interfaces) { + public boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { return interfaces.contains("de.steamwar.linkage.api.Disable"); } diff --git a/src/de/steamwar/linkage/types/EnableLink.java b/src/de/steamwar/linkage/types/EnableLink.java index 4b42f86..d7630a4 100644 --- a/src/de/steamwar/linkage/types/EnableLink.java +++ b/src/de/steamwar/linkage/types/EnableLink.java @@ -20,7 +20,6 @@ package de.steamwar.linkage.types; import de.steamwar.linkage.LinkageType; -import de.steamwar.linkage.api.Enable; import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.MethodBuilder; @@ -30,7 +29,7 @@ import java.util.Set; public class EnableLink implements LinkageType { @Override - public boolean requirements(String superClass, Set interfaces) { + public boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { return interfaces.contains("de.steamwar.linkage.api.Enable"); } diff --git a/src/de/steamwar/linkage/types/ListenerLink.java b/src/de/steamwar/linkage/types/ListenerLink.java index 3eb140a..5c815c5 100644 --- a/src/de/steamwar/linkage/types/ListenerLink.java +++ b/src/de/steamwar/linkage/types/ListenerLink.java @@ -24,7 +24,6 @@ import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.FieldBuilder; import de.steamwar.linkage.plan.MethodBuilder; import de.steamwar.linkage.plan.ParameterBuilder; -import lombok.SneakyThrows; import javax.lang.model.element.*; import javax.lang.model.type.DeclaredType; @@ -35,7 +34,7 @@ import java.util.Set; public class ListenerLink implements LinkageType { @Override - public boolean requirements(String superClass, Set interfaces) { + public boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { if (context == Context.BUNGEE) { return interfaces.contains("net.md_5.bungee.api.plugin.Listener"); } else { @@ -44,70 +43,71 @@ public class ListenerLink implements LinkageType { } @Override - public void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - if (context == Context.BUNGEE) { - buildPlan.addImport("net.md_5.bungee.api.ProxyServer"); - buildPlan.addImport("de.steamwar.bungeecore.BungeeCore"); - linkageTypeMethod.addLine("ProxyServer.getInstance().getPluginManager().registerListener(BungeeCore.get(), " + instance + ");"); - } else { - Map eventClasses = new HashMap<>(); - Map eventMethods = new HashMap<>(); + public void generateCodeBungee(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { + buildPlan.addImport("net.md_5.bungee.api.ProxyServer"); + buildPlan.addImport("de.steamwar.bungeecore.BungeeCore"); + linkageTypeMethod.addLine("ProxyServer.getInstance().getPluginManager().registerListener(BungeeCore.get(), " + instance + ");"); + } - typeElement.getEnclosedElements().stream().filter(e -> e.getKind() == ElementKind.METHOD).map(ExecutableElement.class::cast).filter(e -> { - return e.getAnnotationMirrors().stream().anyMatch(annotationMirror -> { - 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); + @Override + public void generateCodeSpigot(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { + Map eventClasses = new HashMap<>(); + Map eventMethods = new HashMap<>(); + + typeElement.getEnclosedElements().stream().filter(e -> e.getKind() == ElementKind.METHOD).map(ExecutableElement.class::cast).filter(e -> { + return e.getAnnotationMirrors().stream().anyMatch(annotationMirror -> { + 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) -> { - if (buildPlan.hasMethod(s)) return; - buildPlan.addImport("org.bukkit.event.HandlerList"); - buildPlan.addImport("org.bukkit.event.Listener"); - buildPlan.addImport("java.util.function.Consumer"); - buildPlan.addImport("org.bukkit.event.EventPriority"); - buildPlan.addImport("org.bukkit.plugin.RegisteredListener"); - buildPlan.addImport("org.bukkit.plugin.EventExecutor"); - buildPlan.addImport(s); - buildPlan.addField(new FieldBuilder("HandlerList", "handlerList" + eventType.getSimpleName())); - MethodBuilder methodBuilder = new MethodBuilder(eventType.getSimpleName().toString(), "void"); - methodBuilder.addParameter(new ParameterBuilder("Listener", "listener")); - methodBuilder.addParameter(new ParameterBuilder("Consumer<" + eventType.getSimpleName() + ">", "consumer")); - methodBuilder.addParameter(new ParameterBuilder("EventPriority", "eventPriority")); - methodBuilder.addParameter(new ParameterBuilder("boolean", "ignoreCancelled")); - methodBuilder.setPrivate(true); - methodBuilder.addLine("EventExecutor eventExecutor = (l, event) -> {"); - methodBuilder.addLine(" if (event instanceof " + eventType.getSimpleName() + ") {"); - methodBuilder.addLine(" consumer.accept((" + eventType.getSimpleName() + ") event);"); - methodBuilder.addLine(" }"); - methodBuilder.addLine("};"); - methodBuilder.addLine("handlerList" + eventType.getSimpleName() + ".register(new RegisteredListener(listener, eventExecutor, eventPriority, " + getPluginMain() + ".getInstance(), ignoreCancelled));"); - buildPlan.addMethod(methodBuilder); - linkageTypeMethod.addLine("handlerList" + eventType.getSimpleName() + " = " + eventType.getSimpleName() + ".getHandlerList();"); - }); + eventClasses.forEach((s, eventType) -> { + if (buildPlan.hasMethod(s)) return; + buildPlan.addImport("org.bukkit.event.HandlerList"); + buildPlan.addImport("org.bukkit.event.Listener"); + buildPlan.addImport("java.util.function.Consumer"); + buildPlan.addImport("org.bukkit.event.EventPriority"); + buildPlan.addImport("org.bukkit.plugin.RegisteredListener"); + buildPlan.addImport("org.bukkit.plugin.EventExecutor"); + buildPlan.addImport(s); + buildPlan.addField(new FieldBuilder("HandlerList", "handlerList" + eventType.getSimpleName())); + MethodBuilder methodBuilder = new MethodBuilder(eventType.getSimpleName().toString(), "void"); + methodBuilder.addParameter(new ParameterBuilder("Listener", "listener")); + methodBuilder.addParameter(new ParameterBuilder("Consumer<" + eventType.getSimpleName() + ">", "consumer")); + methodBuilder.addParameter(new ParameterBuilder("EventPriority", "eventPriority")); + methodBuilder.addParameter(new ParameterBuilder("boolean", "ignoreCancelled")); + methodBuilder.setPrivate(true); + methodBuilder.addLine("EventExecutor eventExecutor = (l, event) -> {"); + methodBuilder.addLine(" if (event instanceof " + eventType.getSimpleName() + ") {"); + methodBuilder.addLine(" consumer.accept((" + eventType.getSimpleName() + ") event);"); + methodBuilder.addLine(" }"); + methodBuilder.addLine("};"); + methodBuilder.addLine("handlerList" + eventType.getSimpleName() + ".register(new RegisteredListener(listener, eventExecutor, eventPriority, " + getPluginMain() + ".getInstance(), ignoreCancelled));"); + buildPlan.addMethod(methodBuilder); + linkageTypeMethod.addLine("handlerList" + eventType.getSimpleName() + " = " + eventType.getSimpleName() + ".getHandlerList();"); + }); - buildPlan.addImport(typeElement.getQualifiedName().toString()); - String localInstance = "local" + typeElement.getSimpleName().toString(); - linkageTypeMethod.addLine(typeElement.getSimpleName() + " " + localInstance + " = " + instance + ";"); - eventMethods.forEach((type, executableElement) -> { - AnnotationMirror eventHandler = executableElement.getAnnotationMirrors().stream().filter(annotationMirror -> annotationMirror.getAnnotationType().asElement().getSimpleName().toString().equals("EventHandler")).findFirst().orElse(null); - if (eventHandler == null) { - return; + buildPlan.addImport(typeElement.getQualifiedName().toString()); + String localInstance = "local" + typeElement.getSimpleName().toString(); + linkageTypeMethod.addLine(typeElement.getSimpleName() + " " + localInstance + " = " + instance + ";"); + eventMethods.forEach((type, executableElement) -> { + AnnotationMirror eventHandler = executableElement.getAnnotationMirrors().stream().filter(annotationMirror -> annotationMirror.getAnnotationType().asElement().getSimpleName().toString().equals("EventHandler")).findFirst().orElse(null); + if (eventHandler == null) { + return; + } + String priority = "NORMAL"; + String ignoreCancelled = "false"; + for (Map.Entry 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"; - for (Map.Entry 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 + ");"); - }); - } + } + linkageTypeMethod.addLine(type.getSimpleName().toString() + "(" + localInstance + ", " + localInstance + "::" + executableElement.getSimpleName().toString() + ", EventPriority." + priority + ", " + ignoreCancelled + ");"); + }); } } diff --git a/src/de/steamwar/linkage/types/UnlinkListener.java b/src/de/steamwar/linkage/types/UnlinkListener.java index 73966a7..564d567 100644 --- a/src/de/steamwar/linkage/types/UnlinkListener.java +++ b/src/de/steamwar/linkage/types/UnlinkListener.java @@ -24,12 +24,13 @@ import de.steamwar.linkage.plan.BuildPlan; import de.steamwar.linkage.plan.MethodBuilder; import javax.lang.model.element.TypeElement; +import java.lang.reflect.Type; import java.util.Set; public class UnlinkListener implements LinkageType { @Override - public boolean requirements(String superClass, Set interfaces) { + public boolean requirements(String superClass, Set interfaces, TypeElement typeElement) { if (context == Context.BUNGEE) { return interfaces.contains("net.md_5.bungee.api.plugin.Listener"); } else { @@ -38,14 +39,15 @@ public class UnlinkListener implements LinkageType { } @Override - public void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { - if (context == Context.BUNGEE) { - buildPlan.addImport("net.md_5.bungee.api.ProxyServer"); - buildPlan.addImport("de.steamwar.bungeecore.BungeeCore"); - linkageTypeMethod.addLine("ProxyServer.getInstance().getPluginManager().registerListener(" + instance + ");"); - } else { - buildPlan.addImport("org.bukkit.event.HandlerList"); - linkageTypeMethod.addLine("HandlerList.unregisterAll(" + instance + ");"); - } + public void generateCodeBungee(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { + buildPlan.addImport("net.md_5.bungee.api.ProxyServer"); + buildPlan.addImport("de.steamwar.bungeecore.BungeeCore"); + linkageTypeMethod.addLine("ProxyServer.getInstance().getPluginManager().registerListener(" + instance + ");"); + } + + @Override + public void generateCodeSpigot(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) { + buildPlan.addImport("org.bukkit.event.HandlerList"); + linkageTypeMethod.addLine("HandlerList.unregisterAll(" + instance + ");"); } }