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

io.hypersistence.utils.hibernate.util.ClassLoaderUtils Maven / Gradle / Ivy

There is a newer version: 3.7.6
Show newest version
package io.hypersistence.utils.hibernate.util;

import java.io.InputStream;
import java.net.URL;

/**
 * ClassLoaderUtils - Class loading related utilities holder.
 *
 * @author Vlad Mihalcea
 * @since 2.1.0
 */
public final class ClassLoaderUtils {

    private ClassLoaderUtils() {
        throw new UnsupportedOperationException("ClassLoaderUtils is not instantiable!");
    }

    /**
     * Load the available ClassLoader
     *
     * @return ClassLoader
     */
    public static ClassLoader getClassLoader() {
        ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
        return (classLoader != null) ? classLoader : ClassLoaderUtils.class.getClassLoader();
    }

    /**
     * Load the Class denoted by the given string representation
     *
     * @param className class string representation
     * @param  class generic type
     * @return Class
     * @throws ClassNotFoundException if the class cannot be resolved
     */
    @SuppressWarnings("unchecked")
    public static  Class loadClass(String className) throws ClassNotFoundException {
        return (Class) getClassLoader().loadClass(className);
    }

    /**
     * Find if Class denoted by the given string representation is loadable
     *
     * @param className class string representation
     * @return Class
     */
    @SuppressWarnings("unchecked")
    public static boolean findClass(String className) {
        try {
            return getClassLoader().loadClass(className) != null;
        } catch (ClassNotFoundException e) {
            return false;
        } catch (NoClassDefFoundError e) {
            return false;
        }
    }

    /**
     * Get the resource URL
     *
     * @param resourceName resource name
     * @return resource URL
     */
    public static URL getResource(String resourceName) {
        return getClassLoader().getResource(resourceName);
    }

    /**
     * Get the resource InputStream
     *
     * @param resourceName resource name
     * @return resource InputStream
     */
    public static InputStream getResourceAsStream(String resourceName) {
        return getClassLoader().getResourceAsStream(resourceName);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy