Package org.codehaus.groovy.reflection

# Class ReflectionUtils

java.lang.Object
org.codehaus.groovy.reflection.ReflectionUtils

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.

- ## Constructor Summary

  Constructors
  Constructor
  Description
  `ReflectionUtils()`

- ## Method Summary

  Modifier and Type
  Method
  Description
  `static boolean`
  `checkAccessible(Class<?> callerClass, Class<?> declaringClass, int memberModifiers, boolean allowIllegalAccess)`
  `static boolean`
  `checkCanSetAccessible(AccessibleObject accessibleObject, Class<?> caller)`
  `static Class`
  `getCallingClass()`
  Gets the immediate calling class, ignoring MOP frames.
  `static Class`
  `getCallingClass(int matchLevel)`
  Gets the called that is matchLevel stack frames before the call, ignoring MOP frames.
  `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.
  `static Constructor<?>[]`
  `getDeclaredConstructorsSorted(Class<?> type)`
  Returns the declared constructors of a class in a deterministic order.
  `static List<Method>`
  `getDeclaredMethods(Class<?> type, String name, Class<?>... parameterTypes)`
  `static Method[]`
  `getDeclaredMethodsSorted(Class<?> type)`
  Returns the declared methods of a class in a deterministic order.
  `static List<Method>`
  `getMethods(Class<?> type, String name, Class<?>... parameterTypes)`
  `static Class<?>[]`
  `getPermittedSubclasses(Class<?> clazz)`
  `static boolean`
  `isCallingClassReflectionAvailable()`
  Determines whether the getCallingClass methods will return any sensible results.
  `static boolean`
  `isSealed(Class<?> clazz)`
  `static Optional<AccessibleObject>`
  `makeAccessible(AccessibleObject ao)`
  `static AccessibleObject[]`
  `makeAccessible(AccessibleObject[] aoa)`
  `static Optional<AccessibleObject>`
  `makeAccessibleInPrivilegedAction(AccessibleObject ao)`
  `static boolean`
  `parameterTypeMatches(Class<?>[] parameterTypes, Class<?>[] argTypes)`
  `static boolean`
  `trySetAccessible(AccessibleObject ao)`

  ### Methods inherited from class java.lang.Object

  `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- ## 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

    public static Class getCallingClass()
    Gets the immediate calling class, ignoring MOP frames.
    Returns:
      The Class of the caller

  - ### getCallingClass

    public static Class getCallingClass(int matchLevel)
    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

    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.
    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)

  - ### getDeclaredMethodsSorted

    public static Method[] getDeclaredMethodsSorted(Class<?> type)
    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, 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.

    Parameters:
      `type` - the class to introspect
    Returns:
      the declared methods, sorted by name then signature
    Since:
      5.0.8

  - ### getDeclaredConstructorsSorted

    public static Constructor<?>[] getDeclaredConstructorsSorted(Class<?> type)
    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)`.

    Parameters:
      `type` - the class to introspect
    Returns:
      the declared constructors, sorted by parameter types
    Since:
      5.0.8

  - ### getMethods

    public static List<Method> getMethods(Class<?> type, String name, Class<?>... parameterTypes)

  - ### parameterTypeMatches

    public static boolean parameterTypeMatches(Class<?>[] parameterTypes, Class<?>[] argTypes)

  - ### checkCanSetAccessible

    public static boolean checkCanSetAccessible(AccessibleObject accessibleObject, Class<?> caller)

  - ### checkAccessible

    public static boolean checkAccessible(Class<?> callerClass, Class<?> declaringClass, int memberModifiers, boolean allowIllegalAccess)

  - ### trySetAccessible

    public static boolean trySetAccessible(AccessibleObject ao)

  - ### makeAccessibleInPrivilegedAction

    public static Optional<AccessibleObject> makeAccessibleInPrivilegedAction(AccessibleObject ao)

  - ### makeAccessible

    public static Optional<AccessibleObject> makeAccessible(AccessibleObject ao)

  - ### makeAccessible

    public static AccessibleObject[] makeAccessible(AccessibleObject[] aoa)

  - ### isSealed

    public static boolean isSealed(Class<?> clazz)

  - ### getPermittedSubclasses

    public static Class<?>[] getPermittedSubclasses(Class<?> clazz)