< prev index next >

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

Print this page
@@ -130,10 +130,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;

@@ -332,17 +333,25 @@
       */
      public String toString() {
          int mod = getModifiers();
          return (((mod == 0) ? "" : (Modifier.toString(mod) + " "))
              + getType().getTypeName() + " "
-             + getDeclaringClass().getTypeName() + "."
+             + getDeclaringClassTypeName() + "."
              + getName());
      }
  
      @Override
      String toShortString() {
-         return "field " + getDeclaringClass().getTypeName() + "." + getName();
+         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

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

@@ -759,11 +768,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}; and</li>
+      *     hidden class};</li>
+      * <li>the field's declaring class is not a {@linkplain Class#isPrimitiveClass()
+      *     primitive 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 >