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

Compilers accept modification of final fields outside initializer methods

    XMLWordPrintable

Details

    • b127

    Backports

      Description

        According to the Java VM Specification, the putstatic bytecode is allowed to modify a final field only
        - (1) if the field is declared in the current class (the class that declares the current method) and
        - (2) if the putstatic instruction appears in the class or interface initializer method <clinit> of the current class.

        Otherwise an IllegalAccessError must be thrown.

        Similar, the putfield bytecode is allowed to modify a final field only
        - (1) if the field is declared in the current class and
        - (2) if the instruction appears in an instance initializer method <init> of the current class.

        Otherwise an IllegalAccesError must be thrown.

        Currently, HotSpot checks only condition (1) but not (2):
        http://hg.openjdk.java.net/jdk9/hs-comp/hotspot/file/07a5eceac654/src/share/vm/interpreter/linkResolver.cpp#l910

        Methods that do not satisfy condition (2) violate the assumptions of the compilers. Compiling such methods results in different behavior of compiled and interpreted code (see a detailed example below).

        Attachments

          Issue Links

            Activity

              People

                zmajo Zoltan Majo (Inactive)
                zmajo Zoltan Majo (Inactive)
                Votes:
                0 Vote for this issue
                Watchers:
                12 Start watching this issue

                Dates

                  Created:
                  Updated:
                  Resolved: