From a19aad7038661f533a6b3fd1aeda3cab20793025 Mon Sep 17 00:00:00 2001 From: JeremyStarTM Date: Sun, 9 Jun 2024 19:05:30 +0200 Subject: [PATCH] Add event support --- base/build.gradle | 3 + .../staropensource/sosengine/base/Engine.java | 11 ++ .../base/annotations/EventListener.java | 15 +++ .../base/annotations/package-info.java | 6 + .../sosengine/base/classes/Event.java | 113 ++++++++++++++++++ gradle.properties | 1 + 6 files changed, 149 insertions(+) create mode 100644 base/src/main/java/de/staropensource/sosengine/base/annotations/EventListener.java create mode 100644 base/src/main/java/de/staropensource/sosengine/base/annotations/package-info.java create mode 100644 base/src/main/java/de/staropensource/sosengine/base/classes/Event.java diff --git a/base/build.gradle b/base/build.gradle index 0791cab..8ffdb92 100644 --- a/base/build.gradle +++ b/base/build.gradle @@ -15,6 +15,9 @@ dependencies { // ANSI support implementation 'org.fusesource.jansi:jansi:' + project.dependencyJansi + // Reflections + implementation 'org.reflections:reflections:' + project.dependencyReflections + // -> Testing <- // Jetbrains Annotations testCompileOnly 'org.jetbrains:annotations:' + project.dependencyJetbrainsAnnotations diff --git a/base/src/main/java/de/staropensource/sosengine/base/Engine.java b/base/src/main/java/de/staropensource/sosengine/base/Engine.java index 20e1587..b05ed3d 100644 --- a/base/src/main/java/de/staropensource/sosengine/base/Engine.java +++ b/base/src/main/java/de/staropensource/sosengine/base/Engine.java @@ -91,6 +91,9 @@ public class Engine { // Initialize classes initializeClasses(); + // Initialize events + initializeEvents(); + // Populate crash content populateCrashContent(); @@ -116,6 +119,14 @@ public class Engine { new ShortcodeConverter(); } + /** + * Initializes all events. + * + * @since 1-alpha0 + */ + protected void initializeEvents() { + } + /** * This method populates the Crash Handler's content with the default set of content. * diff --git a/base/src/main/java/de/staropensource/sosengine/base/annotations/EventListener.java b/base/src/main/java/de/staropensource/sosengine/base/annotations/EventListener.java new file mode 100644 index 0000000..8cc569b --- /dev/null +++ b/base/src/main/java/de/staropensource/sosengine/base/annotations/EventListener.java @@ -0,0 +1,15 @@ +package de.staropensource.sosengine.base.annotations; + +import de.staropensource.sosengine.base.classes.Event; + +import java.lang.annotation.*; + +/** + * Annotation for registering events on methods. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.METHOD) +@Documented +public @interface EventListener { + Class event(); +} diff --git a/base/src/main/java/de/staropensource/sosengine/base/annotations/package-info.java b/base/src/main/java/de/staropensource/sosengine/base/annotations/package-info.java new file mode 100644 index 0000000..af31de9 --- /dev/null +++ b/base/src/main/java/de/staropensource/sosengine/base/annotations/package-info.java @@ -0,0 +1,6 @@ +/** + * Contains annotations. + * + * @since 1-alpha0 + */ +package de.staropensource.sosengine.base.annotations; diff --git a/base/src/main/java/de/staropensource/sosengine/base/classes/Event.java b/base/src/main/java/de/staropensource/sosengine/base/classes/Event.java new file mode 100644 index 0000000..015a4c9 --- /dev/null +++ b/base/src/main/java/de/staropensource/sosengine/base/classes/Event.java @@ -0,0 +1,113 @@ +package de.staropensource.sosengine.base.classes; + +import de.staropensource.sosengine.base.annotations.EventListener; +import de.staropensource.sosengine.base.logging.Logger; +import de.staropensource.sosengine.base.types.CodePart; +import de.staropensource.sosengine.base.types.LogIssuer; +import lombok.AccessLevel; +import lombok.Getter; +import org.jetbrains.annotations.NotNull; +import org.reflections.Reflections; +import org.reflections.scanners.Scanners; +import org.reflections.util.ClasspathHelper; +import org.reflections.util.ConfigurationBuilder; + +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +/** + * Represents an event. + * + * @since 1-alpha0 + */ +@Getter +@SuppressWarnings({ "unused", "JavadocDeclaration", "JavadocBlankLines" }) +public abstract class Event { + /** + * Instance. + * + * @since 1-alpha0 + * + * -- GETTER -- + * Returns the extending {@link Event} instance. + * + * @return extending {@link Event} instance + * @since 1-alpha0 + */ + @Getter() + protected static Event instance; + + /** + * The name of the event. + * + * @since 1-alpha0 + * + * -- GETTER -- + * Returns the name of the event. + * + * @return the event name + * @since 1-alpha0 + */ + @NotNull + private final String eventName; + + /** + * Constructor. + * + * @since 1-alpha0 + */ + public Event() { + this.eventName = this.getClass().getName().replace(this.getClass().getPackage() + ".", ""); + + // Only allow one instance + if (instance == null) + instance = this; + else + Logger.crash(new LogIssuer(getClass(), CodePart.ENGINE), "Tried reinitializing " + getClass().getName() + " twice"); + } + + /** + * Returns all annotated methods. + * + * @return list of annotated methods + * @since 1-alpha0 + */ + @NotNull + protected List getAnnotatedMethods() { + List methods = new ArrayList<>(); + + Reflections reflections = new Reflections( + new ConfigurationBuilder() + .setUrls(ClasspathHelper.forJavaClassPath()) + .setScanners(Scanners.MethodsAnnotated) + ); + Set annotatedMethods = reflections.getMethodsAnnotatedWith(EventListener.class); + + for (Method method : annotatedMethods) + if (method.getAnnotation(EventListener.class).event() == this.getClass()) + methods.add(method); + + return methods; + } + + /** + * Invokes all annotated methods without any arguments. + */ + protected void invokeAnnotatedMethods() { + for (Method method : getAnnotatedMethods()) { + try { + method.invoke(null); + } catch (IllegalAccessException | InvocationTargetException ignored) {} + } + } + + /** + * Calls the event and notifies all annotated methods about it. + * + * @since 1-alpha0 + */ + public abstract void callEvent(); +} diff --git a/gradle.properties b/gradle.properties index adb2e89..94c0819 100644 --- a/gradle.properties +++ b/gradle.properties @@ -14,6 +14,7 @@ dependencyJoor=0.9.14 # Subsystem dependencies dependencyJansi=2.4.1 +dependencyReflections=0.10.2 # Plugins pluginShadow=8.1.1