diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SunMiscSubstitutions.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SunMiscSubstitutions.java index 169cdf9215f9..970bf42de5d3 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SunMiscSubstitutions.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/jdk/SunMiscSubstitutions.java @@ -32,6 +32,7 @@ import java.security.ProtectionDomain; import java.util.function.Function; +import com.oracle.svm.core.StaticFieldsSupport; import org.graalvm.compiler.nodes.extended.MembarNode; import org.graalvm.compiler.serviceprovider.JavaVersionUtil; import org.graalvm.nativeimage.Platform; @@ -206,14 +207,13 @@ public void ensureClassInitialized(Class c) { DynamicHub.fromClass(c).ensureInitialized(); } - @Substitute - private long staticFieldOffset(Field f) { - throw VMError.unsupportedFeature("Unsupported method of Unsafe"); - } - @Substitute private Object staticFieldBase(Field f) { - throw VMError.unsupportedFeature("Unsupported method of Unsafe"); + if (f.getType().isPrimitive()) { + return StaticFieldsSupport.getStaticPrimitiveFields(); + } else { + return StaticFieldsSupport.getStaticObjectFields(); + } } @Substitute diff --git a/substratevm/src/com.oracle.svm.reflect/src/com/oracle/svm/reflect/target/Target_jdk_internal_misc_Unsafe_Reflection.java b/substratevm/src/com.oracle.svm.reflect/src/com/oracle/svm/reflect/target/Target_jdk_internal_misc_Unsafe_Reflection.java index 0be5575908a4..b8c664d0f31b 100644 --- a/substratevm/src/com.oracle.svm.reflect/src/com/oracle/svm/reflect/target/Target_jdk_internal_misc_Unsafe_Reflection.java +++ b/substratevm/src/com.oracle.svm.reflect/src/com/oracle/svm/reflect/target/Target_jdk_internal_misc_Unsafe_Reflection.java @@ -43,14 +43,7 @@ public final class Target_jdk_internal_misc_Unsafe_Reflection { @Substitute public long objectFieldOffset(Target_java_lang_reflect_Field field) { - int offset = field.root == null ? field.offset : field.root.offset; - if (offset > 0) { - return offset; - } - throw VMError.unsupportedFeature("The offset of " + field + " is accessed without the field being first registered as unsafe accessed. " + - "Please register the field as unsafe accessed. You can do so with a reflection configuration that " + - "contains an entry for the field with the attribute \"allowUnsafeAccess\": true. Such a configuration " + - "file can be generated for you. Read BuildConfiguration.md and Reflection.md for details."); + return FieldUtils.getFieldOffset(field); } @Substitute @@ -67,4 +60,27 @@ public long objectFieldOffset(Class c, String name) { throw new InternalError(); } } + + @Substitute + public long staticFieldOffset(Target_java_lang_reflect_Field field) { + /* + * Since we store the offset in the `offset` field, which is computed through + * `FieldOffsetComputer#compute`, the implementation for this is the same as + * `objectFieldOffset` method + */ + return FieldUtils.getFieldOffset(field); + } + + private static class FieldUtils { + private static long getFieldOffset(Target_java_lang_reflect_Field field) { + int offset = field.root == null ? field.offset : field.root.offset; + if (offset > 0) { + return offset; + } + throw VMError.unsupportedFeature("The offset of " + field + " is accessed without the field being first registered as unsafe accessed. " + + "Please register the field as unsafe accessed. You can do so with a reflection configuration that " + + "contains an entry for the field with the attribute \"allowUnsafeAccess\": true. Such a configuration " + + "file can be generated for you. Read BuildConfiguration.md and Reflection.md for details."); + } + } }