1 |
758 |
jeremybenn |
/* java.lang.reflect.Field - reflection of Java fields
|
2 |
|
|
Copyright (C) 1998, 2001, 2005, 2006 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
|
39 |
|
|
package java.lang.reflect;
|
40 |
|
|
|
41 |
|
|
import gnu.java.lang.ClassHelper;
|
42 |
|
|
|
43 |
|
|
import gnu.java.lang.reflect.FieldSignatureParser;
|
44 |
|
|
import java.lang.annotation.Annotation;
|
45 |
|
|
|
46 |
|
|
/**
|
47 |
|
|
* The Field class represents a member variable of a class. It also allows
|
48 |
|
|
* dynamic access to a member, via reflection. This works for both
|
49 |
|
|
* static and instance fields. Operations on Field objects know how to
|
50 |
|
|
* do widening conversions, but throw {@link IllegalArgumentException} if
|
51 |
|
|
* a narrowing conversion would be necessary. You can query for information
|
52 |
|
|
* on this Field regardless of location, but get and set access may be limited
|
53 |
|
|
* by Java language access controls. If you can't do it in the compiler, you
|
54 |
|
|
* can't normally do it here either.<p>
|
55 |
|
|
*
|
56 |
|
|
* <B>Note:</B> This class returns and accepts types as Classes, even
|
57 |
|
|
* primitive types; there are Class types defined that represent each
|
58 |
|
|
* different primitive type. They are <code>java.lang.Boolean.TYPE,
|
59 |
|
|
* java.lang.Byte.TYPE,</code>, also available as <code>boolean.class,
|
60 |
|
|
* byte.class</code>, etc. These are not to be confused with the
|
61 |
|
|
* classes <code>java.lang.Boolean, java.lang.Byte</code>, etc., which are
|
62 |
|
|
* real classes.<p>
|
63 |
|
|
*
|
64 |
|
|
* Also note that this is not a serializable class. It is entirely feasible
|
65 |
|
|
* to make it serializable using the Externalizable interface, but this is
|
66 |
|
|
* on Sun, not me.
|
67 |
|
|
*
|
68 |
|
|
* @author John Keiser
|
69 |
|
|
* @author Eric Blake <ebb9@email.byu.edu>
|
70 |
|
|
* @author Per Bothner <bothner@cygnus.com>
|
71 |
|
|
* @see Member
|
72 |
|
|
* @see Class
|
73 |
|
|
* @see Class#getField(String)
|
74 |
|
|
* @see Class#getDeclaredField(String)
|
75 |
|
|
* @see Class#getFields()
|
76 |
|
|
* @see Class#getDeclaredFields()
|
77 |
|
|
* @since 1.1
|
78 |
|
|
* @status updated to 1.4
|
79 |
|
|
*/
|
80 |
|
|
public final class Field
|
81 |
|
|
extends AccessibleObject implements Member
|
82 |
|
|
{
|
83 |
|
|
private Class declaringClass;
|
84 |
|
|
private String name;
|
85 |
|
|
|
86 |
|
|
// Offset in bytes from the start of declaringClass's fields array.
|
87 |
|
|
private int offset;
|
88 |
|
|
|
89 |
|
|
// The Class (or primitive TYPE) of this field.
|
90 |
|
|
private Class type;
|
91 |
|
|
|
92 |
|
|
static final int FIELD_MODIFIERS
|
93 |
|
|
= Modifier.FINAL | Modifier.PRIVATE | Modifier.PROTECTED
|
94 |
|
|
| Modifier.PUBLIC | Modifier.STATIC | Modifier.TRANSIENT
|
95 |
|
|
| Modifier.VOLATILE;
|
96 |
|
|
|
97 |
|
|
// This is instantiated by Class sometimes, but it uses C++ and
|
98 |
|
|
// avoids the Java protection check.
|
99 |
|
|
Field ()
|
100 |
|
|
{
|
101 |
|
|
}
|
102 |
|
|
|
103 |
|
|
/**
|
104 |
|
|
* Gets the class that declared this field, or the class where this field
|
105 |
|
|
* is a non-inherited member.
|
106 |
|
|
* @return the class that declared this member
|
107 |
|
|
*/
|
108 |
|
|
public Class<?> getDeclaringClass()
|
109 |
|
|
{
|
110 |
|
|
return declaringClass;
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
/**
|
114 |
|
|
* Gets the name of this field.
|
115 |
|
|
* @return the name of this field
|
116 |
|
|
*/
|
117 |
|
|
public native String getName();
|
118 |
|
|
|
119 |
|
|
/**
|
120 |
|
|
* Return the raw modifiers for this field.
|
121 |
|
|
* @return the field's modifiers
|
122 |
|
|
*/
|
123 |
|
|
private native int getModifiersInternal();
|
124 |
|
|
|
125 |
|
|
/**
|
126 |
|
|
* Gets the modifiers this field uses. Use the <code>Modifier</code>
|
127 |
|
|
* class to interpret the values. A field can only have a subset of the
|
128 |
|
|
* following modifiers: public, private, protected, static, final,
|
129 |
|
|
* transient, and volatile.
|
130 |
|
|
*
|
131 |
|
|
* @return an integer representing the modifiers to this Member
|
132 |
|
|
* @see Modifier
|
133 |
|
|
*/
|
134 |
|
|
public int getModifiers()
|
135 |
|
|
{
|
136 |
|
|
return getModifiersInternal() & FIELD_MODIFIERS;
|
137 |
|
|
}
|
138 |
|
|
|
139 |
|
|
/**
|
140 |
|
|
* Return true if this field is synthetic, false otherwise.
|
141 |
|
|
* @since 1.5
|
142 |
|
|
*/
|
143 |
|
|
public boolean isSynthetic()
|
144 |
|
|
{
|
145 |
|
|
return (getModifiersInternal() & Modifier.SYNTHETIC) != 0;
|
146 |
|
|
}
|
147 |
|
|
|
148 |
|
|
/**
|
149 |
|
|
* Return true if this field represents an enum constant,
|
150 |
|
|
* false otherwise.
|
151 |
|
|
* @since 1.5
|
152 |
|
|
*/
|
153 |
|
|
public boolean isEnumConstant()
|
154 |
|
|
{
|
155 |
|
|
return (getModifiersInternal() & Modifier.ENUM) != 0;
|
156 |
|
|
}
|
157 |
|
|
|
158 |
|
|
/**
|
159 |
|
|
* Gets the type of this field.
|
160 |
|
|
* @return the type of this field
|
161 |
|
|
*/
|
162 |
|
|
public native Class<?> getType();
|
163 |
|
|
|
164 |
|
|
/**
|
165 |
|
|
* Compare two objects to see if they are semantically equivalent.
|
166 |
|
|
* Two Fields are semantically equivalent if they have the same declaring
|
167 |
|
|
* class, name, and type. Since you can't creat a Field except through
|
168 |
|
|
* the VM, this is just the == relation.
|
169 |
|
|
*
|
170 |
|
|
* @param o the object to compare to
|
171 |
|
|
* @return <code>true</code> if they are equal; <code>false</code> if not
|
172 |
|
|
*/
|
173 |
|
|
public boolean equals (Object fld)
|
174 |
|
|
{
|
175 |
|
|
if (! (fld instanceof Field))
|
176 |
|
|
return false;
|
177 |
|
|
Field f = (Field) fld;
|
178 |
|
|
return declaringClass == f.declaringClass && offset == f.offset;
|
179 |
|
|
}
|
180 |
|
|
|
181 |
|
|
/**
|
182 |
|
|
* Get the hash code for the Field. The Field hash code is the hash code
|
183 |
|
|
* of its name XOR'd with the hash code of its class name.
|
184 |
|
|
*
|
185 |
|
|
* @return the hash code for the object.
|
186 |
|
|
*/
|
187 |
|
|
public int hashCode()
|
188 |
|
|
{
|
189 |
|
|
return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
|
190 |
|
|
}
|
191 |
|
|
|
192 |
|
|
/**
|
193 |
|
|
* Get a String representation of the Field. A Field's String
|
194 |
|
|
* representation is "<modifiers> <type>
|
195 |
|
|
* <class>.<fieldname>".<br> Example:
|
196 |
|
|
* <code>public transient boolean gnu.parse.Parser.parseComplete</code>
|
197 |
|
|
*
|
198 |
|
|
* @return the String representation of the Field
|
199 |
|
|
*/
|
200 |
|
|
public String toString ()
|
201 |
|
|
{
|
202 |
|
|
StringBuffer sbuf = new StringBuffer ();
|
203 |
|
|
int mods = getModifiers();
|
204 |
|
|
if (mods != 0)
|
205 |
|
|
{
|
206 |
|
|
Modifier.toString(mods, sbuf);
|
207 |
|
|
sbuf.append(' ');
|
208 |
|
|
}
|
209 |
|
|
Method.appendClassName (sbuf, getType ());
|
210 |
|
|
sbuf.append(' ');
|
211 |
|
|
Method.appendClassName (sbuf, getDeclaringClass());
|
212 |
|
|
sbuf.append('.');
|
213 |
|
|
sbuf.append(getName());
|
214 |
|
|
return sbuf.toString();
|
215 |
|
|
}
|
216 |
|
|
|
217 |
|
|
public String toGenericString()
|
218 |
|
|
{
|
219 |
|
|
StringBuilder sb = new StringBuilder(64);
|
220 |
|
|
Modifier.toString(getModifiers(), sb).append(' ');
|
221 |
|
|
sb.append(getGenericType()).append(' ');
|
222 |
|
|
sb.append(getDeclaringClass().getName()).append('.');
|
223 |
|
|
sb.append(getName());
|
224 |
|
|
return sb.toString();
|
225 |
|
|
}
|
226 |
|
|
|
227 |
|
|
/**
|
228 |
|
|
* Get the value of this Field. If it is primitive, it will be wrapped
|
229 |
|
|
* in the appropriate wrapper type (boolean = java.lang.Boolean).<p>
|
230 |
|
|
*
|
231 |
|
|
* If the field is static, <code>o</code> will be ignored. Otherwise, if
|
232 |
|
|
* <code>o</code> is null, you get a <code>NullPointerException</code>,
|
233 |
|
|
* and if it is incompatible with the declaring class of the field, you
|
234 |
|
|
* get an <code>IllegalArgumentException</code>.<p>
|
235 |
|
|
*
|
236 |
|
|
* Next, if this Field enforces access control, your runtime context is
|
237 |
|
|
* evaluated, and you may have an <code>IllegalAccessException</code> if
|
238 |
|
|
* you could not access this field in similar compiled code. If the field
|
239 |
|
|
* is static, and its class is uninitialized, you trigger class
|
240 |
|
|
* initialization, which may end in a
|
241 |
|
|
* <code>ExceptionInInitializerError</code>.<p>
|
242 |
|
|
*
|
243 |
|
|
* Finally, the field is accessed, and primitives are wrapped (but not
|
244 |
|
|
* necessarily in new objects). This method accesses the field of the
|
245 |
|
|
* declaring class, even if the instance passed in belongs to a subclass
|
246 |
|
|
* which declares another field to hide this one.
|
247 |
|
|
*
|
248 |
|
|
* @param o the object to get the value of this Field from
|
249 |
|
|
* @return the value of the Field
|
250 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
251 |
|
|
* (i.e. it is not public)
|
252 |
|
|
* @throws IllegalArgumentException if <code>o</code> is not an instance of
|
253 |
|
|
* the class or interface declaring this field
|
254 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
255 |
|
|
* requires an instance
|
256 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
257 |
|
|
* class initialization, which then failed
|
258 |
|
|
* @see #getBoolean(Object)
|
259 |
|
|
* @see #getByte(Object)
|
260 |
|
|
* @see #getChar(Object)
|
261 |
|
|
* @see #getShort(Object)
|
262 |
|
|
* @see #getInt(Object)
|
263 |
|
|
* @see #getLong(Object)
|
264 |
|
|
* @see #getFloat(Object)
|
265 |
|
|
* @see #getDouble(Object)
|
266 |
|
|
*/
|
267 |
|
|
public Object get(Object obj)
|
268 |
|
|
throws IllegalAccessException
|
269 |
|
|
{
|
270 |
|
|
return get(null, obj);
|
271 |
|
|
}
|
272 |
|
|
|
273 |
|
|
/**
|
274 |
|
|
* Get the value of this boolean Field. If the field is static,
|
275 |
|
|
* <code>o</code> will be ignored.
|
276 |
|
|
*
|
277 |
|
|
* @param o the object to get the value of this Field from
|
278 |
|
|
* @return the value of the Field
|
279 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
280 |
|
|
* (i.e. it is not public)
|
281 |
|
|
* @throws IllegalArgumentException if this is not a boolean field of
|
282 |
|
|
* <code>o</code>, or if <code>o</code> is not an instance of the
|
283 |
|
|
* declaring class of this field
|
284 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
285 |
|
|
* requires an instance
|
286 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
287 |
|
|
* class initialization, which then failed
|
288 |
|
|
* @see #get(Object)
|
289 |
|
|
*/
|
290 |
|
|
public boolean getBoolean(Object obj)
|
291 |
|
|
throws IllegalAccessException
|
292 |
|
|
{
|
293 |
|
|
return getBoolean(null, obj);
|
294 |
|
|
}
|
295 |
|
|
|
296 |
|
|
/**
|
297 |
|
|
* Get the value of this byte Field. If the field is static,
|
298 |
|
|
* <code>o</code> will be ignored.
|
299 |
|
|
*
|
300 |
|
|
* @param o the object to get the value of this Field from
|
301 |
|
|
* @return the value of the Field
|
302 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
303 |
|
|
* (i.e. it is not public)
|
304 |
|
|
* @throws IllegalArgumentException if this is not a byte field of
|
305 |
|
|
* <code>o</code>, or if <code>o</code> is not an instance of the
|
306 |
|
|
* declaring class of this field
|
307 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
308 |
|
|
* requires an instance
|
309 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
310 |
|
|
* class initialization, which then failed
|
311 |
|
|
* @see #get(Object)
|
312 |
|
|
*/
|
313 |
|
|
public byte getByte(Object obj)
|
314 |
|
|
throws IllegalAccessException
|
315 |
|
|
{
|
316 |
|
|
return getByte(null, obj);
|
317 |
|
|
}
|
318 |
|
|
|
319 |
|
|
/**
|
320 |
|
|
* Get the value of this Field as a char. If the field is static,
|
321 |
|
|
* <code>o</code> will be ignored.
|
322 |
|
|
*
|
323 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
324 |
|
|
* (i.e. it is not public)
|
325 |
|
|
* @throws IllegalArgumentException if this is not a char field of
|
326 |
|
|
* <code>o</code>, or if <code>o</code> is not an instance
|
327 |
|
|
* of the declaring class of this field
|
328 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
329 |
|
|
* requires an instance
|
330 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
331 |
|
|
* class initialization, which then failed
|
332 |
|
|
* @see #get(Object)
|
333 |
|
|
*/
|
334 |
|
|
public char getChar(Object obj)
|
335 |
|
|
throws IllegalAccessException
|
336 |
|
|
{
|
337 |
|
|
return getChar(null, obj);
|
338 |
|
|
}
|
339 |
|
|
|
340 |
|
|
/**
|
341 |
|
|
* Get the value of this Field as a short. If the field is static,
|
342 |
|
|
* <code>o</code> will be ignored.
|
343 |
|
|
*
|
344 |
|
|
* @param o the object to get the value of this Field from
|
345 |
|
|
* @return the value of the Field
|
346 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
347 |
|
|
* (i.e. it is not public)
|
348 |
|
|
* @throws IllegalArgumentException if this is not a byte or short
|
349 |
|
|
* field of <code>o</code>, or if <code>o</code> is not an instance
|
350 |
|
|
* of the declaring class of this field
|
351 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
352 |
|
|
* requires an instance
|
353 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
354 |
|
|
* class initialization, which then failed
|
355 |
|
|
* @see #get(Object)
|
356 |
|
|
*/
|
357 |
|
|
public short getShort(Object obj)
|
358 |
|
|
throws IllegalAccessException
|
359 |
|
|
{
|
360 |
|
|
return getShort(null, obj);
|
361 |
|
|
}
|
362 |
|
|
|
363 |
|
|
/**
|
364 |
|
|
* Get the value of this Field as an int. If the field is static,
|
365 |
|
|
* <code>o</code> will be ignored.
|
366 |
|
|
*
|
367 |
|
|
* @param o the object to get the value of this Field from
|
368 |
|
|
* @return the value of the Field
|
369 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
370 |
|
|
* (i.e. it is not public)
|
371 |
|
|
* @throws IllegalArgumentException if this is not a byte, short, char, or
|
372 |
|
|
* int field of <code>o</code>, or if <code>o</code> is not an
|
373 |
|
|
* instance of the declaring class of this field
|
374 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
375 |
|
|
* requires an instance
|
376 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
377 |
|
|
* class initialization, which then failed
|
378 |
|
|
* @see #get(Object)
|
379 |
|
|
*/
|
380 |
|
|
public int getInt(Object obj)
|
381 |
|
|
throws IllegalAccessException
|
382 |
|
|
{
|
383 |
|
|
return getInt(null, obj);
|
384 |
|
|
}
|
385 |
|
|
|
386 |
|
|
/**
|
387 |
|
|
* Get the value of this Field as a long. If the field is static,
|
388 |
|
|
* <code>o</code> will be ignored.
|
389 |
|
|
*
|
390 |
|
|
* @param o the object to get the value of this Field from
|
391 |
|
|
* @return the value of the Field
|
392 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
393 |
|
|
* (i.e. it is not public)
|
394 |
|
|
* @throws IllegalArgumentException if this is not a byte, short, char, int,
|
395 |
|
|
* or long field of <code>o</code>, or if <code>o</code> is not an
|
396 |
|
|
* instance of the declaring class of this field
|
397 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
398 |
|
|
* requires an instance
|
399 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
400 |
|
|
* class initialization, which then failed
|
401 |
|
|
* @see #get(Object)
|
402 |
|
|
*/
|
403 |
|
|
public long getLong(Object obj)
|
404 |
|
|
throws IllegalAccessException
|
405 |
|
|
{
|
406 |
|
|
return getLong(null, obj);
|
407 |
|
|
}
|
408 |
|
|
|
409 |
|
|
/**
|
410 |
|
|
* Get the value of this Field as a float. If the field is static,
|
411 |
|
|
* <code>o</code> will be ignored.
|
412 |
|
|
*
|
413 |
|
|
* @param o the object to get the value of this Field from
|
414 |
|
|
* @return the value of the Field
|
415 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
416 |
|
|
* (i.e. it is not public)
|
417 |
|
|
* @throws IllegalArgumentException if this is not a byte, short, char, int,
|
418 |
|
|
* long, or float field of <code>o</code>, or if <code>o</code> is
|
419 |
|
|
* not an instance of the declaring class of this field
|
420 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
421 |
|
|
* requires an instance
|
422 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
423 |
|
|
* class initialization, which then failed
|
424 |
|
|
* @see #get(Object)
|
425 |
|
|
*/
|
426 |
|
|
public float getFloat(Object obj)
|
427 |
|
|
throws IllegalAccessException
|
428 |
|
|
{
|
429 |
|
|
return getFloat(null, obj);
|
430 |
|
|
}
|
431 |
|
|
|
432 |
|
|
/**
|
433 |
|
|
* Get the value of this Field as a double. If the field is static,
|
434 |
|
|
* <code>o</code> will be ignored.
|
435 |
|
|
*
|
436 |
|
|
* @param o the object to get the value of this Field from
|
437 |
|
|
* @return the value of the Field
|
438 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
439 |
|
|
* (i.e. it is not public)
|
440 |
|
|
* @throws IllegalArgumentException if this is not a byte, short, char, int,
|
441 |
|
|
* long, float, or double field of <code>o</code>, or if
|
442 |
|
|
* <code>o</code> is not an instance of the declaring class of this
|
443 |
|
|
* field
|
444 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
445 |
|
|
* requires an instance
|
446 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
447 |
|
|
* class initialization, which then failed
|
448 |
|
|
* @see #get(Object)
|
449 |
|
|
*/
|
450 |
|
|
public double getDouble(Object obj)
|
451 |
|
|
throws IllegalAccessException
|
452 |
|
|
{
|
453 |
|
|
return getDouble(null, obj);
|
454 |
|
|
}
|
455 |
|
|
|
456 |
|
|
private native boolean getBoolean (Class caller, Object obj)
|
457 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
458 |
|
|
|
459 |
|
|
private native char getChar (Class caller, Object obj)
|
460 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
461 |
|
|
|
462 |
|
|
private native byte getByte (Class caller, Object obj)
|
463 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
464 |
|
|
|
465 |
|
|
private native short getShort (Class caller, Object obj)
|
466 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
467 |
|
|
|
468 |
|
|
private native int getInt (Class caller, Object obj)
|
469 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
470 |
|
|
|
471 |
|
|
private native long getLong (Class caller, Object obj)
|
472 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
473 |
|
|
|
474 |
|
|
private native float getFloat (Class caller, Object obj)
|
475 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
476 |
|
|
|
477 |
|
|
private native double getDouble (Class caller, Object obj)
|
478 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
479 |
|
|
|
480 |
|
|
private native Object get (Class caller, Object obj)
|
481 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
482 |
|
|
|
483 |
|
|
/**
|
484 |
|
|
* Set the value of this Field. If it is a primitive field, the value
|
485 |
|
|
* will be unwrapped from the passed object (boolean = java.lang.Boolean).<p>
|
486 |
|
|
*
|
487 |
|
|
* If the field is static, <code>o</code> will be ignored. Otherwise, if
|
488 |
|
|
* <code>o</code> is null, you get a <code>NullPointerException</code>,
|
489 |
|
|
* and if it is incompatible with the declaring class of the field, you
|
490 |
|
|
* get an <code>IllegalArgumentException</code>.<p>
|
491 |
|
|
*
|
492 |
|
|
* Next, if this Field enforces access control, your runtime context is
|
493 |
|
|
* evaluated, and you may have an <code>IllegalAccessException</code> if
|
494 |
|
|
* you could not access this field in similar compiled code. This also
|
495 |
|
|
* occurs whether or not there is access control if the field is final.
|
496 |
|
|
* If the field is primitive, and unwrapping your argument fails, you will
|
497 |
|
|
* get an <code>IllegalArgumentException</code>; likewise, this error
|
498 |
|
|
* happens if <code>value</code> cannot be cast to the correct object type.
|
499 |
|
|
* If the field is static, and its class is uninitialized, you trigger class
|
500 |
|
|
* initialization, which may end in a
|
501 |
|
|
* <code>ExceptionInInitializerError</code>.<p>
|
502 |
|
|
*
|
503 |
|
|
* Finally, the field is set with the widened value. This method accesses
|
504 |
|
|
* the field of the declaring class, even if the instance passed in belongs
|
505 |
|
|
* to a subclass which declares another field to hide this one.
|
506 |
|
|
*
|
507 |
|
|
* @param o the object to set this Field on
|
508 |
|
|
* @param value the value to set this Field to
|
509 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
510 |
|
|
* (i.e. it is not public)
|
511 |
|
|
* @throws IllegalArgumentException if <code>value</code> cannot be
|
512 |
|
|
* converted by a widening conversion to the underlying type of
|
513 |
|
|
* the Field, or if <code>o</code> is not an instance of the class
|
514 |
|
|
* declaring this field
|
515 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
516 |
|
|
* requires an instance
|
517 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
518 |
|
|
* class initialization, which then failed
|
519 |
|
|
* @see #setBoolean(Object, boolean)
|
520 |
|
|
* @see #setByte(Object, byte)
|
521 |
|
|
* @see #setChar(Object, char)
|
522 |
|
|
* @see #setShort(Object, short)
|
523 |
|
|
* @see #setInt(Object, int)
|
524 |
|
|
* @see #setLong(Object, long)
|
525 |
|
|
* @see #setFloat(Object, float)
|
526 |
|
|
* @see #setDouble(Object, double)
|
527 |
|
|
*/
|
528 |
|
|
public void set(Object object, Object value)
|
529 |
|
|
throws IllegalAccessException
|
530 |
|
|
{
|
531 |
|
|
set(null, object, value);
|
532 |
|
|
}
|
533 |
|
|
|
534 |
|
|
/**
|
535 |
|
|
* Set this boolean Field. If the field is static, <code>o</code> will be
|
536 |
|
|
* ignored.
|
537 |
|
|
*
|
538 |
|
|
* @param o the object to set this Field on
|
539 |
|
|
* @param value the value to set this Field to
|
540 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
541 |
|
|
* (i.e. it is not public)
|
542 |
|
|
* @throws IllegalArgumentException if this is not a boolean field, or if
|
543 |
|
|
* <code>o</code> is not an instance of the class declaring this
|
544 |
|
|
* field
|
545 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
546 |
|
|
* requires an instance
|
547 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
548 |
|
|
* class initialization, which then failed
|
549 |
|
|
* @see #set(Object, Object)
|
550 |
|
|
*/
|
551 |
|
|
public void setBoolean(Object obj, boolean b)
|
552 |
|
|
throws IllegalAccessException
|
553 |
|
|
{
|
554 |
|
|
setBoolean(null, obj, b, true);
|
555 |
|
|
}
|
556 |
|
|
|
557 |
|
|
/**
|
558 |
|
|
* Set this byte Field. If the field is static, <code>o</code> will be
|
559 |
|
|
* ignored.
|
560 |
|
|
*
|
561 |
|
|
* @param o the object to set this Field on
|
562 |
|
|
* @param value the value to set this Field to
|
563 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
564 |
|
|
* (i.e. it is not public)
|
565 |
|
|
* @throws IllegalArgumentException if this is not a byte, short, int, long,
|
566 |
|
|
* float, or double field, or if <code>o</code> is not an instance
|
567 |
|
|
* of the class declaring this field
|
568 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
569 |
|
|
* requires an instance
|
570 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
571 |
|
|
* class initialization, which then failed
|
572 |
|
|
* @see #set(Object, Object)
|
573 |
|
|
*/
|
574 |
|
|
public void setByte(Object obj, byte b)
|
575 |
|
|
throws IllegalAccessException
|
576 |
|
|
{
|
577 |
|
|
setByte(null, obj, b, true);
|
578 |
|
|
}
|
579 |
|
|
|
580 |
|
|
/**
|
581 |
|
|
* Set this char Field. If the field is static, <code>o</code> will be
|
582 |
|
|
* ignored.
|
583 |
|
|
*
|
584 |
|
|
* @param o the object to set this Field on
|
585 |
|
|
* @param value the value to set this Field to
|
586 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
587 |
|
|
* (i.e. it is not public)
|
588 |
|
|
* @throws IllegalArgumentException if this is not a char, int, long,
|
589 |
|
|
* float, or double field, or if <code>o</code> is not an instance
|
590 |
|
|
* of the class declaring this field
|
591 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
592 |
|
|
* requires an instance
|
593 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
594 |
|
|
* class initialization, which then failed
|
595 |
|
|
* @see #set(Object, Object)
|
596 |
|
|
*/
|
597 |
|
|
public void setChar(Object obj, char c)
|
598 |
|
|
throws IllegalAccessException
|
599 |
|
|
{
|
600 |
|
|
setChar(null, obj, c, true);
|
601 |
|
|
}
|
602 |
|
|
|
603 |
|
|
/**
|
604 |
|
|
* Set this short Field. If the field is static, <code>o</code> will be
|
605 |
|
|
* ignored.
|
606 |
|
|
*
|
607 |
|
|
* @param o the object to set this Field on
|
608 |
|
|
* @param value the value to set this Field to
|
609 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
610 |
|
|
* (i.e. it is not public)
|
611 |
|
|
* @throws IllegalArgumentException if this is not a short, int, long,
|
612 |
|
|
* float, or double field, or if <code>o</code> is not an instance
|
613 |
|
|
* of the class declaring this field
|
614 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
615 |
|
|
* requires an instance
|
616 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
617 |
|
|
* class initialization, which then failed
|
618 |
|
|
* @see #set(Object, Object)
|
619 |
|
|
*/
|
620 |
|
|
public void setShort(Object obj, short s)
|
621 |
|
|
throws IllegalAccessException
|
622 |
|
|
{
|
623 |
|
|
setShort(null, obj, s, true);
|
624 |
|
|
}
|
625 |
|
|
|
626 |
|
|
/**
|
627 |
|
|
* Set this int Field. If the field is static, <code>o</code> will be
|
628 |
|
|
* ignored.
|
629 |
|
|
*
|
630 |
|
|
* @param o the object to set this Field on
|
631 |
|
|
* @param value the value to set this Field to
|
632 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
633 |
|
|
* (i.e. it is not public)
|
634 |
|
|
* @throws IllegalArgumentException if this is not an int, long, float, or
|
635 |
|
|
* double field, or if <code>o</code> is not an instance of the
|
636 |
|
|
* class declaring this field
|
637 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
638 |
|
|
* requires an instance
|
639 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
640 |
|
|
* class initialization, which then failed
|
641 |
|
|
* @see #set(Object, Object)
|
642 |
|
|
*/
|
643 |
|
|
public void setInt(Object obj, int i)
|
644 |
|
|
throws IllegalAccessException
|
645 |
|
|
{
|
646 |
|
|
setInt(null, obj, i, true);
|
647 |
|
|
}
|
648 |
|
|
|
649 |
|
|
/**
|
650 |
|
|
* Set this long Field. If the field is static, <code>o</code> will be
|
651 |
|
|
* ignored.
|
652 |
|
|
*
|
653 |
|
|
* @param o the object to set this Field on
|
654 |
|
|
* @param value the value to set this Field to
|
655 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
656 |
|
|
* (i.e. it is not public)
|
657 |
|
|
* @throws IllegalArgumentException if this is not a long, float, or double
|
658 |
|
|
* field, or if <code>o</code> is not an instance of the class
|
659 |
|
|
* declaring this field
|
660 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
661 |
|
|
* requires an instance
|
662 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
663 |
|
|
* class initialization, which then failed
|
664 |
|
|
* @see #set(Object, Object)
|
665 |
|
|
*/
|
666 |
|
|
public void setLong(Object obj, long l)
|
667 |
|
|
throws IllegalArgumentException, IllegalAccessException
|
668 |
|
|
{
|
669 |
|
|
setLong(null, obj, l, true);
|
670 |
|
|
}
|
671 |
|
|
|
672 |
|
|
/**
|
673 |
|
|
* Set this float Field. If the field is static, <code>o</code> will be
|
674 |
|
|
* ignored.
|
675 |
|
|
*
|
676 |
|
|
* @param o the object to set this Field on
|
677 |
|
|
* @param value the value to set this Field to
|
678 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
679 |
|
|
* (i.e. it is not public)
|
680 |
|
|
* @throws IllegalArgumentException if this is not a float or long field, or
|
681 |
|
|
* if <code>o</code> is not an instance of the class declaring this
|
682 |
|
|
* field
|
683 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
684 |
|
|
* requires an instance
|
685 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
686 |
|
|
* class initialization, which then failed
|
687 |
|
|
* @see #set(Object, Object)
|
688 |
|
|
*/
|
689 |
|
|
public void setFloat(Object obj, float f)
|
690 |
|
|
throws IllegalAccessException
|
691 |
|
|
{
|
692 |
|
|
setFloat(null, obj, f, true);
|
693 |
|
|
}
|
694 |
|
|
|
695 |
|
|
/**
|
696 |
|
|
* Set this double Field. If the field is static, <code>o</code> will be
|
697 |
|
|
* ignored.
|
698 |
|
|
*
|
699 |
|
|
* @param o the object to set this Field on
|
700 |
|
|
* @param value the value to set this Field to
|
701 |
|
|
* @throws IllegalAccessException if you could not normally access this field
|
702 |
|
|
* (i.e. it is not public)
|
703 |
|
|
* @throws IllegalArgumentException if this is not a double field, or if
|
704 |
|
|
* <code>o</code> is not an instance of the class declaring this
|
705 |
|
|
* field
|
706 |
|
|
* @throws NullPointerException if <code>o</code> is null and this field
|
707 |
|
|
* requires an instance
|
708 |
|
|
* @throws ExceptionInInitializerError if accessing a static field triggered
|
709 |
|
|
* class initialization, which then failed
|
710 |
|
|
* @see #set(Object, Object)
|
711 |
|
|
*/
|
712 |
|
|
public void setDouble(Object obj, double d)
|
713 |
|
|
throws IllegalAccessException
|
714 |
|
|
{
|
715 |
|
|
setDouble(null, obj, d, true);
|
716 |
|
|
}
|
717 |
|
|
|
718 |
|
|
/**
|
719 |
|
|
* Return the generic type of the field. If the field type is not a generic
|
720 |
|
|
* type, the method returns the same as <code>getType()</code>.
|
721 |
|
|
*
|
722 |
|
|
* @throws GenericSignatureFormatError if the generic signature does
|
723 |
|
|
* not conform to the format specified in the Virtual Machine
|
724 |
|
|
* specification, version 3.
|
725 |
|
|
* @since 1.5
|
726 |
|
|
*/
|
727 |
|
|
public Type getGenericType()
|
728 |
|
|
{
|
729 |
|
|
String signature = getSignature();
|
730 |
|
|
if (signature == null)
|
731 |
|
|
return getType();
|
732 |
|
|
FieldSignatureParser p = new FieldSignatureParser(getDeclaringClass(),
|
733 |
|
|
signature);
|
734 |
|
|
return p.getFieldType();
|
735 |
|
|
}
|
736 |
|
|
|
737 |
|
|
public <T extends Annotation> T getAnnotation(Class<T> annoClass)
|
738 |
|
|
{
|
739 |
|
|
Annotation[] annos = getDeclaredAnnotations();
|
740 |
|
|
for (int i = 0; i < annos.length; ++i)
|
741 |
|
|
if (annos[i].annotationType() == annoClass)
|
742 |
|
|
return (T) annos[i];
|
743 |
|
|
return null;
|
744 |
|
|
}
|
745 |
|
|
|
746 |
|
|
public Annotation[] getDeclaredAnnotations()
|
747 |
|
|
{
|
748 |
|
|
Annotation[] result = getDeclaredAnnotationsInternal();
|
749 |
|
|
if (result == null)
|
750 |
|
|
result = new Annotation[0];
|
751 |
|
|
return result;
|
752 |
|
|
}
|
753 |
|
|
|
754 |
|
|
private native Annotation[] getDeclaredAnnotationsInternal();
|
755 |
|
|
|
756 |
|
|
/**
|
757 |
|
|
* Return the String in the Signature attribute for this field. If there
|
758 |
|
|
* is no Signature attribute, return null.
|
759 |
|
|
*/
|
760 |
|
|
private native String getSignature();
|
761 |
|
|
|
762 |
|
|
native void setByte (Class caller, Object obj, byte b, boolean checkFinal)
|
763 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
764 |
|
|
|
765 |
|
|
native void setShort (Class caller, Object obj, short s, boolean checkFinal)
|
766 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
767 |
|
|
|
768 |
|
|
native void setInt (Class caller, Object obj, int i, boolean checkFinal)
|
769 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
770 |
|
|
|
771 |
|
|
native void setLong (Class caller, Object obj, long l, boolean checkFinal)
|
772 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
773 |
|
|
|
774 |
|
|
native void setFloat (Class caller, Object obj, float f, boolean checkFinal)
|
775 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
776 |
|
|
|
777 |
|
|
native void setDouble (Class caller, Object obj, double d,
|
778 |
|
|
boolean checkFinal)
|
779 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
780 |
|
|
|
781 |
|
|
native void setChar (Class caller, Object obj, char c, boolean checkFinal)
|
782 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
783 |
|
|
|
784 |
|
|
native void setBoolean (Class caller, Object obj, boolean b,
|
785 |
|
|
boolean checkFinal)
|
786 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
787 |
|
|
|
788 |
|
|
native void set (Class caller, Object obj, Object val, Class type,
|
789 |
|
|
boolean checkFinal)
|
790 |
|
|
throws IllegalArgumentException, IllegalAccessException;
|
791 |
|
|
|
792 |
|
|
private void set (Class caller, Object object, Object value)
|
793 |
|
|
throws IllegalArgumentException, IllegalAccessException
|
794 |
|
|
{
|
795 |
|
|
Class type = getType();
|
796 |
|
|
if (! type.isPrimitive())
|
797 |
|
|
set(caller, object, value, type, true);
|
798 |
|
|
else if (value instanceof Byte)
|
799 |
|
|
setByte(caller, object, ((Byte) value).byteValue(), true);
|
800 |
|
|
else if (value instanceof Short)
|
801 |
|
|
setShort (caller, object, ((Short) value).shortValue(), true);
|
802 |
|
|
else if (value instanceof Integer)
|
803 |
|
|
setInt(caller, object, ((Integer) value).intValue(), true);
|
804 |
|
|
else if (value instanceof Long)
|
805 |
|
|
setLong(caller, object, ((Long) value).longValue(), true);
|
806 |
|
|
else if (value instanceof Float)
|
807 |
|
|
setFloat(caller, object, ((Float) value).floatValue(), true);
|
808 |
|
|
else if (value instanceof Double)
|
809 |
|
|
setDouble(caller, object, ((Double) value).doubleValue(), true);
|
810 |
|
|
else if (value instanceof Character)
|
811 |
|
|
setChar(caller, object, ((Character) value).charValue(), true);
|
812 |
|
|
else if (value instanceof Boolean)
|
813 |
|
|
setBoolean(caller, object, ((Boolean) value).booleanValue(), true);
|
814 |
|
|
else
|
815 |
|
|
throw new IllegalArgumentException();
|
816 |
|
|
}
|
817 |
|
|
}
|