java.lang.Object
org.apache.commons.jexl3.internal.introspection.ClassTool

final class ClassTool extends Object
Utility for Java9+ backport in Java8 of class and module related methods, and Java16+ backport of record introspection (Class#isRecord(), Class#getRecordComponents()).
  • Field Details

    • IS_RECORD

      private static final MethodHandle IS_RECORD
      Class#isRecord(); null on a pre Java-16 runtime.
    • GET_RECORD_COMPONENTS

      private static final MethodHandle GET_RECORD_COMPONENTS
      Class#getRecordComponents(); null on a pre Java-16 runtime.
    • RECORD_COMPONENT_GET_NAME

      private static final MethodHandle RECORD_COMPONENT_GET_NAME
      java.lang.reflect.RecordComponent#getName(); null on a pre Java-16 runtime.
    • GET_MODULE

      private static final MethodHandle GET_MODULE
      Class#getModule(); null on a pre Java-9 runtime.
    • GET_PKGNAME

      private static final MethodHandle GET_PKGNAME
      Class#getPackageName(); null on a pre Java-9 runtime.
    • IS_EXPORTED

      private static final MethodHandle IS_EXPORTED
      Module#isExported(String, Module); null on a pre Java-9 runtime.
    • JEXL_MODULE

      private static final Object JEXL_MODULE
      The java.lang.Module that declares this class; null on a pre Java-9 runtime.
  • Constructor Details

    • ClassTool

      ClassTool()
  • Method Details

    • isRecord

      static boolean isRecord(Class<?> clazz)
      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

      static boolean hasRecordComponent(Class<?> clazz, String property)
      Whether the given class declares a record component named property.

      Used only to confirm property is 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 class
      property - the property name to match against the record's components
      Returns:
      true if clazz declares a record component named property
    • getPackageName

      static String getPackageName(Class<?> clz)
      Gets the package name of a class (class.getPackage() may return null).
      Parameters:
      clz - The class
      Returns:
      The class package name
    • isExported

      static boolean isExported(Class<?> declarator)
      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, qualified opens, or unqualified opens, 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