1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  * Copyright (c) 1996, 2013, Oracle and/or its affiliates. All rights reserved.
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This code is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 only, as
8  * published by the Free Software Foundation.  Oracle designates this
9  * particular file as subject to the "Classpath" exception as provided
10  * by Oracle in the LICENSE file that accompanied this code.
11  *
12  * This code is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15  * version 2 for more details (a copy is included in the LICENSE file that
16  * accompanied this code).
17  *
18  * You should have received a copy of the GNU General Public License version
19  * 2 along with this work; if not, write to the Free Software Foundation,
20  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21  *
22  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
23  * or visit www.oracle.com if you need additional information or have any
24  * questions.
25  */
26 
27 package java.lang.reflect;
28 
29 import dalvik.annotation.optimization.FastNative;
30 import sun.reflect.CallerSensitive;
31 import java.lang.annotation.Annotation;
32 import java.util.Objects;
33 import libcore.reflect.AnnotatedElements;
34 import libcore.reflect.GenericSignatureParser;
35 
36 
37 /**
38  * A {@code Field} provides information about, and dynamic access to, a
39  * single field of a class or an interface.  The reflected field may
40  * be a class (static) field or an instance field.
41  *
42  * <p>A {@code Field} permits widening conversions to occur during a get or
43  * set access operation, but throws an {@code IllegalArgumentException} if a
44  * narrowing conversion would occur.
45  *
46  * @see Member
47  * @see java.lang.Class
48  * @see java.lang.Class#getFields()
49  * @see java.lang.Class#getField(String)
50  * @see java.lang.Class#getDeclaredFields()
51  * @see java.lang.Class#getDeclaredField(String)
52  *
53  * @author Kenneth Russell
54  * @author Nakul Saraiya
55  */
56 public final
57 class Field extends AccessibleObject implements Member {
58     // Android-changed: Extensive modifications made throughout the class for ART.
59     // Android-changed: Many fields and methods removed / modified.
60     // Android-removed: Type annotations runtime code. Not supported on Android.
61 
62     private int accessFlags;
63     private Class<?> declaringClass;
64     private int artFieldIndex;
65     private int offset;
66     private Class<?> type;
67 
Field()68     private Field() {
69     }
70 
71     /**
72      * Returns the {@code Class} object representing the class or interface
73      * that declares the field represented by this {@code Field} object.
74      */
getDeclaringClass()75     public Class<?> getDeclaringClass() {
76         // Android-changed: Adjust code for different field names.
77         return declaringClass;
78     }
79 
80     /**
81      * Returns the name of the field represented by this {@code Field} object.
82      */
getName()83     public String getName() {
84         // Android-changed: getName() implemented differently.
85         if (declaringClass.isProxy()) {
86             // Proxy classes have 1 synthesized static field with no valid dex index.
87             if ((getModifiers() & Modifier.STATIC) == 0) {
88                 throw new AssertionError("Invalid modifiers for proxy field: " + getModifiers());
89             }
90             // Only 2 fields are present on proxy classes.
91             switch (artFieldIndex) {
92                 case 0: return "interfaces";
93                 case 1: return "throws";
94                 default: throw new AssertionError("Invalid index for proxy: " + artFieldIndex);
95             }
96         }
97 
98         return getNameInternal();
99     }
100 
101     // Android-added: getName() implemented differently.
102     @FastNative
getNameInternal()103     private native String getNameInternal();
104 
105     /**
106      * Returns the Java language modifiers for the field represented
107      * by this {@code Field} object, as an integer. The {@code Modifier} class should
108      * be used to decode the modifiers.
109      *
110      * @see Modifier
111      */
getModifiers()112     public int getModifiers() {
113         // Android-changed: Adjust getModifiers() implementation to mask extra bits used on Android.
114         return accessFlags & 0xffff;  // mask out bits not used by Java
115     }
116 
117     /**
118      * Returns {@code true} if this field represents an element of
119      * an enumerated type; returns {@code false} otherwise.
120      *
121      * @return {@code true} if and only if this field represents an element of
122      * an enumerated type.
123      * @since 1.5
124      */
isEnumConstant()125     public boolean isEnumConstant() {
126         return (getModifiers() & Modifier.ENUM) != 0;
127     }
128 
129     /**
130      * Returns {@code true} if this field is a synthetic
131      * field; returns {@code false} otherwise.
132      *
133      * @return true if and only if this field is a synthetic
134      * field as defined by the Java Language Specification.
135      * @since 1.5
136      */
isSynthetic()137     public boolean isSynthetic() {
138         return Modifier.isSynthetic(getModifiers());
139     }
140 
141     /**
142      * Returns a {@code Class} object that identifies the
143      * declared type for the field represented by this
144      * {@code Field} object.
145      *
146      * @return a {@code Class} object identifying the declared
147      * type of the field represented by this object
148      */
getType()149     public Class<?> getType() {
150         return type;
151     }
152 
153     /**
154      * Returns a {@code Type} object that represents the declared type for
155      * the field represented by this {@code Field} object.
156      *
157      * <p>If the {@code Type} is a parameterized type, the
158      * {@code Type} object returned must accurately reflect the
159      * actual type parameters used in the source code.
160      *
161      * <p>If the type of the underlying field is a type variable or a
162      * parameterized type, it is created. Otherwise, it is resolved.
163      *
164      * @return a {@code Type} object that represents the declared type for
165      *     the field represented by this {@code Field} object
166      * @throws GenericSignatureFormatError if the generic field
167      *     signature does not conform to the format specified in
168      *     <cite>The Java&trade; Virtual Machine Specification</cite>
169      * @throws TypeNotPresentException if the generic type
170      *     signature of the underlying field refers to a non-existent
171      *     type declaration
172      * @throws MalformedParameterizedTypeException if the generic
173      *     signature of the underlying field refers to a parameterized type
174      *     that cannot be instantiated for any reason
175      * @since 1.5
176      */
getGenericType()177     public Type getGenericType() {
178         // Android-changed: getGenericType() implemented differently.
179         String signatureAttribute = getSignatureAttribute();
180         ClassLoader cl = declaringClass.getClassLoader();
181         GenericSignatureParser parser = new GenericSignatureParser(cl);
182         parser.parseForField(declaringClass, signatureAttribute);
183         Type genericType = parser.fieldType;
184         if (genericType == null) {
185             genericType = getType();
186         }
187         return genericType;
188     }
189 
190     // BEGIN Android-added: getGenericType() implemented differently.
getSignatureAttribute()191     String getSignatureAttribute() {
192         String[] annotation = getSignatureAnnotation();
193         if (annotation == null) {
194             return null;
195         }
196         StringBuilder result = new StringBuilder();
197         for (String s : annotation) {
198             result.append(s);
199         }
200         return result.toString();
201     }
202     @FastNative
getSignatureAnnotation()203     private native String[] getSignatureAnnotation();
204     // END Android-added: getGenericType() implemented differently.
205 
206 
207     /**
208      * Compares this {@code Field} against the specified object.  Returns
209      * true if the objects are the same.  Two {@code Field} objects are the same if
210      * they were declared by the same class and have the same name
211      * and type.
212      */
equals(Object obj)213     public boolean equals(Object obj) {
214         if (obj != null && obj instanceof Field) {
215             Field other = (Field)obj;
216             return (getDeclaringClass() == other.getDeclaringClass())
217                 && (getName() == other.getName())
218                 && (getType() == other.getType());
219         }
220         return false;
221     }
222 
223     /**
224      * Returns a hashcode for this {@code Field}.  This is computed as the
225      * exclusive-or of the hashcodes for the underlying field's
226      * declaring class name and its name.
227      */
hashCode()228     public int hashCode() {
229         return getDeclaringClass().getName().hashCode() ^ getName().hashCode();
230     }
231 
232     /**
233      * Returns a string describing this {@code Field}.  The format is
234      * the access modifiers for the field, if any, followed
235      * by the field type, followed by a space, followed by
236      * the fully-qualified name of the class declaring the field,
237      * followed by a period, followed by the name of the field.
238      * For example:
239      * <pre>
240      *    public static final int java.lang.Thread.MIN_PRIORITY
241      *    private int java.io.FileDescriptor.fd
242      * </pre>
243      *
244      * <p>The modifiers are placed in canonical order as specified by
245      * "The Java Language Specification".  This is {@code public},
246      * {@code protected} or {@code private} first, and then other
247      * modifiers in the following order: {@code static}, {@code final},
248      * {@code transient}, {@code volatile}.
249      *
250      * @return a string describing this {@code Field}
251      * @jls 8.3.1 Field Modifiers
252      */
toString()253     public String toString() {
254         int mod = getModifiers();
255         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
256             + getType().getTypeName() + " "
257             + getDeclaringClass().getTypeName() + "."
258             + getName());
259     }
260 
261     /**
262      * Returns a string describing this {@code Field}, including
263      * its generic type.  The format is the access modifiers for the
264      * field, if any, followed by the generic field type, followed by
265      * a space, followed by the fully-qualified name of the class
266      * declaring the field, followed by a period, followed by the name
267      * of the field.
268      *
269      * <p>The modifiers are placed in canonical order as specified by
270      * "The Java Language Specification".  This is {@code public},
271      * {@code protected} or {@code private} first, and then other
272      * modifiers in the following order: {@code static}, {@code final},
273      * {@code transient}, {@code volatile}.
274      *
275      * @return a string describing this {@code Field}, including
276      * its generic type
277      *
278      * @since 1.5
279      * @jls 8.3.1 Field Modifiers
280      */
toGenericString()281     public String toGenericString() {
282         int mod = getModifiers();
283         Type fieldType = getGenericType();
284         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
285             + fieldType.getTypeName() + " "
286             + getDeclaringClass().getTypeName() + "."
287             + getName());
288     }
289 
290     /**
291      * Returns the value of the field represented by this {@code Field}, on
292      * the specified object. The value is automatically wrapped in an
293      * object if it has a primitive type.
294      *
295      * <p>The underlying field's value is obtained as follows:
296      *
297      * <p>If the underlying field is a static field, the {@code obj} argument
298      * is ignored; it may be null.
299      *
300      * <p>Otherwise, the underlying field is an instance field.  If the
301      * specified {@code obj} argument is null, the method throws a
302      * {@code NullPointerException}. If the specified object is not an
303      * instance of the class or interface declaring the underlying
304      * field, the method throws an {@code IllegalArgumentException}.
305      *
306      * <p>If this {@code Field} object is enforcing Java language access control, and
307      * the underlying field is inaccessible, the method throws an
308      * {@code IllegalAccessException}.
309      * If the underlying field is static, the class that declared the
310      * field is initialized if it has not already been initialized.
311      *
312      * <p>Otherwise, the value is retrieved from the underlying instance
313      * or static field.  If the field has a primitive type, the value
314      * is wrapped in an object before being returned, otherwise it is
315      * returned as is.
316      *
317      * <p>If the field is hidden in the type of {@code obj},
318      * the field's value is obtained according to the preceding rules.
319      *
320      * @param obj object from which the represented field's value is
321      * to be extracted
322      * @return the value of the represented field in object
323      * {@code obj}; primitive values are wrapped in an appropriate
324      * object before being returned
325      *
326      * @exception IllegalAccessException    if this {@code Field} object
327      *              is enforcing Java language access control and the underlying
328      *              field is inaccessible.
329      * @exception IllegalArgumentException  if the specified object is not an
330      *              instance of the class or interface declaring the underlying
331      *              field (or a subclass or implementor thereof).
332      * @exception NullPointerException      if the specified object is null
333      *              and the field is an instance field.
334      * @exception ExceptionInInitializerError if the initialization provoked
335      *              by this method fails.
336      */
337     @CallerSensitive
338     // Android-changed: get*(Object) implemented natively.
339     @FastNative
get(Object obj)340     public native Object get(Object obj)
341         throws IllegalArgumentException, IllegalAccessException;
342 
343     /**
344      * Gets the value of a static or instance {@code boolean} field.
345      *
346      * @param obj the object to extract the {@code boolean} value
347      * from
348      * @return the value of the {@code boolean} field
349      *
350      * @exception IllegalAccessException    if this {@code Field} object
351      *              is enforcing Java language access control and the underlying
352      *              field is inaccessible.
353      * @exception IllegalArgumentException  if the specified object is not
354      *              an instance of the class or interface declaring the
355      *              underlying field (or a subclass or implementor
356      *              thereof), or if the field value cannot be
357      *              converted to the type {@code boolean} by a
358      *              widening conversion.
359      * @exception NullPointerException      if the specified object is null
360      *              and the field is an instance field.
361      * @exception ExceptionInInitializerError if the initialization provoked
362      *              by this method fails.
363      * @see       Field#get
364      */
365     @CallerSensitive
366     // Android-changed: get*(Object) implemented natively.
367     @FastNative
getBoolean(Object obj)368     public native boolean getBoolean(Object obj)
369         throws IllegalArgumentException, IllegalAccessException;
370 
371     /**
372      * Gets the value of a static or instance {@code byte} field.
373      *
374      * @param obj the object to extract the {@code byte} value
375      * from
376      * @return the value of the {@code byte} field
377      *
378      * @exception IllegalAccessException    if this {@code Field} object
379      *              is enforcing Java language access control and the underlying
380      *              field is inaccessible.
381      * @exception IllegalArgumentException  if the specified object is not
382      *              an instance of the class or interface declaring the
383      *              underlying field (or a subclass or implementor
384      *              thereof), or if the field value cannot be
385      *              converted to the type {@code byte} by a
386      *              widening conversion.
387      * @exception NullPointerException      if the specified object is null
388      *              and the field is an instance field.
389      * @exception ExceptionInInitializerError if the initialization provoked
390      *              by this method fails.
391      * @see       Field#get
392      */
393     @CallerSensitive
394     // Android-changed: get*(Object) implemented natively.
395     @FastNative
getByte(Object obj)396     public native byte getByte(Object obj)
397         throws IllegalArgumentException, IllegalAccessException;
398 
399     /**
400      * Gets the value of a static or instance field of type
401      * {@code char} or of another primitive type convertible to
402      * type {@code char} via a widening conversion.
403      *
404      * @param obj the object to extract the {@code char} value
405      * from
406      * @return the value of the field converted to type {@code char}
407      *
408      * @exception IllegalAccessException    if this {@code Field} object
409      *              is enforcing Java language access control and the underlying
410      *              field is inaccessible.
411      * @exception IllegalArgumentException  if the specified object is not
412      *              an instance of the class or interface declaring the
413      *              underlying field (or a subclass or implementor
414      *              thereof), or if the field value cannot be
415      *              converted to the type {@code char} by a
416      *              widening conversion.
417      * @exception NullPointerException      if the specified object is null
418      *              and the field is an instance field.
419      * @exception ExceptionInInitializerError if the initialization provoked
420      *              by this method fails.
421      * @see Field#get
422      */
423     @CallerSensitive
424     // Android-changed: get*(Object) implemented natively.
425     @FastNative
getChar(Object obj)426     public native char getChar(Object obj)
427         throws IllegalArgumentException, IllegalAccessException;
428 
429     /**
430      * Gets the value of a static or instance field of type
431      * {@code short} or of another primitive type convertible to
432      * type {@code short} via a widening conversion.
433      *
434      * @param obj the object to extract the {@code short} value
435      * from
436      * @return the value of the field converted to type {@code short}
437      *
438      * @exception IllegalAccessException    if this {@code Field} object
439      *              is enforcing Java language access control and the underlying
440      *              field is inaccessible.
441      * @exception IllegalArgumentException  if the specified object is not
442      *              an instance of the class or interface declaring the
443      *              underlying field (or a subclass or implementor
444      *              thereof), or if the field value cannot be
445      *              converted to the type {@code short} by a
446      *              widening conversion.
447      * @exception NullPointerException      if the specified object is null
448      *              and the field is an instance field.
449      * @exception ExceptionInInitializerError if the initialization provoked
450      *              by this method fails.
451      * @see       Field#get
452      */
453     @CallerSensitive
454     // Android-changed: get*(Object) implemented natively.
455     @FastNative
getShort(Object obj)456     public native short getShort(Object obj)
457         throws IllegalArgumentException, IllegalAccessException;
458 
459     /**
460      * Gets the value of a static or instance field of type
461      * {@code int} or of another primitive type convertible to
462      * type {@code int} via a widening conversion.
463      *
464      * @param obj the object to extract the {@code int} value
465      * from
466      * @return the value of the field converted to type {@code int}
467      *
468      * @exception IllegalAccessException    if this {@code Field} object
469      *              is enforcing Java language access control and the underlying
470      *              field is inaccessible.
471      * @exception IllegalArgumentException  if the specified object is not
472      *              an instance of the class or interface declaring the
473      *              underlying field (or a subclass or implementor
474      *              thereof), or if the field value cannot be
475      *              converted to the type {@code int} by a
476      *              widening conversion.
477      * @exception NullPointerException      if the specified object is null
478      *              and the field is an instance field.
479      * @exception ExceptionInInitializerError if the initialization provoked
480      *              by this method fails.
481      * @see       Field#get
482      */
483     @CallerSensitive
484     // Android-changed: get*(Object) implemented natively.
485     @FastNative
getInt(Object obj)486     public native int getInt(Object obj)
487         throws IllegalArgumentException, IllegalAccessException;
488 
489     /**
490      * Gets the value of a static or instance field of type
491      * {@code long} or of another primitive type convertible to
492      * type {@code long} via a widening conversion.
493      *
494      * @param obj the object to extract the {@code long} value
495      * from
496      * @return the value of the field converted to type {@code long}
497      *
498      * @exception IllegalAccessException    if this {@code Field} object
499      *              is enforcing Java language access control and the underlying
500      *              field is inaccessible.
501      * @exception IllegalArgumentException  if the specified object is not
502      *              an instance of the class or interface declaring the
503      *              underlying field (or a subclass or implementor
504      *              thereof), or if the field value cannot be
505      *              converted to the type {@code long} by a
506      *              widening conversion.
507      * @exception NullPointerException      if the specified object is null
508      *              and the field is an instance field.
509      * @exception ExceptionInInitializerError if the initialization provoked
510      *              by this method fails.
511      * @see       Field#get
512      */
513     @CallerSensitive
514     // Android-changed: get*(Object) implemented natively.
515     @FastNative
getLong(Object obj)516     public native long getLong(Object obj)
517         throws IllegalArgumentException, IllegalAccessException;
518 
519     /**
520      * Gets the value of a static or instance field of type
521      * {@code float} or of another primitive type convertible to
522      * type {@code float} via a widening conversion.
523      *
524      * @param obj the object to extract the {@code float} value
525      * from
526      * @return the value of the field converted to type {@code float}
527      *
528      * @exception IllegalAccessException    if this {@code Field} object
529      *              is enforcing Java language access control and the underlying
530      *              field is inaccessible.
531      * @exception IllegalArgumentException  if the specified object is not
532      *              an instance of the class or interface declaring the
533      *              underlying field (or a subclass or implementor
534      *              thereof), or if the field value cannot be
535      *              converted to the type {@code float} by a
536      *              widening conversion.
537      * @exception NullPointerException      if the specified object is null
538      *              and the field is an instance field.
539      * @exception ExceptionInInitializerError if the initialization provoked
540      *              by this method fails.
541      * @see Field#get
542      */
543     @CallerSensitive
544     // Android-changed: get*(Object) implemented natively.
545     @FastNative
getFloat(Object obj)546     public native float getFloat(Object obj)
547         throws IllegalArgumentException, IllegalAccessException;
548 
549     /**
550      * Gets the value of a static or instance field of type
551      * {@code double} or of another primitive type convertible to
552      * type {@code double} via a widening conversion.
553      *
554      * @param obj the object to extract the {@code double} value
555      * from
556      * @return the value of the field converted to type {@code double}
557      *
558      * @exception IllegalAccessException    if this {@code Field} object
559      *              is enforcing Java language access control and the underlying
560      *              field is inaccessible.
561      * @exception IllegalArgumentException  if the specified object is not
562      *              an instance of the class or interface declaring the
563      *              underlying field (or a subclass or implementor
564      *              thereof), or if the field value cannot be
565      *              converted to the type {@code double} by a
566      *              widening conversion.
567      * @exception NullPointerException      if the specified object is null
568      *              and the field is an instance field.
569      * @exception ExceptionInInitializerError if the initialization provoked
570      *              by this method fails.
571      * @see       Field#get
572      */
573     @CallerSensitive
574     // Android-changed: get*(Object) implemented natively.
575     @FastNative
getDouble(Object obj)576     public native double getDouble(Object obj)
577         throws IllegalArgumentException, IllegalAccessException;
578 
579     /**
580      * Sets the field represented by this {@code Field} object on the
581      * specified object argument to the specified new value. The new
582      * value is automatically unwrapped if the underlying field has a
583      * primitive type.
584      *
585      * <p>The operation proceeds as follows:
586      *
587      * <p>If the underlying field is static, the {@code obj} argument is
588      * ignored; it may be null.
589      *
590      * <p>Otherwise the underlying field is an instance field.  If the
591      * specified object argument is null, the method throws a
592      * {@code NullPointerException}.  If the specified object argument is not
593      * an instance of the class or interface declaring the underlying
594      * field, the method throws an {@code IllegalArgumentException}.
595      *
596      * <p>If this {@code Field} object is enforcing Java language access control, and
597      * the underlying field is inaccessible, the method throws an
598      * {@code IllegalAccessException}.
599      *
600      * <p>If the underlying field is final, this {@code Field} object has
601      * <em>write</em> access if and only if the following conditions are met:
602      * <ul>
603      * <li>{@link #setAccessible(boolean) setAccessible(true)} has succeeded for
604      *     this {@code Field} object;</li>
605      * <li>the field is non-static; and</li>
606      * <li>the field's declaring class is not a {@linkplain Class#isRecord()
607      *     record class}.</li>
608      * </ul>
609      * If any of the above checks is not met, this method throws an
610      * {@code IllegalAccessException}.
611      *
612      * <p> Setting a final field in this way
613      * is meaningful only during deserialization or reconstruction of
614      * instances of classes with blank final fields, before they are
615      * made available for access by other parts of a program. Use in
616      * any other context may have unpredictable effects, including cases
617      * in which other parts of a program continue to use the original
618      * value of this field.
619      *
620      * <p>If the underlying field is of a primitive type, an unwrapping
621      * conversion is attempted to convert the new value to a value of
622      * a primitive type.  If this attempt fails, the method throws an
623      * {@code IllegalArgumentException}.
624      *
625      * <p>If, after possible unwrapping, the new value cannot be
626      * converted to the type of the underlying field by an identity or
627      * widening conversion, the method throws an
628      * {@code IllegalArgumentException}.
629      *
630      * <p>If the underlying field is static, the class that declared the
631      * field is initialized if it has not already been initialized.
632      *
633      * <p>The field is set to the possibly unwrapped and widened new value.
634      *
635      * <p>If the field is hidden in the type of {@code obj},
636      * the field's value is set according to the preceding rules.
637      *
638      * @param obj the object whose field should be modified
639      * @param value the new value for the field of {@code obj}
640      * being modified
641      *
642      * @exception IllegalAccessException    if this {@code Field} object
643      *              is enforcing Java language access control and the underlying
644      *              field is either inaccessible or final.
645      * @exception IllegalArgumentException  if the specified object is not an
646      *              instance of the class or interface declaring the underlying
647      *              field (or a subclass or implementor thereof),
648      *              or if an unwrapping conversion fails.
649      * @exception NullPointerException      if the specified object is null
650      *              and the field is an instance field.
651      * @exception ExceptionInInitializerError if the initialization provoked
652      *              by this method fails.
653      */
654     @CallerSensitive
655     // Android-changed: set*(Object, ...) implemented natively.
656     @FastNative
set(Object obj, Object value)657     public native void set(Object obj, Object value)
658         throws IllegalArgumentException, IllegalAccessException;
659 
660     /**
661      * Sets the value of a field as a {@code boolean} on the specified object.
662      * This method is equivalent to
663      * {@code set(obj, zObj)},
664      * where {@code zObj} is a {@code Boolean} object and
665      * {@code zObj.booleanValue() == z}.
666      *
667      * @param obj the object whose field should be modified
668      * @param z   the new value for the field of {@code obj}
669      * being modified
670      *
671      * @exception IllegalAccessException    if this {@code Field} object
672      *              is enforcing Java language access control and the underlying
673      *              field is either inaccessible or final.
674      * @exception IllegalArgumentException  if the specified object is not an
675      *              instance of the class or interface declaring the underlying
676      *              field (or a subclass or implementor thereof),
677      *              or if an unwrapping conversion fails.
678      * @exception NullPointerException      if the specified object is null
679      *              and the field is an instance field.
680      * @exception ExceptionInInitializerError if the initialization provoked
681      *              by this method fails.
682      * @see       Field#set
683      */
684     @CallerSensitive
685     // Android-changed: set*(Object, ...) implemented natively.
686     @FastNative
setBoolean(Object obj, boolean z)687     public native void setBoolean(Object obj, boolean z)
688         throws IllegalArgumentException, IllegalAccessException;
689 
690     /**
691      * Sets the value of a field as a {@code byte} on the specified object.
692      * This method is equivalent to
693      * {@code set(obj, bObj)},
694      * where {@code bObj} is a {@code Byte} object and
695      * {@code bObj.byteValue() == b}.
696      *
697      * @param obj the object whose field should be modified
698      * @param b   the new value for the field of {@code obj}
699      * being modified
700      *
701      * @exception IllegalAccessException    if this {@code Field} object
702      *              is enforcing Java language access control and the underlying
703      *              field is either inaccessible or final.
704      * @exception IllegalArgumentException  if the specified object is not an
705      *              instance of the class or interface declaring the underlying
706      *              field (or a subclass or implementor thereof),
707      *              or if an unwrapping conversion fails.
708      * @exception NullPointerException      if the specified object is null
709      *              and the field is an instance field.
710      * @exception ExceptionInInitializerError if the initialization provoked
711      *              by this method fails.
712      * @see       Field#set
713      */
714     @CallerSensitive
715     // Android-changed: set*(Object, ...) implemented natively.
716     @FastNative
setByte(Object obj, byte b)717     public native void setByte(Object obj, byte b)
718         throws IllegalArgumentException, IllegalAccessException;
719 
720     /**
721      * Sets the value of a field as a {@code char} on the specified object.
722      * This method is equivalent to
723      * {@code set(obj, cObj)},
724      * where {@code cObj} is a {@code Character} object and
725      * {@code cObj.charValue() == c}.
726      *
727      * @param obj the object whose field should be modified
728      * @param c   the new value for the field of {@code obj}
729      * being modified
730      *
731      * @exception IllegalAccessException    if this {@code Field} object
732      *              is enforcing Java language access control and the underlying
733      *              field is either inaccessible or final.
734      * @exception IllegalArgumentException  if the specified object is not an
735      *              instance of the class or interface declaring the underlying
736      *              field (or a subclass or implementor thereof),
737      *              or if an unwrapping conversion fails.
738      * @exception NullPointerException      if the specified object is null
739      *              and the field is an instance field.
740      * @exception ExceptionInInitializerError if the initialization provoked
741      *              by this method fails.
742      * @see       Field#set
743      */
744     @CallerSensitive
745     // Android-changed: set*(Object, ...) implemented natively.
746     @FastNative
setChar(Object obj, char c)747     public native void setChar(Object obj, char c)
748         throws IllegalArgumentException, IllegalAccessException;
749 
750     /**
751      * Sets the value of a field as a {@code short} on the specified object.
752      * This method is equivalent to
753      * {@code set(obj, sObj)},
754      * where {@code sObj} is a {@code Short} object and
755      * {@code sObj.shortValue() == s}.
756      *
757      * @param obj the object whose field should be modified
758      * @param s   the new value for the field of {@code obj}
759      * being modified
760      *
761      * @exception IllegalAccessException    if this {@code Field} object
762      *              is enforcing Java language access control and the underlying
763      *              field is either inaccessible or final.
764      * @exception IllegalArgumentException  if the specified object is not an
765      *              instance of the class or interface declaring the underlying
766      *              field (or a subclass or implementor thereof),
767      *              or if an unwrapping conversion fails.
768      * @exception NullPointerException      if the specified object is null
769      *              and the field is an instance field.
770      * @exception ExceptionInInitializerError if the initialization provoked
771      *              by this method fails.
772      * @see       Field#set
773      */
774     @CallerSensitive
775     // Android-changed: set*(Object, ...) implemented natively.
776     @FastNative
setShort(Object obj, short s)777     public native void setShort(Object obj, short s)
778         throws IllegalArgumentException, IllegalAccessException;
779 
780     /**
781      * Sets the value of a field as an {@code int} on the specified object.
782      * This method is equivalent to
783      * {@code set(obj, iObj)},
784      * where {@code iObj} is a {@code Integer} object and
785      * {@code iObj.intValue() == i}.
786      *
787      * @param obj the object whose field should be modified
788      * @param i   the new value for the field of {@code obj}
789      * being modified
790      *
791      * @exception IllegalAccessException    if this {@code Field} object
792      *              is enforcing Java language access control and the underlying
793      *              field is either inaccessible or final.
794      * @exception IllegalArgumentException  if the specified object is not an
795      *              instance of the class or interface declaring the underlying
796      *              field (or a subclass or implementor thereof),
797      *              or if an unwrapping conversion fails.
798      * @exception NullPointerException      if the specified object is null
799      *              and the field is an instance field.
800      * @exception ExceptionInInitializerError if the initialization provoked
801      *              by this method fails.
802      * @see       Field#set
803      */
804     @CallerSensitive
805     // Android-changed: set*(Object, ...) implemented natively.
806     @FastNative
setInt(Object obj, int i)807     public native void setInt(Object obj, int i)
808         throws IllegalArgumentException, IllegalAccessException;
809 
810     /**
811      * Sets the value of a field as a {@code long} on the specified object.
812      * This method is equivalent to
813      * {@code set(obj, lObj)},
814      * where {@code lObj} is a {@code Long} object and
815      * {@code lObj.longValue() == l}.
816      *
817      * @param obj the object whose field should be modified
818      * @param l   the new value for the field of {@code obj}
819      * being modified
820      *
821      * @exception IllegalAccessException    if this {@code Field} object
822      *              is enforcing Java language access control and the underlying
823      *              field is either inaccessible or final.
824      * @exception IllegalArgumentException  if the specified object is not an
825      *              instance of the class or interface declaring the underlying
826      *              field (or a subclass or implementor thereof),
827      *              or if an unwrapping conversion fails.
828      * @exception NullPointerException      if the specified object is null
829      *              and the field is an instance field.
830      * @exception ExceptionInInitializerError if the initialization provoked
831      *              by this method fails.
832      * @see       Field#set
833      */
834     @CallerSensitive
835     // Android-changed: set*(Object, ...) implemented natively.
836     @FastNative
setLong(Object obj, long l)837     public native void setLong(Object obj, long l)
838         throws IllegalArgumentException, IllegalAccessException;
839 
840     /**
841      * Sets the value of a field as a {@code float} on the specified object.
842      * This method is equivalent to
843      * {@code set(obj, fObj)},
844      * where {@code fObj} is a {@code Float} object and
845      * {@code fObj.floatValue() == f}.
846      *
847      * @param obj the object whose field should be modified
848      * @param f   the new value for the field of {@code obj}
849      * being modified
850      *
851      * @exception IllegalAccessException    if this {@code Field} object
852      *              is enforcing Java language access control and the underlying
853      *              field is either inaccessible or final.
854      * @exception IllegalArgumentException  if the specified object is not an
855      *              instance of the class or interface declaring the underlying
856      *              field (or a subclass or implementor thereof),
857      *              or if an unwrapping conversion fails.
858      * @exception NullPointerException      if the specified object is null
859      *              and the field is an instance field.
860      * @exception ExceptionInInitializerError if the initialization provoked
861      *              by this method fails.
862      * @see       Field#set
863      */
864     @CallerSensitive
865     // Android-changed: set*(Object, ...) implemented natively.
866     @FastNative
setFloat(Object obj, float f)867     public native void setFloat(Object obj, float f)
868         throws IllegalArgumentException, IllegalAccessException;
869 
870     /**
871      * Sets the value of a field as a {@code double} on the specified object.
872      * This method is equivalent to
873      * {@code set(obj, dObj)},
874      * where {@code dObj} is a {@code Double} object and
875      * {@code dObj.doubleValue() == d}.
876      *
877      * @param obj the object whose field should be modified
878      * @param d   the new value for the field of {@code obj}
879      * being modified
880      *
881      * @exception IllegalAccessException    if this {@code Field} object
882      *              is enforcing Java language access control and the underlying
883      *              field is either inaccessible or final.
884      * @exception IllegalArgumentException  if the specified object is not an
885      *              instance of the class or interface declaring the underlying
886      *              field (or a subclass or implementor thereof),
887      *              or if an unwrapping conversion fails.
888      * @exception NullPointerException      if the specified object is null
889      *              and the field is an instance field.
890      * @exception ExceptionInInitializerError if the initialization provoked
891      *              by this method fails.
892      * @see       Field#set
893      */
894     @CallerSensitive
895     // Android-changed: set*(Object, ...) implemented natively.
896     @FastNative
setDouble(Object obj, double d)897     public native void setDouble(Object obj, double d)
898         throws IllegalArgumentException, IllegalAccessException;
899 
900     /**
901      * @throws NullPointerException {@inheritDoc}
902      * @since 1.5
903      */
getAnnotation(Class<T> annotationClass)904     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
905         Objects.requireNonNull(annotationClass);
906         // Android-changed: getAnnotation(Class) implemented differently.
907         return getAnnotationNative(annotationClass);
908     }
909     // Android-added: getAnnotation(Class) implemented differently.
910     @FastNative
getAnnotationNative(Class<A> annotationType)911     private native <A extends Annotation> A getAnnotationNative(Class<A> annotationType);
912 
913     /**
914      * {@inheritDoc}
915      * @throws NullPointerException {@inheritDoc}
916      * @since 1.8
917      */
918     @Override
getAnnotationsByType(Class<T> annotationClass)919     public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
920         // Android-added: getAnnotationsByType(Class) implemented differently.
921         return AnnotatedElements.getDirectOrIndirectAnnotationsByType(this, annotationClass);
922     }
923 
924     // BEGIN Android-added: isAnnotationPresent(Class) overridden in Field.
925     @Override
isAnnotationPresent(Class<? extends Annotation> annotationType)926     public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
927         if (annotationType == null) {
928             throw new NullPointerException("annotationType == null");
929         }
930         return isAnnotationPresentNative(annotationType);
931     }
932     @FastNative
isAnnotationPresentNative(Class<? extends Annotation> annotationType)933     private native boolean isAnnotationPresentNative(Class<? extends Annotation> annotationType);
934     // END Android-added: isAnnotationPresent(Class) overridden in Field.
935 
936     /**
937      * {@inheritDoc}
938      */
939     @Override
940     @FastNative
getDeclaredAnnotations()941     public native Annotation[] getDeclaredAnnotations();
942 
943     // BEGIN Android-added: Methods for use by Android-specific code.
944     /**
945      * Returns the offset of the field within an instance, or for static fields, the class.
946      *
947      * @hide
948      */
getOffset()949     public int getOffset() {
950         return offset;
951     }
952 
953     /**
954      * @hide - export for use by {@code java.lang.invoke.*}
955      */
956     @FastNative
getArtField()957     public native long getArtField();
958     // END Android-added: Methods for use by Android-specific code.
959 }
960