src/java.base/share/classes/jdk/internal/reflect/UnsafeFieldAccessorImpl.java
author redestad
Thu, 13 Dec 2018 15:31:05 +0100
changeset 53018 8bf9268df0e2
parent 47216 71c04702a3d5
permissions -rw-r--r--
8215281: Use String.isEmpty() when applicable in java.base Reviewed-by: dfuchs, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 10342
diff changeset
     2
 * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
37363
329dba26ffd2 8137058: Clear out all non-Critical APIs from sun.reflect
chegar
parents: 33674
diff changeset
    26
package jdk.internal.reflect;
2
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;
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 25859
diff changeset
    30
import jdk.internal.misc.Unsafe;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
33674
566777f73c32 8140606: Update library code to use internal Unsafe
chegar
parents: 25859
diff changeset
    32
/** Base class for jdk.internal.misc.Unsafe-based FieldAccessors. The
2
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;
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    43
    protected final long    fieldOffset;
2
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;
10342
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    48
        if (Modifier.isStatic(field.getModifiers()))
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    49
            fieldOffset = unsafe.staticFieldOffset(field);
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    50
        else
ca0984bc9d32 7077389: Reflection classes do not build with javac -Xlint:all -Werror
jjg
parents: 5506
diff changeset
    51
            fieldOffset = unsafe.objectFieldOffset(field);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        isFinal = Modifier.isFinal(field.getModifiers());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    protected void ensureObj(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        // NOTE: will throw NullPointerException, as specified, if o is null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            throwSetIllegalArgumentException(o);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    private String getQualifiedFieldName() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
      return field.getDeclaringClass().getName() + "." +field.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    protected IllegalArgumentException newGetIllegalArgumentException(String type) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        return new IllegalArgumentException(
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
          "Attempt to get "+field.getType().getName()+" field \"" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
          getQualifiedFieldName() + "\" with illegal data type conversion to "+type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        );
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    protected void throwFinalFieldIllegalAccessException(String attemptedType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
                                                         String attemptedValue)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                                                         throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        throw new IllegalAccessException(getSetMessage(attemptedType, attemptedValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    protected void throwFinalFieldIllegalAccessException(Object o) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        throwFinalFieldIllegalAccessException(o != null ? o.getClass().getName() : "", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
    protected void throwFinalFieldIllegalAccessException(boolean z) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        throwFinalFieldIllegalAccessException("boolean", Boolean.toString(z));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    protected void throwFinalFieldIllegalAccessException(char b) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        throwFinalFieldIllegalAccessException("char", Character.toString(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    protected void throwFinalFieldIllegalAccessException(byte b) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        throwFinalFieldIllegalAccessException("byte", Byte.toString(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    protected void throwFinalFieldIllegalAccessException(short b) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        throwFinalFieldIllegalAccessException("short", Short.toString(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    protected void throwFinalFieldIllegalAccessException(int i) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        throwFinalFieldIllegalAccessException("int", Integer.toString(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
    protected void throwFinalFieldIllegalAccessException(long i) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        throwFinalFieldIllegalAccessException("long", Long.toString(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    protected void throwFinalFieldIllegalAccessException(float f) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        throwFinalFieldIllegalAccessException("float", Float.toString(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    protected void throwFinalFieldIllegalAccessException(double f) throws IllegalAccessException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        throwFinalFieldIllegalAccessException("double", Double.toString(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    protected IllegalArgumentException newGetBooleanIllegalArgumentException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        return newGetIllegalArgumentException("boolean");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    protected IllegalArgumentException newGetByteIllegalArgumentException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return newGetIllegalArgumentException("byte");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    protected IllegalArgumentException newGetCharIllegalArgumentException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        return newGetIllegalArgumentException("char");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    protected IllegalArgumentException newGetShortIllegalArgumentException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        return newGetIllegalArgumentException("short");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    protected IllegalArgumentException newGetIntIllegalArgumentException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        return newGetIllegalArgumentException("int");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    protected IllegalArgumentException newGetLongIllegalArgumentException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        return newGetIllegalArgumentException("long");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    protected IllegalArgumentException newGetFloatIllegalArgumentException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        return newGetIllegalArgumentException("float");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
    protected IllegalArgumentException newGetDoubleIllegalArgumentException() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        return newGetIllegalArgumentException("double");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
    protected String getSetMessage(String attemptedType, String attemptedValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        String err = "Can not set";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        if (Modifier.isStatic(field.getModifiers()))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            err += " static";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        if (isFinal)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            err += " final";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        err += " " + field.getType().getName() + " field " + getQualifiedFieldName() + " to ";
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
   154
        if (!attemptedValue.isEmpty()) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            err += "(" + attemptedType + ")" + attemptedValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        } else {
53018
8bf9268df0e2 8215281: Use String.isEmpty() when applicable in java.base
redestad
parents: 47216
diff changeset
   157
            if (!attemptedType.isEmpty())
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
                err += attemptedType;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
                err += "null value";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
        return err;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
    protected void throwSetIllegalArgumentException(String attemptedType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
                                                    String attemptedValue) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        throw new IllegalArgumentException(getSetMessage(attemptedType,attemptedValue));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
    protected void throwSetIllegalArgumentException(Object o) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        throwSetIllegalArgumentException(o != null ? o.getClass().getName() : "", "");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    protected void throwSetIllegalArgumentException(boolean b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        throwSetIllegalArgumentException("boolean", Boolean.toString(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
    protected void throwSetIllegalArgumentException(byte b) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        throwSetIllegalArgumentException("byte", Byte.toString(b));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    protected void throwSetIllegalArgumentException(char c) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        throwSetIllegalArgumentException("char", Character.toString(c));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
    protected void throwSetIllegalArgumentException(short s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        throwSetIllegalArgumentException("short", Short.toString(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    protected void throwSetIllegalArgumentException(int i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
        throwSetIllegalArgumentException("int", Integer.toString(i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
    protected void throwSetIllegalArgumentException(long l) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
        throwSetIllegalArgumentException("long", Long.toString(l));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
    protected void throwSetIllegalArgumentException(float f) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        throwSetIllegalArgumentException("float", Float.toString(f));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
    protected void throwSetIllegalArgumentException(double d) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
        throwSetIllegalArgumentException("double", Double.toString(d));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
}