public class ReflectionUtils
extends Object
This class contains utility methods to determine which class called the current class to multiple levels of depth. Calls used to handle the groovy MOP are excluded from the level counting.
| Type Params | Return Type | Name and description |
|---|---|---|
|
public static boolean |
checkAccessible(Class<?> callerClass, Class<?> declaringClass, int memberModifiers, boolean allowIllegalAccess)Whether callerClass can access a member of declaringClass
with the given modifiers, as decided by the VM plugin. |
|
public static boolean |
checkCanSetAccessible(AccessibleObject accessibleObject, Class<?> caller)Whether the VM plugin allows accessibleObject to be made accessible
from caller. |
|
public static Class |
getCallingClass()Gets the immediate calling class, ignoring MOP frames. |
|
public static Class |
getCallingClass(int matchLevel)Gets the called that is matchLevel stack frames before the call, ignoring MOP frames. |
|
public static Class |
getCallingClass(int matchLevel, Collection<String> extraIgnoredPackages)Gets the called that is matchLevel stack frames before the call, ignoring MOP frames and desired exclude packages. |
|
public static Constructor<?>[] |
getDeclaredConstructorsSorted(Class<?> type)Returns the declared constructors of a class in a deterministic order. |
|
public static List<Method> |
getDeclaredMethods(Class<?> type, String name, Class<?> parameterTypes)Returns the methods type declares whose name is name and
whose parameter list parameterTypeMatches(Class[], Class[]) accepts.
|
|
public static Method[] |
getDeclaredMethodsSorted(Class<?> type)Returns the declared methods of a class in a deterministic order. |
|
public static List<Method> |
getMethods(Class<?> type, String name, Class<?> parameterTypes)Returns the public methods of type, including inherited methods, whose
name is name and whose parameter list
parameterTypeMatches(Class[], Class[]) accepts. |
|
public static Class<?>[] |
getPermittedSubclasses(Class<?> clazz)The direct permitted subclasses of a sealed class. |
|
public static boolean |
isCallingClassReflectionAvailable()Determines whether the getCallingClass methods will return any sensible results. |
|
public static boolean |
isSealed(Class<?> clazz)Whether clazz is sealed. |
|
public static Optional<AccessibleObject> |
makeAccessible(AccessibleObject ao)Enables access to ao when trySetAccessible(AccessibleObject)
succeeds. |
|
public static AccessibleObject[] |
makeAccessible(AccessibleObject[] aoa)Enables access to every element of aoa. |
|
public static Optional<AccessibleObject> |
makeAccessibleInPrivilegedAction(AccessibleObject ao)Makes ao accessible when the VM plugin allows it. |
|
public static boolean |
parameterTypeMatches(Class<?>[] parameterTypes, Class<?>[] argTypes)Whether each parameter type accepts the argument type in the same position. |
|
public static boolean |
trySetAccessible(AccessibleObject ao)Tries to set the accessible flag on ao. |
Whether callerClass can access a member of declaringClass
with the given modifiers, as decided by the VM plugin.
callerClass - the class requesting accessdeclaringClass - the class that declares the membermemberModifiers - the member's modifiersallowIllegalAccess - whether illegal access is permitted Whether the VM plugin allows accessibleObject to be made accessible
from caller.
accessibleObject - the reflected member to checkcaller - the class that would call setAccessibleGets the immediate calling class, ignoring MOP frames.
Gets the called that is matchLevel stack frames before the call, ignoring MOP frames.
matchLevel - how may call stacks down to look.
If it is less than 1 it is treated as though it was 1.Gets the called that is matchLevel stack frames before the call, ignoring MOP frames and desired exclude packages.
matchLevel - how may call stacks down to look.
If it is less than 1 it is treated as though it was 1.extraIgnoredPackages - A collection of string names of packages to exclude
in addition to the MOP packages when counting stack frames.Returns the declared constructors of a class in a deterministic order.
Class.getDeclaredConstructors does not guarantee an order, and a nondeterministic order can flow into generated bytecode; see getDeclaredMethodsSorted(Class).
type - the class to introspect Returns the methods type declares whose name is name and
whose parameter list parameterTypeMatches(Class[], Class[]) accepts.
Inherited methods are omitted. The list is empty when none match.
type - the class to inspectname - the method nameparameterTypes - the expected parameter types; an empty array matches only
methods that declare no parametersReturns the declared methods of a class in a deterministic order.
Class.getDeclaredMethods does not guarantee an order, and
HotSpot's varies between JVM runs. When members of a precompiled class
are enumerated at compile time (annotation members copied to generated
code, trait methods woven into implementing classes, MOP super$
bridge methods, etc.), that order flows into the generated bytecode, so
a nondeterministic order produces byte-different class files from
identical sources. Callers whose output depends on member order should
use this variant so builds are reproducible.
type - the class to introspect Returns the public methods of type, including inherited methods, whose
name is name and whose parameter list
parameterTypeMatches(Class[], Class[]) accepts.
type - the class to inspectname - the method nameparameterTypes - the expected parameter types; an empty array matches only
methods that declare no parameters The direct permitted subclasses of a sealed class. Returns a shared empty
array when clazz is null, when
Class.getPermittedSubclasses is unavailable, or when calling it
fails. When the JVM method returns null because the class is not
sealed, this method returns null as well.
clazz - the class to inspect, or nullnullDetermines whether the getCallingClass methods will return any sensible results. On JVMs that are not Sun derived i.e. (gcj, Harmony) this will likely return false. When not available all getCallingClass methods will return null.
Whether clazz is sealed. Returns false when clazz is
null, when Class.isSealed is unavailable on this JVM, or
when calling it fails.
clazz - the class to test, or nulltrue only when the JVM reports that the class is sealed Enables access to ao when trySetAccessible(AccessibleObject)
succeeds. A failure leaves the member unchanged.
ao - the reflected memberao when it was made accessible, otherwise an empty optional Enables access to every element of aoa. When
AccessibleObject.setAccessible succeeds,
the same array is returned. When that call fails, each element is tried on
its own and only the members that succeed are returned, using aoa's
component type.
aoa - the reflected membersaoa after a successful bulk update, otherwise a new array of
the members that could be made accessible Makes ao accessible when the VM plugin allows it. This does not enter
a privileged action; it calls makeAccessible(AccessibleObject).
ao - the reflected memberao when access was enabled, otherwise an empty optional Whether each parameter type accepts the argument type in the same position.
A parameter of Object matches any argument type, including null.
Any other parameter rejects a null argument type. Remaining pairs match
when the classes are the same, or when the boxed parameter type is assignable
from the boxed argument type.
parameterTypes - the method's parameter typesargTypes - the argument types, with null for an unknown argumenttrue when the arrays have equal length and every position matches Tries to set the accessible flag on ao. A security, module, or other
failure is swallowed.
ao - the reflected membertrue when the flag was set; false when it could not be