Class ClassTool
java.lang.Object
org.apache.commons.jexl3.internal.introspection.ClassTool
Utility for Java9+ backport in Java8 of class and module related methods, and Java16+ backport of
record introspection (
Class#isRecord(), Class#getRecordComponents()).-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate static final MethodHandleClass#getModule(); null on a pre Java-9 runtime.private static final MethodHandleClass#getPackageName(); null on a pre Java-9 runtime.private static final MethodHandleClass#getRecordComponents(); null on a pre Java-16 runtime.private static final MethodHandleModule#isExported(String, Module); null on a pre Java-9 runtime.private static final MethodHandleClass#isRecord(); null on a pre Java-16 runtime.private static final ObjectThejava.lang.Modulethat declares this class; null on a pre Java-9 runtime.private static final MethodHandlejava.lang.reflect.RecordComponent#getName(); null on a pre Java-16 runtime. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription(package private) static StringgetPackageName(Class<?> clz) Gets the package name of a class (class.getPackage() may return null).(package private) static booleanhasRecordComponent(Class<?> clazz, String property) Whether the given class declares a record component namedproperty.(package private) static booleanisExported(Class<?> declarator) Checks whether a class is exported by its module (Java 9+) to JEXL.(package private) static booleanWhether the given class is a record on this runtime.
-
Field Details
-
IS_RECORD
Class#isRecord(); null on a pre Java-16 runtime. -
GET_RECORD_COMPONENTS
Class#getRecordComponents(); null on a pre Java-16 runtime. -
RECORD_COMPONENT_GET_NAME
java.lang.reflect.RecordComponent#getName(); null on a pre Java-16 runtime. -
GET_MODULE
Class#getModule(); null on a pre Java-9 runtime. -
GET_PKGNAME
Class#getPackageName(); null on a pre Java-9 runtime. -
IS_EXPORTED
Module#isExported(String, Module); null on a pre Java-9 runtime. -
JEXL_MODULE
Thejava.lang.Modulethat declares this class; null on a pre Java-9 runtime.
-
-
Constructor Details
-
ClassTool
ClassTool()
-
-
Method Details
-
isRecord
Whether the given class is a record on this runtime.- Parameters:
clazz- the class to check- Returns:
- true if clazz is a record, false if it is not or if records are unsupported here
-
hasRecordComponent
Whether the given class declares a record component namedproperty.Used only to confirm
propertyis a genuine record component before the accessor is resolved through the (permission-checked)Introspector; the accessor method instances gathered here are discarded.- Parameters:
clazz- the record classproperty- the property name to match against the record's components- Returns:
- true if clazz declares a record component named property
-
getPackageName
Gets the package name of a class (class.getPackage() may return null).- Parameters:
clz- The class- Returns:
- The class package name
-
isExported
Checks whether a class is exported by its module (Java 9+) to JEXL. The code performs the following sequence through reflection (since the same jar can run on a Java8 or Java9+ runtime and the module features does not exist on 8).Module jexlModule = ClassTool.class.getModule(); Module module = declarator.getModule(); return module.isExported(declarator.getPackageName(), jexlModule);This is required since some classes and methods may not be exported thus not callable through reflection. A package can be non-exported, unconditionally exported (to all reading modules), or use qualified exports to only export the package to specifically named modules. This method is only concerned with whether JEXL may reflectively access the package, so a qualified export naming the JEXL module is the least-privilege access required. The declarator's module may also use: unqualified exports, qualifiedopens, or unqualifiedopens, in increasing order of privilege; the last two allow reflective access to non-public members and are not recommended.- Parameters:
declarator- The class- Returns:
- true if class is exported (to JEXL) or no module support exists
-