Last active
June 5, 2024 06:51
Revisions
-
IdanKoblik revised this gist
Jun 4, 2024 . 1 changed file with 6 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,12 @@ import lombok.extern.log4j.Log4j2; import com.google.common.reflect.ClassPath; import java.util.function.Consumer; /** * The DynamicInstantiator class is a utility class that provides a way to dynamically instantiate * and register classes that implement a specific base class. */ @Log4J2 public class DynamicInstantiator { private static DynamicInstantiator instance; @@ -41,7 +46,7 @@ public <T> void register(String packageName, Class<T> baseClass, Consumer<T> act if (baseClass.isAssignableFrom(clazz) && !clazz.equals(baseClass)) action.accept(clazz.asSubclass(baseClass).getDeclaredConstructor().newInstance()); } catch (Throwable e) { log.error(e.getMessage()); } } } -
IdanKoblik created this gist
Jun 4, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,48 @@ /** * The DynamicInstantiator class is a utility class that provides a way to dynamically instantiate * and register classes that implement a specific base class. */ public class DynamicInstantiator { private static DynamicInstantiator instance; /** * Returns the singleton instance of the DynamicInstantiator class. * * @return The instance of the DynamicInstantiator class. */ public static DynamicInstantiator getInstance() { return instance == null ? (instance = new DynamicInstantiator()) : instance; } /** * Dynamically instantiates and registers classes that extend or implement the specified baseClass * within the given packageName. For each found class, the provided action is executed with the * instantiated object as a parameter. * * @param packageName The package name to scan for classes. * @param baseClass The base class that the desired classes should extend or implement. * @param action The action to perform on each found class, represented as a Consumer lambda. * @param <T> The type parameter for the baseClass. */ public <T> void register(String packageName, Class<T> baseClass, Consumer<T> action) { ClassLoader classLoader = getClass().getClassLoader(); ClassPath classPath; try { classPath = ClassPath.from(classLoader); } catch (Exception e) { throw new RuntimeException(e); } for (ClassPath.ClassInfo classInfo : classPath.getTopLevelClassesRecursive(packageName)) { try { Class<?> clazz = Class.forName(classInfo.getName(), true, classLoader); if (baseClass.isAssignableFrom(clazz) && !clazz.equals(baseClass)) action.accept(clazz.asSubclass(baseClass).getDeclaredConstructor().newInstance()); } catch (Throwable e) { SheepWarsPlugin.getInstance().getLogger().info(e.getMessage()); } } } }