jdk/src/share/classes/sun/reflect/UnsafeFieldAccessorImpl.java
author duke
Sat, 01 Dec 2007 00:00:00 +0000
changeset 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
Initial load
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 2001-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package sun.reflect;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.Field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.lang.reflect.Modifier;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import sun.misc.Unsafe;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/** Base class for sun.misc.Unsafe-based FieldAccessors. The
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    observation is that there are only nine types of fields from the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    standpoint of reflection code: the eight primitive types and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    Object. Using class Unsafe instead of generated bytecodes saves
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    memory and loading time for the dynamically-generated
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    FieldAccessors. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
abstract class UnsafeFieldAccessorImpl extends FieldAccessorImpl {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    static final Unsafe unsafe = Unsafe.getUnsafe();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    protected final Field   field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    protected final int     fieldOffset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    protected final boolean isFinal;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    UnsafeFieldAccessorImpl(Field field) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        this.field = field;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        fieldOffset = unsafe.fieldOffset(field);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        isFinal = Modifier.isFinal(field.getModifiers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    protected void ensureObj(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        // NOTE: will throw NullPointerException, as specified, if o is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            throwSetIllegalArgumentException(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private String getQualifiedFieldName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
      return field.getDeclaringClass().getName() + "." +field.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    protected IllegalArgumentException newGetIllegalArgumentException(String type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        return new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
          "Attempt to get "+field.getType().getName()+" field \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
          getQualifiedFieldName() + "\" with illegal data type conversion to "+type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
    protected void throwFinalFieldIllegalAccessException(String attemptedType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                                                         String attemptedValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                                                         throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        throw new IllegalAccessException(getSetMessage(attemptedType, attemptedValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    protected void throwFinalFieldIllegalAccessException(Object o) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        throwFinalFieldIllegalAccessException(o != null ? o.getClass().getName() : "", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    protected void throwFinalFieldIllegalAccessException(boolean z) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        throwFinalFieldIllegalAccessException("boolean", Boolean.toString(z));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
    protected void throwFinalFieldIllegalAccessException(char b) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        throwFinalFieldIllegalAccessException("char", Character.toString(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    protected void throwFinalFieldIllegalAccessException(byte b) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        throwFinalFieldIllegalAccessException("byte", Byte.toString(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    protected void throwFinalFieldIllegalAccessException(short b) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        throwFinalFieldIllegalAccessException("short", Short.toString(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    protected void throwFinalFieldIllegalAccessException(int i) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        throwFinalFieldIllegalAccessException("int", Integer.toString(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    protected void throwFinalFieldIllegalAccessException(long i) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        throwFinalFieldIllegalAccessException("long", Long.toString(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    protected void throwFinalFieldIllegalAccessException(float f) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        throwFinalFieldIllegalAccessException("float", Float.toString(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    protected void throwFinalFieldIllegalAccessException(double f) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        throwFinalFieldIllegalAccessException("double", Double.toString(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    protected IllegalArgumentException newGetBooleanIllegalArgumentException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        return newGetIllegalArgumentException("boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    protected IllegalArgumentException newGetByteIllegalArgumentException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
        return newGetIllegalArgumentException("byte");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    protected IllegalArgumentException newGetCharIllegalArgumentException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        return newGetIllegalArgumentException("char");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    protected IllegalArgumentException newGetShortIllegalArgumentException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        return newGetIllegalArgumentException("short");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    protected IllegalArgumentException newGetIntIllegalArgumentException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        return newGetIllegalArgumentException("int");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
    protected IllegalArgumentException newGetLongIllegalArgumentException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        return newGetIllegalArgumentException("long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    protected IllegalArgumentException newGetFloatIllegalArgumentException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        return newGetIllegalArgumentException("float");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    protected IllegalArgumentException newGetDoubleIllegalArgumentException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        return newGetIllegalArgumentException("double");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    protected String getSetMessage(String attemptedType, String attemptedValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        String err = "Can not set";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        if (Modifier.isStatic(field.getModifiers()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            err += " static";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (isFinal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            err += " final";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
        err += " " + field.getType().getName() + " field " + getQualifiedFieldName() + " to ";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (attemptedValue.length() > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            err += "(" + attemptedType + ")" + attemptedValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
            if (attemptedType.length() > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                err += attemptedType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
                err += "null value";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    protected void throwSetIllegalArgumentException(String attemptedType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                                                    String attemptedValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        throw new IllegalArgumentException(getSetMessage(attemptedType,attemptedValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    protected void throwSetIllegalArgumentException(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        throwSetIllegalArgumentException(o != null ? o.getClass().getName() : "", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    protected void throwSetIllegalArgumentException(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
        throwSetIllegalArgumentException("boolean", Boolean.toString(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
    protected void throwSetIllegalArgumentException(byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        throwSetIllegalArgumentException("byte", Byte.toString(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
    protected void throwSetIllegalArgumentException(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
        throwSetIllegalArgumentException("char", Character.toString(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
    protected void throwSetIllegalArgumentException(short s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
        throwSetIllegalArgumentException("short", Short.toString(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
    protected void throwSetIllegalArgumentException(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
        throwSetIllegalArgumentException("int", Integer.toString(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
    protected void throwSetIllegalArgumentException(long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
        throwSetIllegalArgumentException("long", Long.toString(l));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    protected void throwSetIllegalArgumentException(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        throwSetIllegalArgumentException("float", Float.toString(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    protected void throwSetIllegalArgumentException(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
        throwSetIllegalArgumentException("double", Double.toString(d));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
}