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