Class ClassTool


  • final class ClassTool
    extends java.lang.Object
    Utility for Java9+ backport in Java8 of class and module related methods.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.invoke.MethodHandle GET_MODULE
      The Class.getModule() method.
      private static java.lang.invoke.MethodHandle GET_PKGNAME
      The Class.getPackageName() method.
      private static java.lang.invoke.MethodHandle IS_EXPORTED
      The Module.isExported(String packageName) method.
      private static java.lang.Object JEXL_MODULE
      The Module of JEXL itself.
    • Constructor Summary

      Constructors 
      Constructor Description
      ClassTool()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      (package private) static java.lang.String getPackageName​(java.lang.Class<?> clz)
      Gets the package name of a class (class.getPackage() may return null).
      (package private) static boolean isExported​(java.lang.Class<?> declarator)
      Checks whether a class is exported by its module (Java 9+) to JEXL.
      • Methods inherited from class java.lang.Object

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

      • GET_MODULE

        private static final java.lang.invoke.MethodHandle GET_MODULE
        The Class.getModule() method.
      • GET_PKGNAME

        private static final java.lang.invoke.MethodHandle GET_PKGNAME
        The Class.getPackageName() method.
      • IS_EXPORTED

        private static final java.lang.invoke.MethodHandle IS_EXPORTED
        The Module.isExported(String packageName) method.
      • JEXL_MODULE

        private static final java.lang.Object JEXL_MODULE
        The Module of JEXL itself.
    • Constructor Detail

      • ClassTool

        ClassTool()
    • Method Detail

      • getPackageName

        static java.lang.String getPackageName​(java.lang.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​(java.lang.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.getClass().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