< prev index next >

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

Print this page
*** 35,10 ***
--- 35,11 ---
  import sun.reflect.generics.factory.CoreReflectionFactory;
  import sun.reflect.generics.factory.GenericsFactory;
  import sun.reflect.generics.scope.ClassScope;
  import java.lang.annotation.Annotation;
  import java.util.Map;
+ import java.util.Set;
  import java.util.Objects;
  import sun.reflect.annotation.AnnotationParser;
  import sun.reflect.annotation.AnnotationSupport;
  import sun.reflect.annotation.TypeAnnotation;
  import sun.reflect.annotation.TypeAnnotationParser;

*** 126,10 ***
--- 127,11 ---
            boolean trustedFinal,
            int slot,
            String signature,
            byte[] annotations)
      {
+         assert declaringClass.isPrimaryType();
          this.clazz = declaringClass;
          this.name = name;
          this.type = type;
          this.modifiers = modifiers;
          this.trustedFinal = trustedFinal;

*** 200,17 ***
--- 202,30 ---
       * Returns the Java language modifiers for the field represented
       * by this {@code Field} object, as an integer. The {@code Modifier} class should
       * be used to decode the modifiers.
       *
       * @see Modifier
+      * @see #accessFlags()
       * @jls 8.3 Field Declarations
       * @jls 9.3 Field (Constant) Declarations
       */
      public int getModifiers() {
          return modifiers;
      }
  
+     /**
+      * {@return an unmodifiable set of the {@linkplain AccessFlag
+      * access flags} for this field, possibly empty}
+      * @see #getModifiers()
+      * @jvms 4.5 Fields
+      * @since 20
+      */
+     @Override
+     public Set<AccessFlag> accessFlags() {
+         return AccessFlag.maskToAccessFlags(getModifiers(), AccessFlag.Location.FIELD);
+     }
+ 
      /**
       * Returns {@code true} if this field represents an element of
       * an enumerated class; returns {@code false} otherwise.
       *
       * @return {@code true} if and only if this field represents an element of

*** 328,17 ***
       */
      public String toString() {
          int mod = getModifiers();
          return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
              + getType().getTypeName() + " "
!             + getDeclaringClass().getTypeName() + "."
              + getName());
      }
  
      @Override
      String toShortString() {
!         return "field " + getDeclaringClass().getTypeName() + "." + getName();
      }
  
      /**
       * Returns a string describing this {@code Field}, including
       * its generic type.  The format is the access modifiers for the
--- 343,25 ---
       */
      public String toString() {
          int mod = getModifiers();
          return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
              + getType().getTypeName() + " "
!             + getDeclaringClassTypeName() + "."
              + getName());
      }
  
      @Override
      String toShortString() {
!         return "field " + getDeclaringClassTypeName() + "." + getName();
+     }
+ 
+     String getDeclaringClassTypeName() {
+         Class<?> c = getDeclaringClass();
+         if (c.isPrimitiveClass()) {
+             c = c.asValueType();
+         }
+         return c.getTypeName();
      }
  
      /**
       * Returns a string describing this {@code Field}, including
       * its generic type.  The format is the access modifiers for the

*** 362,11 ***
      public String toGenericString() {
          int mod = getModifiers();
          Type fieldType = getGenericType();
          return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
              + fieldType.getTypeName() + " "
!             + getDeclaringClass().getTypeName() + "."
              + getName());
      }
  
      /**
       * Returns the value of the field represented by this {@code Field}, on
--- 385,11 ---
      public String toGenericString() {
          int mod = getModifiers();
          Type fieldType = getGenericType();
          return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
              + fieldType.getTypeName() + " "
!             + getDeclaringClassTypeName() + "."
              + getName());
      }
  
      /**
       * Returns the value of the field represented by this {@code Field}, on

*** 755,11 ***
       * <ul>
       * <li>{@link #setAccessible(boolean) setAccessible(true)} has succeeded for
       *     this {@code Field} object;</li>
       * <li>the field is non-static; and</li>
       * <li>the field's declaring class is not a {@linkplain Class#isHidden()
!      *     hidden class}; and</li>
       * <li>the field's declaring class is not a {@linkplain Class#isRecord()
       *     record class}.</li>
       * </ul>
       * If any of the above checks is not met, this method throws an
       * {@code IllegalAccessException}.
--- 778,13 ---
       * <ul>
       * <li>{@link #setAccessible(boolean) setAccessible(true)} has succeeded for
       *     this {@code Field} object;</li>
       * <li>the field is non-static; and</li>
       * <li>the field's declaring class is not a {@linkplain Class#isHidden()
!      *     hidden class};</li>
+      * <li>the field's declaring class is not a {@linkplain Class#isValue()
+      *     value class}; and</li>
       * <li>the field's declaring class is not a {@linkplain Class#isRecord()
       *     record class}.</li>
       * </ul>
       * If any of the above checks is not met, this method throws an
       * {@code IllegalAccessException}.
< prev index next >