Class Permit
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> TRetrieves the value of the given field from the given receiver.static <T> Constructor<T> getConstructor(@NotNull Class<T> c, Class<?>... parameterTypes) Finds a constructor with the given parameter types and makes it accessible.static FieldRetrieves a field with the given name from the specified class or its superclasses.static MethodGets a method declared in the given class or any of its superclasses.static voidhandleReflectionDebug(@NotNull Throwable t, Throwable initError) Handles a reflection-related exception by printing out a helpful message about the exception and, if given, an exception that occurred while setting up reflection.static Objectstatic ObjectInvokes the specified method on the given receiver with the provided arguments.static ObjectinvokeSneaky(Method m, Object receiver, Object... args) static @Nullable ObjectinvokeSneaky(Throwable initError, Method m, Object receiver, Object... args) Invokes the specified method on the given receiver with the provided arguments.static <T> TnewInstance(Constructor<T> c, Object... args) Invokes the specified constructor with the given arguments.static <T> TnewInstance(Throwable initError, Constructor<T> c, Object... args) Invokes the specified constructor with the given arguments.static <T> TnewInstanceSneaky(Constructor<T> c, Object... args) Invokes the specified constructor with the given arguments.static <T> TnewInstanceSneaky(Throwable initError, Constructor<T> c, Object... args) Invokes the specified constructor with the given arguments.static @Nullable FieldpermissiveGetField(Class<?> c, String fName) Retrieves a field with the given name from the specified class or its superclasses.static @Nullable MethodpermissiveGetMethod(Class<?> c, String mName, Class<?>... parameterTypes) LikegetMethod(Class, String, Class[]), but won't throw an exception if the method doesn't exist.static <T> TpermissiveReadField(Class<T> type, Field f, Object instance) Like, but won't throw an exception if the field doesn't exist or is not accessible.invalid reference
#readField(Class, Field, Object)static voidreportReflectionProblem(Throwable initError, String msg) Reports a reflection-related problem by printing out a message to the error stream.static voidSet the value of the field tonewValue.static <T extends AccessibleObject>
TsetAccessible(T accessor) Set the accessible flag of the given accessible object to true.static RuntimeExceptionWraps aThrowablein aRuntimeException, but only if it isn't already aRuntimeException.
-
Method Details
-
setAccessible
Set the accessible flag of the given accessible object to true.This method is used to set the accessible flag of a field, method, or constructor without having to declare the checked exception IllegalAccessException.
- Parameters:
accessor- the accessible object to set the accessible flag of- Returns:
- the given accessible object
-
getMethod
public static Method getMethod(Class<?> c, String mName, Class<?>... parameterTypes) throws NoSuchMethodException Gets a method declared in the given class or any of its superclasses.This method will traverse the class hierarchy and find the given method, regardless of whether it is declared as public, private, protected, or default (package-private).
- Parameters:
c- the class to search for the methodmName- the name of the method to search forparameterTypes- the parameter types of the method to search for- Returns:
- the method found, or
nullif no such method exists - Throws:
NoSuchMethodException- if the method does not exist
-
permissiveGetMethod
@Nullable public static @Nullable Method permissiveGetMethod(Class<?> c, String mName, Class<?>... parameterTypes) LikegetMethod(Class, String, Class[]), but won't throw an exception if the method doesn't exist.Instead, this method will return
nullif the method doesn't exist.- Parameters:
c- the class to search for the methodmName- the name of the method to search forparameterTypes- the parameter types of the method to search for- Returns:
- the method found, or
nullif no such method exists
-
getField
Retrieves a field with the given name from the specified class or its superclasses.This method searches for a field with the specified name in the given class and, if not found, traverses the superclass hierarchy to find the field. Once found, it sets the field to be accessible and returns it.
- Parameters:
c- the class to search for the fieldfName- the name of the field to search for- Returns:
- the found field with accessible set to true
- Throws:
NoSuchFieldException- if the field does not exist in the given class or its superclasses
-
permissiveGetField
Retrieves a field with the given name from the specified class or its superclasses.This method searches for a field with the specified name in the given class and, if not found, traverses the superclass hierarchy to find the field. Once found, it sets the field to be accessible and returns it. If no field is found, it returns null.
- Parameters:
c- the class to search for the fieldfName- the name of the field to search for- Returns:
- the found field with accessible set to true, or null if the field does not exist
-
permissiveReadField
Like, but won't throw an exception if the field doesn't exist or is not accessible.invalid reference
#readField(Class, Field, Object)Instead, this method will return
nullif the field doesn't exist or if any exception occurs while trying to read the field.- Parameters:
type- the type of the object to be returnedf- the field to readinstance- the object containing the field- Returns:
- the value of the field, or
nullif the field doesn't exist or any exception occurs
-
getConstructor
public static <T> Constructor<T> getConstructor(@NotNull @NotNull Class<T> c, Class<?>... parameterTypes) throws NoSuchMethodException Finds a constructor with the given parameter types and makes it accessible.This method is like
Class.getDeclaredConstructor(Class[]), but sets the accessible flag of the constructor to true before returning it.- Parameters:
c- the class to search for the constructorparameterTypes- the parameter types of the constructor to search for- Returns:
- the found constructor with accessible set to true
- Throws:
NoSuchMethodException- if no such constructor exists
-
handleReflectionDebug
Handles a reflection-related exception by printing out a helpful message about the exception and, if given, an exception that occurred while setting up reflection.- Parameters:
t- the exception to handleinitError- the exception that occurred while setting up reflection, or null if no such exception occurred
-
invoke
public static Object invoke(Method m, Object receiver, Object... args) throws IllegalAccessException, InvocationTargetException -
invoke
public static Object invoke(Throwable initError, Method m, Object receiver, Object... args) throws IllegalAccessException, InvocationTargetException Invokes the specified method on the given receiver with the provided arguments.This method attempts to call the specified method using reflection. If any exceptions are thrown during the invocation, they are caught and handled by logging the relevant debug information, and then re-thrown.
- Parameters:
initError- the optional exception that occurred while setting up reflectionm- the method to invokereceiver- the object on which to invoke the methodargs- the arguments to pass to the method- Returns:
- the result of the method invocation
- Throws:
IllegalAccessException- if the method is not accessibleInvocationTargetException- if the underlying method throws an exception
-
invokeSneaky
-
invokeSneaky
@Nullable public static @Nullable Object invokeSneaky(Throwable initError, Method m, Object receiver, Object... args) Invokes the specified method on the given receiver with the provided arguments.This method attempts to call the specified method using reflection. If any exceptions are thrown during the invocation, they are caught and handled by logging the relevant debug information, and then re-thrown. If the exception is a
NoClassDefFoundErroror aNullPointerException, the exception is ignored andnullis returned.- Parameters:
initError- the optional exception that occurred while setting up reflectionm- the method to invokereceiver- the object on which to invoke the methodargs- the arguments to pass to the method- Returns:
- the result of the method invocation, or
nullif aNoClassDefFoundErroror aNullPointerExceptionwas thrown - Throws:
IllegalAccessException- if the method is not accessibleInvocationTargetException- if the underlying method throws an exception
-
newInstance
@NotNull public static <T> T newInstance(Constructor<T> c, Object... args) throws IllegalAccessException, InvocationTargetException, InstantiationException Invokes the specified constructor with the given arguments.This method attempts to call the specified constructor using reflection. If any exceptions are thrown during the invocation, they are caught and handled by logging the relevant debug information, and then re-thrown.
- Parameters:
c- the constructor to invokeargs- the arguments to pass to the constructor- Returns:
- the result of the constructor invocation
- Throws:
IllegalAccessException- if the constructor is not accessibleInvocationTargetException- if the underlying constructor throws an exceptionInstantiationException- if the underlying constructor throws an exception
-
newInstance
@NotNull public static <T> T newInstance(Throwable initError, Constructor<T> c, Object... args) throws IllegalAccessException, InvocationTargetException, InstantiationException Invokes the specified constructor with the given arguments.This method attempts to call the specified constructor using reflection. If any exceptions are thrown during the invocation, they are caught and handled by logging the relevant debug information, and then re-thrown.
- Parameters:
initError- the optional exception that occurred while setting up reflectionc- the constructor to invokeargs- the arguments to pass to the constructor- Returns:
- the result of the constructor invocation
- Throws:
IllegalAccessException- if the constructor is not accessibleInvocationTargetException- if the underlying constructor throws an exceptionInstantiationException- if the underlying constructor throws an exception
-
newInstanceSneaky
Invokes the specified constructor with the given arguments.This method attempts to call the specified constructor using reflection. If any exceptions are thrown during the invocation, they are caught and handled by logging the relevant debug information, and then re-thrown. If the exception is a
NoClassDefFoundErroror aNullPointerException, the exception is ignored andnullis returned.- Parameters:
c- the constructor to invokeargs- the arguments to pass to the constructor- Returns:
- the result of the constructor invocation, or
nullif aNoClassDefFoundErroror aNullPointerExceptionwas thrown
-
newInstanceSneaky
@Nullable public static <T> T newInstanceSneaky(Throwable initError, Constructor<T> c, Object... args) Invokes the specified constructor with the given arguments.This method attempts to call the specified constructor using reflection. If any exceptions are thrown during the invocation, they are caught and handled by logging the relevant debug information, and then re-thrown. If the exception is a
NoClassDefFoundErroror aNullPointerException, the exception is ignored andnullis returned.- Parameters:
initError- the optional exception that occurred while setting up reflectionc- the constructor to invokeargs- the arguments to pass to the constructor- Returns:
- the result of the constructor invocation, or
nullif aNoClassDefFoundErroror aNullPointerExceptionwas thrown
-
get
Retrieves the value of the given field from the given receiver.This method attempts to call the specified field using reflection. If any exceptions are thrown during the invocation, they are caught and handled by logging the relevant debug information, and then re-thrown.
- Parameters:
f- the field to retrievereceiver- the object from which to retrieve the field- Returns:
- the value of the field
- Throws:
IllegalAccessException- if the field is not accessible
-
set
Set the value of the field tonewValue.- Parameters:
f- the field to setreceiver- the object in which to set the fieldnewValue- the value to set- Throws:
IllegalAccessException- if the field is not accessible
-
reportReflectionProblem
Reports a reflection-related problem by printing out a message to the error stream.This method logs a specified message indicating a reflection issue and, if provided, also logs an exception that occurred during the setup of reflection.
- Parameters:
initError- the exception that occurred while setting up reflection, or null if no such exception occurredmsg- the message describing the reflection problem
-
sneakyThrow
Wraps aThrowablein aRuntimeException, but only if it isn't already aRuntimeException. This is useful for wrapping exceptions that you want to propagate up the call stack without having to declare them in the method signature.If the given
Throwableis already aRuntimeException, this method simply returns it as is. Otherwise, it wraps it in aRuntimeExceptionand returns that.- Parameters:
t- the exception to wrap- Returns:
- the wrapped exception
- Throws:
NullPointerException- if the given exception is null
-