From 7f885db883a54f55dd8ebbe9b5e5dfaa17b55934 Mon Sep 17 00:00:00 2001 From: Tinglyyy Date: Mon, 29 Sep 2025 15:11:21 +0200 Subject: [PATCH] Added reflections utility --- .idea/misc.xml | 2 +- pom.xml | 8 +++ .../reflections/GenericReflectClass.java | 18 +++++ .../processing/AnnotationProcessor.java | 68 +++++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 src/main/java/dev/unlegitdqrk/unlegitlibrary/reflections/GenericReflectClass.java create mode 100644 src/main/java/dev/unlegitdqrk/unlegitlibrary/reflections/annotation/processing/AnnotationProcessor.java diff --git a/.idea/misc.xml b/.idea/misc.xml index 41c898a..001e756 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -8,7 +8,7 @@ - + \ No newline at end of file diff --git a/pom.xml b/pom.xml index 5f59358..728488b 100644 --- a/pom.xml +++ b/pom.xml @@ -35,6 +35,14 @@ + + + org.reflections + reflections + 0.10.2 + + + Issue Tracker https://repo.unlegitdqrk.dev/UnlegitDqrk/unlegitlibrary/issues diff --git a/src/main/java/dev/unlegitdqrk/unlegitlibrary/reflections/GenericReflectClass.java b/src/main/java/dev/unlegitdqrk/unlegitlibrary/reflections/GenericReflectClass.java new file mode 100644 index 0000000..a9f3cb8 --- /dev/null +++ b/src/main/java/dev/unlegitdqrk/unlegitlibrary/reflections/GenericReflectClass.java @@ -0,0 +1,18 @@ +// Author: maple +// date: 9/29/25 + +package dev.unlegitdqrk.unlegitlibrary.reflections; + +import java.lang.reflect.ParameterizedType; + +/** + * Implements utility for handling the Generic type + */ +public abstract class GenericReflectClass { + protected final Class persistentClass; + public GenericReflectClass() { + this.persistentClass = (Class) + ((ParameterizedType)getClass().getGenericSuperclass()) + .getActualTypeArguments()[0]; + } +} diff --git a/src/main/java/dev/unlegitdqrk/unlegitlibrary/reflections/annotation/processing/AnnotationProcessor.java b/src/main/java/dev/unlegitdqrk/unlegitlibrary/reflections/annotation/processing/AnnotationProcessor.java new file mode 100644 index 0000000..f30d86e --- /dev/null +++ b/src/main/java/dev/unlegitdqrk/unlegitlibrary/reflections/annotation/processing/AnnotationProcessor.java @@ -0,0 +1,68 @@ +// Author: maple +// date: 9/29/25 + +package dev.unlegitdqrk.unlegitlibrary.reflections.annotation.processing; + +import dev.unlegitdqrk.unlegitlibrary.reflections.GenericReflectClass; +import org.reflections.Reflections; + +import java.lang.annotation.Annotation; +import java.lang.reflect.Constructor; +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.util.Set; + +public abstract class AnnotationProcessor extends GenericReflectClass { + protected Set> projectClasses; + + protected Set> annotatedTypes; + + protected Set annotatedMethods; + + protected Set annotatedFields; + + protected Set annotatedConstructors; + + private final Reflections reflections; + + public AnnotationProcessor(String packageName) { + super(); + + this.reflections = new Reflections(packageName); + + // load local sets + this.reload(); + } + + /** + * reload local sets + */ + public void reload() { + this.projectClasses = this.reflections.getSubTypesOf(Object.class); + + this.annotatedTypes = this.reflections.getTypesAnnotatedWith(this.persistentClass); + this.annotatedMethods = this.reflections.getMethodsAnnotatedWith(this.persistentClass); + this.annotatedFields = this.reflections.getFieldsAnnotatedWith(this.persistentClass); + this.annotatedConstructors = this.reflections.getConstructorsAnnotatedWith(this.persistentClass); + } + + public void process() { + for(Class type : this.annotatedTypes) + processType(type); + + for(Method method : this.annotatedMethods) + processMethod(method); + + for(Field field : this.annotatedFields) + processField(field); + + for(Constructor constructor : this.annotatedConstructors) + processConstructor(constructor); + } + + + protected abstract void processType(Class type); + protected abstract void processMethod(Method method); + protected abstract void processField(Field field); + protected abstract void processConstructor(Constructor constructor); +}