Class ReflectionUtils
-
Nested Class Summary
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionfindCommonAncestorOfType
(Class<?> lhs, Class<?> rhs, Class<?> lookFor) Search through both classes hierarchies, looking for the most recent ancestor that is either equal tolookFor
or an ancestor that implementslookFor
.static <T> Class<? extends T>
findImplementingClass
(Class<T> iface, Class<? extends T> search, ReflectionUtils.SearchOptions... optionsArray) Search through a class's hierarchy for an interface, returning the class or interface that implemented it.static <T> Class<T>
findParameterClass
(Class<?> subclass) static <T> Class<T>
findParameterClass
(Class<?> subclass, int parameterIndex) Extracts the parameterized type (TheT
fromMyClass<T>
) from a parameterized subclass (e.g.getAnnotatedFields
(Class<?> parameterClass, Class<? extends Annotation> annotationClass) getInterfaces
(Class<?> clazz) getPublicField
(Class<?> clazz, String fieldName) Very simple public field lookup method that returns empty if no such field existsstatic <T> T
newInstance
(Class<T> clazz) Callsclazz.newInstance
, wrapping the checked exceptions in an unchecked runtime exceptionstatic <T,
E extends Exception>
TnewInstance
(Class<T> clazz, Function<Exception, E> constructor) Callsclazz.newInstance
, wrapping the checked exceptions in an exception provided fromconstructor
static void
Calls field.set(instance, value), wrapping the checked exception in an unchecked runtime exception
-
Constructor Details
-
ReflectionUtils
public ReflectionUtils()
-
-
Method Details
-
getAnnotatedFields
public static List<Field> getAnnotatedFields(Class<?> parameterClass, Class<? extends Annotation> annotationClass) - Returns:
- all fields annotated with the desired annotation in the class hierarchy of the given class
-
getInterfaces
- Returns:
- all interfaces that a class implements by following the class hierarchy up to object.
It does not include interfaces that are inherited by implemented interfaces. E.g. class foo implements bar. interface bar extends baz, getInterfaces(Foo.class) returns bar, but not baz.
-
findCommonAncestorOfType
public static Optional<Class<?>> findCommonAncestorOfType(Class<?> lhs, Class<?> rhs, Class<?> lookFor) Search through both classes hierarchies, looking for the most recent ancestor that is either equal to
lookFor
or an ancestor that implementslookFor
. See ReflectionUtilsTest for examples.- Parameters:
lhs
- a type to search throughrhs
- another type to search throughlookFor
- either a parent class in the shared ancestry, or an interface a shared ancestor should implement.- Returns:
- empty if lhs and rhs have no common ancestor of lookFor. If they have a common ancestor that is either exactly lookFor, or implements lookFor, then the ancestor is returned.
-
findImplementingClass
public static <T> Class<? extends T> findImplementingClass(Class<T> iface, Class<? extends T> search, ReflectionUtils.SearchOptions... optionsArray) Search through a class's hierarchy for an interface, returning the class or interface that implemented it. See ReflectionUtilsTest for more details.
- Type Parameters:
T
- The type of the interface we're looking for- Parameters:
iface
- the class instance for the interface we're looking forsearch
- where to start searching fromoptionsArray
- search options- Returns:
- The implementing class. Will never return null, but can throw a
RiskscapeException
if the interface is implemented multiple times or is not found given theReflectionUtils.SearchOptions
.
-
newInstance
Calls
clazz.newInstance
, wrapping the checked exceptions in an unchecked runtime exception -
newInstance
public static <T,E extends Exception> T newInstance(Class<T> clazz, Function<Exception, E> constructor) throws ECalls
clazz.newInstance
, wrapping the checked exceptions in an exception provided fromconstructor
- Throws:
E extends Exception
-
setField
Calls field.set(instance, value), wrapping the checked exception in an unchecked runtime exception
Also ensures that the field is set to accessible before attempting to set the value.
-
getPublicField
Very simple public field lookup method that returns empty if no such field exists
-
findParameterClass
-
findParameterClass
Extracts the parameterized type (The
T
fromMyClass<T>
) from a parameterized subclass (e.g.Foo extends MyClass<Bar>
). Normally, java's generics are runtime only, but subclasses keep their declared parameter types in their class metadata, and these are available in the reflection API - this method will extract a parameterized type and return it, as well as fudging the casting.- Parameters:
subclass
- a class that declares type argumentsparameterIndex
- the index of the declared type argument
-