JDK-6409414 : javac generates bad code at -target 1.4 for StringBuffer.append(CharSequence)
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0
  • Priority: P4
  • Status: Closed
  • Resolution: Duplicate
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2006-04-05
  • Updated: 2010-04-02
  • Resolved: 2006-04-06
Related Reports
Duplicate :  
Description
FULL PRODUCT VERSION :
java version "1.5.0_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0_06-b05)
Java HotSpot(TM) Client VM (build 1.5.0_06-b05, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]
(actually, not OS-specific)

A DESCRIPTION OF THE PROBLEM :
javac generates incorrect code with "-source 1.4 -target 1.4" for StringBuffer.append(CharSequence).

In the "real" 1.4, it would generate a call to StringBuffer.append(Object), whereas using the 1.5 JDK, even at -source 1.4, it blindly generates a call to the (new in 1.5) StringBuffer.append(CharSequence).

When this object is later run with a "real" 1.4 JRE, I get a NoSuchMethodError.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
1. Compile the following source using the 5.0 JDK with
       -source 1.4 -target 1.4:

    class foo
    {
      public static void main(String[] args)
      {
        String line = "abcd";
        StringBuffer sb = new StringBuffer();
        sb.append(line.subSequence(0, 2));
        System.out.println(sb.toString());
      }
    }

2. Run this code using a ***1.4*** Javac:

  C:\> c:\java\bin\java -version
java version "1.4.2_08"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)
  c:\> c:\java\bin\java foo
Exception in thread "main" java.lang.NoSuchMethodError: java.lang.StringBuffer.append(Ljava/lang/CharSequence;)Ljava/lang/StringBuffer;
        at foo.main(foo.java:7)


EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Expected result is that it prints out the line

 ab


ACTUAL -
Runtime error

ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.NoSuchMethodError: java.lang.StringBuffer.append(Ljava/lang/CharSequence;)Ljava/lang/StringBuffer;
        at foo.main(foo.java:7)


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
See "steps to reproduce"
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
The workaround (source code only!) is to use substring() instead of subsequence() in the call.