Path: blob/master/src/java.base/share/classes/jdk/internal/reflect/UnsafeFieldAccessorImpl.java
41159 views
/*1* Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package jdk.internal.reflect;2627import java.lang.reflect.Field;28import java.lang.reflect.Modifier;29import jdk.internal.misc.Unsafe;3031/** Base class for jdk.internal.misc.Unsafe-based FieldAccessors. The32observation is that there are only nine types of fields from the33standpoint of reflection code: the eight primitive types and34Object. Using class Unsafe instead of generated bytecodes saves35memory and loading time for the dynamically-generated36FieldAccessors. */3738abstract class UnsafeFieldAccessorImpl extends FieldAccessorImpl {39static final Unsafe unsafe = Unsafe.getUnsafe();4041protected final Field field;42protected final long fieldOffset;43protected final boolean isFinal;4445UnsafeFieldAccessorImpl(Field field) {46this.field = field;47if (Modifier.isStatic(field.getModifiers()))48fieldOffset = unsafe.staticFieldOffset(field);49else50fieldOffset = unsafe.objectFieldOffset(field);51isFinal = Modifier.isFinal(field.getModifiers());52}5354protected void ensureObj(Object o) {55// NOTE: will throw NullPointerException, as specified, if o is null56if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) {57throwSetIllegalArgumentException(o);58}59}6061private String getQualifiedFieldName() {62return field.getDeclaringClass().getName() + "." +field.getName();63}6465protected IllegalArgumentException newGetIllegalArgumentException(String type) {66return new IllegalArgumentException(67"Attempt to get "+field.getType().getName()+" field \"" +68getQualifiedFieldName() + "\" with illegal data type conversion to "+type69);70}7172protected void throwFinalFieldIllegalAccessException(String attemptedType,73String attemptedValue)74throws IllegalAccessException {75throw new IllegalAccessException(getSetMessage(attemptedType, attemptedValue));7677}78protected void throwFinalFieldIllegalAccessException(Object o) throws IllegalAccessException {79throwFinalFieldIllegalAccessException(o != null ? o.getClass().getName() : "", "");80}8182protected void throwFinalFieldIllegalAccessException(boolean z) throws IllegalAccessException {83throwFinalFieldIllegalAccessException("boolean", Boolean.toString(z));84}8586protected void throwFinalFieldIllegalAccessException(char b) throws IllegalAccessException {87throwFinalFieldIllegalAccessException("char", Character.toString(b));88}8990protected void throwFinalFieldIllegalAccessException(byte b) throws IllegalAccessException {91throwFinalFieldIllegalAccessException("byte", Byte.toString(b));92}9394protected void throwFinalFieldIllegalAccessException(short b) throws IllegalAccessException {95throwFinalFieldIllegalAccessException("short", Short.toString(b));96}9798protected void throwFinalFieldIllegalAccessException(int i) throws IllegalAccessException {99throwFinalFieldIllegalAccessException("int", Integer.toString(i));100}101102protected void throwFinalFieldIllegalAccessException(long i) throws IllegalAccessException {103throwFinalFieldIllegalAccessException("long", Long.toString(i));104}105106protected void throwFinalFieldIllegalAccessException(float f) throws IllegalAccessException {107throwFinalFieldIllegalAccessException("float", Float.toString(f));108}109110protected void throwFinalFieldIllegalAccessException(double f) throws IllegalAccessException {111throwFinalFieldIllegalAccessException("double", Double.toString(f));112}113114protected IllegalArgumentException newGetBooleanIllegalArgumentException() {115return newGetIllegalArgumentException("boolean");116}117118protected IllegalArgumentException newGetByteIllegalArgumentException() {119return newGetIllegalArgumentException("byte");120}121122protected IllegalArgumentException newGetCharIllegalArgumentException() {123return newGetIllegalArgumentException("char");124}125126protected IllegalArgumentException newGetShortIllegalArgumentException() {127return newGetIllegalArgumentException("short");128}129130protected IllegalArgumentException newGetIntIllegalArgumentException() {131return newGetIllegalArgumentException("int");132}133134protected IllegalArgumentException newGetLongIllegalArgumentException() {135return newGetIllegalArgumentException("long");136}137138protected IllegalArgumentException newGetFloatIllegalArgumentException() {139return newGetIllegalArgumentException("float");140}141142protected IllegalArgumentException newGetDoubleIllegalArgumentException() {143return newGetIllegalArgumentException("double");144}145146protected String getSetMessage(String attemptedType, String attemptedValue) {147String err = "Can not set";148if (Modifier.isStatic(field.getModifiers()))149err += " static";150if (isFinal)151err += " final";152err += " " + field.getType().getName() + " field " + getQualifiedFieldName() + " to ";153if (!attemptedValue.isEmpty()) {154err += "(" + attemptedType + ")" + attemptedValue;155} else {156if (!attemptedType.isEmpty())157err += attemptedType;158else159err += "null value";160}161return err;162}163164protected void throwSetIllegalArgumentException(String attemptedType,165String attemptedValue) {166throw new IllegalArgumentException(getSetMessage(attemptedType,attemptedValue));167}168169protected void throwSetIllegalArgumentException(Object o) {170throwSetIllegalArgumentException(o != null ? o.getClass().getName() : "", "");171}172173protected void throwSetIllegalArgumentException(boolean b) {174throwSetIllegalArgumentException("boolean", Boolean.toString(b));175}176177protected void throwSetIllegalArgumentException(byte b) {178throwSetIllegalArgumentException("byte", Byte.toString(b));179}180181protected void throwSetIllegalArgumentException(char c) {182throwSetIllegalArgumentException("char", Character.toString(c));183}184185protected void throwSetIllegalArgumentException(short s) {186throwSetIllegalArgumentException("short", Short.toString(s));187}188189protected void throwSetIllegalArgumentException(int i) {190throwSetIllegalArgumentException("int", Integer.toString(i));191}192193protected void throwSetIllegalArgumentException(long l) {194throwSetIllegalArgumentException("long", Long.toString(l));195}196197protected void throwSetIllegalArgumentException(float f) {198throwSetIllegalArgumentException("float", Float.toString(f));199}200201protected void throwSetIllegalArgumentException(double d) {202throwSetIllegalArgumentException("double", Double.toString(d));203}204205}206207208