Class ClassUtil


  • public final class ClassUtil
    extends Object
    Utility classes with respect to the class operations.
    Since:
    1.0
    Author:
    Gurkan Erdogdu
    • Field Detail

      • PRIMITIVE_TO_WRAPPERS_MAP

        public static final Map<Class<?>,​Class<?>> PRIMITIVE_TO_WRAPPERS_MAP
      • DEFAULT_VALUES_MAP

        public static final Map<Class<?>,​Object> DEFAULT_VALUES_MAP
      • NO_TYPES

        public static final Type[] NO_TYPES
    • Method Detail

      • getClassFromName

        public static Class<?> getClassFromName​(String name)
      • getClassFromName

        public static Class<?> getClassFromName​(String name,
                                                ClassLoader providedLoader,
                                                boolean init)
      • isInnerClazz

        public static boolean isInnerClazz​(Class<?> clazz)
        Check the class is inner or not
        Parameters:
        clazz - to check
        Returns:
        true or false
      • isSame

        public static boolean isSame​(Type type1,
                                     Type type2)
      • getPrimitiveWrapper

        public static Class<?> getPrimitiveWrapper​(Class<?> clazz)
      • getDefaultValue

        public static Object getDefaultValue​(Class<?> type)
      • getClass

        public static Class<?> getClass​(Type type)
        Gets the class of the given type arguments.

        If the given type Type parameters is an instance of the ParameterizedType, it returns the raw type otherwise it return the casted Class of the type argument.

        Parameters:
        type - class or parametrized type
        Returns:
      • isMethodHasCheckedException

        public static boolean isMethodHasCheckedException​(Method method)
        Check method throws checked exception or not.
        Parameters:
        method - method instance
      • callInstanceMethod

        public static Object callInstanceMethod​(Method method,
                                                Object instance,
                                                Object[] args)
        Call method on the instance with given arguments.
        Parameters:
        method - method instance
        instance - object instance
        args - arguments
        Returns:
        the method result
      • getNonPrivateMethods

        public static List<Method> getNonPrivateMethods​(Class<?> topClass,
                                                        boolean excludeFinalMethods)
        collect all non-private, non-static and non-abstract methods from the given class. This method removes any overloaded methods from the list automatically. We also do skip bridge methods as they exist for and are handled solely by the JVM itself. The returned Map contains the methods divided by the methodName as key in the map following all the methods with the same methodName in a List. There is some special rule for package-private methods. Any non-visible package-private method will get skipped and treated similarly to private methods. Note: we filter out the Object.finalize() method as users must not deal with it.
        Parameters:
        topClass - the class to start with. Then move up the hierarchy
        excludeFinalMethods - whether final classes should get excluded from the result
      • getAbstractMethods

        public static List<Method> getAbstractMethods​(Class<?> clazz)
        collect all abstract methods for the given class if the given class is Modifier.ABSTRACT
        Parameters:
        clazz - Class to check
        Returns:
        List with all abstract methods of the given class or an empty list if the given class is not abstract or doesn't contain an abstract method
      • isMethodDeclared

        public static boolean isMethodDeclared​(Class<?> clazz,
                                               String name,
                                               Class<?>... parameterTypes)
        Checks, if the given Class declares the Method with the given name und parameterTypes.
        Parameters:
        clazz - to check
        name - of the method
        parameterTypes - of the method
        Returns:
        true if the given class contains a method with the given name and parameterTypes, otherwise false
      • isMethodImplemented

        public static boolean isMethodImplemented​(Class<?> clazz,
                                                  Class<?> interfase,
                                                  String methodName,
                                                  Class<?>... parameterTypes)
        Checks, if the given Class implements the Method with the given name und parameterTypes. Returns false if the method is only a default method in the interface!
        Parameters:
        clazz - to check
        interfase - the Interface which declares the method
        methodName - of the method
        parameterTypes - of the method
        Returns:
        true if the given class contains a method with the given name and parameterTypes, otherwise false
      • isOverridden

        public static boolean isOverridden​(List<Method> subclassMethods,
                                           Method superclassMethod)
        Check if the method is already defined in a subclass
        Parameters:
        subclassMethods -
        superclassMethod -
      • isObjectMethod

        public static boolean isObjectMethod​(String methodName)
        Checks if the given method if from Object.class
        Parameters:
        methodName -
        Returns:
        true if the given method is from Object.class (either directly or overloaded)
      • isParametrizedType

        public static boolean isParametrizedType​(Type type)
        Returns true if type is an instance of ParameterizedType else otherwise.
        Parameters:
        type - type of the artifact
        Returns:
        true if type is an instance of ParameterizedType
      • isWildCardType

        public static boolean isWildCardType​(Type type)
        Returns true if type is an instance of WildcardType else otherwise.
        Parameters:
        type - type of the artifact
        Returns:
        true if type is an instance of WildcardType
      • isUnboundedTypeVariable

        public static boolean isUnboundedTypeVariable​(Type type)
      • isTypeVariable

        public static boolean isTypeVariable​(Type type)
        Returns true if type is an instance of TypeVariable else otherwise.
        Parameters:
        type - type of the artifact
        Returns:
        true if type is an instance of TypeVariable
      • isConcrete

        public static boolean isConcrete​(Class<?> clazz)
        Return true if the class is not abstract and interface.
        Parameters:
        clazz - class type
        Returns:
        true if the class is not abstract and interface
      • isClassAssignableFrom

        public static boolean isClassAssignableFrom​(Class<?> lhs,
                                                    Class<?> rhs)
        Returns true if rhs is assignable type to the lhs, false otherwise.
        Parameters:
        lhs - left hand side class
        rhs - right hand side class
        Returns:
        true if rhs is assignable to lhs
      • checkRequiredTypeIsWildCard

        public static boolean checkRequiredTypeIsWildCard​(Type beanTypeArg,
                                                          Type requiredTypeArg)
        Check bean type and required type.

        Required type is a wildcard type.

        Parameters:
        beanTypeArg - bean type
        requiredTypeArg - required type
        Returns:
        true if condition satisfies
        Since:
        1.1.1
      • getActualTypeArguments

        public static Type[] getActualTypeArguments​(Type type)
        Returns declared type arguments if type is a ParameterizedType instance, else an empty array. Get the actual type arguments of a type.
        Parameters:
        type -
        Returns:
        array of type arguments available
        Since:
        1.1.1
      • getClazz

        public static Class<?> getClazz​(Type type)
        Return raw class type for given type.
        Parameters:
        type - base type instance
        Returns:
        class type for given type
      • checkParametrizedType

        public static boolean checkParametrizedType​(ParameterizedType pType)
        Return true if it does not contain type variable for wildcard type false otherwise.
        Parameters:
        pType - parameterized type
        Returns:
        true if it does not contain type variable for wildcard type
      • getRawTypeForInjectionPoint

        public static Class<?> getRawTypeForInjectionPoint​(javax.enterprise.inject.spi.InjectionPoint injectionPoint)
        Returns injection point raw type.
        Parameters:
        injectionPoint - injection point definition
        Returns:
        injection point raw type
      • isOverridden

        public static boolean isOverridden​(Method subClassMethod,
                                           Method superClassMethod)
        Check whether superClassMethod is overridden by subClassMethod.
        Parameters:
        subClassMethod - potentially overriding
        superClassMethod - potentially overridden
        Returns:
        true if overridden
        Since:
        1.1.1
      • isRawClassEquals

        public static boolean isRawClassEquals​(Type ipType,
                                               Type apiType)