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

se.hiq.oss.commons.reflection.util.ClassUtils Maven / Gradle / Ivy

The newest version!
package se.hiq.oss.commons.reflection.util;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

import se.hiq.oss.commons.reflection.filter.ClassFilter;


/**
 * Class loading Utilities.
 *
 * @author rikardwi
 **/
public final class ClassUtils {

    private static final Map> PRIMITIVE_CLASSES = new HashMap>();

    static {
        PRIMITIVE_CLASSES.put("byte", byte.class);
        PRIMITIVE_CLASSES.put("short", short.class);
        PRIMITIVE_CLASSES.put("char", char.class);
        PRIMITIVE_CLASSES.put("int", int.class);
        PRIMITIVE_CLASSES.put("long", long.class);
        PRIMITIVE_CLASSES.put("float", float.class);
        PRIMITIVE_CLASSES.put("double", double.class);
        PRIMITIVE_CLASSES.put("boolean", boolean.class);
        PRIMITIVE_CLASSES.put("[B", byte[].class);
        PRIMITIVE_CLASSES.put("[Lbyte", byte[].class);
        PRIMITIVE_CLASSES.put("[S", short[].class);
        PRIMITIVE_CLASSES.put("[Lshort", short[].class);
        PRIMITIVE_CLASSES.put("[C", char[].class);
        PRIMITIVE_CLASSES.put("[Lchar", char[].class);
        PRIMITIVE_CLASSES.put("[I", int[].class);
        PRIMITIVE_CLASSES.put("[Lint", int[].class);
        PRIMITIVE_CLASSES.put("[L", long[].class);
        PRIMITIVE_CLASSES.put("[Llong", long[].class);
        PRIMITIVE_CLASSES.put("[F", float[].class);
        PRIMITIVE_CLASSES.put("[Lfloat", float[].class);
        PRIMITIVE_CLASSES.put("[D", double[].class);
        PRIMITIVE_CLASSES.put("[Ldouble", double[].class);
        PRIMITIVE_CLASSES.put("[Z", boolean[].class);
        PRIMITIVE_CLASSES.put("[Lboolean", boolean[].class);

    }

    private ClassUtils() {
    }

    public static Class getPrimitiveClass(String primitiveClassName) {
        return PRIMITIVE_CLASSES.get(primitiveClassName);
    }


    /**
     * Returns a set of interfaces from the target class that passes the supplied filter.
     * This method also inspects any interfaces implemented by super classes.
     *
     * @param target The class to inspect.
     * @param filter The class filter to use.
     *
     * @return a set of interfaces from the target class that passes the supplied filter.
     */
    public static Set> getInterfaces(Class target, ClassFilter filter) {
        Class clazz = target;
        Set> interfacesFound = getDeclaredInterfaces(clazz, filter);

        while ((clazz = clazz.getSuperclass()) != null) {
            interfacesFound.addAll(getDeclaredInterfaces(clazz, filter));
        }
        return interfacesFound;

    }

    /**
     * Returns the interface from target class that passes the supplied filter.
     * This method also inspects any interfaces implemented by super classes.
     * 

* If no interface is found null is returned. * * @param target The class to inspect. * @param filter The class filter to use. * * @return the interface from target class that passes the supplied filter, may * be null if no match is found. */ public static Class getInterface(Class target, ClassFilter filter) { Set> interfaces = getInterfaces(target, filter); if (!interfaces.isEmpty()) { return interfaces.iterator().next(); } return null; } /** * Returns a set of interfaces that the from clazz that passes the supplied filter. * * @param clazz The class to inspect * @param filter The class filter to use. * * @return all Interface classes from clazz that passes the filter. */ public static Set> getDeclaredInterfaces(Class clazz, ClassFilter filter) { Set> interfacesFound = new HashSet>(); Class[] interfaces = clazz.getInterfaces(); for (Class interfaceClass : interfaces) { if (filter.apply(interfaceClass)) { interfacesFound.add(interfaceClass); } } return interfacesFound; } /** * Loads and returns the class named className of type superClass. * * @param Type of the class * @param className Name of the class to load * @param ofType Type of class * * @return The loaded class of type superClass. * @throws IllegalArgumentException if the class with className could not be loaded or * if the that class does not extend the class supplied in the superClass parameter. **/ @SuppressWarnings("unchecked") public static Class loadClass(String className, Class ofType) { try { Class clazz = Class.forName(className); if (ofType == null || !ofType.isAssignableFrom(clazz)) { throw new IllegalArgumentException("Class " + className + " must extend or implement " + ofType + "!"); } return (Class) clazz; } catch (ClassNotFoundException cnfe) { throw new IllegalArgumentException("Class " + className + " could not be found!", cnfe); } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy