Skip to content

Instantly share code, notes, and snippets.

@IdanKoblik
Last active June 5, 2024 06:51
  • Select an option

Select an option

Revisions

  1. IdanKoblik revised this gist Jun 4, 2024. 1 changed file with 6 additions and 1 deletion.
    7 changes: 6 additions & 1 deletion DynamicInstantiator.java
    Original 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) {
    SheepWarsPlugin.getInstance().getLogger().info(e.getMessage());
    log.error(e.getMessage());
    }
    }
    }
  2. IdanKoblik created this gist Jun 4, 2024.
    48 changes: 48 additions & 0 deletions DynamicInstantiator.java
    Original 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());
    }
    }
    }
    }