Class DefineClassHelper


  • public class DefineClassHelper
    extends java.lang.Object
    Helper class for invoking ClassLoader.defineClass(String,byte[],int,int).
    Since:
    3.22
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.lang.Class<?> toClass​(java.lang.Class<?> neighbor, byte[] bcode)
      Loads a class file by java.lang.invoke.MethodHandles.Lookup.
      static java.lang.Class<?> toClass​(java.lang.invoke.MethodHandles.Lookup lookup, byte[] bcode)
      Loads a class file by java.lang.invoke.MethodHandles.Lookup.
      static java.lang.Class<?> toClass​(java.lang.String className, java.lang.Class<?> neighbor, java.lang.ClassLoader loader, java.security.ProtectionDomain domain, byte[] bcode)
      Loads a class file by a given class loader.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • toClass

        public static java.lang.Class<?> toClass​(java.lang.String className,
                                                 java.lang.Class<?> neighbor,
                                                 java.lang.ClassLoader loader,
                                                 java.security.ProtectionDomain domain,
                                                 byte[] bcode)
                                          throws CannotCompileException
        Loads a class file by a given class loader.

        This first tries to use java.lang.invoke.MethodHandle to load a class. Otherwise, or if neighbor is null, this tries to use sun.misc.Unsafe to load a class. Then it tries to use a protected method in java.lang.ClassLoader via PrivilegedAction. Since the latter approach is not available any longer by default in Java 9 or later, the JVM argument --add-opens java.base/java.lang=ALL-UNNAMED must be given to the JVM. If this JVM argument cannot be given, toPublicClass(String,byte[]) should be used instead.

        Parameters:
        className - the name of the loaded class.
        neighbor - the class contained in the same package as the loaded class.
        loader - the class loader. It can be null if neighbor is not null and the JVM is Java 11 or later.
        domain - if it is null, a default domain is used.
        bcode - the bytecode for the loaded class.
        Throws:
        CannotCompileException
        Since:
        3.22
      • toClass

        public static java.lang.Class<?> toClass​(java.lang.Class<?> neighbor,
                                                 byte[] bcode)
                                          throws CannotCompileException
        Loads a class file by java.lang.invoke.MethodHandles.Lookup. It is obtained by using neighbor.
        Parameters:
        neighbor - a class belonging to the same package that the loaded class belogns to.
        bcode - the bytecode.
        Throws:
        CannotCompileException
        Since:
        3.24
      • toClass

        public static java.lang.Class<?> toClass​(java.lang.invoke.MethodHandles.Lookup lookup,
                                                 byte[] bcode)
                                          throws CannotCompileException
        Loads a class file by java.lang.invoke.MethodHandles.Lookup. It can be obtained by MethodHandles.lookup() called from somewhere in the package that the loaded class belongs to.
        Parameters:
        bcode - the bytecode.
        Throws:
        CannotCompileException
        Since:
        3.24