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

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: P2
    • Resolution: Fixed
    • Affects Version/s: 18
    • Fix Version/s: 18
    • Component/s: core-libs
    • Environment:
    • Subcomponent:
    • Introduced In Build:
      b22
    • Introduced In Version:
      18
    • Resolved In Build:
      b26
    • CPU:
      x86_64
    • OS:
      linux

      Description

      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].

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              mchung Mandy Chung
              Reporter:
              yrodiere Yoann Rodiere
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

                Dates

                Created:
                Updated:
                Resolved: