1 /*
  2  * Copyright (c) 2001, 2021, 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 
 31 /** Package-private implementation of the FieldAccessor interface
 32     which has access to all classes and all fields, regardless of
 33     language restrictions. See MagicAccessorImpl. */
 34 
 35 abstract class FieldAccessorImpl extends MagicAccessorImpl
 36     implements FieldAccessor {
 37     protected final Field field;
 38 
 39     FieldAccessorImpl(Field field) {
 40         this.field = field;
 41     }
 42 
 43     /** Matches specification in {@link java.lang.reflect.Field} */
 44     public abstract Object get(Object obj)
 45         throws IllegalArgumentException;
 46 
 47     /** Matches specification in {@link java.lang.reflect.Field} */
 48     public abstract boolean getBoolean(Object obj)
 49         throws IllegalArgumentException;
 50 
 51     /** Matches specification in {@link java.lang.reflect.Field} */
 52     public abstract byte getByte(Object obj)
 53         throws IllegalArgumentException;
 54 
 55     /** Matches specification in {@link java.lang.reflect.Field} */
 56     public abstract char getChar(Object obj)
 57         throws IllegalArgumentException;
 58 
 59     /** Matches specification in {@link java.lang.reflect.Field} */
 60     public abstract short getShort(Object obj)
 61         throws IllegalArgumentException;
 62 
 63     /** Matches specification in {@link java.lang.reflect.Field} */
 64     public abstract int getInt(Object obj)
 65         throws IllegalArgumentException;
 66 
 67     /** Matches specification in {@link java.lang.reflect.Field} */
 68     public abstract long getLong(Object obj)
 69         throws IllegalArgumentException;
 70 
 71     /** Matches specification in {@link java.lang.reflect.Field} */
 72     public abstract float getFloat(Object obj)
 73         throws IllegalArgumentException;
 74 
 75     /** Matches specification in {@link java.lang.reflect.Field} */
 76     public abstract double getDouble(Object obj)
 77         throws IllegalArgumentException;
 78 
 79     /** Matches specification in {@link java.lang.reflect.Field} */
 80     public abstract void set(Object obj, Object value)
 81         throws IllegalArgumentException, IllegalAccessException;
 82 
 83     /** Matches specification in {@link java.lang.reflect.Field} */
 84     public abstract void setBoolean(Object obj, boolean z)
 85         throws IllegalArgumentException, IllegalAccessException;
 86 
 87     /** Matches specification in {@link java.lang.reflect.Field} */
 88     public abstract void setByte(Object obj, byte b)
 89         throws IllegalArgumentException, IllegalAccessException;
 90 
 91     /** Matches specification in {@link java.lang.reflect.Field} */
 92     public abstract void setChar(Object obj, char c)
 93         throws IllegalArgumentException, IllegalAccessException;
 94 
 95     /** Matches specification in {@link java.lang.reflect.Field} */
 96     public abstract void setShort(Object obj, short s)
 97         throws IllegalArgumentException, IllegalAccessException;
 98 
 99     /** Matches specification in {@link java.lang.reflect.Field} */
100     public abstract void setInt(Object obj, int i)
101         throws IllegalArgumentException, IllegalAccessException;
102 
103     /** Matches specification in {@link java.lang.reflect.Field} */
104     public abstract void setLong(Object obj, long l)
105         throws IllegalArgumentException, IllegalAccessException;
106 
107     /** Matches specification in {@link java.lang.reflect.Field} */
108     public abstract void setFloat(Object obj, float f)
109         throws IllegalArgumentException, IllegalAccessException;
110 
111     /** Matches specification in {@link java.lang.reflect.Field} */
112     public abstract void setDouble(Object obj, double d)
113         throws IllegalArgumentException, IllegalAccessException;
114 
115 
116     protected void ensureObj(Object o) {
117         // NOTE: will throw NullPointerException, as specified, if o is null
118         if (!field.getDeclaringClass().isAssignableFrom(o.getClass())) {
119             throwSetIllegalArgumentException(o);
120         }
121     }
122 
123     private String getQualifiedFieldName() {
124         return field.getDeclaringClass().getName() + "." +field.getName();
125     }
126 
127     protected IllegalArgumentException newGetIllegalArgumentException(String type) {
128         return new IllegalArgumentException(
129                 "Attempt to get "+field.getType().getName()+" field \"" +
130                         getQualifiedFieldName() + "\" with illegal data type conversion to "+type
131         );
132     }
133 
134     protected void throwFinalFieldIllegalAccessException(String attemptedType,
135                                                          String attemptedValue)
136             throws IllegalAccessException {
137         throw new IllegalAccessException(getSetMessage(attemptedType, attemptedValue));
138     }
139 
140     protected void throwFinalFieldIllegalAccessException(Object o) throws IllegalAccessException {
141         throwFinalFieldIllegalAccessException(o != null ? o.getClass().getName() : "", "");
142     }
143 
144     protected void throwFinalFieldIllegalAccessException(boolean z) throws IllegalAccessException {
145         throwFinalFieldIllegalAccessException("boolean", Boolean.toString(z));
146     }
147 
148     protected void throwFinalFieldIllegalAccessException(char b) throws IllegalAccessException {
149         throwFinalFieldIllegalAccessException("char", Character.toString(b));
150     }
151 
152     protected void throwFinalFieldIllegalAccessException(byte b) throws IllegalAccessException {
153         throwFinalFieldIllegalAccessException("byte", Byte.toString(b));
154     }
155 
156     protected void throwFinalFieldIllegalAccessException(short b) throws IllegalAccessException {
157         throwFinalFieldIllegalAccessException("short", Short.toString(b));
158     }
159 
160     protected void throwFinalFieldIllegalAccessException(int i) throws IllegalAccessException {
161         throwFinalFieldIllegalAccessException("int", Integer.toString(i));
162     }
163 
164     protected void throwFinalFieldIllegalAccessException(long i) throws IllegalAccessException {
165         throwFinalFieldIllegalAccessException("long", Long.toString(i));
166     }
167 
168     protected void throwFinalFieldIllegalAccessException(float f) throws IllegalAccessException {
169         throwFinalFieldIllegalAccessException("float", Float.toString(f));
170     }
171 
172     protected void throwFinalFieldIllegalAccessException(double f) throws IllegalAccessException {
173         throwFinalFieldIllegalAccessException("double", Double.toString(f));
174     }
175 
176     protected IllegalArgumentException newGetBooleanIllegalArgumentException() {
177         return newGetIllegalArgumentException("boolean");
178     }
179 
180     protected IllegalArgumentException newGetByteIllegalArgumentException() {
181         return newGetIllegalArgumentException("byte");
182     }
183 
184     protected IllegalArgumentException newGetCharIllegalArgumentException() {
185         return newGetIllegalArgumentException("char");
186     }
187 
188     protected IllegalArgumentException newGetShortIllegalArgumentException() {
189         return newGetIllegalArgumentException("short");
190     }
191 
192     protected IllegalArgumentException newGetIntIllegalArgumentException() {
193         return newGetIllegalArgumentException("int");
194     }
195 
196     protected IllegalArgumentException newGetLongIllegalArgumentException() {
197         return newGetIllegalArgumentException("long");
198     }
199 
200     protected IllegalArgumentException newGetFloatIllegalArgumentException() {
201         return newGetIllegalArgumentException("float");
202     }
203 
204     protected IllegalArgumentException newGetDoubleIllegalArgumentException() {
205         return newGetIllegalArgumentException("double");
206     }
207 
208     protected String getSetMessage(String attemptedType, String attemptedValue) {
209         String err = "Can not set";
210         if (Modifier.isStatic(field.getModifiers()))
211             err += " static";
212         if (Modifier.isFinal(field.getModifiers()))
213             err += " final";
214         err += " " + field.getType().getName() + " field " + getQualifiedFieldName() + " to ";
215         if (!attemptedValue.isEmpty()) {
216             err += "(" + attemptedType + ")" + attemptedValue;
217         } else {
218             if (!attemptedType.isEmpty())
219                 err += attemptedType;
220             else
221                 err += "null value";
222         }
223         return err;
224     }
225 
226     protected String getMessage(boolean getter, String attemptedType) {
227         String err = "Can not " + (getter ? "get" : "set");
228         if (Modifier.isStatic(field.getModifiers()))
229             err += " static";
230         if (Modifier.isFinal(field.getModifiers()))
231             err += " final";
232         err += " " + field.getType().getName() + " field " + getQualifiedFieldName() + " on " + attemptedType;
233         return err;
234     }
235 
236     protected void throwSetIllegalArgumentException(String attemptedType,
237                                                     String attemptedValue) {
238         throw new IllegalArgumentException(getSetMessage(attemptedType,attemptedValue));
239     }
240 
241     protected void throwSetIllegalArgumentException(Object o) {
242         throwSetIllegalArgumentException(o != null ? o.getClass().getName() : "", "");
243     }
244 
245     protected void throwSetIllegalArgumentException(boolean b) {
246         throwSetIllegalArgumentException("boolean", Boolean.toString(b));
247     }
248 
249     protected void throwSetIllegalArgumentException(byte b) {
250         throwSetIllegalArgumentException("byte", Byte.toString(b));
251     }
252 
253     protected void throwSetIllegalArgumentException(char c) {
254         throwSetIllegalArgumentException("char", Character.toString(c));
255     }
256 
257     protected void throwSetIllegalArgumentException(short s) {
258         throwSetIllegalArgumentException("short", Short.toString(s));
259     }
260 
261     protected void throwSetIllegalArgumentException(int i) {
262         throwSetIllegalArgumentException("int", Integer.toString(i));
263     }
264 
265     protected void throwSetIllegalArgumentException(long l) {
266         throwSetIllegalArgumentException("long", Long.toString(l));
267     }
268 
269     protected void throwSetIllegalArgumentException(float f) {
270         throwSetIllegalArgumentException("float", Float.toString(f));
271     }
272 
273     protected void throwSetIllegalArgumentException(double d) {
274         throwSetIllegalArgumentException("double", Double.toString(d));
275     }
276 
277 }