JDK-8181443 : Replace usages of jdk.internal.misc.Unsafe with MethodHandles.Lookup.defineClass
  • Type: Bug
  • Component: core-libs
  • Sub-Component: java.lang.invoke
  • Affected Version: 9
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2017-06-01
  • Updated: 2019-04-26
  • Resolved: 2018-10-04
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 12
12 b15Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Sample of usages:

    defineClass(String, byte[], int, int, ClassLoader, ProtectionDomain)
Found usages  (5 usages found)
    Production  (5 usages found)
        jdk  (5 usages found)
            java.lang.invoke  (1 usage found)
                BoundMethodHandle.java  (1 usage found)
                    582UNSAFE.defineClass(className, classFile, 0, classFile.length,
            java.lang.reflect  (1 usage found)
                Proxy.java  (1 usage found)
                    538Class<?> pc = UNSAFE.defineClass(proxyName, proxyClassFile,
            jdk.internal.reflect  (1 usage found)
                ClassDefiner.java  (1 usage found)
                    63return unsafe.defineClass(name, bytes, off, len, newLoader, null);
            jdk.jfr.internal  (1 usage found)
                SecuritySupport.java  (1 usage found)
                    366return unsafe.defineClass(name, bytes, 0, bytes.length, classLoader, null);
            sun.misc  (1 usage found)
                Unsafe.java  (1 usage found)
                    822return theInternalUnsafe.defineClass(name, b, off, len, loader, protectionDomain);

java.lang.reflect.Proxy and jdk.internal.reflect.ClassDefiner can't use Lookup::defineClass as it does not have the ability to create a Lookup object. JFR uses jdk.jfr.internal.SecuritySupport. Currently it defines the classes in its own packages (jdk.jfr and jdk.jfr.internal.handlers). It should be converted to use Lookup::defineClass. java.lang.invoke.ClassSpecializer.Factory defines a specialization class of the top class. It can create a Lookup of the top class and call Lookup::defineClass to define the concrete species.