Package org.coliper.ibean.util
Class ReflectionUtil
- java.lang.Object
-
- org.coliper.ibean.util.ReflectionUtil
-
public class ReflectionUtil extends Object
Contains static helper methods related to classes and reflection.
-
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static booleanareClassesRelated(Class<?> type1, Class<?> type2)Determines if two classes are linked in a class hierarchy.static booleandoesMethodBelongToType(Method method, Class<?> type)Checks if method belongs to a given type or super-type.static List<Class<?>>getSuperTypesInclRoot(Class<?> rootType)Collects all classes and interfaces a given type extends or implements.static ObjectinvokeMethodUnchecked(Object object, Method method)CallsMethod.invoke(Object, Object...)on the given method but catches away all thrown Exceptions.static <T> MethodlookupInterfaceMethod(Class<T> interfaceType, Consumer<T> methodSpecifier)static ObjectprimitiveTypeDefaultValue(Class<?> primitiveType)Returnes the default value for a given primitive type, basically all flavours of zero for the number types andfalseforboolean.class.
-
-
-
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 ornulltype2- the class to compare type1 with ornull- Returns:
trueif both classes are identical or if one type is base class of other.falseif one of the given types or both arenullor 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 checktype- any class to search for the given method- Returns:
trueif given method is declared in given type or one of its super types.
-
invokeMethodUnchecked
public static Object invokeMethodUnchecked(Object object, Method method)
CallsMethod.invoke(Object, Object...)on the given method but catches away all thrown Exceptions.- Parameters:
object- the object the method to call on. Can benullfor static methodsmethod- 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 andfalseforboolean.class.- Parameters:
primitiveType- a Java primitive type likeboolean.class,int.classorfloat.class- Returns:
- zero of
falsevalue of type matching to givenprimitiveType. If for exampleprimitiveTypeisdouble.classthe returned value will have typeDouble.classand value 0.0.
-
-