From a73f428bc486c3013bdee2ebdfe795782508a35f Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sat, 7 Nov 2020 15:00:47 +0100 Subject: [PATCH] add ReflexionUtil.getAllSubclasses(Class) with 10 minutes cache (because it takes time to compute the result) --- pom.xml | 32 +++++++++++++++++++ .../java/fr/pandacube/util/ReflexionUtil.java | 30 +++++++++++++++++ 2 files changed, 62 insertions(+) diff --git a/pom.xml b/pom.xml index b841f65..a0fe38a 100644 --- a/pom.xml +++ b/pom.xml @@ -33,5 +33,37 @@ 1.2 compile + + io.github.classgraph + classgraph + 4.8.90 + compile + + + + + + org.apache.maven.plugins + maven-shade-plugin + 2.4.3 + + + + package + + shade + + + + + io.github.classgraph:classgraph + + + + + + + + diff --git a/src/main/java/fr/pandacube/util/ReflexionUtil.java b/src/main/java/fr/pandacube/util/ReflexionUtil.java index c087699..a07474c 100644 --- a/src/main/java/fr/pandacube/util/ReflexionUtil.java +++ b/src/main/java/fr/pandacube/util/ReflexionUtil.java @@ -2,6 +2,15 @@ package fr.pandacube.util; import java.lang.reflect.Field; import java.lang.reflect.Method; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; + +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; + +import io.github.classgraph.ClassGraph; +import io.github.classgraph.ScanResult; public class ReflexionUtil { @@ -150,6 +159,27 @@ public class ReflexionUtil { + private static Cache, List>> subClassesLists = CacheBuilder.newBuilder() + .expireAfterAccess(10, TimeUnit.MINUTES) + .build(); + + public static List> getAllSubclasses(Class clazz) { + try { + @SuppressWarnings("unchecked") + List> classes = (List>) (List) subClassesLists.get(clazz, () -> { + try (ScanResult scanResult = new ClassGraph().enableClassInfo().ignoreClassVisibility().scan()) { + return scanResult.getSubclasses(clazz.getName()).loadClasses(); + } + }); + return classes; + } catch(ExecutionException e) { + Log.severe(e); + return null; + } + + } + + }