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

Compilers accept modification of final fields outside initializer methods

XMLWordPrintable

    • b127

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

          1. jck.out
            2.04 MB
            Zoltan Majo

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

                Created:
                Updated:
                Resolved: