0N/A/*
2362N/A * Copyright (c) 2001, 2005, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage sun.reflect;
0N/A
0N/Aimport java.lang.reflect.Field;
0N/Aimport java.lang.reflect.Modifier;
0N/Aimport sun.misc.Unsafe;
0N/A
0N/A/** Base class for sun.misc.Unsafe-based FieldAccessors. The
0N/A observation is that there are only nine types of fields from the
0N/A standpoint of reflection code: the eight primitive types and
0N/A Object. Using class Unsafe instead of generated bytecodes saves
0N/A memory and loading time for the dynamically-generated
0N/A FieldAccessors. */
0N/A
0N/Aabstract class UnsafeFieldAccessorImpl extends FieldAccessorImpl {
0N/A static final Unsafe unsafe = Unsafe.getUnsafe();
0N/A
0N/A protected final Field field;
0N/A protected final int fieldOffset;
0N/A protected final boolean isFinal;
0N/A
0N/A UnsafeFieldAccessorImpl(Field field) {
0N/A this.field = field;
0N/A fieldOffset = unsafe.fieldOffset(field);
0N/A isFinal = Modifier.isFinal(field.getModifiers());
0N/A }
0N/A
0N/A protected void ensureObj(Object o) {
0N/A // NOTE: will throw NullPointerException, as specified, if o is null
0N/A if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) {
0N/A throwSetIllegalArgumentException(o);
0N/A }
0N/A }
0N/A
0N/A private String getQualifiedFieldName() {
0N/A return field.getDeclaringClass().getName() + "." +field.getName();
0N/A }
0N/A
0N/A protected IllegalArgumentException newGetIllegalArgumentException(String type) {
0N/A return new IllegalArgumentException(
0N/A "Attempt to get "+field.getType().getName()+" field \"" +
0N/A getQualifiedFieldName() + "\" with illegal data type conversion to "+type
0N/A );
0N/A }
0N/A
0N/A protected void throwFinalFieldIllegalAccessException(String attemptedType,
0N/A String attemptedValue)
0N/A throws IllegalAccessException {
0N/A throw new IllegalAccessException(getSetMessage(attemptedType, attemptedValue));
0N/A
0N/A }
0N/A protected void throwFinalFieldIllegalAccessException(Object o) throws IllegalAccessException {
0N/A throwFinalFieldIllegalAccessException(o != null ? o.getClass().getName() : "", "");
0N/A }
0N/A
0N/A protected void throwFinalFieldIllegalAccessException(boolean z) throws IllegalAccessException {
0N/A throwFinalFieldIllegalAccessException("boolean", Boolean.toString(z));
0N/A }
0N/A
0N/A protected void throwFinalFieldIllegalAccessException(char b) throws IllegalAccessException {
0N/A throwFinalFieldIllegalAccessException("char", Character.toString(b));
0N/A }
0N/A
0N/A protected void throwFinalFieldIllegalAccessException(byte b) throws IllegalAccessException {
0N/A throwFinalFieldIllegalAccessException("byte", Byte.toString(b));
0N/A }
0N/A
0N/A protected void throwFinalFieldIllegalAccessException(short b) throws IllegalAccessException {
0N/A throwFinalFieldIllegalAccessException("short", Short.toString(b));
0N/A }
0N/A
0N/A protected void throwFinalFieldIllegalAccessException(int i) throws IllegalAccessException {
0N/A throwFinalFieldIllegalAccessException("int", Integer.toString(i));
0N/A }
0N/A
0N/A protected void throwFinalFieldIllegalAccessException(long i) throws IllegalAccessException {
0N/A throwFinalFieldIllegalAccessException("long", Long.toString(i));
0N/A }
0N/A
0N/A protected void throwFinalFieldIllegalAccessException(float f) throws IllegalAccessException {
0N/A throwFinalFieldIllegalAccessException("float", Float.toString(f));
0N/A }
0N/A
0N/A protected void throwFinalFieldIllegalAccessException(double f) throws IllegalAccessException {
0N/A throwFinalFieldIllegalAccessException("double", Double.toString(f));
0N/A }
0N/A
0N/A protected IllegalArgumentException newGetBooleanIllegalArgumentException() {
0N/A return newGetIllegalArgumentException("boolean");
0N/A }
0N/A
0N/A protected IllegalArgumentException newGetByteIllegalArgumentException() {
0N/A return newGetIllegalArgumentException("byte");
0N/A }
0N/A
0N/A protected IllegalArgumentException newGetCharIllegalArgumentException() {
0N/A return newGetIllegalArgumentException("char");
0N/A }
0N/A
0N/A protected IllegalArgumentException newGetShortIllegalArgumentException() {
0N/A return newGetIllegalArgumentException("short");
0N/A }
0N/A
0N/A protected IllegalArgumentException newGetIntIllegalArgumentException() {
0N/A return newGetIllegalArgumentException("int");
0N/A }
0N/A
0N/A protected IllegalArgumentException newGetLongIllegalArgumentException() {
0N/A return newGetIllegalArgumentException("long");
0N/A }
0N/A
0N/A protected IllegalArgumentException newGetFloatIllegalArgumentException() {
0N/A return newGetIllegalArgumentException("float");
0N/A }
0N/A
0N/A protected IllegalArgumentException newGetDoubleIllegalArgumentException() {
0N/A return newGetIllegalArgumentException("double");
0N/A }
0N/A
0N/A protected String getSetMessage(String attemptedType, String attemptedValue) {
0N/A String err = "Can not set";
0N/A if (Modifier.isStatic(field.getModifiers()))
0N/A err += " static";
0N/A if (isFinal)
0N/A err += " final";
0N/A err += " " + field.getType().getName() + " field " + getQualifiedFieldName() + " to ";
0N/A if (attemptedValue.length() > 0) {
0N/A err += "(" + attemptedType + ")" + attemptedValue;
0N/A } else {
0N/A if (attemptedType.length() > 0)
0N/A err += attemptedType;
0N/A else
0N/A err += "null value";
0N/A }
0N/A return err;
0N/A }
0N/A
0N/A protected void throwSetIllegalArgumentException(String attemptedType,
0N/A String attemptedValue) {
0N/A throw new IllegalArgumentException(getSetMessage(attemptedType,attemptedValue));
0N/A }
0N/A
0N/A protected void throwSetIllegalArgumentException(Object o) {
0N/A throwSetIllegalArgumentException(o != null ? o.getClass().getName() : "", "");
0N/A }
0N/A
0N/A protected void throwSetIllegalArgumentException(boolean b) {
0N/A throwSetIllegalArgumentException("boolean", Boolean.toString(b));
0N/A }
0N/A
0N/A protected void throwSetIllegalArgumentException(byte b) {
0N/A throwSetIllegalArgumentException("byte", Byte.toString(b));
0N/A }
0N/A
0N/A protected void throwSetIllegalArgumentException(char c) {
0N/A throwSetIllegalArgumentException("char", Character.toString(c));
0N/A }
0N/A
0N/A protected void throwSetIllegalArgumentException(short s) {
0N/A throwSetIllegalArgumentException("short", Short.toString(s));
0N/A }
0N/A
0N/A protected void throwSetIllegalArgumentException(int i) {
0N/A throwSetIllegalArgumentException("int", Integer.toString(i));
0N/A }
0N/A
0N/A protected void throwSetIllegalArgumentException(long l) {
0N/A throwSetIllegalArgumentException("long", Long.toString(l));
0N/A }
0N/A
0N/A protected void throwSetIllegalArgumentException(float f) {
0N/A throwSetIllegalArgumentException("float", Float.toString(f));
0N/A }
0N/A
0N/A protected void throwSetIllegalArgumentException(double d) {
0N/A throwSetIllegalArgumentException("double", Double.toString(d));
0N/A }
0N/A
0N/A}