$ cat MyRecord.java
record MyRecord(int x, int y) {}
$ jdk-jdk-ea/bin/javac --enable-preview --release 16 MyRecord.java
$ jdk-jdk-ea/bin/java --enable-preview -jar jol-cli/target/jol-cli.jar internals MyRecord -cp .
# WARNING: Unable to get Instrumentation. Dynamic Attach failed. You may add this JAR as -javaagent manually, or supply -Djdk.attach.allowAttachSelf
# WARNING: Unable to attach Serviceability Agent. You can try again with escalated privileges. Two options: a) use -Djol.tryWithSudo=true to try with sudo; b) echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-bit shift.
# WARNING | Compressed references base/shifts are guessed by the experiment!
# WARNING | Therefore, computed addresses are just guesses, and ARE NOT RELIABLE.
# WARNING | Make sure to attach Serviceability Agent to get the reliable addresses.
# Objects are 8 bytes aligned.
# Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
# Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
Instantiated the sample instance via MyRecord(int,int)
java.lang.UnsupportedOperationException: can't get field offset on a record (preview): private final int MyRecord.x
at jdk.unsupported/sun.misc.Unsafe.objectFieldOffset(Unsafe.java:649)
at org.openjdk.jol.vm.HotspotUnsafe.fieldOffset(HotspotUnsafe.java:412)
at org.openjdk.jol.layouters.CurrentLayouter.layout(CurrentLayouter.java:66)
at org.openjdk.jol.info.ClassLayout.parseInstance(ClassLayout.java:100)
at org.openjdk.jol.info.ClassLayout.parseInstance(ClassLayout.java:83)
at org.openjdk.jol.operations.ObjectInternals.runWith(ObjectInternals.java:51)
at org.openjdk.jol.operations.ClasspathedOperation.run(ClasspathedOperation.java:76)
This is becauseJDK-8247444 introduced this check:
https://hg.openjdk.java.net/jdk/jdk/file/ae002489df78/src/jdk.unsupported/share/classes/sun/misc/Unsafe.java#l640
This can be side-stepped for JOL uses that only needs to take a look at Record field/components offset.
record MyRecord(int x, int y) {}
$ jdk-jdk-ea/bin/javac --enable-preview --release 16 MyRecord.java
$ jdk-jdk-ea/bin/java --enable-preview -jar jol-cli/target/jol-cli.jar internals MyRecord -cp .
# WARNING: Unable to get Instrumentation. Dynamic Attach failed. You may add this JAR as -javaagent manually, or supply -Djdk.attach.allowAttachSelf
# WARNING: Unable to attach Serviceability Agent. You can try again with escalated privileges. Two options: a) use -Djol.tryWithSudo=true to try with sudo; b) echo 0 | sudo tee /proc/sys/kernel/yama/ptrace_scope
# Running 64-bit HotSpot VM.
# Using compressed oop with 3-bit shift.
# Using compressed klass with 3-bit shift.
# WARNING | Compressed references base/shifts are guessed by the experiment!
# WARNING | Therefore, computed addresses are just guesses, and ARE NOT RELIABLE.
# WARNING | Make sure to attach Serviceability Agent to get the reliable addresses.
# Objects are 8 bytes aligned.
# Field sizes by type: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
# Array element sizes: 4, 1, 1, 2, 2, 4, 4, 8, 8 [bytes]
Instantiated the sample instance via MyRecord(int,int)
java.lang.UnsupportedOperationException: can't get field offset on a record (preview): private final int MyRecord.x
at jdk.unsupported/sun.misc.Unsafe.objectFieldOffset(Unsafe.java:649)
at org.openjdk.jol.vm.HotspotUnsafe.fieldOffset(HotspotUnsafe.java:412)
at org.openjdk.jol.layouters.CurrentLayouter.layout(CurrentLayouter.java:66)
at org.openjdk.jol.info.ClassLayout.parseInstance(ClassLayout.java:100)
at org.openjdk.jol.info.ClassLayout.parseInstance(ClassLayout.java:83)
at org.openjdk.jol.operations.ObjectInternals.runWith(ObjectInternals.java:51)
at org.openjdk.jol.operations.ClasspathedOperation.run(ClasspathedOperation.java:76)
This is because
https://hg.openjdk.java.net/jdk/jdk/file/ae002489df78/src/jdk.unsupported/share/classes/sun/misc/Unsafe.java#l640
This can be side-stepped for JOL uses that only needs to take a look at Record field/components offset.
- relates to
-
JDK-8243287 Removal of Unsafe::defineAnonymousClass
- Resolved
-
CODETOOLS-7902720 JOL: Use Lookup.defineClass instead of deprecating Unsafe.defineAnonymousClass
- Closed
-
CODETOOLS-7902723 Instantiate the Unsafe trampoline only when it is needed
- Closed
-
CODETOOLS-7902931 JOL: Rework magic field offset accessor
- Closed
-
JDK-8247444 Trust final fields in records
- Resolved