Class ClassUtils

    • Method Detail

      • getClassLoader

        public static ClassLoader getClassLoader​(Object o)
        Detect the right ClassLoader. The lookup order is determined by:
        1. ContextClassLoader of the current Thread
        2. ClassLoader of the given Object 'o'
        3. ClassLoader of this very ClassUtils class
        Parameters:
        o - if not null it may get used to detect the classloader.
        Returns:
        The ClassLoader which should get used to create new instances
      • isProxyableClass

        public static boolean isProxyableClass​(Type type)
        Checks whether the CDI rules for proxyable beans are met. See CDI spec unproxyable bean types
        Parameters:
        type - type
        Returns:
        true if all proxy conditions are met, false otherwise
      • tryToLoadClassForName

        public static <T> Class<T> tryToLoadClassForName​(String name,
                                                         Class<T> targetType)
        Tries to load a class based on the given name and interface or abstract class.
        Type Parameters:
        T - current type
        Parameters:
        name - name of the concrete class
        targetType - target type (interface or abstract class)
        Returns:
        loaded class or null if it isn't in the classpath
      • tryToLoadClassForName

        public static <T> Class<T> tryToLoadClassForName​(String name,
                                                         Class<T> targetType,
                                                         ClassLoader classLoader)
        Tries to load a class based on the given name and interface or abstract class.
        Type Parameters:
        T - current type
        Parameters:
        name - name of the concrete class
        targetType - target type (interface or abstract class)
        classLoader - The ClassLoader.
        Returns:
        loaded class or null if it isn't in the classpath
      • tryToLoadClassForName

        public static Class tryToLoadClassForName​(String name)
        Tries to load a class based on the given name
        Parameters:
        name - name of the class
        Returns:
        loaded class or null if it isn't in the classpath
      • tryToLoadClassForName

        public static Class tryToLoadClassForName​(String name,
                                                  ClassLoader classLoader)
        Tries to load a class based on the given name
        Parameters:
        name - name of the class
        classLoader - The ClassLoader.
        Returns:
        loaded class or null if it isn't in the classpath
      • tryToInstantiateClass

        public static <T> T tryToInstantiateClass​(Class<T> targetClass)
        Instantiates a given class via the default constructor
        Type Parameters:
        T - current type
        Parameters:
        targetClass - class which should be instantiated
        Returns:
        created instance or null if the instantiation failed
      • tryToInstantiateClassForName

        public static <T> T tryToInstantiateClassForName​(String className,
                                                         Class<T> targetType)
        Tries to instantiate a class for the given name and type via the default constructor
        Type Parameters:
        T - current type
        Parameters:
        className - name of the class
        targetType - target type
        Returns:
        created instance or null if the instantiation failed
      • tryToInstantiateClassForName

        public static Object tryToInstantiateClassForName​(String className)
        Tries to instantiate a class for the given name via the default constructor
        Parameters:
        className - name of the class
        Returns:
        created instance or null if the instantiation failed
      • getJarVersion

        public static String getJarVersion​(Class targetClass)
        Reads the version of the jar which contains the given class
        Parameters:
        targetClass - class within the jar
        Returns:
        version-string which has been found in the manifest or null if there is no version information available
      • getRevision

        public static String getRevision​(Class targetClass)
        Reads the VCS revision which was used for creating the jar
        Parameters:
        targetClass - class within the jar
        Returns:
        revision-string which has been found in the manifest or null if there is no information available
      • containsMethod

        public static boolean containsMethod​(Class<?> targetClass,
                                             Method method)
        Checks if the given class contains a method with the same signature.
        Parameters:
        targetClass - The class to check
        method - The source method
        Returns:
        if it contains a method with the same signature.
      • extractMethod

        public static Method extractMethod​(Class<?> clazz,
                                           Method sourceMethod)
        Extracts a method with same signature as the source method.
        Parameters:
        clazz - The target class
        sourceMethod - The source method.
        Returns:
        the extracted method or null
      • containsPossiblyGenericMethod

        public static boolean containsPossiblyGenericMethod​(Class<?> targetClass,
                                                            Method method)
        Checks if the given class has a method with the same signature, taking in to account generic types
        Parameters:
        targetClass - class
        method - method
        Returns:
        if it contains a method with the same signature.
      • extractPossiblyGenericMethod

        public static Method extractPossiblyGenericMethod​(Class<?> clazz,
                                                          Method sourceMethod)
        Extracts a method matching the source method, allowing generic type parameters to be substituted as long as they are properly castable.
        Parameters:
        clazz - The target class
        sourceMethod - The source method.
        Returns:
        the extracted method or null
      • returns

        public static boolean returns​(Method method,
                                      Class<?> clazz)
        returns
        Parameters:
        method - method
        clazz - class
        Returns:
        values