Package org.codehaus.groovy.reflection
Class ReflectionUtils
java.lang.Object
org.codehaus.groovy.reflection.ReflectionUtils
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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleancheckAccessible(Class<?> callerClass, Class<?> declaringClass, int memberModifiers, boolean allowIllegalAccess) WhethercallerClasscan access a member ofdeclaringClasswith the given modifiers, as decided by the VM plugin.static booleancheckCanSetAccessible(AccessibleObject accessibleObject, Class<?> caller) Whether the VM plugin allowsaccessibleObjectto be made accessible fromcaller.static ClassGets the immediate calling class, ignoring MOP frames.static ClassgetCallingClass(int matchLevel) Gets the called that is matchLevel stack frames before the call, ignoring MOP frames.static ClassgetCallingClass(int matchLevel, Collection<String> extraIgnoredPackages) Gets the called that is matchLevel stack frames before the call, ignoring MOP frames and desired exclude packages.static Constructor<?>[]getDeclaredConstructorsSorted(Class<?> type) Returns the declared constructors of a class in a deterministic order.getDeclaredMethods(Class<?> type, String name, Class<?>... parameterTypes) Returns the methodstypedeclares whose name isnameand whose parameter listparameterTypeMatches(Class[], Class[])accepts.static Method[]getDeclaredMethodsSorted(Class<?> type) Returns the declared methods of a class in a deterministic order.getMethods(Class<?> type, String name, Class<?>... parameterTypes) Returns the public methods oftype, including inherited methods, whose name isnameand whose parameter listparameterTypeMatches(Class[], Class[])accepts.static Class<?>[]getPermittedSubclasses(Class<?> clazz) The direct permitted subclasses of a sealed class.static booleanDetermines whether the getCallingClass methods will return any sensible results.static booleanWhetherclazzis sealed.static Optional<AccessibleObject>Enables access toaowhentrySetAccessible(AccessibleObject)succeeds.static AccessibleObject[]makeAccessible(AccessibleObject[] aoa) Enables access to every element ofaoa.static Optional<AccessibleObject>Makesaoaccessible when the VM plugin allows it.static booleanparameterTypeMatches(Class<?>[] parameterTypes, Class<?>[] argTypes) Whether each parameter type accepts the argument type in the same position.static booleanTries to set the accessible flag onao.
-
Constructor Details
-
ReflectionUtils
public ReflectionUtils()
-
-
Method Details
-
isCallingClassReflectionAvailable
public static boolean isCallingClassReflectionAvailable()Determines 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.- Returns:
- true if getCallingClass can return anything but null, false if it will only return null.
-
getCallingClass
Gets the immediate calling class, ignoring MOP frames.- Returns:
- The Class of the caller
-
getCallingClass
Gets the called that is matchLevel stack frames before the call, ignoring MOP frames.- Parameters:
matchLevel- how may call stacks down to look. If it is less than 1 it is treated as though it was 1.- Returns:
- The Class of the matched caller, or null if there aren't enough stackframes to satisfy matchLevel
-
getCallingClass
Gets the called that is matchLevel stack frames before the call, ignoring MOP frames and desired exclude packages.- Parameters:
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 Class of the matched caller, or null if there aren't enough stackframes to satisfy matchLevel
-
getDeclaredMethods
public static List<Method> getDeclaredMethods(Class<?> type, String name, Class<?>... parameterTypes) Returns the methodstypedeclares whose name isnameand whose parameter listparameterTypeMatches(Class[], Class[])accepts. Inherited methods are omitted. The list is empty when none match.- Parameters:
type- the class to inspectname- the method nameparameterTypes- the expected parameter types; an empty array matches only methods that declare no parameters- Returns:
- the matching methods, in the order
Class.getDeclaredMethods()produced
-
getDeclaredMethodsSorted
Returns 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, MOPsuper$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.- Parameters:
type- the class to introspect- Returns:
- the declared methods, sorted by name then signature
- Since:
- 5.0.8
-
getDeclaredConstructorsSorted
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; seegetDeclaredMethodsSorted(Class).- Parameters:
type- the class to introspect- Returns:
- the declared constructors, sorted by parameter types
- Since:
- 5.0.8
-
getMethods
Returns the public methods oftype, including inherited methods, whose name isnameand whose parameter listparameterTypeMatches(Class[], Class[])accepts.- Parameters:
type- the class to inspectname- the method nameparameterTypes- the expected parameter types; an empty array matches only methods that declare no parameters- Returns:
- the matching methods, in the order
Class.getMethods()produced
-
parameterTypeMatches
Whether each parameter type accepts the argument type in the same position. A parameter ofObjectmatches any argument type, includingnull. Any other parameter rejects anullargument type. Remaining pairs match when the classes are the same, or when the boxed parameter type is assignable from the boxed argument type.- Parameters:
parameterTypes- the method's parameter typesargTypes- the argument types, withnullfor an unknown argument- Returns:
truewhen the arrays have equal length and every position matches
-
checkCanSetAccessible
Whether the VM plugin allowsaccessibleObjectto be made accessible fromcaller.- Parameters:
accessibleObject- the reflected member to checkcaller- the class that would callsetAccessible- Returns:
- what
VMPlugin.checkCanSetAccessible(AccessibleObject, Class)returns
-
checkAccessible
public static boolean checkAccessible(Class<?> callerClass, Class<?> declaringClass, int memberModifiers, boolean allowIllegalAccess) WhethercallerClasscan access a member ofdeclaringClasswith the given modifiers, as decided by the VM plugin.- Parameters:
callerClass- the class requesting accessdeclaringClass- the class that declares the membermemberModifiers- the member's modifiersallowIllegalAccess- whether illegal access is permitted- Returns:
- what
VMPlugin.checkAccessible(Class, Class, int, boolean)returns
-
trySetAccessible
Tries to set the accessible flag onao. A security, module, or other failure is swallowed.- Parameters:
ao- the reflected member- Returns:
truewhen the flag was set;falsewhen it could not be
-
makeAccessibleInPrivilegedAction
Makesaoaccessible when the VM plugin allows it. This does not enter a privileged action; it callsmakeAccessible(AccessibleObject).- Parameters:
ao- the reflected member- Returns:
aowhen access was enabled, otherwise an empty optional
-
makeAccessible
Enables access toaowhentrySetAccessible(AccessibleObject)succeeds. A failure leaves the member unchanged.- Parameters:
ao- the reflected member- Returns:
aowhen it was made accessible, otherwise an empty optional
-
makeAccessible
Enables access to every element ofaoa. WhenAccessibleObject.setAccessible(AccessibleObject[], boolean)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, usingaoa's component type.- Parameters:
aoa- the reflected members- Returns:
aoaafter a successful bulk update, otherwise a new array of the members that could be made accessible
-
isSealed
Whetherclazzis sealed. Returnsfalsewhenclazzisnull, whenClass.isSealed()is unavailable on this JVM, or when calling it fails.- Parameters:
clazz- the class to test, ornull- Returns:
trueonly when the JVM reports that the class is sealed
-
getPermittedSubclasses
The direct permitted subclasses of a sealed class. Returns a shared empty array whenclazzisnull, whenClass.getPermittedSubclasses()is unavailable, or when calling it fails. When the JVM method returnsnullbecause the class is not sealed, this method returnsnullas well.- Parameters:
clazz- the class to inspect, ornull- Returns:
- the permitted subclasses, a shared empty array, or
null
-