From 51f690c9cb6cbaca0a2d5e3bb01e63aff1e6f2d4 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Fri, 16 Jul 2021 15:57:17 +0200 Subject: [PATCH] Removed the need to register updatable classes - added reflections --- .../java/com/willfp/eco/core/EcoPlugin.java | 9 --- .../core/config/updating/ConfigHandler.java | 7 -- eco-core/core-backend/build.gradle | 1 + .../config/updating/EcoConfigHandler.java | 68 +++++-------------- .../InvalidUpdatableClassException.java | 15 ---- .../core-plugin/src/main/resources/plugin.yml | 4 +- 6 files changed, 22 insertions(+), 82 deletions(-) delete mode 100644 eco-core/core-backend/src/main/java/com/willfp/eco/internal/config/updating/exceptions/InvalidUpdatableClassException.java diff --git a/eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java b/eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java index b3393e64..f683737e 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java +++ b/eco-api/src/main/java/com/willfp/eco/core/EcoPlugin.java @@ -67,11 +67,6 @@ public abstract class EcoPlugin extends JavaPlugin { @Getter private final Set loadedIntegrations = new HashSet<>(); - /** - * Set of classes to be processed on config update. - */ - private final List> updatableClasses = new ArrayList<>(); - /** * The internal plugin scheduler. */ @@ -362,16 +357,12 @@ public abstract class EcoPlugin extends JavaPlugin { } }); - updatableClasses.addAll(this.loadUpdatableClasses()); - this.loadListeners().forEach(listener -> this.getEventManager().registerListener(listener)); this.loadPluginCommands().forEach(PluginCommand::register); this.getScheduler().runLater(this::afterLoad, 1); - this.updatableClasses.forEach(clazz -> this.getConfigHandler().registerUpdatableClass(clazz)); - if (this.isSupportingExtensions()) { this.getExtensionLoader().loadExtensions(); diff --git a/eco-api/src/main/java/com/willfp/eco/core/config/updating/ConfigHandler.java b/eco-api/src/main/java/com/willfp/eco/core/config/updating/ConfigHandler.java index a736f53f..30b17f09 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/config/updating/ConfigHandler.java +++ b/eco-api/src/main/java/com/willfp/eco/core/config/updating/ConfigHandler.java @@ -9,13 +9,6 @@ public interface ConfigHandler { */ void callUpdate(); - /** - * Register an updatable class. - * - * @param updatableClass The class with an update method. - */ - void registerUpdatableClass(@NotNull Class updatableClass); - /** * Save all configs. */ diff --git a/eco-core/core-backend/build.gradle b/eco-core/core-backend/build.gradle index 59279308..93d6594c 100644 --- a/eco-core/core-backend/build.gradle +++ b/eco-core/core-backend/build.gradle @@ -4,4 +4,5 @@ version rootProject.version dependencies { compileOnly 'org.spigotmc:spigot-api:1.17-R0.1-SNAPSHOT' compileOnly 'me.clip:placeholderapi:2.10.9' + compileOnly 'org.reflections:reflections:0.9.12' } \ No newline at end of file diff --git a/eco-core/core-backend/src/main/java/com/willfp/eco/internal/config/updating/EcoConfigHandler.java b/eco-core/core-backend/src/main/java/com/willfp/eco/internal/config/updating/EcoConfigHandler.java index 966b73aa..b71457ea 100644 --- a/eco-core/core-backend/src/main/java/com/willfp/eco/internal/config/updating/EcoConfigHandler.java +++ b/eco-core/core-backend/src/main/java/com/willfp/eco/internal/config/updating/EcoConfigHandler.java @@ -2,83 +2,51 @@ package com.willfp.eco.internal.config.updating; import com.willfp.eco.core.EcoPlugin; import com.willfp.eco.core.PluginDependent; +import com.willfp.eco.core.config.interfaces.LoadableConfig; import com.willfp.eco.core.config.updating.ConfigHandler; import com.willfp.eco.core.config.updating.ConfigUpdater; -import com.willfp.eco.core.config.interfaces.LoadableConfig; -import com.willfp.eco.internal.config.updating.exceptions.InvalidUpdatableClassException; import com.willfp.eco.internal.config.updating.exceptions.InvalidUpdateMethodException; import com.willfp.eco.internal.config.yaml.EcoUpdatableYamlConfig; import org.jetbrains.annotations.NotNull; +import org.reflections.Reflections; +import org.reflections.scanners.MethodAnnotationsScanner; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; import java.util.List; -import java.util.Set; public class EcoConfigHandler extends PluginDependent implements ConfigHandler { - /** - * A set of all configs that can be saved. - */ private final List configs = new ArrayList<>(); - /** - * A set of all classes that can be updated. - */ - private final Set> updatableClasses = new HashSet<>(); + private final Reflections reflections = new Reflections(this.getPlugin().getClass().getPackageName(), new MethodAnnotationsScanner()); - /** - * Creates a new config handler and links it to an {@link EcoPlugin}. - * - * @param plugin The plugin to manage. - */ public EcoConfigHandler(@NotNull final EcoPlugin plugin) { super(plugin); } @Override public void callUpdate() { - updatableClasses.forEach(clazz -> Arrays.stream(clazz.getDeclaredMethods()).forEach(method -> { - if (method.isAnnotationPresent(ConfigUpdater.class)) { - if (!Modifier.isStatic(method.getModifiers())) { - throw new InvalidUpdateMethodException("Update method must be static."); - } - - try { - if (method.getParameterCount() == 0) { - method.invoke(null); - } else if (method.getParameterCount() == 1) { - method.invoke(null, this.getPlugin()); - } else { - throw new InvalidUpdateMethodException("Update method must have 0 parameters or a plugin parameter."); - } - } catch (IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); - throw new InvalidUpdateMethodException("Update method generated an exception."); - } + for (Method method : reflections.getMethodsAnnotatedWith(ConfigUpdater.class)) { + if (!Modifier.isStatic(method.getModifiers())) { + throw new InvalidUpdateMethodException("Update method must be static."); } - })); - } - @Override - public void registerUpdatableClass(@NotNull final Class updatableClass) { - boolean isValid = false; - for (Method method : updatableClass.getDeclaredMethods()) { - if (Modifier.isStatic(method.getModifiers()) && (method.getParameterCount() == 0 || method.getParameterCount() == 1) && method.isAnnotationPresent(ConfigUpdater.class)) { - isValid = true; - break; + try { + if (method.getParameterCount() == 0) { + method.invoke(null); + } else if (method.getParameterCount() == 1) { + method.invoke(null, this.getPlugin()); + } else { + throw new InvalidUpdateMethodException("Update method must have 0 parameters or a plugin parameter."); + } + } catch (IllegalAccessException | InvocationTargetException e) { + e.printStackTrace(); + throw new InvalidUpdateMethodException("Update method generated an exception."); } } - - if (!isValid) { - throw new InvalidUpdatableClassException("Registered updatable class " + updatableClass + " must have an annotated static method with no modifiers."); - } - - updatableClasses.add(updatableClass); } @Override diff --git a/eco-core/core-backend/src/main/java/com/willfp/eco/internal/config/updating/exceptions/InvalidUpdatableClassException.java b/eco-core/core-backend/src/main/java/com/willfp/eco/internal/config/updating/exceptions/InvalidUpdatableClassException.java deleted file mode 100644 index d1ca769f..00000000 --- a/eco-core/core-backend/src/main/java/com/willfp/eco/internal/config/updating/exceptions/InvalidUpdatableClassException.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.willfp.eco.internal.config.updating.exceptions; - -import com.willfp.eco.internal.config.updating.EcoConfigHandler; -import org.jetbrains.annotations.NotNull; - -public class InvalidUpdatableClassException extends RuntimeException { - /** - * Called when an updatable class is registered into an {@link EcoConfigHandler}. - * - * @param message The error message. - */ - public InvalidUpdatableClassException(@NotNull final String message) { - super(message); - } -} diff --git a/eco-core/core-plugin/src/main/resources/plugin.yml b/eco-core/core-plugin/src/main/resources/plugin.yml index f70563e9..082abf0d 100644 --- a/eco-core/core-plugin/src/main/resources/plugin.yml +++ b/eco-core/core-plugin/src/main/resources/plugin.yml @@ -25,4 +25,6 @@ softdepend: - Spartan - PlaceholderAPI - mcMMO - - CombatLogX \ No newline at end of file + - CombatLogX +libraries: + - org.reflections:reflections:0.9.12 \ No newline at end of file