Skip to content

Commit

Permalink
Improved ReflectionUtil to deduplicate classes
Browse files Browse the repository at this point in the history
  • Loading branch information
foxfirecodes committed Apr 19, 2019
1 parent 63ca98a commit 96cc6c1
Showing 1 changed file with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
import java.util.stream.Collectors;

public class ReflectionUtil {
private static final Map<ClassLoader, List<Class<? extends Event>>> eventClassCache = new HashMap<>();
private static final Map<ClassLoader, List<Class<Event>>> eventClassCache = new HashMap<>();

public static List<Class<? extends Event>> getAllEventClasses() {
return Arrays.stream(Bukkit.getServer().getPluginManager().getPlugins())
.flatMap(plugin -> getEventClasses(plugin.getClass().getClassLoader()).stream())
.distinct()
.collect(Collectors.toList());
}

@SuppressWarnings({"unchecked", "UnstableApiUsage"})
public static List<Class<? extends Event>> getEventClasses(ClassLoader classLoader) {
public static List<Class<Event>> getEventClasses(ClassLoader classLoader) {
return eventClassCache.computeIfAbsent(classLoader, loader -> {
try {
return ClassPath.from(classLoader).getTopLevelClasses().stream()
Expand All @@ -39,7 +40,7 @@ public static List<Class<? extends Event>> getEventClasses(ClassLoader classLoad
return false;
}
})
.map(eventClass -> (Class<? extends Event>) eventClass)
.map(eventClass -> (Class<Event>) eventClass)
.collect(Collectors.toList());
} catch (IOException e) {
e.printStackTrace();
Expand Down

0 comments on commit 96cc6c1

Please sign in to comment.