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.
    • 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+).
      • 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.
    • 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+). 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 module = declarator.getModule(); return module.isExported(declarator.getPackageName()); This is required since some classes and methods may not be exported thus not callable through reflection.
        Parameters:
        declarator - the class
        Returns:
        true if class is exported or no module support exists