Class AccessUtils


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

      Constructors 
      Modifier Constructor Description
      private AccessUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static boolean areNestMates​(java.lang.Class<?> lhs, java.lang.Class<?> rhs)
      Polyfill Class.isNestMateOf() for pre-11 runtime.
      static int getClassAccessFlags​(java.lang.Class<?> c)
      Polyfill for Reflection.getClassAccessFlags(Class) as jdk.internal.reflect.Reflection is not exported.
      static java.lang.reflect.Field getFieldOnHierarchy​(java.lang.Class<?> clazz, java.lang.String name)
      Like Class.getDeclaredField, but also gets fields declared by ancestors and interfaces.
      static java.lang.Class<?> getNestHost​(java.lang.Class<?> clazz)
      Polyfill Class.getNestHost() for pre-11 runtime.
      static java.lang.Class<?>[] getNestMembers​(java.lang.Class<?> clazz)
      Polyfill Class.getNestMembers() for pre-11 runtime.
      static java.lang.String getPackageName​(java.lang.Class<?> clazz)
      polyfill for Class.getPackageName(Class) for pre-9 Java.
      static boolean isAccessible​(java.lang.Class<?> targetClass, java.lang.reflect.Field field, java.lang.Class<?> currentClass)
      Checks whether a field can be accessed from a caller context.
      static boolean isSameClassPackage​(java.lang.Class<?> lhs, java.lang.Class<?> rhs)
      Check whether the two classes exist in the same package
      static boolean isSubclassOf​(java.lang.Class<?> queryClass, java.lang.Class<?> ofClass)
      Check whether a class is a subclass of the other
      static boolean verifyMemberAccess​(java.lang.Class<?> targetClass, java.lang.Class<?> memberClass, java.lang.Class<?> currentClass, int modifiers)
      Checks whether the field/method/inner class modifier allows access from a caller context
      static boolean verifyModuleAccess​(java.lang.Class<?> targetClass, java.lang.Class<?> callerClass)
      Check whether the module has the class exported for the caller to access.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • AccessUtils

        private AccessUtils()
    • Method Detail

      • getFieldOnHierarchy

        public static java.lang.reflect.Field getFieldOnHierarchy​(java.lang.Class<?> clazz,
                                                                  java.lang.String name)
                                                           throws java.lang.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:
        java.lang.NoSuchFieldException - if a field with the specified name is not found.
      • isAccessible

        public static boolean isAccessible​(java.lang.Class<?> targetClass,
                                           java.lang.reflect.Field field,
                                           java.lang.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​(java.lang.Class<?> targetClass,
                                                 java.lang.Class<?> memberClass,
                                                 java.lang.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​(java.lang.Class<?> targetClass,
                                                 java.lang.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 java.lang.String getPackageName​(java.lang.Class<?> clazz)
        polyfill for Class.getPackageName(Class) 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​(java.lang.Class<?> c)
        Polyfill for Reflection.getClassAccessFlags(Class) 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​(java.lang.Class<?> lhs,
                                                 java.lang.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​(java.lang.Class<?> queryClass,
                                           java.lang.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 java.lang.Class<?>[] getNestMembers​(java.lang.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​(java.lang.Class<?> lhs,
                                           java.lang.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 java.lang.Class<?> getNestHost​(java.lang.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