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

io.quarkiverse.operatorsdk.common.ClassLoadingUtils Maven / Gradle / Ivy

There is a newer version: 6.8.0
Show newest version
package io.quarkiverse.operatorsdk.common;

import java.lang.reflect.InvocationTargetException;

public class ClassLoadingUtils {

    private ClassLoadingUtils() {
    }

    @SuppressWarnings({ "unchecked", "unused" })
    public static  Class loadClass(String className, Class expected) {
        try {
            return (Class) Thread.currentThread().getContextClassLoader().loadClass(className);
        } catch (Exception e) {
            throw new IllegalArgumentException("Couldn't find class " + className, e);
        }
    }

    public static  T instantiate(Class toInstantiate) {
        try {
            final var constructor = toInstantiate.getConstructor();
            constructor.setAccessible(true);
            return constructor.newInstance();
        } catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
            throw new IllegalArgumentException("Couldn't instantiate " + toInstantiate.getName(),
                    e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy