Top Description Inners Fields Constructors Methods
nonapi.io.github.classgraph.reflection

public final Class ReflectionUtils

extends Object
Class Inheritance
Imports
java.lang.reflect.Field, .InvocationHandler, .Method, .Proxy, java.util.concurrent.Callable, io.github.classgraph.ClassGraph, .ClassGraph.CircumventEncapsulationMethod

Reflection utility methods that can be used by ClassLoaderHandlers.

Nested and Inner Type Summary

Modifier and TypeClass and Description
private class

Field Summary

Modifier and TypeField and Description
private Class<?>
private Method
private Class<?>
public ReflectionDriver
reflectionDriver

The reflection driver to use.

Constructor Summary

AccessConstructor and Description
public
ReflectionUtils()

Call this if you change the value of ClassGraph#CIRCUMVENT_ENCAPSULATION.

Method Summary

Modifier and TypeMethod and Description
public Class<?>

Returns:

The class of the requested name, or null if an exception was thrown while trying to load the class.
classForNameOrNull
(final String
The class name to load.
className
)

Call Class.forName(className), but return null if any exception is thrown.

public <T> T
doPrivileged(final Callable<T> callable)

Call a method in the AccessController.doPrivileged(PrivilegedAction) context, using reflection, if possible (AccessController is deprecated in JDK 17).

public Object

Returns:

The field value.
getFieldVal
(final boolean
If true, throw an exception if the field value could not be read.
throwException
,
final Object
The object.
obj
,
final Field
The field.
field
)

Get the value of the field in the class of the given object or any of its superclasses.

public Object

Returns:

The field value.
getFieldVal
(final boolean
If true, throw an exception if the field value could not be read.
throwException
,
final Object
The object.
obj
,
final String
The field name.
fieldName
)

Get the value of the named field in the class of the given object or any of its superclasses.

public Object

Returns:

The field value.
getStaticFieldVal
(final boolean
If true, throw an exception if the field value could not be read.
throwException
,
final Class<?>
The class.
cls
,
final String
The field name.
fieldName
)

Get the value of the named field in the given class or any of its superclasses.

public Object

Returns:

The result of the method invocation.
invokeMethod
(final boolean
If true, throw an exception if the field value could not be read.
throwException
,
final Object
The object.
obj
,
final String
The method name.
methodName
)

Invoke the named method in the given object or its superclasses.

public Object

Returns:

The result of the method invocation.
invokeMethod
(final boolean
Whether to throw an exception on failure.
throwException
,
final Object
The object.
obj
,
final String
The method name.
methodName
,
final Class<?>
The type of the method argument.
argType
,
final Object
The parameter value to use when invoking the method.
param
)

Invoke the named method in the given object or its superclasses.

public Object

Returns:

The result of the method invocation.
invokeStaticMethod
(final boolean
Whether to throw an exception on failure.
throwException
,
final Class<?>
The class.
cls
,
final String
The method name.
methodName
)

Invoke the named method.

public Object

Returns:

The result of the method invocation.
invokeStaticMethod
(final boolean
Whether to throw an exception on failure.
throwException
,
final Class<?>
The class.
cls
,
final String
The method name.
methodName
,
final Class<?>
The type of the method argument.
argType
,
final Object
The parameter value to use when invoking the method.
param
)

Invoke the named method.

public Method

Returns:

The class of the requested name, or null if an exception was thrown while trying to load the class.
staticMethodForNameOrNull
(final String
The class name to load.
className
,
final String staticMethodName)

Get a method by name, but return null if any exception is thrown.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

accessControllerClassback to summary
private Class<?> accessControllerClass
accessControllerDoPrivilegedback to summary
private Method accessControllerDoPrivileged
privilegedActionClassback to summary
private Class<?> privilegedActionClass
reflectionDriverback to summary
public ReflectionDriver reflectionDriver

The reflection driver to use.

Constructor Detail

ReflectionUtilsback to summary
public ReflectionUtils()

Call this if you change the value of ClassGraph#CIRCUMVENT_ENCAPSULATION.

Method Detail

classForNameOrNullback to summary
public Class<?> classForNameOrNull(final String className)

Call Class.forName(className), but return null if any exception is thrown.

Parameters
className:String

The class name to load.

Returns:Class<?>

The class of the requested name, or null if an exception was thrown while trying to load the class.

doPrivilegedback to summary
public <T> T doPrivileged(final Callable<T> callable) throws Throwable

Call a method in the AccessController.doPrivileged(PrivilegedAction) context, using reflection, if possible (AccessController is deprecated in JDK 17).

Annotations
@SuppressWarnings:unchecked
getFieldValback to summary
public Object getFieldVal(final boolean throwException, final Object obj, final Field field) throws IllegalArgumentException

Get the value of the field in the class of the given object or any of its superclasses. If an exception is thrown while trying to read the field, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null object, returns null unless throwException is true, then throws IllegalArgumentException.

Parameters
throwException:boolean

If true, throw an exception if the field value could not be read.

obj:Object

The object.

field:Field

The field.

Returns:Object

The field value.

Exceptions
IllegalArgumentException:
If the field value could not be read.
getFieldValback to summary
public Object getFieldVal(final boolean throwException, final Object obj, final String fieldName) throws IllegalArgumentException

Get the value of the named field in the class of the given object or any of its superclasses. If an exception is thrown while trying to read the field, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null object, returns null unless throwException is true, then throws IllegalArgumentException.

Parameters
throwException:boolean

If true, throw an exception if the field value could not be read.

obj:Object

The object.

fieldName:String

The field name.

Returns:Object

The field value.

Exceptions
IllegalArgumentException:
If the field value could not be read.
getStaticFieldValback to summary
public Object getStaticFieldVal(final boolean throwException, final Class<?> cls, final String fieldName) throws IllegalArgumentException

Get the value of the named field in the given class or any of its superclasses. If an exception is thrown while trying to read the field value, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null class reference, returns null unless throwException is true, then throws IllegalArgumentException.

Parameters
throwException:boolean

If true, throw an exception if the field value could not be read.

cls:Class<?>

The class.

fieldName:String

The field name.

Returns:Object

The field value.

Exceptions
IllegalArgumentException:
If the field value could not be read.
invokeMethodback to summary
public Object invokeMethod(final boolean throwException, final Object obj, final String methodName) throws IllegalArgumentException

Invoke the named method in the given object or its superclasses. If an exception is thrown while trying to call the method, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null object, returns null unless throwException is true, then throws IllegalArgumentException.

Parameters
throwException:boolean

If true, throw an exception if the field value could not be read.

obj:Object

The object.

methodName:String

The method name.

Returns:Object

The result of the method invocation.

Exceptions
IllegalArgumentException:
If the method could not be invoked.
invokeMethodback to summary
public Object invokeMethod(final boolean throwException, final Object obj, final String methodName, final Class<?> argType, final Object param) throws IllegalArgumentException

Invoke the named method in the given object or its superclasses. If an exception is thrown while trying to call the method, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null object, returns null unless throwException is true, then throws IllegalArgumentException.

Parameters
throwException:boolean

Whether to throw an exception on failure.

obj:Object

The object.

methodName:String

The method name.

argType:Class<?>

The type of the method argument.

param:Object

The parameter value to use when invoking the method.

Returns:Object

The result of the method invocation.

Exceptions
IllegalArgumentException:
If the method could not be invoked.
invokeStaticMethodback to summary
public Object invokeStaticMethod(final boolean throwException, final Class<?> cls, final String methodName) throws IllegalArgumentException

Invoke the named method. If an exception is thrown while trying to call the method, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null class reference, returns null unless throwException is true, then throws IllegalArgumentException.

Parameters
throwException:boolean

Whether to throw an exception on failure.

cls:Class<?>

The class.

methodName:String

The method name.

Returns:Object

The result of the method invocation.

Exceptions
IllegalArgumentException:
If the method could not be invoked.
invokeStaticMethodback to summary
public Object invokeStaticMethod(final boolean throwException, final Class<?> cls, final String methodName, final Class<?> argType, final Object param) throws IllegalArgumentException

Invoke the named method. If an exception is thrown while trying to call the method, and throwException is true, then IllegalArgumentException is thrown wrapping the cause, otherwise this will return null. If passed a null class reference, returns null unless throwException is true, then throws IllegalArgumentException.

Parameters
throwException:boolean

Whether to throw an exception on failure.

cls:Class<?>

The class.

methodName:String

The method name.

argType:Class<?>

The type of the method argument.

param:Object

The parameter value to use when invoking the method.

Returns:Object

The result of the method invocation.

Exceptions
IllegalArgumentException:
If the method could not be invoked.
staticMethodForNameOrNullback to summary
public Method staticMethodForNameOrNull(final String className, final String staticMethodName)

Get a method by name, but return null if any exception is thrown.

Parameters
className:String

The class name to load.

Returns:Method

The class of the requested name, or null if an exception was thrown while trying to load the class.

nonapi.io.github.classgraph.reflection back to summary

private Class ReflectionUtils.PrivilegedActionInvocationHandler<T>

extends Object
implements InvocationHandler
Class Inheritance
  • java.lang.Object
  • nonapi.io.github.classgraph.reflection.ReflectionUtils.PrivilegedActionInvocationHandler
All Implemented Interfaces
java.lang.reflect.InvocationHandler

Field Summary

Modifier and TypeField and Description
private final Callable<T>

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
public Object
invoke(final Object
the proxy instance that the method was invoked on
proxy
,
final Method
the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.
method
,
final Object[]
an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.
args
)

Implements java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

callableback to summary
private final Callable<T> callable

Constructor Detail

PrivilegedActionInvocationHandlerback to summary
public PrivilegedActionInvocationHandler(final Callable<T> callable)

Method Detail

invokeback to summary
public Object invoke(final Object proxy, final Method method, final Object[] args) throws Throwable

Implements java.lang.reflect.InvocationHandler.invoke.

Doc from java.lang.reflect.InvocationHandler.invoke.

Processes a method invocation on a proxy instance and returns the result. This method will be invoked on an invocation handler when a method is invoked on a proxy instance that it is associated with.

Parameters
proxy:Object

the proxy instance that the method was invoked on

method:Method

the Method instance corresponding to the interface method invoked on the proxy instance. The declaring class of the Method object will be the interface that the method was declared in, which may be a superinterface of the proxy interface that the proxy class inherits the method through.

args:Object[]

an array of objects containing the values of the arguments passed in the method invocation on the proxy instance, or null if interface method takes no arguments. Arguments of primitive types are wrapped in instances of the appropriate primitive wrapper class, such as java.lang.Integer or java.lang.Boolean.

Returns:Object

the value to return from the method invocation on the proxy instance. If the declared return type of the interface method is a primitive type, then the value returned by this method must be an instance of the corresponding primitive wrapper class; otherwise, it must be a type assignable to the declared return type. If the value returned by this method is null and the interface method's return type is primitive, then a NullPointerException will be thrown by the method invocation on the proxy instance. If the value returned by this method is otherwise not compatible with the interface method's declared return type as described above, a ClassCastException will be thrown by the method invocation on the proxy instance.

Annotations
@Override
Exceptions
Throwable:
the exception to throw from the method invocation on the proxy instance. The exception's type must be assignable either to any of the exception types declared in the throws clause of the interface method or to the unchecked exception types java.lang.RuntimeException or java.lang.Error. If a checked exception is thrown by this method that is not assignable to any of the exception types declared in the throws clause of the interface method, then an UndeclaredThrowableException containing the exception that was thrown by this method will be thrown by the method invocation on the proxy instance.