Class ReflectionUtil


  • public class ReflectionUtil
    extends Object
    Contains static helper methods related to classes and reflection.
    • Method Detail

      • areClassesRelated

        public static boolean areClassesRelated​(Class<?> type1,
                                                Class<?> type2)
        Determines if two classes are linked in a class hierarchy.
        Parameters:
        type1 - a class or null
        type2 - the class to compare type1 with or null
        Returns:
        true if both classes are identical or if one type is base class of other. false if one of the given types or both are null or not in a direct hierarchical relation.
      • doesMethodBelongToType

        public static boolean doesMethodBelongToType​(Method method,
                                                     Class<?> type)
        Checks if method belongs to a given type or super-type.
        Parameters:
        method - the method to check
        type - any class to search for the given method
        Returns:
        true if given method is declared in given type or one of its super types.
      • invokeMethodUnchecked

        public static Object invokeMethodUnchecked​(Object object,
                                                   Method method)
        Calls Method.invoke(Object, Object...) on the given method but catches away all thrown Exceptions.
        Parameters:
        object - the object the method to call on. Can be null for static methods
        method - the method to execute
        Returns:
        the value returned from method call
        Throws:
        RuntimeException - wrapped around the checked exception from the method invocation
      • getSuperTypesInclRoot

        public static List<Class<?>> getSuperTypesInclRoot​(Class<?> rootType)
        Collects all classes and interfaces a given type extends or implements. This methods executes fast as its results are cached.
        Parameters:
        rootType - the class to examine
        Returns:
        an immutable list of super classes and interfaces including the given type but excluding Object
      • lookupInterfaceMethod

        public static <T> Method lookupInterfaceMethod​(Class<T> interfaceType,
                                                       Consumer<T> methodSpecifier)
      • primitiveTypeDefaultValue

        public static Object primitiveTypeDefaultValue​(Class<?> primitiveType)
        Returnes the default value for a given primitive type, basically all flavours of zero for the number types and false for boolean.class.
        Parameters:
        primitiveType - a Java primitive type like boolean.class, int.class or float.class
        Returns:
        zero of false value of type matching to given primitiveType. If for example primitiveType is double.class the returned value will have type Double.class and value 0.0.