Linkage #22

Zusammengeführt
Lixfel hat 4 Commits von Linkage nach master 2022-09-24 19:44:12 +02:00 zusammengeführt
10 geänderte Dateien mit 139 neuen und 185 gelöschten Zeilen
Nur Änderungen aus Commit 49a6a6f23f werden angezeigt - Alle Commits anzeigen

Datei anzeigen

@ -22,6 +22,7 @@ package de.steamwar.linkage;
import de.steamwar.linkage.plan.BuildPlan;
import de.steamwar.linkage.plan.FieldBuilder;
import de.steamwar.linkage.plan.MethodBuilder;
import de.steamwar.linkage.types.Plain_GENERIC;
import lombok.Getter;
import lombok.SneakyThrows;
@ -117,10 +118,7 @@ public class LinkageProcessor extends AbstractProcessor {
Writer writer = processingEnv.getFiler().createSourceFile("de.steamwar." + name + ".linkage.LinkageUtils").openWriter();
BuildPlan buildPlan = new BuildPlan(packageName, className);
buildPlan.addImport("java.util.Set");
buildPlan.addImport("java.lang.Class");
buildPlan.addImport("de.steamwar.linkage.LinkageType");
buildPlan.addImport("java.util.HashSet");
Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(Linked.class);
Map<String, TypeElement> neededFields = new HashMap<>();
@ -168,17 +166,7 @@ public class LinkageProcessor extends AbstractProcessor {
}
neededFields.forEach((s, typeElement) -> {
buildPlan.addImport(typeElement.getQualifiedName().toString());
buildPlan.addField(new FieldBuilder(typeElement.getSimpleName().toString(), typeElement.getSimpleName().toString()));
MethodBuilder initializer = new MethodBuilder(typeElement.getSimpleName().toString(), typeElement.getSimpleName().toString());
initializer.setPrivate(true);
specialElements(typeElement, buildPlan, initializer::addLine, () -> {
initializer.addLine("if (" + typeElement.getSimpleName().toString() + " == null) {");
initializer.addLine(" " + typeElement.getSimpleName().toString() + " = new " + typeElement.getSimpleName().toString() + "();");
initializer.addLine("}");
});
initializer.addLine("return " + typeElement.getSimpleName().toString() + ";");
buildPlan.addMethod(initializer);
buildPlan.addField(new FieldBuilder(typeElement.getSimpleName().toString(), typeElement.getSimpleName().toString(), "new " + typeElement.getSimpleName().toString() + "()"));
});
Map<String, MethodBuilder> methods = new HashMap<>();
@ -196,10 +184,13 @@ public class LinkageProcessor extends AbstractProcessor {
for (Map.Entry<String, List<LinkageType>> entry : linkages.entrySet()) {
MethodBuilder method = methods.computeIfAbsent(entry.getKey(), s -> {
return new MethodBuilder(s, "void");
MethodBuilder methodBuilder = new MethodBuilder(s, "void");
buildPlan.addMethod(methodBuilder);
return methodBuilder;
});
for (LinkageType type : entry.getValue()) {
specialElements(typeElement, buildPlan, method::addLine, () -> {
buildPlan.addImport(typeElement.getQualifiedName().toString());
type.generateCode(buildPlan, method, getElement(typeElement, neededFields), typeElement);
});
}
@ -214,9 +205,9 @@ public class LinkageProcessor extends AbstractProcessor {
private String getElement(TypeElement typeElement, Map<String, TypeElement> neededFields) {
if (neededFields.containsKey(typeElement.getQualifiedName().toString())) {
return typeElement.getSimpleName().toString() + "()";
return typeElement.getSimpleName().toString();
}
return "new " + typeElement.getQualifiedName().toString() + "()";
return "new " + typeElement.getSimpleName().toString() + "()";
}
private void specialElements(TypeElement typeElement, BuildPlan buildPlan, Consumer<String> stringConsumer, Runnable inner) {
@ -271,6 +262,8 @@ public class LinkageProcessor extends AbstractProcessor {
}
}
private Plain_GENERIC plain_GENERIC = new Plain_GENERIC();
private Map<String, List<LinkageType>> getLinkagesOfType(TypeElement typeElement) {
Map<String, List<LinkageType>> linkages = new HashMap<>();
LinkageType superClassType = resolveSingle(typeElement.getSuperclass());
@ -283,6 +276,9 @@ public class LinkageProcessor extends AbstractProcessor {
linkages.computeIfAbsent(interfaceType.method(), s -> new ArrayList<>()).add(interfaceType);
}
}
if (!linkages.containsKey(plain_GENERIC.method())) {
linkages.put(plain_GENERIC.method(), Collections.singletonList(plain_GENERIC));
}
return linkages;
}
@ -290,12 +286,12 @@ public class LinkageProcessor extends AbstractProcessor {
String qualifier = typeMirror.toString();
qualifier = qualifier.substring(qualifier.lastIndexOf('.') + 1);
try {
return (LinkageType) Class.forName("de.steamwar.linkage.types." + qualifier + context.name()).getDeclaredConstructor().newInstance();
return (LinkageType) Class.forName("de.steamwar.linkage.types." + qualifier + "_" + context.name()).getDeclaredConstructor().newInstance();
} catch (Exception e) {
// Ignore
}
try {
return (LinkageType) Class.forName("de.steamwar.linkage.types." + qualifier).getDeclaredConstructor().newInstance();
return (LinkageType) Class.forName("de.steamwar.linkage.types." + qualifier + "_GENERIC").getDeclaredConstructor().newInstance();
} catch (Exception e) {
// Ignore
}

Datei anzeigen

@ -1,52 +0,0 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.linkage;
import de.steamwar.linkage.plan.BuildPlan;
import de.steamwar.linkage.plan.MethodBuilder;
import javax.lang.model.element.TypeElement;
import java.util.Set;
public interface LinkageTypeOld {
default String getPluginMain() {
return LinkageProcessor.getPluginMain();
}
default boolean requirements(String superClass, Set<String> interfaces, TypeElement typeElement) {
return true;
}
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

@ -1,7 +1,7 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
@ -17,24 +17,23 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.linkage.typesold;
package de.steamwar.linkage.types;
import de.steamwar.linkage.LinkageTypeOld;
import de.steamwar.linkage.LinkageType;
import de.steamwar.linkage.plan.BuildPlan;
import de.steamwar.linkage.plan.MethodBuilder;
import javax.lang.model.element.TypeElement;
import java.util.Set;
public class EnableLink implements LinkageTypeOld {
public class Disable_GENERIC implements LinkageType {
@Override
public boolean requirements(String superClass, Set<String> interfaces, TypeElement typeElement) {
return interfaces.contains("de.steamwar.linkage.api.Enable");
public String method() {
return "unlink";
}
@Override
public void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) {
linkageTypeMethod.addLine(instance + ".enable();");
public void generateCode(BuildPlan buildPlan, MethodBuilder method, String instance, TypeElement typeElement) {
method.addLine(instance + ".disable();");
}
}

Datei anzeigen

@ -1,7 +1,7 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
@ -17,24 +17,23 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.linkage.typesold;
package de.steamwar.linkage.types;
import de.steamwar.linkage.LinkageTypeOld;
import de.steamwar.linkage.LinkageType;
import de.steamwar.linkage.plan.BuildPlan;
import de.steamwar.linkage.plan.MethodBuilder;
import javax.lang.model.element.TypeElement;
import java.util.Set;
public class DisableLink implements LinkageTypeOld {
public class Enable_GENERIC implements LinkageType {
@Override
public boolean requirements(String superClass, Set<String> interfaces, TypeElement typeElement) {
return interfaces.contains("de.steamwar.linkage.api.Disable");
public String method() {
return "link";
}
@Override
public void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) {
linkageTypeMethod.addLine(instance + ".disable();");
public void generateCode(BuildPlan buildPlan, MethodBuilder method, String instance, TypeElement typeElement) {
method.addLine(instance + ".enable();");
}
}

Datei anzeigen

@ -1,7 +1,7 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
@ -17,29 +17,25 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.linkage.typesold;
package de.steamwar.linkage.types;
import de.steamwar.linkage.LinkageTypeOld;
import de.steamwar.linkage.LinkageType;
import de.steamwar.linkage.plan.BuildPlan;
import de.steamwar.linkage.plan.MethodBuilder;
import javax.lang.model.element.TypeElement;
import java.util.Set;
public class Command implements LinkageTypeOld {
public class Listener_BUNGEE implements LinkageType {
@Override
public boolean requirements(String superClass, Set<String> interfaces, TypeElement typeElement) {
return superClass.equals("de.steamwar.command.SWCommand");
public String method() {
return "link";
}
@Override
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);");
public void generateCode(BuildPlan buildPlan, MethodBuilder method, String instance, TypeElement typeElement) {
buildPlan.addImport("net.md_5.bungee.api.ProxyServer");
buildPlan.addImport("de.steamwar.bungeecore.BungeeCore");
method.addLine("ProxyServer.getInstance().getPluginManager().registerListener(BungeeCore.get(), " + instance + ");");
}
}

Datei anzeigen

@ -17,10 +17,9 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.linkage.typesold;
package de.steamwar.linkage.types;
import de.steamwar.linkage.Context;
import de.steamwar.linkage.LinkageTypeOld;
import de.steamwar.linkage.LinkageType;
import de.steamwar.linkage.plan.BuildPlan;
import de.steamwar.linkage.plan.FieldBuilder;
import de.steamwar.linkage.plan.MethodBuilder;
@ -30,28 +29,16 @@ import javax.lang.model.element.*;
import javax.lang.model.type.DeclaredType;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class ListenerLink implements LinkageTypeOld {
public class Listener_SPIGOT implements LinkageType {
@Override
public boolean requirements(String superClass, Set<String> interfaces, TypeElement typeElement) {
if (context == Context.BUNGEE) {
return interfaces.contains("net.md_5.bungee.api.plugin.Listener");
} else {
return interfaces.contains("org.bukkit.event.Listener");
}
public String method() {
return "link";
}
@Override
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 + ");");
}
@Override
public void generateCodeSpigot(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) {
public void generateCode(BuildPlan buildPlan, MethodBuilder method, String instance, TypeElement typeElement) {
Map<String, TypeElement> eventClasses = new HashMap<>();
Map<TypeElement, ExecutableElement> eventMethods = new HashMap<>();
@ -88,12 +75,11 @@ public class ListenerLink implements LinkageTypeOld {
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();");
method.addLine("handlerList" + eventType.getSimpleName() + " = " + eventType.getSimpleName() + ".getHandlerList();");
});
buildPlan.addImport(typeElement.getQualifiedName().toString());
String localInstance = "local" + typeElement.getSimpleName().toString();
linkageTypeMethod.addLine(typeElement.getSimpleName() + " " + localInstance + " = " + instance + ";");
method.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) {
@ -108,7 +94,7 @@ public class ListenerLink implements LinkageTypeOld {
ignoreCancelled = entry.getValue().getValue().toString();
}
}
linkageTypeMethod.addLine(type.getSimpleName().toString() + "(" + localInstance + ", " + localInstance + "::" + executableElement.getSimpleName().toString() + ", EventPriority." + priority + ", " + ignoreCancelled + ");");
method.addLine(type.getSimpleName().toString() + "(" + localInstance + ", " + localInstance + "::" + executableElement.getSimpleName().toString() + ", EventPriority." + priority + ", " + ignoreCancelled + ");");
});
}
}

Datei anzeigen

@ -1,7 +1,7 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2022 SteamWar.de-Serverteam
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
@ -17,18 +17,23 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.linkage.typesold;
package de.steamwar.linkage.types;
import de.steamwar.linkage.LinkageTypeOld;
import de.steamwar.linkage.LinkageType;
import de.steamwar.linkage.plan.BuildPlan;
import de.steamwar.linkage.plan.MethodBuilder;
import javax.lang.model.element.TypeElement;
public class Plain implements LinkageTypeOld {
public class Plain_GENERIC implements LinkageType {
@Override
public void generateCode(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) {
linkageTypeMethod.addLine(instance + ";");
public String method() {
return "link";
}
@Override
public void generateCode(BuildPlan buildPlan, MethodBuilder method, String instance, TypeElement typeElement) {
method.addLine(instance + ";");
}
}

Datei anzeigen

@ -0,0 +1,39 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.linkage.types;
import de.steamwar.linkage.LinkageType;
import de.steamwar.linkage.plan.BuildPlan;
import de.steamwar.linkage.plan.MethodBuilder;
import javax.lang.model.element.TypeElement;
public class SWCommand_BUNGEE implements LinkageType {
@Override
public String method() {
return "link";
}
@Override
public void generateCode(BuildPlan buildPlan, MethodBuilder method, String instance, TypeElement typeElement) {
method.addLine(instance + ";");
}
}

Datei anzeigen

@ -0,0 +1,39 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.linkage.types;
import de.steamwar.linkage.LinkageType;
import de.steamwar.linkage.plan.BuildPlan;
import de.steamwar.linkage.plan.MethodBuilder;
import javax.lang.model.element.TypeElement;
public class SWCommand_SPIGOT implements LinkageType {
@Override
public String method() {
return "link";
}
@Override
public void generateCode(BuildPlan buildPlan, MethodBuilder method, String instance, TypeElement typeElement) {
method.addLine(instance + ".setMessage(" + getPluginMain() + ".MESSAGE);");
}
}

Datei anzeigen

@ -1,53 +0,0 @@
/*
* This file is a part of the SteamWar software.
*
* Copyright (C) 2020 SteamWar.de-Serverteam
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package de.steamwar.linkage.typesold;
import de.steamwar.linkage.Context;
import de.steamwar.linkage.LinkageTypeOld;
import de.steamwar.linkage.plan.BuildPlan;
import de.steamwar.linkage.plan.MethodBuilder;
import javax.lang.model.element.TypeElement;
import java.util.Set;
public class UnlinkListener implements LinkageTypeOld {
@Override
public boolean requirements(String superClass, Set<String> interfaces, TypeElement typeElement) {
if (context == Context.BUNGEE) {
return interfaces.contains("net.md_5.bungee.api.plugin.Listener");
} else {
return interfaces.contains("org.bukkit.event.Listener");
}
}
@Override
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().unregisterListener(" + instance + ");");
}
@Override
public void generateCodeSpigot(BuildPlan buildPlan, MethodBuilder linkageTypeMethod, String instance, TypeElement typeElement) {
buildPlan.addImport("org.bukkit.event.HandlerList");
linkageTypeMethod.addLine("HandlerList.unregisterAll(" + instance + ");");
}
}