pjfanning commented on code in PR #1995: URL: https://github.com/apache/pekko/pull/1995#discussion_r2249921805
########## 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: * this exact same issue exists in the old code * sun.misc.Unsafe requires exactly the same level of access granted by --add-opens * I'm not against logging something though * the code does not fail here though if insufficient access, it just works with standard algorithm instead (ie String getBytes public API) -- 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