src/share/classes/java/lang/reflect/Field.java

Print this page




 278      * the fully-qualified name of the class declaring the field,
 279      * followed by a period, followed by the name of the field.
 280      * For example:
 281      * <pre>
 282      *    public static final int java.lang.Thread.MIN_PRIORITY
 283      *    private int java.io.FileDescriptor.fd
 284      * </pre>
 285      *
 286      * <p>The modifiers are placed in canonical order as specified by
 287      * "The Java Language Specification".  This is {@code public},
 288      * {@code protected} or {@code private} first, and then other
 289      * modifiers in the following order: {@code static}, {@code final},
 290      * {@code transient}, {@code volatile}.
 291      *
 292      * @return a string describing this {@code Field}
 293      * @jls 8.3.1 Field Modifiers
 294      */
 295     public String toString() {
 296         int mod = getModifiers();
 297         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
 298             + getTypeName(getType()) + " "
 299             + getTypeName(getDeclaringClass()) + "."
 300             + getName());
 301     }
 302 
 303     /**
 304      * Returns a string describing this {@code Field}, including
 305      * its generic type.  The format is the access modifiers for the
 306      * field, if any, followed by the generic field type, followed by
 307      * a space, followed by the fully-qualified name of the class
 308      * declaring the field, followed by a period, followed by the name
 309      * of the field.
 310      *
 311      * <p>The modifiers are placed in canonical order as specified by
 312      * "The Java Language Specification".  This is {@code public},
 313      * {@code protected} or {@code private} first, and then other
 314      * modifiers in the following order: {@code static}, {@code final},
 315      * {@code transient}, {@code volatile}.
 316      *
 317      * @return a string describing this {@code Field}, including
 318      * its generic type
 319      *
 320      * @since 1.5
 321      * @jls 8.3.1 Field Modifiers
 322      */
 323     public String toGenericString() {
 324         int mod = getModifiers();
 325         Type fieldType = getGenericType();
 326         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
 327             +  ((fieldType instanceof Class) ?
 328                 getTypeName((Class)fieldType): fieldType.toString())+ " "
 329             + getTypeName(getDeclaringClass()) + "."
 330             + getName());
 331     }
 332 
 333     /**
 334      * Returns the value of the field represented by this {@code Field}, on
 335      * the specified object. The value is automatically wrapped in an
 336      * object if it has a primitive type.
 337      *
 338      * <p>The underlying field's value is obtained as follows:
 339      *
 340      * <p>If the underlying field is a static field, the {@code obj} argument
 341      * is ignored; it may be null.
 342      *
 343      * <p>Otherwise, the underlying field is an instance field.  If the
 344      * specified {@code obj} argument is null, the method throws a
 345      * {@code NullPointerException}. If the specified object is not an
 346      * instance of the class or interface declaring the underlying
 347      * field, the method throws an {@code IllegalArgumentException}.
 348      *
 349      * <p>If this {@code Field} object is enforcing Java language access control, and


 977             overrideFieldAccessor = accessor;
 978         else
 979             fieldAccessor = accessor;
 980         // Propagate up
 981         if (root != null) {
 982             root.setFieldAccessor(accessor, overrideFinalCheck);
 983         }
 984     }
 985 
 986     // NOTE: be very careful if you change the stack depth of this
 987     // routine. The depth of the "getCallerClass" call is hardwired so
 988     // that the compiler can have an easier time if this gets inlined.
 989     private void doSecurityCheck(Object obj) throws IllegalAccessException {
 990         if (!override) {
 991             if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
 992                 Class<?> caller = Reflection.getCallerClass(4);
 993 
 994                 checkAccess(caller, clazz, obj, modifiers);
 995             }
 996         }
 997     }
 998 
 999     /*
1000      * Utility routine to paper over array type names
1001      */
1002     static String getTypeName(Class<?> type) {
1003         if (type.isArray()) {
1004             try {
1005                 Class<?> cl = type;
1006                 int dimensions = 0;
1007                 while (cl.isArray()) {
1008                     dimensions++;
1009                     cl = cl.getComponentType();
1010                 }
1011                 StringBuffer sb = new StringBuffer();
1012                 sb.append(cl.getName());
1013                 for (int i = 0; i < dimensions; i++) {
1014                     sb.append("[]");
1015                 }
1016                 return sb.toString();
1017             } catch (Throwable e) { /*FALLTHRU*/ }
1018         }
1019         return type.getName();
1020     }
1021 
1022     /**
1023      * @throws NullPointerException {@inheritDoc}
1024      * @since 1.5
1025      */
1026     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
1027         Objects.requireNonNull(annotationClass);
1028         return annotationClass.cast(declaredAnnotations().get(annotationClass));
1029     }
1030 
1031     /**
1032      * {@inheritDoc}
1033      * @throws NullPointerException {@inheritDoc}
1034      * @since 1.8
1035      */
1036     @Override
1037     public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
1038         Objects.requireNonNull(annotationClass);
1039 




 278      * the fully-qualified name of the class declaring the field,
 279      * followed by a period, followed by the name of the field.
 280      * For example:
 281      * <pre>
 282      *    public static final int java.lang.Thread.MIN_PRIORITY
 283      *    private int java.io.FileDescriptor.fd
 284      * </pre>
 285      *
 286      * <p>The modifiers are placed in canonical order as specified by
 287      * "The Java Language Specification".  This is {@code public},
 288      * {@code protected} or {@code private} first, and then other
 289      * modifiers in the following order: {@code static}, {@code final},
 290      * {@code transient}, {@code volatile}.
 291      *
 292      * @return a string describing this {@code Field}
 293      * @jls 8.3.1 Field Modifiers
 294      */
 295     public String toString() {
 296         int mod = getModifiers();
 297         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
 298             + getType().getTypeName() + " "
 299             + getDeclaringClass().getTypeName() + "."
 300             + getName());
 301     }
 302 
 303     /**
 304      * Returns a string describing this {@code Field}, including
 305      * its generic type.  The format is the access modifiers for the
 306      * field, if any, followed by the generic field type, followed by
 307      * a space, followed by the fully-qualified name of the class
 308      * declaring the field, followed by a period, followed by the name
 309      * of the field.
 310      *
 311      * <p>The modifiers are placed in canonical order as specified by
 312      * "The Java Language Specification".  This is {@code public},
 313      * {@code protected} or {@code private} first, and then other
 314      * modifiers in the following order: {@code static}, {@code final},
 315      * {@code transient}, {@code volatile}.
 316      *
 317      * @return a string describing this {@code Field}, including
 318      * its generic type
 319      *
 320      * @since 1.5
 321      * @jls 8.3.1 Field Modifiers
 322      */
 323     public String toGenericString() {
 324         int mod = getModifiers();
 325         Type fieldType = getGenericType();
 326         return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
 327             + fieldType.getTypeName() + " "
 328             + getDeclaringClass().getTypeName() + "."

 329             + getName());
 330     }
 331 
 332     /**
 333      * Returns the value of the field represented by this {@code Field}, on
 334      * the specified object. The value is automatically wrapped in an
 335      * object if it has a primitive type.
 336      *
 337      * <p>The underlying field's value is obtained as follows:
 338      *
 339      * <p>If the underlying field is a static field, the {@code obj} argument
 340      * is ignored; it may be null.
 341      *
 342      * <p>Otherwise, the underlying field is an instance field.  If the
 343      * specified {@code obj} argument is null, the method throws a
 344      * {@code NullPointerException}. If the specified object is not an
 345      * instance of the class or interface declaring the underlying
 346      * field, the method throws an {@code IllegalArgumentException}.
 347      *
 348      * <p>If this {@code Field} object is enforcing Java language access control, and


 976             overrideFieldAccessor = accessor;
 977         else
 978             fieldAccessor = accessor;
 979         // Propagate up
 980         if (root != null) {
 981             root.setFieldAccessor(accessor, overrideFinalCheck);
 982         }
 983     }
 984 
 985     // NOTE: be very careful if you change the stack depth of this
 986     // routine. The depth of the "getCallerClass" call is hardwired so
 987     // that the compiler can have an easier time if this gets inlined.
 988     private void doSecurityCheck(Object obj) throws IllegalAccessException {
 989         if (!override) {
 990             if (!Reflection.quickCheckMemberAccess(clazz, modifiers)) {
 991                 Class<?> caller = Reflection.getCallerClass(4);
 992 
 993                 checkAccess(caller, clazz, obj, modifiers);
 994             }
 995         }























 996     }
 997 
 998     /**
 999      * @throws NullPointerException {@inheritDoc}
1000      * @since 1.5
1001      */
1002     public <T extends Annotation> T getAnnotation(Class<T> annotationClass) {
1003         Objects.requireNonNull(annotationClass);
1004         return annotationClass.cast(declaredAnnotations().get(annotationClass));
1005     }
1006 
1007     /**
1008      * {@inheritDoc}
1009      * @throws NullPointerException {@inheritDoc}
1010      * @since 1.8
1011      */
1012     @Override
1013     public <T extends Annotation> T[] getAnnotationsByType(Class<T> annotationClass) {
1014         Objects.requireNonNull(annotationClass);
1015