1 /*
  2  * Copyright (c) 2001, 2011, 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 jdk.internal.reflect;
 27 
 28 import java.lang.reflect.Field;
 29 import java.lang.reflect.Modifier;
 30 import jdk.internal.misc.Unsafe;
 31 import jdk.internal.vm.annotation.Stable;
 32 
 33 /** Base class for jdk.internal.misc.Unsafe-based FieldAccessors. The
 34     observation is that there are only nine types of fields from the
 35     standpoint of reflection code: the eight primitive types and
 36     Object. Using class Unsafe instead of generated bytecodes saves
 37     memory and loading time for the dynamically-generated
 38     FieldAccessors. */
 39 
 40 abstract class UnsafeFieldAccessorImpl extends FieldAccessorImpl {
 41     static final Unsafe unsafe = Unsafe.getUnsafe();
 42 
 43     @Stable
 44     protected final long    fieldOffset;
 45     protected final boolean isFinal;
 46 
 47     UnsafeFieldAccessorImpl(Field field) {
 48         super(field);
 49         if (Modifier.isStatic(field.getModifiers()))
 50             this.fieldOffset = unsafe.staticFieldOffset(field);
 51         else
 52             this.fieldOffset = unsafe.objectFieldOffset(field);
 53         this.isFinal = Modifier.isFinal(field.getModifiers());
 54     }
 55 
 56     protected void ensureObj(Object o) {
 57         // NOTE: will throw NullPointerException, as specified, if o is null
 58         if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) {
 59             throwSetIllegalArgumentException(o);
 60         }
 61     }
 62 
 63     protected boolean isFlattened() {
 64         return unsafe.isFlattened(field);
 65     }
 66 
 67     protected boolean canBeNull() {
 68         return !field.getType().isPrimitiveClass() || field.getType().isPrimaryType();
 69     }
 70 
 71     protected Object checkValue(Object value) {
 72         if (!canBeNull() && value == null)
 73             throw new NullPointerException(field + " cannot be set to null");
 74 
 75         if (value != null) {
 76             Class<?> type = value.getClass();
 77             if (type.isPrimitiveClass()) {
 78                 type = type.asValueType();
 79             }
 80             if (!field.getType().isAssignableFrom(type)) {
 81                 throwSetIllegalArgumentException(value);
 82             }
 83         }
 84         return value;
 85     }
 86 
 87     private String getQualifiedFieldName() {
 88       return field.getDeclaringClass().getName() + "." +field.getName();
 89     }
 90 
 91     protected IllegalArgumentException newGetIllegalArgumentException(String type) {
 92         return new IllegalArgumentException(
 93           "Attempt to get "+field.getType().getName()+" field \"" +
 94           getQualifiedFieldName() + "\" with illegal data type conversion to "+type
 95         );
 96     }
 97 
 98     protected void throwFinalFieldIllegalAccessException(String attemptedType,
 99                                                          String attemptedValue)
100                                                          throws IllegalAccessException {
101         throw new IllegalAccessException(getSetMessage(attemptedType, attemptedValue));
102 
103     }
104     protected void throwFinalFieldIllegalAccessException(Object o) throws IllegalAccessException {
105         throwFinalFieldIllegalAccessException(o != null ? o.getClass().getName() : "", "");
106     }
107 
108     protected void throwFinalFieldIllegalAccessException(boolean z) throws IllegalAccessException {
109         throwFinalFieldIllegalAccessException("boolean", Boolean.toString(z));
110     }
111 
112     protected void throwFinalFieldIllegalAccessException(char b) throws IllegalAccessException {
113         throwFinalFieldIllegalAccessException("char", Character.toString(b));
114     }
115 
116     protected void throwFinalFieldIllegalAccessException(byte b) throws IllegalAccessException {
117         throwFinalFieldIllegalAccessException("byte", Byte.toString(b));
118     }
119 
120     protected void throwFinalFieldIllegalAccessException(short b) throws IllegalAccessException {
121         throwFinalFieldIllegalAccessException("short", Short.toString(b));
122     }
123 
124     protected void throwFinalFieldIllegalAccessException(int i) throws IllegalAccessException {
125         throwFinalFieldIllegalAccessException("int", Integer.toString(i));
126     }
127 
128     protected void throwFinalFieldIllegalAccessException(long i) throws IllegalAccessException {
129         throwFinalFieldIllegalAccessException("long", Long.toString(i));
130     }
131 
132     protected void throwFinalFieldIllegalAccessException(float f) throws IllegalAccessException {
133         throwFinalFieldIllegalAccessException("float", Float.toString(f));
134     }
135 
136     protected void throwFinalFieldIllegalAccessException(double f) throws IllegalAccessException {
137         throwFinalFieldIllegalAccessException("double", Double.toString(f));
138     }
139 
140     protected IllegalArgumentException newGetBooleanIllegalArgumentException() {
141         return newGetIllegalArgumentException("boolean");
142     }
143 
144     protected IllegalArgumentException newGetByteIllegalArgumentException() {
145         return newGetIllegalArgumentException("byte");
146     }
147 
148     protected IllegalArgumentException newGetCharIllegalArgumentException() {
149         return newGetIllegalArgumentException("char");
150     }
151 
152     protected IllegalArgumentException newGetShortIllegalArgumentException() {
153         return newGetIllegalArgumentException("short");
154     }
155 
156     protected IllegalArgumentException newGetIntIllegalArgumentException() {
157         return newGetIllegalArgumentException("int");
158     }
159 
160     protected IllegalArgumentException newGetLongIllegalArgumentException() {
161         return newGetIllegalArgumentException("long");
162     }
163 
164     protected IllegalArgumentException newGetFloatIllegalArgumentException() {
165         return newGetIllegalArgumentException("float");
166     }
167 
168     protected IllegalArgumentException newGetDoubleIllegalArgumentException() {
169         return newGetIllegalArgumentException("double");
170     }
171 
172     protected String getSetMessage(String attemptedType, String attemptedValue) {
173         String err = "Can not set";
174         if (Modifier.isStatic(field.getModifiers()))
175             err += " static";
176         if (isFinal)
177             err += " final";
178         err += " " + field.getType().getName() + " field " + getQualifiedFieldName() + " to ";
179         if (!attemptedValue.isEmpty()) {
180             err += "(" + attemptedType + ")" + attemptedValue;
181         } else {
182             if (!attemptedType.isEmpty())
183                 err += attemptedType;
184             else
185                 err += "null value";
186         }
187         return err;
188     }
189 
190     protected void throwSetIllegalArgumentException(String attemptedType,
191                                                     String attemptedValue) {
192         throw new IllegalArgumentException(getSetMessage(attemptedType,attemptedValue));
193     }
194 
195     protected void throwSetIllegalArgumentException(Object o) {
196         throwSetIllegalArgumentException(o != null ? o.getClass().getName() : "", "");
197     }
198 
199     protected void throwSetIllegalArgumentException(boolean b) {
200         throwSetIllegalArgumentException("boolean", Boolean.toString(b));
201     }
202 
203     protected void throwSetIllegalArgumentException(byte b) {
204         throwSetIllegalArgumentException("byte", Byte.toString(b));
205     }
206 
207     protected void throwSetIllegalArgumentException(char c) {
208         throwSetIllegalArgumentException("char", Character.toString(c));
209     }
210 
211     protected void throwSetIllegalArgumentException(short s) {
212         throwSetIllegalArgumentException("short", Short.toString(s));
213     }
214 
215     protected void throwSetIllegalArgumentException(int i) {
216         throwSetIllegalArgumentException("int", Integer.toString(i));
217     }
218 
219     protected void throwSetIllegalArgumentException(long l) {
220         throwSetIllegalArgumentException("long", Long.toString(l));
221     }
222 
223     protected void throwSetIllegalArgumentException(float f) {
224         throwSetIllegalArgumentException("float", Float.toString(f));
225     }
226 
227     protected void throwSetIllegalArgumentException(double d) {
228         throwSetIllegalArgumentException("double", Double.toString(d));
229     }
230 
231 }