Package com.pnfsoftware.jeb.util.reflect
Class ReflectionHelper
java.lang.Object
com.pnfsoftware.jeb.util.reflect.ReflectionHelper
Helper routines using the Java Reflection API.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic ObjectRetrieve a field value, temporarily making the field accessible if necessary.static booleanhasNoArgConstructor(Class<?> c) Determine whether a class declares a no-argument constructor.static <T> TinstantiateSafe(Class<? extends T> c) Instantiate a class using its public no-argument constructor.static ObjectInvoke a method, temporarily making it accessible if necessary.static booleanisInstance(Object o, Class<?>... classObjects) A multi-element variant ofClass.isInstance(Object).static <T> booleanisInstance(Object o, Collection<Class<? extends T>> classObjects) A multi-element variant ofClass.isInstance(Object).static ObjectnewInstance2(Constructor<?> constructor) Instantiate an object using a no-argument constructor, temporarily making it accessible if necessary.static voidopenAllModules(Class<?> clzInUnnamedModule) Grant reflection access to non-public members of Java named modules to an unnamed module.static Class<?> reduceDimensions(Class<?> c) Reduce dimensions of an array type.static Class<?> removeDimensions(Class<?> c) Remove dimensions of an array type.static voidSet a field value, temporarily making the field accessible if necessary.static booleanDetermine if a type is a descendant of the provided ancestor.
-
Constructor Details
-
ReflectionHelper
public ReflectionHelper()
-
-
Method Details
-
hasNoArgConstructor
Determine whether a class declares a no-argument constructor.- Parameters:
c- class to inspect- Returns:
- true if the class declares a no-argument constructor
-
newInstance2
public static Object newInstance2(Constructor<?> constructor) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException Instantiate an object using a no-argument constructor, temporarily making it accessible if necessary.- Parameters:
constructor- constructor to invoke- Returns:
- the new instance
- Throws:
InstantiationException- if the declaring class cannot be instantiatedIllegalAccessException- if the constructor cannot be accessedIllegalArgumentException- if the constructor invocation arguments are invalidInvocationTargetException- if the constructor throws an exception
-
invoke2
public static Object invoke2(Method m, Object o, Object... args) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException Invoke a method, temporarily making it accessible if necessary.- Parameters:
m- method to invokeo- target object, or null for a static methodargs- invocation arguments- Returns:
- the invocation result
- Throws:
IllegalAccessException- if the method cannot be accessedIllegalArgumentException- if the target object or arguments are invalidInvocationTargetException- if the method throws an exception
-
set2
public static void set2(Field f, Object o, Object value) throws IllegalAccessException, IllegalArgumentException Set a field value, temporarily making the field accessible if necessary.- Parameters:
f- field to seto- target object, or null for a static fieldvalue- value to set- Throws:
IllegalAccessException- if the field cannot be accessedIllegalArgumentException- if the target object or value is invalid
-
get2
public static Object get2(Field f, Object o) throws IllegalAccessException, IllegalArgumentException Retrieve a field value, temporarily making the field accessible if necessary.- Parameters:
f- field to reado- target object, or null for a static field- Returns:
- the field value
- Throws:
IllegalAccessException- if the field cannot be accessedIllegalArgumentException- if the target object is invalid
-
removeDimensions
Remove dimensions of an array type.- non-array => non-array
- type[]...[] => type
- Parameters:
c- a type- Returns:
- the dimension-less type
-
reduceDimensions
Reduce dimensions of an array type. Careful, caveat for primitives:- non-array => non-array
- Object[]...[] => Object
- prim[]...[] => prim[]
- Parameters:
c- a type- Returns:
- the reduced type
-
instantiateSafe
Instantiate a class using its public no-argument constructor.- Type Parameters:
T- object type- Parameters:
c- class to instantiate- Returns:
- the new instance, or null if instantiation failed
-
isInstance
A multi-element variant ofClass.isInstance(Object).- Parameters:
o- the object to be testedclassObjects- a collection ofClassobjects- Returns:
- true if o can be cast to one of the provided class objects
-
isInstance
A multi-element variant ofClass.isInstance(Object).- Parameters:
o- the object to be testedclassObjects- a collection ofClassobjects- Returns:
- true if o can be cast to one of the provided class objects
-
subtypeOf
Determine if a type is a descendant of the provided ancestor.- Parameters:
c- a type (class or interface)parent- a candidate ancestor type (class or interface)- Returns:
- true or false
-
openAllModules
Grant reflection access to non-public members of Java named modules to an unnamed module. Required for JDK 17+, since--illegal-access=is no longer an option.Implementation note: this code relies on
Module.implAddExportsOrOpens(); therefore access tojava.langmust have been granted to the JVM (e.g. via--add-opens java.base/java.lang=ALL-UNNAMEDon the command line).- Parameters:
clzInUnnamedModule- a class in the unnamed module- Throws:
Exception- raised if any reflection error or illegal access error happens
-