Class AccessUtils

java.lang.Object
org.openjdk.jmc.agent.util.AccessUtils

public final class AccessUtils extends Object
Helper methods for checking accessibility, implied as modifiers, from various contexts.
  • Constructor Details

    • AccessUtils

      private AccessUtils()
  • Method Details

    • getFieldOnHierarchy

      public static Field getFieldOnHierarchy(Class<?> clazz, String name) throws NoSuchFieldException
      Like Class.getDeclaredField, but also gets fields declared by ancestors and interfaces.
      Parameters:
      clazz - the class to lookup from
      name - the name of the field
      Returns:
      the Field object for the specified field in this class
      Throws:
      NoSuchFieldException - if a field with the specified name is not found.
    • isAccessible

      public static boolean isAccessible(Class<?> targetClass, Field field, Class<?> currentClass)
      Checks whether a field can be accessed from a caller context.
      Parameters:
      targetClass - the class being referenced
      field - the field being accessed
      currentClass - the caller class
      Returns:
      whether the field is accessible from given context
    • verifyMemberAccess

      public static boolean verifyMemberAccess(Class<?> targetClass, Class<?> memberClass, Class<?> currentClass, int modifiers)
      Checks whether the field/method/inner class modifier allows access from a caller context
      Parameters:
      targetClass - the class being referenced
      memberClass - the class declaring the field/method/inner class
      currentClass - the caller class
      modifiers - member access modifiers in bit flags as a integer
      Returns:
    • verifyModuleAccess

      public static boolean verifyModuleAccess(Class<?> targetClass, Class<?> callerClass)
      Check whether the module has the class exported for the caller to access. For Pre-9 Java runtime, this function always returns true.
      Parameters:
      targetClass - the class being accessed
      callerClass - the caller class
      Returns:
      whether the class is accessible
    • getPackageName

      public static String getPackageName(Class<?> clazz)
      polyfill for Class.getPackageName(Classinvalid input: '<'?>) for pre-9 Java.
      Parameters:
      clazz - the class to lookup the package name against
      Returns:
      the name of the package containing the class
    • getClassAccessFlags

      public static int getClassAccessFlags(Class<?> c)
      Polyfill for Reflection.getClassAccessFlags(Classinvalid input: '<'?>) as jdk.internal.reflect.Reflection is not exported.
      Parameters:
      c - the class being inspected
      Returns:
      the access flags written to the class file
    • isSameClassPackage

      public static boolean isSameClassPackage(Class<?> lhs, Class<?> rhs)
      Check whether the two classes exist in the same package
      Parameters:
      lhs - the first class
      rhs - the second class
      Returns:
      whether the given classes exist in the same package
    • isSubclassOf

      public static boolean isSubclassOf(Class<?> queryClass, Class<?> ofClass)
      Check whether a class is a subclass of the other
      Parameters:
      queryClass - the subclass
      ofClass - the superclass
      Returns:
      whether it's a subclass-superclass relationship
    • getNestMembers

      public static Class<?>[] getNestMembers(Class<?> clazz)
      Polyfill Class.getNestMembers() for pre-11 runtime. This function does not fully respect the definition of nesting from JVM's perspective. It's only used for validating access.
      Parameters:
      clazz - the class to inspect against
      Returns:
      an array of all nest members
    • areNestMates

      public static boolean areNestMates(Class<?> lhs, Class<?> rhs)
      Polyfill Class.isNestMateOf() for pre-11 runtime. This function does not fully respect the definition of nesting from JVM's perspective. It's only used for validating access.
      Parameters:
      lhs - the first class
      rhs - the second class
      Returns:
      whether the given classes are nestmates
    • getNestHost

      public static Class<?> getNestHost(Class<?> clazz)
      Polyfill Class.getNestHost() for pre-11 runtime. This function does not fully respect the definition of nesting from JVM's perspective. It's only used for validating access.
      Parameters:
      clazz - the class the inspect against
      Returns:
      the nesthost of the class