All Downloads are FREE. Search and download functionalities are using the official Maven repository.

me.deecaad.core.file.JarInstancer Maven / Gradle / Ivy

package me.deecaad.core.file;

import me.deecaad.core.utils.LogLevel;
import org.jetbrains.annotations.NotNull;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarFile;

import static me.deecaad.core.MechanicsCore.debug;

public class JarInstancer extends JarSearcher {

    public JarInstancer(@NotNull JarFile jar) {
        super(jar);
    }

    public  List createAllInstances(Class clazz, ClassLoader classLoader, boolean isIgnoreAbstract, Class... classes) {
        List> validClasses = findAllSubclasses(clazz, classLoader, isIgnoreAbstract, classes);

        List instances = new ArrayList<>();
        for (Class validClass : validClasses) {
            Constructor emptyConstructor;
            try {
                emptyConstructor = validClass.getConstructor();
            } catch (NoSuchMethodException e) {
                debug.log(LogLevel.ERROR,
                    "Found an class implementing " + clazz.getSimpleName() + " class which didn't have empty constructor!",
                    "Please add empty constructor for class " + validClass.getSimpleName());
                continue;
            }
            try {
                // noinspection unchecked
                T instance = (T) emptyConstructor.newInstance();
                instances.add(instance);

            } catch (InstantiationException | IllegalAccessException | InvocationTargetException e) {
                e.printStackTrace();
            }
        }

        if (instances.isEmpty())
            debug.warn("Did not instantiate anything? For " + clazz);
        return instances;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy