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             fieldOffset = unsafe.staticFieldOffset(field);
 51         else
 52             fieldOffset = unsafe.objectFieldOffset(field);
 53         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     private String getQualifiedFieldName() {
 64       return field.getDeclaringClass().getName() + "." +field.getName();
 65     }
 66 
 67     protected IllegalArgumentException newGetIllegalArgumentException(String type) {
 68         return new IllegalArgumentException(
 69           "Attempt to get "+field.getType().getName()+" field \"" +
 70           getQualifiedFieldName() + "\" with illegal data type conversion to "+type
 71         );
 72     }
 73 
 74     protected void throwFinalFieldIllegalAccessException(String attemptedType,
 75                                                          String attemptedValue)
 76                                                          throws IllegalAccessException {
 77         throw new IllegalAccessException(getSetMessage(attemptedType, attemptedValue));
 78 
 79     }
 80     protected void throwFinalFieldIllegalAccessException(Object o) throws IllegalAccessException {
 81         throwFinalFieldIllegalAccessException(o != null ? o.getClass().getName() : "", "");
 82     }
 83 
 84     protected void throwFinalFieldIllegalAccessException(boolean z) throws IllegalAccessException {
 85         throwFinalFieldIllegalAccessException("boolean", Boolean.toString(z));
 86     }
 87 
 88     protected void throwFinalFieldIllegalAccessException(char b) throws IllegalAccessException {
 89         throwFinalFieldIllegalAccessException("char", Character.toString(b));
 90     }
 91 
 92     protected void throwFinalFieldIllegalAccessException(byte b) throws IllegalAccessException {
 93         throwFinalFieldIllegalAccessException("byte", Byte.toString(b));
 94     }
 95 
 96     protected void throwFinalFieldIllegalAccessException(short b) throws IllegalAccessException {
 97         throwFinalFieldIllegalAccessException("short", Short.toString(b));
 98     }
 99 
100     protected void throwFinalFieldIllegalAccessException(int i) throws IllegalAccessException {
101         throwFinalFieldIllegalAccessException("int", Integer.toString(i));
102     }
103 
104     protected void throwFinalFieldIllegalAccessException(long i) throws IllegalAccessException {
105         throwFinalFieldIllegalAccessException("long", Long.toString(i));
106     }
107 
108     protected void throwFinalFieldIllegalAccessException(float f) throws IllegalAccessException {
109         throwFinalFieldIllegalAccessException("float", Float.toString(f));
110     }
111 
112     protected void throwFinalFieldIllegalAccessException(double f) throws IllegalAccessException {
113         throwFinalFieldIllegalAccessException("double", Double.toString(f));
114     }
115 
116     protected IllegalArgumentException newGetBooleanIllegalArgumentException() {
117         return newGetIllegalArgumentException("boolean");
118     }
119 
120     protected IllegalArgumentException newGetByteIllegalArgumentException() {
121         return newGetIllegalArgumentException("byte");
122     }
123 
124     protected IllegalArgumentException newGetCharIllegalArgumentException() {
125         return newGetIllegalArgumentException("char");
126     }
127 
128     protected IllegalArgumentException newGetShortIllegalArgumentException() {
129         return newGetIllegalArgumentException("short");
130     }
131 
132     protected IllegalArgumentException newGetIntIllegalArgumentException() {
133         return newGetIllegalArgumentException("int");
134     }
135 
136     protected IllegalArgumentException newGetLongIllegalArgumentException() {
137         return newGetIllegalArgumentException("long");
138     }
139 
140     protected IllegalArgumentException newGetFloatIllegalArgumentException() {
141         return newGetIllegalArgumentException("float");
142     }
143 
144     protected IllegalArgumentException newGetDoubleIllegalArgumentException() {
145         return newGetIllegalArgumentException("double");
146     }
147 
148     protected String getSetMessage(String attemptedType, String attemptedValue) {
149         String err = "Can not set";
150         if (Modifier.isStatic(field.getModifiers()))
151             err += " static";
152         if (isFinal)
153             err += " final";
154         err += " " + field.getType().getName() + " field " + getQualifiedFieldName() + " to ";
155         if (!attemptedValue.isEmpty()) {
156             err += "(" + attemptedType + ")" + attemptedValue;
157         } else {
158             if (!attemptedType.isEmpty())
159                 err += attemptedType;
160             else
161                 err += "null value";
162         }
163         return err;
164     }
165 
166     protected void throwSetIllegalArgumentException(String attemptedType,
167                                                     String attemptedValue) {
168         throw new IllegalArgumentException(getSetMessage(attemptedType,attemptedValue));
169     }
170 
171     protected void throwSetIllegalArgumentException(Object o) {
172         throwSetIllegalArgumentException(o != null ? o.getClass().getName() : "", "");
173     }
174 
175     protected void throwSetIllegalArgumentException(boolean b) {
176         throwSetIllegalArgumentException("boolean", Boolean.toString(b));
177     }
178 
179     protected void throwSetIllegalArgumentException(byte b) {
180         throwSetIllegalArgumentException("byte", Byte.toString(b));
181     }
182 
183     protected void throwSetIllegalArgumentException(char c) {
184         throwSetIllegalArgumentException("char", Character.toString(c));
185     }
186 
187     protected void throwSetIllegalArgumentException(short s) {
188         throwSetIllegalArgumentException("short", Short.toString(s));
189     }
190 
191     protected void throwSetIllegalArgumentException(int i) {
192         throwSetIllegalArgumentException("int", Integer.toString(i));
193     }
194 
195     protected void throwSetIllegalArgumentException(long l) {
196         throwSetIllegalArgumentException("long", Long.toString(l));
197     }
198 
199     protected void throwSetIllegalArgumentException(float f) {
200         throwSetIllegalArgumentException("float", Float.toString(f));
201     }
202 
203     protected void throwSetIllegalArgumentException(double d) {
204         throwSetIllegalArgumentException("double", Double.toString(d));
205     }
206 
207 }