Replace reflections

This commit is contained in:
Zontreck 2024-02-13 02:16:58 -07:00
parent ccddf83a7c
commit 702b68a7ac
2 changed files with 20 additions and 10 deletions

View file

@ -60,7 +60,7 @@ repositories {
} }
dependencies { dependencies {
provided "org.reflections:reflections:0.10.2" provided "com.google.guava:guava:31.1-jre"
} }

View file

@ -1,13 +1,14 @@
package dev.zontreck.eventsbus; package dev.zontreck.eventsbus;
import com.google.common.reflect.ClassPath;
import dev.zontreck.eventsbus.annotations.EventSubscriber; import dev.zontreck.eventsbus.annotations.EventSubscriber;
import dev.zontreck.eventsbus.annotations.Priority; import dev.zontreck.eventsbus.annotations.Priority;
import dev.zontreck.eventsbus.annotations.SingleshotEvent; import dev.zontreck.eventsbus.annotations.SingleshotEvent;
import dev.zontreck.eventsbus.annotations.Subscribe; import dev.zontreck.eventsbus.annotations.Subscribe;
import dev.zontreck.eventsbus.events.EventBusReadyEvent; import dev.zontreck.eventsbus.events.EventBusReadyEvent;
import dev.zontreck.eventsbus.events.ResetEventBusEvent; import dev.zontreck.eventsbus.events.ResetEventBusEvent;
import org.reflections.Reflections;
import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method; import java.lang.reflect.Method;
@ -102,19 +103,28 @@ public class EventDispatcher
for(Package pkg : packages) for(Package pkg : packages)
{ {
String packageName = pkg.getName(); try {
Reflections reflections = new Reflections(packageName); String packageName = pkg.getName();
var classes = reflections.getSubTypesOf(Object.class).stream().collect(Collectors.toList());
for(Class<?> clazz : classes) var classes = ClassPath.from(ClassLoader.getSystemClassLoader())
{ .getAllClasses()
if(clazz.getPackage().getName().equalsIgnoreCase(packageName)) .stream()
.filter(clz->clz.getPackageName().equals(packageName))
.map(c->c.load())
.collect(Collectors.toList());
for(Class<?> clazz : classes)
{ {
if(clazz.getPackage().getName().equalsIgnoreCase(packageName))
{
if(clazz.isAnnotationPresent(EventSubscriber.class)) if(clazz.isAnnotationPresent(EventSubscriber.class))
loaded.add(clazz); loaded.add(clazz);
}
} }
} catch (IOException e) {
throw new RuntimeException(e);
} }
} }