Package org.openjdk.jmc.agent.util
Class AccessUtils
java.lang.Object
org.openjdk.jmc.agent.util.AccessUtils
Helper methods for checking accessibility, implied as modifiers, from various contexts.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic booleanareNestMates(Class<?> lhs, Class<?> rhs) Polyfill Class.isNestMateOf() for pre-11 runtime.static intgetClassAccessFlags(Class<?> c) Polyfill forReflection.getClassAccessFlags(Classinvalid input: '<'?>)asjdk.internal.reflect.Reflectionis not exported.static FieldgetFieldOnHierarchy(Class<?> clazz, String name) Like Class.getDeclaredField, but also gets fields declared by ancestors and interfaces.static Class<?> getNestHost(Class<?> clazz) Polyfill Class.getNestHost() for pre-11 runtime.static Class<?>[]getNestMembers(Class<?> clazz) Polyfill Class.getNestMembers() for pre-11 runtime.static StringgetPackageName(Class<?> clazz) polyfill forClass.getPackageName(Classinvalid input: '<'?>)for pre-9 Java.static booleanisAccessible(Class<?> targetClass, Field field, Class<?> currentClass) Checks whether a field can be accessed from a caller context.static booleanisSameClassPackage(Class<?> lhs, Class<?> rhs) Check whether the two classes exist in the same packagestatic booleanisSubclassOf(Class<?> queryClass, Class<?> ofClass) Check whether a class is a subclass of the otherstatic booleanverifyMemberAccess(Class<?> targetClass, Class<?> memberClass, Class<?> currentClass, int modifiers) Checks whether the field/method/inner class modifier allows access from a caller contextstatic booleanverifyModuleAccess(Class<?> targetClass, Class<?> callerClass) Check whether the module has the class exported for the caller to access.
-
Constructor Details
-
AccessUtils
private AccessUtils()
-
-
Method Details
-
getFieldOnHierarchy
Like Class.getDeclaredField, but also gets fields declared by ancestors and interfaces.- Parameters:
clazz- the class to lookup fromname- the name of the field- Returns:
- the
Fieldobject for the specified field in this class - Throws:
NoSuchFieldException- if a field with the specified name is not found.
-
isAccessible
Checks whether a field can be accessed from a caller context.- Parameters:
targetClass- the class being referencedfield- the field being accessedcurrentClass- 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 referencedmemberClass- the class declaring the field/method/inner classcurrentClass- the caller classmodifiers- member access modifiers in bit flags as a integer- Returns:
-
verifyModuleAccess
Check whether the module has the class exported for the caller to access. For Pre-9 Java runtime, this function always returnstrue.- Parameters:
targetClass- the class being accessedcallerClass- the caller class- Returns:
- whether the class is accessible
-
getPackageName
polyfill forClass.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
Polyfill forReflection.getClassAccessFlags(Classinvalid input: '<'?>)asjdk.internal.reflect.Reflectionis not exported.- Parameters:
c- the class being inspected- Returns:
- the access flags written to the class file
-
isSameClassPackage
Check whether the two classes exist in the same package- Parameters:
lhs- the first classrhs- the second class- Returns:
- whether the given classes exist in the same package
-
isSubclassOf
Check whether a class is a subclass of the other- Parameters:
queryClass- the subclassofClass- the superclass- Returns:
- whether it's a subclass-superclass relationship
-
getNestMembers
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
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 classrhs- the second class- Returns:
- whether the given classes are nestmates
-
getNestHost
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
-