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

inline substitution of non-static final field value

    XMLWordPrintable

    Details

    • Subcomponent:
    • CPU:
      generic
    • OS:
      generic

      Description



      Name: dkC59003 Date: 07/20/99



      Javac makes questionable optimization of non-static final field.
      When such a field has ConstantValue attribute then compiler
      substitutes reference to the field with a value from the attribute.
      But real value of instance variable may differ from value in
      ConstantValue attribute. Following test shows that javac
      (JDK 1.2.2W, 1.3K) generates code where result of static
      access to the field differs from result of dynamic access to same
      field through reflection.

      The section "4.7.2 The ConstantValue Attribute" of JVM spec says:
      "If a field_info structure representing a non-static field has a
      ConstantValue attribute, then that attribute must silently be ignored."

      It's not clear however if this requirement is applicable to compiler.


      Note, class "test_ff" is valid from the point of view of VM spec.
      --------------------- test_ff.jasm ---------------------
      public class test_ff
      {

      public final Field fnl_str:"Ljava/lang/String;" = String "Wrong string";
      //fnl_str is final non-static(instance) field

      public Method "<init>":"()V"
      stack 2 locals 1
      {
      aload_0;
      invokespecial Method java/lang/Object."<init>":"()V";
      aload_0;
      ldc String "Good string";
      putfield Field fnl_str:"Ljava/lang/String;";
      return;
      }

      } // end Class test_ff

      --------------------- test.java ---------------------
      public class test {
      public static void main(String args[]) {
      try{
      test_ff obj = new test_ff();
      Class checksClass = Class.forName("test_ff");
      String reflected_fnl_str = (String)checksClass.getField("fnl_str").get(obj);
      System.out.println(obj.fnl_str + " equals to " + reflected_fnl_str);
      }catch(Throwable e){
      System.out.println(e);
      }
      }
      }

      --------------------- runing ---------------------
      %jasm test_ff.jasm
      %javac test.java
      %java test
      Wrong string equals to Good string
      (must be "Good string equals to Good string")

      ---------------------disassembled test.class ---------------------
      public class test
      {

      public Method "<init>":"()V"
      stack 1 locals 1
      {
      aload_0;
      invokespecial Method java/lang/Object."<init>":"()V";
      return;
      }

      public static Method main:"([Ljava/lang/String;)V"
      stack 4 locals 4
      {
      try t53;
      new class test_ff;
      dup;
      ...... skiped ..........

      dup;
      ldc String "Wrong string equals to "; //**
      // ^^^^^^^^^^^^ must be ignored in test_ff.class
      // it looks like questionable optimization
      // real value of obj.fnl_str at that point is "Good string"
      //
      invokespecial Method java/lang/StringBuffer."<init>":"(Ljava/lang/String;)V";
      aload_3;

      ...... skiped ..........
      getstatic Field java/lang/System.out:"Ljava/io/PrintStream;";
      aload_1;
      invokevirtual Method java/io/PrintStream.println:"(Ljava/lang/Object;)V";
      L61: return;
      }

      } // end Class test

      ------------------------------------------
      Correct lines (instead of line marked **) could be lines like
      dup;
      aload_1;
      getfield Field test_ff.fnl_str:"Ljava/lang/String;";
      ...etc

      ======================================================================

        Attachments

          Activity

            People

            Assignee:
            wmaddoxsunw William Maddox (Inactive)
            Reporter:
            dkhukhrosunw Dmitry Khukhro (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: