• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     private 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, the method throws an
601      * {@code IllegalAccessException} unless {@code setAccessible(true)}
602      * has succeeded for this {@code Field} object
603      * and the field is non-static. Setting a final field in this way
604      * is meaningful only during deserialization or reconstruction of
605      * instances of classes with blank final fields, before they are
606      * made available for access by other parts of a program. Use in
607      * any other context may have unpredictable effects, including cases
608      * in which other parts of a program continue to use the original
609      * value of this field.
610      *
611      * <p>If the underlying field is of a primitive type, an unwrapping
612      * conversion is attempted to convert the new value to a value of
613      * a primitive type.  If this attempt fails, the method throws an
614      * {@code IllegalArgumentException}.
615      *
616      * <p>If, after possible unwrapping, the new value cannot be
617      * converted to the type of the underlying field by an identity or
618      * widening conversion, the method throws an
619      * {@code IllegalArgumentException}.
620      *
621      * <p>If the underlying field is static, the class that declared the
622      * field is initialized if it has not already been initialized.
623      *
624      * <p>The field is set to the possibly unwrapped and widened new value.
625      *
626      * <p>If the field is hidden in the type of {@code obj},
627      * the field's value is set according to the preceding rules.
628      *
629      * @param obj the object whose field should be modified
630      * @param value the new value for the field of {@code obj}
631      * being modified
632      *
633      * @exception IllegalAccessException    if this {@code Field} object
634      *              is enforcing Java language access control and the underlying
635      *              field is either inaccessible or final.
636      * @exception IllegalArgumentException  if the specified object is not an
637      *              instance of the class or interface declaring the underlying
638      *              field (or a subclass or implementor thereof),
639      *              or if an unwrapping conversion fails.
640      * @exception NullPointerException      if the specified object is null
641      *              and the field is an instance field.
642      * @exception ExceptionInInitializerError if the initialization provoked
643      *              by this method fails.
644      */
645     @CallerSensitive
646     // Android-changed: set*(Object, ...) implemented natively.
647     @FastNative
set(Object obj, Object value)648     public native void set(Object obj, Object value)
649         throws IllegalArgumentException, IllegalAccessException;
650 
651     /**
652      * Sets the value of a field as a {@code boolean} on the specified object.
653      * This method is equivalent to
654      * {@code set(obj, zObj)},
655      * where {@code zObj} is a {@code Boolean} object and
656      * {@code zObj.booleanValue() == z}.
657      *
658      * @param obj the object whose field should be modified
659      * @param z   the new value for the field of {@code obj}
660      * being modified
661      *
662      * @exception IllegalAccessException    if this {@code Field} object
663      *              is enforcing Java language access control and the underlying
664      *              field is either inaccessible or final.
665      * @exception IllegalArgumentException  if the specified object is not an
666      *              instance of the class or interface declaring the underlying
667      *              field (or a subclass or implementor thereof),
668      *              or if an unwrapping conversion fails.
669      * @exception NullPointerException      if the specified object is null
670      *              and the field is an instance field.
671      * @exception ExceptionInInitializerError if the initialization provoked
672      *              by this method fails.
673      * @see       Field#set
674      */
675     @CallerSensitive
676     // Android-changed: set*(Object, ...) implemented natively.
677     @FastNative
setBoolean(Object obj, boolean z)678     public native void setBoolean(Object obj, boolean z)
679         throws IllegalArgumentException, IllegalAccessException;
680 
681     /**
682      * Sets the value of a field as a {@code byte} on the specified object.
683      * This method is equivalent to
684      * {@code set(obj, bObj)},
685      * where {@code bObj} is a {@code Byte} object and
686      * {@code bObj.byteValue() == b}.
687      *
688      * @param obj the object whose field should be modified
689      * @param b   the new value for the field of {@code obj}
690      * being modified
691      *
692      * @exception IllegalAccessException    if this {@code Field} object
693      *              is enforcing Java language access control and the underlying
694      *              field is either inaccessible or final.
695      * @exception IllegalArgumentException  if the specified object is not an
696      *              instance of the class or interface declaring the underlying
697      *              field (or a subclass or implementor thereof),
698      *              or if an unwrapping conversion fails.
699      * @exception NullPointerException      if the specified object is null
700      *              and the field is an instance field.
701      * @exception ExceptionInInitializerError if the initialization provoked
702      *              by this method fails.
703      * @see       Field#set
704      */
705     @CallerSensitive
706     // Android-changed: set*(Object, ...) implemented natively.
707     @FastNative
setByte(Object obj, byte b)708     public native void setByte(Object obj, byte b)
709         throws IllegalArgumentException, IllegalAccessException;
710 
711     /**
712      * Sets the value of a field as a {@code char} on the specified object.
713      * This method is equivalent to
714      * {@code set(obj, cObj)},
715      * where {@code cObj} is a {@code Character} object and
716      * {@code cObj.charValue() == c}.
717      *
718      * @param obj the object whose field should be modified
719      * @param c   the new value for the field of {@code obj}
720      * being modified
721      *
722      * @exception IllegalAccessException    if this {@code Field} object
723      *              is enforcing Java language access control and the underlying
724      *              field is either inaccessible or final.
725      * @exception IllegalArgumentException  if the specified object is not an
726      *              instance of the class or interface declaring the underlying
727      *              field (or a subclass or implementor thereof),
728      *              or if an unwrapping conversion fails.
729      * @exception NullPointerException      if the specified object is null
730      *              and the field is an instance field.
731      * @exception ExceptionInInitializerError if the initialization provoked
732      *              by this method fails.
733      * @see       Field#set
734      */
735     @CallerSensitive
736     // Android-changed: set*(Object, ...) implemented natively.
737     @FastNative
setChar(Object obj, char c)738     public native void setChar(Object obj, char c)
739         throws IllegalArgumentException, IllegalAccessException;
740 
741     /**
742      * Sets the value of a field as a {@code short} on the specified object.
743      * This method is equivalent to
744      * {@code set(obj, sObj)},
745      * where {@code sObj} is a {@code Short} object and
746      * {@code sObj.shortValue() == s}.
747      *
748      * @param obj the object whose field should be modified
749      * @param s   the new value for the field of {@code obj}
750      * being modified
751      *
752      * @exception IllegalAccessException    if this {@code Field} object
753      *              is enforcing Java language access control and the underlying
754      *              field is either inaccessible or final.
755      * @exception IllegalArgumentException  if the specified object is not an
756      *              instance of the class or interface declaring the underlying
757      *              field (or a subclass or implementor thereof),
758      *              or if an unwrapping conversion fails.
759      * @exception NullPointerException      if the specified object is null
760      *              and the field is an instance field.
761      * @exception ExceptionInInitializerError if the initialization provoked
762      *              by this method fails.
763      * @see       Field#set
764      */
765     @CallerSensitive
766     // Android-changed: set*(Object, ...) implemented natively.
767     @FastNative
setShort(Object obj, short s)768     public native void setShort(Object obj, short s)
769         throws IllegalArgumentException, IllegalAccessException;
770 
771     /**
772      * Sets the value of a field as an {@code int} on the specified object.
773      * This method is equivalent to
774      * {@code set(obj, iObj)},
775      * where {@code iObj} is a {@code Integer} object and
776      * {@code iObj.intValue() == i}.
777      *
778      * @param obj the object whose field should be modified
779      * @param i   the new value for the field of {@code obj}
780      * being modified
781      *
782      * @exception IllegalAccessException    if this {@code Field} object
783      *              is enforcing Java language access control and the underlying
784      *              field is either inaccessible or final.
785      * @exception IllegalArgumentException  if the specified object is not an
786      *              instance of the class or interface declaring the underlying
787      *              field (or a subclass or implementor thereof),
788      *              or if an unwrapping conversion fails.
789      * @exception NullPointerException      if the specified object is null
790      *              and the field is an instance field.
791      * @exception ExceptionInInitializerError if the initialization provoked
792      *              by this method fails.
793      * @see       Field#set
794      */
795     @CallerSensitive
796     // Android-changed: set*(Object, ...) implemented natively.
797     @FastNative
setInt(Object obj, int i)798     public native void setInt(Object obj, int i)
799         throws IllegalArgumentException, IllegalAccessException;
800 
801     /**
802      * Sets the value of a field as a {@code long} on the specified object.
803      * This method is equivalent to
804      * {@code set(obj, lObj)},
805      * where {@code lObj} is a {@code Long} object and
806      * {@code lObj.longValue() == l}.
807      *
808      * @param obj the object whose field should be modified
809      * @param l   the new value for the field of {@code obj}
810      * being modified
811      *
812      * @exception IllegalAccessException    if this {@code Field} object
813      *              is enforcing Java language access control and the underlying
814      *              field is either inaccessible or final.
815      * @exception IllegalArgumentException  if the specified object is not an
816      *              instance of the class or interface declaring the underlying
817      *              field (or a subclass or implementor thereof),
818      *              or if an unwrapping conversion fails.
819      * @exception NullPointerException      if the specified object is null
820      *              and the field is an instance field.
821      * @exception ExceptionInInitializerError if the initialization provoked
822      *              by this method fails.
823      * @see       Field#set
824      */
825     @CallerSensitive
826     // Android-changed: set*(Object, ...) implemented natively.
827     @FastNative
setLong(Object obj, long l)828     public native void setLong(Object obj, long l)
829         throws IllegalArgumentException, IllegalAccessException;
830 
831     /**
832      * Sets the value of a field as a {@code float} on the specified object.
833      * This method is equivalent to
834      * {@code set(obj, fObj)},
835      * where {@code fObj} is a {@code Float} object and
836      * {@code fObj.floatValue() == f}.
837      *
838      * @param obj the object whose field should be modified
839      * @param f   the new value for the field of {@code obj}
840      * being modified
841      *
842      * @exception IllegalAccessException    if this {@code Field} object
843      *              is enforcing Java language access control and the underlying
844      *              field is either inaccessible or final.
845      * @exception IllegalArgumentException  if the specified object is not an
846      *              instance of the class or interface declaring the underlying
847      *              field (or a subclass or implementor thereof),
848      *              or if an unwrapping conversion fails.
849      * @exception NullPointerException      if the specified object is null
850      *              and the field is an instance field.
851      * @exception ExceptionInInitializerError if the initialization provoked
852      *              by this method fails.
853      * @see       Field#set
854      */
855     @CallerSensitive
856     // Android-changed: set*(Object, ...) implemented natively.
857     @FastNative
setFloat(Object obj, float f)858     public native void setFloat(Object obj, float f)
859         throws IllegalArgumentException, IllegalAccessException;
860 
861     /**
862      * Sets the value of a field as a {@code double} on the specified object.
863      * This method is equivalent to
864      * {@code set(obj, dObj)},
865      * where {@code dObj} is a {@code Double} object and
866      * {@code dObj.doubleValue() == d}.
867      *
868      * @param obj the object whose field should be modified
869      * @param d   the new value for the field of {@code obj}
870      * being modified
871      *
872      * @exception IllegalAccessException    if this {@code Field} object
873      *              is enforcing Java language access control and the underlying
874      *              field is either inaccessible or final.
875      * @exception IllegalArgumentException  if the specified object is not an
876      *              instance of the class or interface declaring the underlying
877      *              field (or a subclass or implementor thereof),
878      *              or if an unwrapping conversion fails.
879      * @exception NullPointerException      if the specified object is null
880      *              and the field is an instance field.
881      * @exception ExceptionInInitializerError if the initialization provoked
882      *              by this method fails.
883      * @see       Field#set
884      */
885     @CallerSensitive
886     // Android-changed: set*(Object, ...) implemented natively.
887     @FastNative
setDouble(Object obj, double d)888     public native void setDouble(Object obj, double d)
889         throws IllegalArgumentException, IllegalAccessException;
890 
891     /**
892      * @throws NullPointerException {@inheritDoc}
893      * @since 1.5
894      */
getAnnotation(Class<T> annotationClass)895     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
896         Objects.requireNonNull(annotationClass);
897         // Android-changed: getAnnotation(Class) implemented differently.
898         return getAnnotationNative(annotationClass);
899     }
900     // Android-added: getAnnotation(Class) implemented differently.
901     @FastNative
getAnnotationNative(Class<A> annotationType)902     private native <A extends Annotation> A getAnnotationNative(Class<A> annotationType);
903 
904     /**
905      * {@inheritDoc}
906      * @throws NullPointerException {@inheritDoc}
907      * @since 1.8
908      */
909     @Override
getAnnotationsByType(Class<T> annotationClass)910     public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
911         // Android-added: getAnnotationsByType(Class) implemented differently.
912         return AnnotatedElements.getDirectOrIndirectAnnotationsByType(this, annotationClass);
913     }
914 
915     // BEGIN Android-added: isAnnotationPresent(Class) overridden in Field.
916     @Override
isAnnotationPresent(Class<? extends Annotation> annotationType)917     public boolean isAnnotationPresent(Class<? extends Annotation> annotationType) {
918         if (annotationType == null) {
919             throw new NullPointerException("annotationType == null");
920         }
921         return isAnnotationPresentNative(annotationType);
922     }
923     @FastNative
isAnnotationPresentNative(Class<? extends Annotation> annotationType)924     private native boolean isAnnotationPresentNative(Class<? extends Annotation> annotationType);
925     // END Android-added: isAnnotationPresent(Class) overridden in Field.
926 
927     /**
928      * {@inheritDoc}
929      */
930     @Override
931     @FastNative
getDeclaredAnnotations()932     public native Annotation[] getDeclaredAnnotations();
933 
934     // BEGIN Android-added: Methods for use by Android-specific code.
935     /**
936      * Returns the offset of the field within an instance, or for static fields, the class.
937      *
938      * @hide
939      */
getOffset()940     public int getOffset() {
941         return offset;
942     }
943 
944     /**
945      * @hide - export for use by {@code java.lang.invoke.*}
946      */
947     @FastNative
getArtField()948     public native long getArtField();
949     // END Android-added: Methods for use by Android-specific code.
950 }
951