He-Pin commented on code in PR #1995:
URL: https://github.com/apache/pekko/pull/1995#discussion_r2249920073


##########
actor/src/main/java/org/apache/pekko/util/Unsafe.java:
##########
@@ -15,47 +15,37 @@
 
 import org.apache.pekko.annotation.InternalApi;
 
-import java.lang.reflect.Field;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.VarHandle;
 import java.nio.charset.StandardCharsets;
 
 /** INTERNAL API */
 @InternalApi
 public final class Unsafe {
-  private static final sun.misc.Unsafe instance;
-
-  private static final long stringValueFieldOffset;
+  private static final VarHandle stringValueFieldHandle;
   private static final int copyUSAsciiStrToBytesAlgorithm;
 
   static {
     try {
-      sun.misc.Unsafe found = null;
-      for (Field field : sun.misc.Unsafe.class.getDeclaredFields()) {
-        if (field.getType() == sun.misc.Unsafe.class) {
-          field.setAccessible(true);
-          found = (sun.misc.Unsafe) field.get(null);
-          break;
-        }
-      }
-      if (found == null) throw new IllegalStateException("Can't find instance 
of sun.misc.Unsafe");
-      else instance = found;
-
-      long fo;
+      VarHandle handle;
       try {
-        fo = 
instance.objectFieldOffset(String.class.getDeclaredField("value"));
-      } catch (NoSuchFieldException nsfe) {
-        // The platform's implementation of String doesn't have a 'value' 
field, so we have to use
-        // algorithm 0
-        fo = -1;
+        MethodHandles.Lookup lookup =
+            MethodHandles.privateLookupIn(String.class, 
MethodHandles.lookup());
+        handle = lookup.findVarHandle(String.class, "value", byte[].class);
+      } catch (NoSuchFieldException | IllegalAccessException e) {
+        // The platform's implementation of String doesn't have a 'value' field
+        // or the field is inaccessible, so we have to use algorithm 0.
+        // You need `--add-opens=java.base/java.lang=ALL-UNNAMED` or similar 
to access it.
+        handle = null;

Review Comment:
   should we print this to error message like kyo?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscr...@pekko.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscr...@pekko.apache.org
For additional commands, e-mail: notifications-h...@pekko.apache.org

Reply via email to