JDK-8266760 : Remove sun.misc.Unsafe::defineAnonymousClass
  • Type: CSR
  • Component: hotspot
  • Sub-Component: runtime
  • Priority: P3
  • Status: Closed
  • Resolution: Approved
  • Fix Versions: 17
  • Submitted: 2021-05-07
  • Updated: 2021-05-11
  • Resolved: 2021-05-11
Related Reports
CSR :  
Description
Summary
-------

Remove `sun.misc.Unsafe::defineAnonymousClass`

Problem
-------

`sun.misc.Unsafe::defineAnonymousClass` was deprecated in JDK 15
and deprecated for removal in JDK 16.  The API replacement
is `java.lang.invoke.MethodHandles.Lookup::defineHiddenClass` and
`java.lang.invoke.MethodHandles.Lookup::defineHiddenClassWithClassData`.

Solution
--------

Remove `sun.misc.Unsafe::defineAnonymousClass` from `jdk.unsupported` module.

Specification
-------------

Remove `sun.misc.Unsafe::defineAnonymousClass`:

```  
-     /**
-      * Defines a class but does not make it known to the class loader or system dictionary.
-      * <p>
-      * For each CP entry, the corresponding CP patch must either be null or have
-      * the a format that matches its tag:
-      * <ul>
-      * <li>Integer, Long, Float, Double: the corresponding wrapper object type from java.lang
-      * <li>Utf8: a string (must have suitable syntax if used as signature or name)
-      * <li>Class: any java.lang.Class object
-      * <li>String: any object (not just a java.lang.String)
-      * <li>InterfaceMethodRef: (NYI) a method handle to invoke on that call site's arguments
-      * </ul>
-      *
-      * @deprecated Use {@link java.lang.invoke.MethodHandles.Lookup#defineHiddenClass(byte[], boolean, MethodHandles.Lookup.ClassOption...)}
-      * or {@link java.lang.invoke.MethodHandles.Lookup#defineHiddenClassWithClassData(byte[], Object, boolean, MethodHandles.Lookup.ClassOption...)}
-      * instead.
-      *
-      * @param hostClass context for linkage, access control, protection domain, and class loader
-      * @param data      bytes of a class file
-      * @param cpPatches where non-null entries exist, they replace corresponding CP entries in data
-      */
-     @ForceInline
-     @Deprecated(since = "15", forRemoval = true)
-     @SuppressWarnings("removal")
-     public Class<?> defineAnonymousClass(Class<?> hostClass, byte[] data, Object[] cpPatches) {
-         return theInternalUnsafe.defineAnonymousClass(hostClass, data, cpPatches);
-     }
``` 


Comments
Moving to Approved.
11-05-2021

corpus search shows the following projects using `sun.misc.Unsafe::defineAnonymousClass` and the latest version of com.github.harbby:gadtry no longer uses it. ``` com.github.harbby:gadtry:1.7.2 org.burningwave:core:7.33.0 org.dyvil:dyvil:0.46.5 org.graalvm.nativeimage:svm:20.2.0 org.neo4j:neo4j-unsafe:4.1.1 org.questdb:questdb-core:3.3.3 org.questdb:questdb:5.0.2 org.robolectric:sandbox:4.4 ```
07-05-2021