Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8277451

java.lang.reflect.Field::set on static field with invalid argument type should throw IAE

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P2 P2
    • 18
    • 18
    • core-libs
    • b22
    • 18
    • b26
    • x86_64
    • linux
    • Verified

      In JDK17 as well as JDK18-18-ea+20, when one called `Field#set()` on a static field with an argument of invalid type, it used to throw an {{IllegalArgumentException}}.

      In JDK18-ea+23 and JDK18-ea+24, it throws a {{NullPointerException}}:

      {noformat}
      java.lang.NullPointerException: Cannot invoke "Object.getClass()" because "obj" is null
      at java.base/jdk.internal.reflect.MethodHandleObjectFieldAccessorImpl.set(MethodHandleObjectFieldAccessorImpl.java:114)
      at java.base/java.lang.reflect.Field.set(Field.java:818)
      at org.hibernate.jdk.playground.JavaReflectTest.lambda$staticField_invalidArgumentType$0(JavaReflectTest.java:16)
      at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:55)
      ... 68 more
      {noformat}

      It seems the NPE is caused by new code expecting the "self" argument to be non-null when building the {{IllegalArgumentException}}. But for static fields, "self" is always {{null}}.

      Reproducer:

      * Clone https://github.com/yrodiere/jdk-playground.git
      * Switch to branch {{java-reflect-npe}}
      * Using JDK17, run {{./mvw clean test}} => No problem
      * Using JDK18-ea+23, run {{./mvw clean test}} => Test failure

      This bug was discovered by running the Hibernate ORM test suite on JDK 18 as part of the [OpenJDK Quality Outreach program|https://wiki.openjdk.java.net/display/quality/Quality+Outreach].

            mchung Mandy Chung (Inactive)
            yrodiere Yoann Rodiere
            Votes:
            0 Vote for this issue
            Watchers:
            6 Start watching this issue

              Created:
              Updated:
              Resolved: