diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java index f3b3e7cc1b00..cf6fe7e14ab9 100644 --- a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/hub/DynamicHub.java @@ -72,6 +72,9 @@ import java.util.Optional; import java.util.StringJoiner; +import com.oracle.svm.core.reflect.fieldaccessor.UnsafeFieldAccessorFactory; +import jdk.internal.access.JavaLangReflectAccess; +import jdk.internal.reflect.FieldAccessor; import org.graalvm.compiler.core.common.NumUtil; import org.graalvm.compiler.core.common.SuppressFBWarnings; import org.graalvm.nativeimage.AnnotationAccess; @@ -1881,6 +1884,9 @@ final class Target_jdk_internal_reflect_ReflectionFactory { @Alias // private static ReflectionFactory soleInstance; + @Alias // + private JavaLangReflectAccess langReflectAccess; + /** * This substitution eliminates the SecurityManager check in the original method, which would * make some build-time verifications fail. @@ -1890,15 +1896,20 @@ public static ReflectionFactory getReflectionFactory() { return soleInstance; } - /** - * Do not use the field handle based field accessor but the one based on unsafe. It takes effect - * when {@code Target_java_lang_reflect_Field#fieldAccessorField#fieldAccessor} is recomputed at - * runtime. See also GR-39586. - */ - @TargetElement(onlyWith = JDK19OrLater.class) @Substitute - static boolean useFieldHandleAccessor() { - return false; + public FieldAccessor newFieldAccessor(Field field0, boolean override) { + Field field = field0; + Field root = langReflectAccess.getRoot(field); + if (root != null) { + // FieldAccessor will use the root unless the modifiers have + // been overridden + if (root.getModifiers() == field.getModifiers() || !override) { + field = root; + } + } + boolean isFinal = Modifier.isFinal(field.getModifiers()); + boolean isReadOnly = isFinal && (!override || langReflectAccess.isTrustedFinalField(field)); + return UnsafeFieldAccessorFactory.newFieldAccessor(field, isReadOnly); } } diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeBooleanFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeBooleanFieldAccessorImpl.java new file mode 100644 index 000000000000..2b02775ac740 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeBooleanFieldAccessorImpl.java @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeBooleanFieldAccessorImpl extends UnsafeFieldAccessorImpl { + UnsafeBooleanFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Boolean.valueOf(getBoolean(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getBoolean(obj, fieldOffset); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + throw newGetLongIllegalArgumentException(); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + throw newGetFloatIllegalArgumentException(); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + throw newGetDoubleIllegalArgumentException(); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Boolean) { + unsafe.putBoolean(obj, fieldOffset, ((Boolean) value).booleanValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(z); + } + unsafe.putBoolean(obj, fieldOffset, z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeByteFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeByteFieldAccessorImpl.java new file mode 100644 index 000000000000..d4b1146d362c --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeByteFieldAccessorImpl.java @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeByteFieldAccessorImpl extends UnsafeFieldAccessorImpl { + UnsafeByteFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Byte.valueOf(getByte(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getByte(obj, fieldOffset); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putByte(obj, fieldOffset, ((Byte) value).byteValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(b); + } + unsafe.putByte(obj, fieldOffset, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeCharacterFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeCharacterFieldAccessorImpl.java new file mode 100644 index 000000000000..ed0d04cd2b12 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeCharacterFieldAccessorImpl.java @@ -0,0 +1,149 @@ +/* + * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeCharacterFieldAccessorImpl extends UnsafeFieldAccessorImpl { + UnsafeCharacterFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Character.valueOf(getChar(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getChar(obj, fieldOffset); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + return getChar(obj); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return getChar(obj); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getChar(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getChar(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Character) { + unsafe.putChar(obj, fieldOffset, ((Character) value).charValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(c); + } + unsafe.putChar(obj, fieldOffset, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeDoubleFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeDoubleFieldAccessorImpl.java new file mode 100644 index 000000000000..c3d6d951e7e4 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeDoubleFieldAccessorImpl.java @@ -0,0 +1,173 @@ +/* + * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeDoubleFieldAccessorImpl extends UnsafeFieldAccessorImpl { + UnsafeDoubleFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Double.valueOf(getDouble(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + throw newGetLongIllegalArgumentException(); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + throw newGetFloatIllegalArgumentException(); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getDouble(obj, fieldOffset); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putDouble(obj, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putDouble(obj, fieldOffset, ((Short) value).shortValue()); + return; + } + if (value instanceof Character) { + unsafe.putDouble(obj, fieldOffset, ((Character) value).charValue()); + return; + } + if (value instanceof Integer) { + unsafe.putDouble(obj, fieldOffset, ((Integer) value).intValue()); + return; + } + if (value instanceof Long) { + unsafe.putDouble(obj, fieldOffset, ((Long) value).longValue()); + return; + } + if (value instanceof Float) { + unsafe.putDouble(obj, fieldOffset, ((Float) value).floatValue()); + return; + } + if (value instanceof Double) { + unsafe.putDouble(obj, fieldOffset, ((Double) value).doubleValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(d); + } + unsafe.putDouble(obj, fieldOffset, d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeFieldAccessorFactory.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeFieldAccessorFactory.java new file mode 100644 index 000000000000..314131fe4849 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeFieldAccessorFactory.java @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import jdk.internal.reflect.FieldAccessor; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +public class UnsafeFieldAccessorFactory { + public static FieldAccessor newFieldAccessor(Field field, boolean isReadOnly) { + Class type = field.getType(); + boolean isStatic = Modifier.isStatic(field.getModifiers()); + boolean isFinal = Modifier.isFinal(field.getModifiers()); + boolean isVolatile = Modifier.isVolatile(field.getModifiers()); + boolean isQualified = isFinal || isVolatile; + if (isStatic) { + // This code path does not guarantee that the field's + // declaring class has been initialized, but it must be + // before performing reflective operations. + UnsafeFieldAccessorImpl.unsafe.ensureClassInitialized(field.getDeclaringClass()); + + if (!isQualified) { + if (type == Boolean.TYPE) { + return new UnsafeStaticBooleanFieldAccessorImpl(field); + } else if (type == Byte.TYPE) { + return new UnsafeStaticByteFieldAccessorImpl(field); + } else if (type == Short.TYPE) { + return new UnsafeStaticShortFieldAccessorImpl(field); + } else if (type == Character.TYPE) { + return new UnsafeStaticCharacterFieldAccessorImpl(field); + } else if (type == Integer.TYPE) { + return new UnsafeStaticIntegerFieldAccessorImpl(field); + } else if (type == Long.TYPE) { + return new UnsafeStaticLongFieldAccessorImpl(field); + } else if (type == Float.TYPE) { + return new UnsafeStaticFloatFieldAccessorImpl(field); + } else if (type == Double.TYPE) { + return new UnsafeStaticDoubleFieldAccessorImpl(field); + } else { + return new UnsafeStaticObjectFieldAccessorImpl(field); + } + } else { + if (type == Boolean.TYPE) { + return new UnsafeQualifiedStaticBooleanFieldAccessorImpl(field, isReadOnly); + } else if (type == Byte.TYPE) { + return new UnsafeQualifiedStaticByteFieldAccessorImpl(field, isReadOnly); + } else if (type == Short.TYPE) { + return new UnsafeQualifiedStaticShortFieldAccessorImpl(field, isReadOnly); + } else if (type == Character.TYPE) { + return new UnsafeQualifiedStaticCharacterFieldAccessorImpl(field, isReadOnly); + } else if (type == Integer.TYPE) { + return new UnsafeQualifiedStaticIntegerFieldAccessorImpl(field, isReadOnly); + } else if (type == Long.TYPE) { + return new UnsafeQualifiedStaticLongFieldAccessorImpl(field, isReadOnly); + } else if (type == Float.TYPE) { + return new UnsafeQualifiedStaticFloatFieldAccessorImpl(field, isReadOnly); + } else if (type == Double.TYPE) { + return new UnsafeQualifiedStaticDoubleFieldAccessorImpl(field, isReadOnly); + } else { + return new UnsafeQualifiedStaticObjectFieldAccessorImpl(field, isReadOnly); + } + } + } else { + if (!isQualified) { + if (type == Boolean.TYPE) { + return new UnsafeBooleanFieldAccessorImpl(field); + } else if (type == Byte.TYPE) { + return new UnsafeByteFieldAccessorImpl(field); + } else if (type == Short.TYPE) { + return new UnsafeShortFieldAccessorImpl(field); + } else if (type == Character.TYPE) { + return new UnsafeCharacterFieldAccessorImpl(field); + } else if (type == Integer.TYPE) { + return new UnsafeIntegerFieldAccessorImpl(field); + } else if (type == Long.TYPE) { + return new UnsafeLongFieldAccessorImpl(field); + } else if (type == Float.TYPE) { + return new UnsafeFloatFieldAccessorImpl(field); + } else if (type == Double.TYPE) { + return new UnsafeDoubleFieldAccessorImpl(field); + } else { + return new UnsafeObjectFieldAccessorImpl(field); + } + } else { + if (type == Boolean.TYPE) { + return new UnsafeQualifiedBooleanFieldAccessorImpl(field, isReadOnly); + } else if (type == Byte.TYPE) { + return new UnsafeQualifiedByteFieldAccessorImpl(field, isReadOnly); + } else if (type == Short.TYPE) { + return new UnsafeQualifiedShortFieldAccessorImpl(field, isReadOnly); + } else if (type == Character.TYPE) { + return new UnsafeQualifiedCharacterFieldAccessorImpl(field, isReadOnly); + } else if (type == Integer.TYPE) { + return new UnsafeQualifiedIntegerFieldAccessorImpl(field, isReadOnly); + } else if (type == Long.TYPE) { + return new UnsafeQualifiedLongFieldAccessorImpl(field, isReadOnly); + } else if (type == Float.TYPE) { + return new UnsafeQualifiedFloatFieldAccessorImpl(field, isReadOnly); + } else if (type == Double.TYPE) { + return new UnsafeQualifiedDoubleFieldAccessorImpl(field, isReadOnly); + } else { + return new UnsafeQualifiedObjectFieldAccessorImpl(field, isReadOnly); + } + } + } + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeFieldAccessorImpl.java new file mode 100644 index 000000000000..7309e6626018 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeFieldAccessorImpl.java @@ -0,0 +1,305 @@ +/* + * Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; + +import com.oracle.svm.core.SubstrateUtil; +import com.oracle.svm.core.util.VMError; +import jdk.internal.misc.Unsafe; +import jdk.internal.reflect.FieldAccessor; + +/** + * Base class for jdk.internal.misc.Unsafe-based FieldAccessors. The observation is that there are + * only nine types of fields from the standpoint of reflection code: the eight primitive types and + * Object. Using class Unsafe instead of generated bytecodes saves memory and loading time for the + * dynamically-generated FieldAccessors. + */ +abstract class UnsafeFieldAccessorImpl implements FieldAccessor { + protected final Field field; + + static final Unsafe unsafe = Unsafe.getUnsafe(); + + protected final long fieldOffset; + protected final boolean isFinal; + + UnsafeFieldAccessorImpl(Field field) { + VMError.guarantee(!SubstrateUtil.HOSTED, "UnsafeFieldAccessors must only be create at runtime."); + this.field = field; + int mods = field.getModifiers(); + this.isFinal = Modifier.isFinal(mods); + if (Modifier.isStatic(mods)) { + fieldOffset = unsafe.staticFieldOffset(field); + } else { + fieldOffset = unsafe.objectFieldOffset(field); + } + } + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract Object get(Object obj) + throws IllegalArgumentException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract boolean getBoolean(Object obj) + throws IllegalArgumentException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract byte getByte(Object obj) + throws IllegalArgumentException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract char getChar(Object obj) + throws IllegalArgumentException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract short getShort(Object obj) + throws IllegalArgumentException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract int getInt(Object obj) + throws IllegalArgumentException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract long getLong(Object obj) + throws IllegalArgumentException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract float getFloat(Object obj) + throws IllegalArgumentException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract double getDouble(Object obj) + throws IllegalArgumentException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException; + + /** Matches specification in {@link java.lang.reflect.Field}. */ + @Override + public abstract void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException; + + protected void ensureObj(Object o) { + // NOTE: will throw NullPointerException, as specified, if o is null + if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) { + throwSetIllegalArgumentException(o); + } + } + + protected String getQualifiedFieldName() { + return field.getDeclaringClass().getName() + "." + field.getName(); + } + + protected IllegalArgumentException newGetIllegalArgumentException(String type) { + return new IllegalArgumentException( + "Attempt to get " + field.getType().getName() + " field \"" + + getQualifiedFieldName() + "\" with illegal data type conversion to " + type); + } + + protected void throwFinalFieldIllegalAccessException(String attemptedType, + String attemptedValue) + throws IllegalAccessException { + throw new IllegalAccessException(getSetMessage(attemptedType, attemptedValue)); + } + + protected void throwFinalFieldIllegalAccessException(Object o) throws IllegalAccessException { + throwFinalFieldIllegalAccessException(o != null ? o.getClass().getName() : "", ""); + } + + protected void throwFinalFieldIllegalAccessException(boolean z) throws IllegalAccessException { + throwFinalFieldIllegalAccessException("boolean", Boolean.toString(z)); + } + + protected void throwFinalFieldIllegalAccessException(char b) throws IllegalAccessException { + throwFinalFieldIllegalAccessException("char", Character.toString(b)); + } + + protected void throwFinalFieldIllegalAccessException(byte b) throws IllegalAccessException { + throwFinalFieldIllegalAccessException("byte", Byte.toString(b)); + } + + protected void throwFinalFieldIllegalAccessException(short b) throws IllegalAccessException { + throwFinalFieldIllegalAccessException("short", Short.toString(b)); + } + + protected void throwFinalFieldIllegalAccessException(int i) throws IllegalAccessException { + throwFinalFieldIllegalAccessException("int", Integer.toString(i)); + } + + protected void throwFinalFieldIllegalAccessException(long i) throws IllegalAccessException { + throwFinalFieldIllegalAccessException("long", Long.toString(i)); + } + + protected void throwFinalFieldIllegalAccessException(float f) throws IllegalAccessException { + throwFinalFieldIllegalAccessException("float", Float.toString(f)); + } + + protected void throwFinalFieldIllegalAccessException(double f) throws IllegalAccessException { + throwFinalFieldIllegalAccessException("double", Double.toString(f)); + } + + protected IllegalArgumentException newGetBooleanIllegalArgumentException() { + return newGetIllegalArgumentException("boolean"); + } + + protected IllegalArgumentException newGetByteIllegalArgumentException() { + return newGetIllegalArgumentException("byte"); + } + + protected IllegalArgumentException newGetCharIllegalArgumentException() { + return newGetIllegalArgumentException("char"); + } + + protected IllegalArgumentException newGetShortIllegalArgumentException() { + return newGetIllegalArgumentException("short"); + } + + protected IllegalArgumentException newGetIntIllegalArgumentException() { + return newGetIllegalArgumentException("int"); + } + + protected IllegalArgumentException newGetLongIllegalArgumentException() { + return newGetIllegalArgumentException("long"); + } + + protected IllegalArgumentException newGetFloatIllegalArgumentException() { + return newGetIllegalArgumentException("float"); + } + + protected IllegalArgumentException newGetDoubleIllegalArgumentException() { + return newGetIllegalArgumentException("double"); + } + + protected String getSetMessage(String attemptedType, String attemptedValue) { + String err = "Can not set"; + if (Modifier.isStatic(field.getModifiers())) { + err += " static"; + } + if (Modifier.isFinal(field.getModifiers())) { + err += " final"; + } + err += " " + field.getType().getName() + " field " + getQualifiedFieldName() + " to "; + if (!attemptedValue.isEmpty()) { + err += "(" + attemptedType + ")" + attemptedValue; + } else { + if (!attemptedType.isEmpty()) { + err += attemptedType; + } else { + err += "null value"; + } + } + return err; + } + + protected void throwSetIllegalArgumentException(String attemptedType, + String attemptedValue) { + throw new IllegalArgumentException(getSetMessage(attemptedType, attemptedValue)); + } + + protected void throwSetIllegalArgumentException(Object o) { + throwSetIllegalArgumentException(o != null ? o.getClass().getName() : "", ""); + } + + protected void throwSetIllegalArgumentException(boolean b) { + throwSetIllegalArgumentException("boolean", Boolean.toString(b)); + } + + protected void throwSetIllegalArgumentException(byte b) { + throwSetIllegalArgumentException("byte", Byte.toString(b)); + } + + protected void throwSetIllegalArgumentException(char c) { + throwSetIllegalArgumentException("char", Character.toString(c)); + } + + protected void throwSetIllegalArgumentException(short s) { + throwSetIllegalArgumentException("short", Short.toString(s)); + } + + protected void throwSetIllegalArgumentException(int i) { + throwSetIllegalArgumentException("int", Integer.toString(i)); + } + + protected void throwSetIllegalArgumentException(long l) { + throwSetIllegalArgumentException("long", Long.toString(l)); + } + + protected void throwSetIllegalArgumentException(float f) { + throwSetIllegalArgumentException("float", Float.toString(f)); + } + + protected void throwSetIllegalArgumentException(double d) { + throwSetIllegalArgumentException("double", Double.toString(d)); + } + +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeFloatFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeFloatFieldAccessorImpl.java new file mode 100644 index 000000000000..e7c285f0caeb --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeFloatFieldAccessorImpl.java @@ -0,0 +1,169 @@ +/* + * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeFloatFieldAccessorImpl extends UnsafeFieldAccessorImpl { + UnsafeFloatFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Float.valueOf(getFloat(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + throw newGetLongIllegalArgumentException(); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getFloat(obj, fieldOffset); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getFloat(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putFloat(obj, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putFloat(obj, fieldOffset, ((Short) value).shortValue()); + return; + } + if (value instanceof Character) { + unsafe.putFloat(obj, fieldOffset, ((Character) value).charValue()); + return; + } + if (value instanceof Integer) { + unsafe.putFloat(obj, fieldOffset, ((Integer) value).intValue()); + return; + } + if (value instanceof Long) { + unsafe.putFloat(obj, fieldOffset, ((Long) value).longValue()); + return; + } + if (value instanceof Float) { + unsafe.putFloat(obj, fieldOffset, ((Float) value).floatValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(f); + } + unsafe.putFloat(obj, fieldOffset, f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeIntegerFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeIntegerFieldAccessorImpl.java new file mode 100644 index 000000000000..c11b868e6e7a --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeIntegerFieldAccessorImpl.java @@ -0,0 +1,161 @@ +/* + * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeIntegerFieldAccessorImpl extends UnsafeFieldAccessorImpl { + UnsafeIntegerFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Integer.valueOf(getInt(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getInt(obj, fieldOffset); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return getInt(obj); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getInt(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getInt(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putInt(obj, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putInt(obj, fieldOffset, ((Short) value).shortValue()); + return; + } + if (value instanceof Character) { + unsafe.putInt(obj, fieldOffset, ((Character) value).charValue()); + return; + } + if (value instanceof Integer) { + unsafe.putInt(obj, fieldOffset, ((Integer) value).intValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setInt(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + setInt(obj, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + setInt(obj, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(i); + } + unsafe.putInt(obj, fieldOffset, i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeLongFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeLongFieldAccessorImpl.java new file mode 100644 index 000000000000..d5baa1a39d79 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeLongFieldAccessorImpl.java @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeLongFieldAccessorImpl extends UnsafeFieldAccessorImpl { + UnsafeLongFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Long.valueOf(getLong(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getLong(obj, fieldOffset); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getLong(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getLong(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putLong(obj, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putLong(obj, fieldOffset, ((Short) value).shortValue()); + return; + } + if (value instanceof Character) { + unsafe.putLong(obj, fieldOffset, ((Character) value).charValue()); + return; + } + if (value instanceof Integer) { + unsafe.putLong(obj, fieldOffset, ((Integer) value).intValue()); + return; + } + if (value instanceof Long) { + unsafe.putLong(obj, fieldOffset, ((Long) value).longValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setLong(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + setLong(obj, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + setLong(obj, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + setLong(obj, i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(l); + } + unsafe.putLong(obj, fieldOffset, l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeObjectFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeObjectFieldAccessorImpl.java new file mode 100644 index 000000000000..2661d0cd7e28 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeObjectFieldAccessorImpl.java @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeObjectFieldAccessorImpl extends UnsafeFieldAccessorImpl { + UnsafeObjectFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getReference(obj, fieldOffset); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + throw newGetLongIllegalArgumentException(); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + throw newGetFloatIllegalArgumentException(); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + throw newGetDoubleIllegalArgumentException(); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value != null) { + if (!field.getType().isInstance(value)) { + throwSetIllegalArgumentException(value); + } + } + unsafe.putReference(obj, fieldOffset, value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedBooleanFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedBooleanFieldAccessorImpl.java new file mode 100644 index 000000000000..cd4820b9e36d --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedBooleanFieldAccessorImpl.java @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedBooleanFieldAccessorImpl + extends UnsafeQualifiedFieldAccessorImpl { + UnsafeQualifiedBooleanFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Boolean.valueOf(getBoolean(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getBooleanVolatile(obj, fieldOffset); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + throw newGetLongIllegalArgumentException(); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + throw newGetFloatIllegalArgumentException(); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + throw newGetDoubleIllegalArgumentException(); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Boolean) { + unsafe.putBooleanVolatile(obj, fieldOffset, ((Boolean) value).booleanValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(z); + } + unsafe.putBooleanVolatile(obj, fieldOffset, z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedByteFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedByteFieldAccessorImpl.java new file mode 100644 index 000000000000..1675067e62f9 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedByteFieldAccessorImpl.java @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedByteFieldAccessorImpl + extends UnsafeQualifiedFieldAccessorImpl { + UnsafeQualifiedByteFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Byte.valueOf(getByte(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getByteVolatile(obj, fieldOffset); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putByteVolatile(obj, fieldOffset, ((Byte) value).byteValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(b); + } + unsafe.putByteVolatile(obj, fieldOffset, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedCharacterFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedCharacterFieldAccessorImpl.java new file mode 100644 index 000000000000..3e7171077e68 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedCharacterFieldAccessorImpl.java @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedCharacterFieldAccessorImpl + extends UnsafeQualifiedFieldAccessorImpl { + UnsafeQualifiedCharacterFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Character.valueOf(getChar(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getCharVolatile(obj, fieldOffset); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + return getChar(obj); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return getChar(obj); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getChar(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getChar(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Character) { + unsafe.putCharVolatile(obj, fieldOffset, ((Character) value).charValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(c); + } + unsafe.putCharVolatile(obj, fieldOffset, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedDoubleFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedDoubleFieldAccessorImpl.java new file mode 100644 index 000000000000..3c3213262a36 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedDoubleFieldAccessorImpl.java @@ -0,0 +1,174 @@ +/* + * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedDoubleFieldAccessorImpl + extends UnsafeQualifiedFieldAccessorImpl { + UnsafeQualifiedDoubleFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Double.valueOf(getDouble(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + throw newGetLongIllegalArgumentException(); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + throw newGetFloatIllegalArgumentException(); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getDoubleVolatile(obj, fieldOffset); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putDoubleVolatile(obj, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putDoubleVolatile(obj, fieldOffset, ((Short) value).shortValue()); + return; + } + if (value instanceof Character) { + unsafe.putDoubleVolatile(obj, fieldOffset, ((Character) value).charValue()); + return; + } + if (value instanceof Integer) { + unsafe.putDoubleVolatile(obj, fieldOffset, ((Integer) value).intValue()); + return; + } + if (value instanceof Long) { + unsafe.putDoubleVolatile(obj, fieldOffset, ((Long) value).longValue()); + return; + } + if (value instanceof Float) { + unsafe.putDoubleVolatile(obj, fieldOffset, ((Float) value).floatValue()); + return; + } + if (value instanceof Double) { + unsafe.putDoubleVolatile(obj, fieldOffset, ((Double) value).doubleValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(d); + } + unsafe.putDoubleVolatile(obj, fieldOffset, d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedFieldAccessorImpl.java new file mode 100644 index 000000000000..dd50323895ac --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedFieldAccessorImpl.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2004, 2023, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +/** + * Base class for jdk.internal.misc.Unsafe-based FieldAccessors for fields with final or volatile + * qualifiers. These differ from unqualified versions in that (1) they check for read-only status + * (2) they use the volatile forms of Unsafe get/put methods. (When accessed via reflection, finals + * act as slightly "lighter" forms of volatiles. So the volatile forms are heavier than necessary in + * terms of underlying reordering rules and memory barriers, but preserve correctness.) + */ + +abstract class UnsafeQualifiedFieldAccessorImpl + extends UnsafeFieldAccessorImpl { + protected final boolean isReadOnly; + + UnsafeQualifiedFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field); + this.isReadOnly = isReadOnly; + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedFloatFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedFloatFieldAccessorImpl.java new file mode 100644 index 000000000000..4c27edb05a64 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedFloatFieldAccessorImpl.java @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedFloatFieldAccessorImpl + extends UnsafeQualifiedFieldAccessorImpl { + UnsafeQualifiedFloatFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Float.valueOf(getFloat(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + throw newGetLongIllegalArgumentException(); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getFloatVolatile(obj, fieldOffset); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getFloat(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putFloatVolatile(obj, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putFloatVolatile(obj, fieldOffset, ((Short) value).shortValue()); + return; + } + if (value instanceof Character) { + unsafe.putFloatVolatile(obj, fieldOffset, ((Character) value).charValue()); + return; + } + if (value instanceof Integer) { + unsafe.putFloatVolatile(obj, fieldOffset, ((Integer) value).intValue()); + return; + } + if (value instanceof Long) { + unsafe.putFloatVolatile(obj, fieldOffset, ((Long) value).longValue()); + return; + } + if (value instanceof Float) { + unsafe.putFloatVolatile(obj, fieldOffset, ((Float) value).floatValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(f); + } + unsafe.putFloatVolatile(obj, fieldOffset, f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedIntegerFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedIntegerFieldAccessorImpl.java new file mode 100644 index 000000000000..a666652ea8a8 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedIntegerFieldAccessorImpl.java @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedIntegerFieldAccessorImpl + extends UnsafeQualifiedFieldAccessorImpl { + UnsafeQualifiedIntegerFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Integer.valueOf(getInt(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getIntVolatile(obj, fieldOffset); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return getInt(obj); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getInt(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getInt(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putIntVolatile(obj, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putIntVolatile(obj, fieldOffset, ((Short) value).shortValue()); + return; + } + if (value instanceof Character) { + unsafe.putIntVolatile(obj, fieldOffset, ((Character) value).charValue()); + return; + } + if (value instanceof Integer) { + unsafe.putIntVolatile(obj, fieldOffset, ((Integer) value).intValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setInt(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + setInt(obj, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + setInt(obj, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(i); + } + unsafe.putIntVolatile(obj, fieldOffset, i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedLongFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedLongFieldAccessorImpl.java new file mode 100644 index 000000000000..efcad055613c --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedLongFieldAccessorImpl.java @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedLongFieldAccessorImpl + extends UnsafeQualifiedFieldAccessorImpl { + UnsafeQualifiedLongFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Long.valueOf(getLong(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getLongVolatile(obj, fieldOffset); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getLong(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getLong(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putLongVolatile(obj, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putLongVolatile(obj, fieldOffset, ((Short) value).shortValue()); + return; + } + if (value instanceof Character) { + unsafe.putLongVolatile(obj, fieldOffset, ((Character) value).charValue()); + return; + } + if (value instanceof Integer) { + unsafe.putLongVolatile(obj, fieldOffset, ((Integer) value).intValue()); + return; + } + if (value instanceof Long) { + unsafe.putLongVolatile(obj, fieldOffset, ((Long) value).longValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setLong(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + setLong(obj, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + setLong(obj, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + setLong(obj, i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(l); + } + unsafe.putLongVolatile(obj, fieldOffset, l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedObjectFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedObjectFieldAccessorImpl.java new file mode 100644 index 000000000000..c42d131e49e5 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedObjectFieldAccessorImpl.java @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2004, 2022, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedObjectFieldAccessorImpl + extends UnsafeQualifiedFieldAccessorImpl { + UnsafeQualifiedObjectFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getReferenceVolatile(obj, fieldOffset); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + throw newGetLongIllegalArgumentException(); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + throw newGetFloatIllegalArgumentException(); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + throw newGetDoubleIllegalArgumentException(); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value != null) { + if (!field.getType().isInstance(value)) { + throwSetIllegalArgumentException(value); + } + } + unsafe.putReferenceVolatile(obj, fieldOffset, value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedShortFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedShortFieldAccessorImpl.java new file mode 100644 index 000000000000..1195c19ca52f --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedShortFieldAccessorImpl.java @@ -0,0 +1,154 @@ +/* + * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedShortFieldAccessorImpl + extends UnsafeQualifiedFieldAccessorImpl { + UnsafeQualifiedShortFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Short.valueOf(getShort(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getShortVolatile(obj, fieldOffset); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + return getShort(obj); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return getShort(obj); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getShort(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getShort(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putShortVolatile(obj, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putShortVolatile(obj, fieldOffset, ((Short) value).shortValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setShort(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isReadOnly) { + throwFinalFieldIllegalAccessException(s); + } + unsafe.putShortVolatile(obj, fieldOffset, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticBooleanFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticBooleanFieldAccessorImpl.java new file mode 100644 index 000000000000..d011e21505b8 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticBooleanFieldAccessorImpl.java @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedStaticBooleanFieldAccessorImpl + extends UnsafeQualifiedStaticFieldAccessorImpl { + UnsafeQualifiedStaticBooleanFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Boolean.valueOf(getBoolean(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + return unsafe.getBooleanVolatile(base, fieldOffset); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + throw newGetLongIllegalArgumentException(); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + throw newGetFloatIllegalArgumentException(); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + throw newGetDoubleIllegalArgumentException(); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Boolean) { + unsafe.putBooleanVolatile(base, fieldOffset, ((Boolean) value).booleanValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(z); + } + unsafe.putBooleanVolatile(base, fieldOffset, z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticByteFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticByteFieldAccessorImpl.java new file mode 100644 index 000000000000..1436a3e85bd5 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticByteFieldAccessorImpl.java @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedStaticByteFieldAccessorImpl + extends UnsafeQualifiedStaticFieldAccessorImpl { + UnsafeQualifiedStaticByteFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Byte.valueOf(getByte(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + return unsafe.getByteVolatile(base, fieldOffset); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putByteVolatile(base, fieldOffset, ((Byte) value).byteValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(b); + } + unsafe.putByteVolatile(base, fieldOffset, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticCharacterFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticCharacterFieldAccessorImpl.java new file mode 100644 index 000000000000..39c0c6be2863 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticCharacterFieldAccessorImpl.java @@ -0,0 +1,147 @@ +/* + * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedStaticCharacterFieldAccessorImpl + extends UnsafeQualifiedStaticFieldAccessorImpl { + UnsafeQualifiedStaticCharacterFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Character.valueOf(getChar(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + return unsafe.getCharVolatile(base, fieldOffset); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + return getChar(obj); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return getChar(obj); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getChar(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getChar(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Character) { + unsafe.putCharVolatile(base, fieldOffset, ((Character) value).charValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(c); + } + unsafe.putCharVolatile(base, fieldOffset, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticDoubleFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticDoubleFieldAccessorImpl.java new file mode 100644 index 000000000000..f56ae102a343 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticDoubleFieldAccessorImpl.java @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedStaticDoubleFieldAccessorImpl + extends UnsafeQualifiedStaticFieldAccessorImpl { + UnsafeQualifiedStaticDoubleFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Double.valueOf(getDouble(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + throw newGetLongIllegalArgumentException(); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + throw newGetFloatIllegalArgumentException(); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return unsafe.getDoubleVolatile(base, fieldOffset); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putDoubleVolatile(base, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putDoubleVolatile(base, fieldOffset, ((Short) value).shortValue()); + return; + } + if (value instanceof Character) { + unsafe.putDoubleVolatile(base, fieldOffset, ((Character) value).charValue()); + return; + } + if (value instanceof Integer) { + unsafe.putDoubleVolatile(base, fieldOffset, ((Integer) value).intValue()); + return; + } + if (value instanceof Long) { + unsafe.putDoubleVolatile(base, fieldOffset, ((Long) value).longValue()); + return; + } + if (value instanceof Float) { + unsafe.putDoubleVolatile(base, fieldOffset, ((Float) value).floatValue()); + return; + } + if (value instanceof Double) { + unsafe.putDoubleVolatile(base, fieldOffset, ((Double) value).doubleValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(d); + } + unsafe.putDoubleVolatile(base, fieldOffset, d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticFieldAccessorImpl.java new file mode 100644 index 000000000000..a772fb49da2c --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticFieldAccessorImpl.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2004, 2023, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +/** + * Base class for jdk.internal.misc.Unsafe-based FieldAccessors for final or static volatile fields. + */ + +abstract class UnsafeQualifiedStaticFieldAccessorImpl + extends UnsafeStaticFieldAccessorImpl { + protected final boolean isReadOnly; + + UnsafeQualifiedStaticFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field); + this.isReadOnly = isReadOnly; + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticFloatFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticFloatFieldAccessorImpl.java new file mode 100644 index 000000000000..79ab415e368d --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticFloatFieldAccessorImpl.java @@ -0,0 +1,167 @@ +/* + * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedStaticFloatFieldAccessorImpl + extends UnsafeQualifiedStaticFieldAccessorImpl { + UnsafeQualifiedStaticFloatFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Float.valueOf(getFloat(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + throw newGetLongIllegalArgumentException(); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return unsafe.getFloatVolatile(base, fieldOffset); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getFloat(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putFloatVolatile(base, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putFloatVolatile(base, fieldOffset, ((Short) value).shortValue()); + return; + } + if (value instanceof Character) { + unsafe.putFloatVolatile(base, fieldOffset, ((Character) value).charValue()); + return; + } + if (value instanceof Integer) { + unsafe.putFloatVolatile(base, fieldOffset, ((Integer) value).intValue()); + return; + } + if (value instanceof Long) { + unsafe.putFloatVolatile(base, fieldOffset, ((Long) value).longValue()); + return; + } + if (value instanceof Float) { + unsafe.putFloatVolatile(base, fieldOffset, ((Float) value).floatValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(f); + } + unsafe.putFloatVolatile(base, fieldOffset, f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticIntegerFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticIntegerFieldAccessorImpl.java new file mode 100644 index 000000000000..c34ee359e7c6 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticIntegerFieldAccessorImpl.java @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedStaticIntegerFieldAccessorImpl + extends UnsafeQualifiedStaticFieldAccessorImpl { + UnsafeQualifiedStaticIntegerFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Integer.valueOf(getInt(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + return unsafe.getIntVolatile(base, fieldOffset); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return getInt(obj); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getInt(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getInt(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putIntVolatile(base, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putIntVolatile(base, fieldOffset, ((Short) value).shortValue()); + return; + } + if (value instanceof Character) { + unsafe.putIntVolatile(base, fieldOffset, ((Character) value).charValue()); + return; + } + if (value instanceof Integer) { + unsafe.putIntVolatile(base, fieldOffset, ((Integer) value).intValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setInt(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + setInt(obj, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + setInt(obj, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(i); + } + unsafe.putIntVolatile(base, fieldOffset, i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticLongFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticLongFieldAccessorImpl.java new file mode 100644 index 000000000000..3a98475b42ac --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticLongFieldAccessorImpl.java @@ -0,0 +1,163 @@ +/* + * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedStaticLongFieldAccessorImpl + extends UnsafeQualifiedStaticFieldAccessorImpl { + UnsafeQualifiedStaticLongFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Long.valueOf(getLong(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return unsafe.getLongVolatile(base, fieldOffset); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getLong(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getLong(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putLongVolatile(base, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putLongVolatile(base, fieldOffset, ((Short) value).shortValue()); + return; + } + if (value instanceof Character) { + unsafe.putLongVolatile(base, fieldOffset, ((Character) value).charValue()); + return; + } + if (value instanceof Integer) { + unsafe.putLongVolatile(base, fieldOffset, ((Integer) value).intValue()); + return; + } + if (value instanceof Long) { + unsafe.putLongVolatile(base, fieldOffset, ((Long) value).longValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setLong(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + setLong(obj, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + setLong(obj, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + setLong(obj, i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(l); + } + unsafe.putLongVolatile(base, fieldOffset, l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticObjectFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticObjectFieldAccessorImpl.java new file mode 100644 index 000000000000..b5f962c0d47c --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticObjectFieldAccessorImpl.java @@ -0,0 +1,142 @@ +/* + * Copyright (c) 2004, 2022, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedStaticObjectFieldAccessorImpl + extends UnsafeQualifiedStaticFieldAccessorImpl { + UnsafeQualifiedStaticObjectFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return unsafe.getReferenceVolatile(base, fieldOffset); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + throw newGetLongIllegalArgumentException(); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + throw newGetFloatIllegalArgumentException(); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + throw newGetDoubleIllegalArgumentException(); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value != null) { + if (!field.getType().isInstance(value)) { + throwSetIllegalArgumentException(value); + } + } + unsafe.putReferenceVolatile(base, fieldOffset, value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticShortFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticShortFieldAccessorImpl.java new file mode 100644 index 000000000000..3e82010d6c31 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeQualifiedStaticShortFieldAccessorImpl.java @@ -0,0 +1,151 @@ +/* + * Copyright (c) 2004, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeQualifiedStaticShortFieldAccessorImpl + extends UnsafeQualifiedStaticFieldAccessorImpl { + UnsafeQualifiedStaticShortFieldAccessorImpl(Field field, boolean isReadOnly) { + super(field, isReadOnly); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Short.valueOf(getShort(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + return unsafe.getShortVolatile(base, fieldOffset); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + return getShort(obj); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return getShort(obj); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getShort(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getShort(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putShortVolatile(base, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putShortVolatile(base, fieldOffset, ((Short) value).shortValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setShort(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + if (isReadOnly) { + throwFinalFieldIllegalAccessException(s); + } + unsafe.putShortVolatile(base, fieldOffset, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeShortFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeShortFieldAccessorImpl.java new file mode 100644 index 000000000000..c3cccac17f78 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeShortFieldAccessorImpl.java @@ -0,0 +1,153 @@ +/* + * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeShortFieldAccessorImpl extends UnsafeFieldAccessorImpl { + UnsafeShortFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Short.valueOf(getShort(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + ensureObj(obj); + return unsafe.getShort(obj, fieldOffset); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + return getShort(obj); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return getShort(obj); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getShort(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getShort(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putShort(obj, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putShort(obj, fieldOffset, ((Short) value).shortValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setShort(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + ensureObj(obj); + if (isFinal) { + throwFinalFieldIllegalAccessException(s); + } + unsafe.putShort(obj, fieldOffset, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticBooleanFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticBooleanFieldAccessorImpl.java new file mode 100644 index 000000000000..8e121943bca9 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticBooleanFieldAccessorImpl.java @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeStaticBooleanFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl { + UnsafeStaticBooleanFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Boolean.valueOf(getBoolean(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + return unsafe.getBoolean(base, fieldOffset); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + throw newGetLongIllegalArgumentException(); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + throw newGetFloatIllegalArgumentException(); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + throw newGetDoubleIllegalArgumentException(); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Boolean) { + unsafe.putBoolean(base, fieldOffset, ((Boolean) value).booleanValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(z); + } + unsafe.putBoolean(base, fieldOffset, z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticByteFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticByteFieldAccessorImpl.java new file mode 100644 index 000000000000..42f817c9909b --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticByteFieldAccessorImpl.java @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeStaticByteFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl { + UnsafeStaticByteFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Byte.valueOf(getByte(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + return unsafe.getByte(base, fieldOffset); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getByte(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putByte(base, fieldOffset, ((Byte) value).byteValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(b); + } + unsafe.putByte(base, fieldOffset, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticCharacterFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticCharacterFieldAccessorImpl.java new file mode 100644 index 000000000000..4b570bd06ae8 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticCharacterFieldAccessorImpl.java @@ -0,0 +1,146 @@ +/* + * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeStaticCharacterFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl { + UnsafeStaticCharacterFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Character.valueOf(getChar(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + return unsafe.getChar(base, fieldOffset); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + return getChar(obj); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return getChar(obj); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getChar(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getChar(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Character) { + unsafe.putChar(base, fieldOffset, ((Character) value).charValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(c); + } + unsafe.putChar(base, fieldOffset, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticDoubleFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticDoubleFieldAccessorImpl.java new file mode 100644 index 000000000000..fa5eb8b807e1 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticDoubleFieldAccessorImpl.java @@ -0,0 +1,170 @@ +/* + * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeStaticDoubleFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl { + UnsafeStaticDoubleFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Double.valueOf(getDouble(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + throw newGetLongIllegalArgumentException(); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + throw newGetFloatIllegalArgumentException(); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return unsafe.getDouble(base, fieldOffset); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putDouble(base, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putDouble(base, fieldOffset, ((Short) value).shortValue()); + return; + } + if (value instanceof Character) { + unsafe.putDouble(base, fieldOffset, ((Character) value).charValue()); + return; + } + if (value instanceof Integer) { + unsafe.putDouble(base, fieldOffset, ((Integer) value).intValue()); + return; + } + if (value instanceof Long) { + unsafe.putDouble(base, fieldOffset, ((Long) value).longValue()); + return; + } + if (value instanceof Float) { + unsafe.putDouble(base, fieldOffset, ((Float) value).floatValue()); + return; + } + if (value instanceof Double) { + unsafe.putDouble(base, fieldOffset, ((Double) value).doubleValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + setDouble(obj, f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(d); + } + unsafe.putDouble(base, fieldOffset, d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticFieldAccessorImpl.java new file mode 100644 index 000000000000..dd2c6289805e --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticFieldAccessorImpl.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2001, 2021, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +/** + * Base class for jdk.internal.misc.Unsafe-based FieldAccessors for static fields. The observation + * is that there are only nine types of fields from the standpoint of reflection code: the eight + * primitive types and Object. Using class Unsafe instead of generated bytecodes saves memory and + * loading time for the dynamically-generated FieldAccessors. + */ + +abstract class UnsafeStaticFieldAccessorImpl extends UnsafeFieldAccessorImpl { + + protected final Object base; // base + + UnsafeStaticFieldAccessorImpl(Field field) { + super(field); + base = unsafe.staticFieldBase(field); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticFloatFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticFloatFieldAccessorImpl.java new file mode 100644 index 000000000000..a28502e967f7 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticFloatFieldAccessorImpl.java @@ -0,0 +1,166 @@ +/* + * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeStaticFloatFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl { + UnsafeStaticFloatFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Float.valueOf(getFloat(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + throw newGetLongIllegalArgumentException(); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return unsafe.getFloat(base, fieldOffset); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getFloat(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putFloat(base, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putFloat(base, fieldOffset, ((Short) value).shortValue()); + return; + } + if (value instanceof Character) { + unsafe.putFloat(base, fieldOffset, ((Character) value).charValue()); + return; + } + if (value instanceof Integer) { + unsafe.putFloat(base, fieldOffset, ((Integer) value).intValue()); + return; + } + if (value instanceof Long) { + unsafe.putFloat(base, fieldOffset, ((Long) value).longValue()); + return; + } + if (value instanceof Float) { + unsafe.putFloat(base, fieldOffset, ((Float) value).floatValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + setFloat(obj, l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(f); + } + unsafe.putFloat(base, fieldOffset, f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticIntegerFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticIntegerFieldAccessorImpl.java new file mode 100644 index 000000000000..8da78c2f1d83 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticIntegerFieldAccessorImpl.java @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeStaticIntegerFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl { + UnsafeStaticIntegerFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Integer.valueOf(getInt(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + return unsafe.getInt(base, fieldOffset); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return getInt(obj); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getInt(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getInt(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putInt(base, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putInt(base, fieldOffset, ((Short) value).shortValue()); + return; + } + if (value instanceof Character) { + unsafe.putInt(base, fieldOffset, ((Character) value).charValue()); + return; + } + if (value instanceof Integer) { + unsafe.putInt(base, fieldOffset, ((Integer) value).intValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setInt(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + setInt(obj, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + setInt(obj, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(i); + } + unsafe.putInt(base, fieldOffset, i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticLongFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticLongFieldAccessorImpl.java new file mode 100644 index 000000000000..fb7312e1a00e --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticLongFieldAccessorImpl.java @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeStaticLongFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl { + UnsafeStaticLongFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Long.valueOf(getLong(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return unsafe.getLong(base, fieldOffset); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getLong(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getLong(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putLong(base, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putLong(base, fieldOffset, ((Short) value).shortValue()); + return; + } + if (value instanceof Character) { + unsafe.putLong(base, fieldOffset, ((Character) value).charValue()); + return; + } + if (value instanceof Integer) { + unsafe.putLong(base, fieldOffset, ((Integer) value).intValue()); + return; + } + if (value instanceof Long) { + unsafe.putLong(base, fieldOffset, ((Long) value).longValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setLong(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + setLong(obj, c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + setLong(obj, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + setLong(obj, i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(l); + } + unsafe.putLong(base, fieldOffset, l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticObjectFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticObjectFieldAccessorImpl.java new file mode 100644 index 000000000000..fe8caee94e57 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticObjectFieldAccessorImpl.java @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2001, 2022, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeStaticObjectFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl { + UnsafeStaticObjectFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return unsafe.getReference(base, fieldOffset); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + throw newGetShortIllegalArgumentException(); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + throw newGetIntIllegalArgumentException(); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + throw newGetLongIllegalArgumentException(); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + throw newGetFloatIllegalArgumentException(); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + throw newGetDoubleIllegalArgumentException(); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value != null) { + if (!field.getType().isInstance(value)) { + throwSetIllegalArgumentException(value); + } + } + unsafe.putReference(base, fieldOffset, value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticShortFieldAccessorImpl.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticShortFieldAccessorImpl.java new file mode 100644 index 000000000000..3e3590742d35 --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/UnsafeStaticShortFieldAccessorImpl.java @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package com.oracle.svm.core.reflect.fieldaccessor; + +import java.lang.reflect.Field; + +class UnsafeStaticShortFieldAccessorImpl extends UnsafeStaticFieldAccessorImpl { + UnsafeStaticShortFieldAccessorImpl(Field field) { + super(field); + } + + @Override + public Object get(Object obj) throws IllegalArgumentException { + return Short.valueOf(getShort(obj)); + } + + @Override + public boolean getBoolean(Object obj) throws IllegalArgumentException { + throw newGetBooleanIllegalArgumentException(); + } + + @Override + public byte getByte(Object obj) throws IllegalArgumentException { + throw newGetByteIllegalArgumentException(); + } + + @Override + public char getChar(Object obj) throws IllegalArgumentException { + throw newGetCharIllegalArgumentException(); + } + + @Override + public short getShort(Object obj) throws IllegalArgumentException { + return unsafe.getShort(base, fieldOffset); + } + + @Override + public int getInt(Object obj) throws IllegalArgumentException { + return getShort(obj); + } + + @Override + public long getLong(Object obj) throws IllegalArgumentException { + return getShort(obj); + } + + @Override + public float getFloat(Object obj) throws IllegalArgumentException { + return getShort(obj); + } + + @Override + public double getDouble(Object obj) throws IllegalArgumentException { + return getShort(obj); + } + + @Override + public void set(Object obj, Object value) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(value); + } + if (value == null) { + throwSetIllegalArgumentException(value); + } + if (value instanceof Byte) { + unsafe.putShort(base, fieldOffset, ((Byte) value).byteValue()); + return; + } + if (value instanceof Short) { + unsafe.putShort(base, fieldOffset, ((Short) value).shortValue()); + return; + } + throwSetIllegalArgumentException(value); + } + + @Override + public void setBoolean(Object obj, boolean z) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(z); + } + + @Override + public void setByte(Object obj, byte b) + throws IllegalArgumentException, IllegalAccessException { + setShort(obj, b); + } + + @Override + public void setChar(Object obj, char c) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(c); + } + + @Override + public void setShort(Object obj, short s) + throws IllegalArgumentException, IllegalAccessException { + if (isFinal) { + throwFinalFieldIllegalAccessException(s); + } + unsafe.putShort(base, fieldOffset, s); + } + + @Override + public void setInt(Object obj, int i) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(i); + } + + @Override + public void setLong(Object obj, long l) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(l); + } + + @Override + public void setFloat(Object obj, float f) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(f); + } + + @Override + public void setDouble(Object obj, double d) + throws IllegalArgumentException, IllegalAccessException { + throwSetIllegalArgumentException(d); + } +} diff --git a/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/package-info.java b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/package-info.java new file mode 100644 index 000000000000..b62829ac261e --- /dev/null +++ b/substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/reflect/fieldaccessor/package-info.java @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2023, 2023, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +/** + * This package includes the implementation of the + * {@link com.oracle.svm.core.reflect.fieldaccessor.UnsafeFieldAccessorImpl}. + * + * Background: JDK 19 ships with JEP 416, which + * reimplements core reflection with method + * handles. However, the old Unsafe-based implementation was still available and used by Native + * Image. In JDK 22, the old core reflection + * implementation was removed. Since the method handle based implementation is designed with + * Just-in-Time compilation in mind, and would have bad performance in an Ahead-of-Time scenario, we + * decided to keep old implementation. + * + * This package contains the Unsafe-based core reflection implementation as available until JDK + * 22 build +1. + */ +package com.oracle.svm.core.reflect.fieldaccessor;