JDK-6578661 : StringBuffer.insert() compiles with CharSequence with 1.4 target
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6
  • Priority: P4
  • Status: Closed
  • Resolution: Not an Issue
  • OS: linux
  • CPU: x86
  • Submitted: 2007-07-10
  • Updated: 2011-02-16
  • Resolved: 2007-07-11
Related Reports
Relates :  
Relates :  
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_01"
Java(TM) SE Runtime Environment (build 1.6.0_01-b06)
Java HotSpot(TM) Client VM (build 1.6.0_01-b06, mixed mode, sharing)


ADDITIONAL OS VERSION INFORMATION :
Linux vorlon.ice 2.6.14-1.1644_FC4 #1 Sun Nov 27 03:25:11 EST 2005 i686 i686 i386 GNU/Linux

A DESCRIPTION OF THE PROBLEM :

When StringBuffer.insert(0, StringBuffer buf) is compiled on JDK 1.6 or JDK 1.5 with a JDK 1.4 target,
the following exception is thrown:

Exception in thread "main" java.lang.NoSuchMethodError: java.lang.StringBuffer.insert(ILjava/lang/CharSequence;)Ljava/lang/StringBuffer;
	at InsertTest.main(InsertTest.java:6)


STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
public class InsertTest {
    public static void main(String[] args)  {
        StringBuffer buf = new StringBuffer("hello");
        buf.insert(0, buf);
        System.out.println(buf);
    }
}

/usr/local/jdk1.6.0_01/bin/javac -target 1.4 -source 1.4 InsertTest.java
/usr/local/java/bin/java InsertTest

Exception in thread "main" java.lang.NoSuchMethodError: java.lang.StringBuffer.insert(ILjava/lang/CharSequence;)Ljava/lang/StringBuffer;
	at InsertTest.main(InsertTest.java:6)



EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
The code should execute with no exceptions.


ACTUAL -
Exception in thread "main" java.lang.NoSuchMethodError: java.lang.StringBuffer.insert(ILjava/lang/CharSequence;)Ljava/lang/StringBuffer;
	at InsertTest.main(InsertTest.java:6)



ERROR MESSAGES/STACK TRACES THAT OCCUR :
Exception in thread "main" java.lang.NoSuchMethodError: java.lang.StringBuffer.insert(ILjava/lang/CharSequence;)Ljava/lang/StringBuffer;
	at InsertTest.main(InsertTest.java:6)



REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class InsertTest {
    public static void main(String[] args)  {
        StringBuffer buf = new StringBuffer("hello");
        buf.insert(0, buf);
        System.out.println(buf);
    }
}


---------- END SOURCE ----------

Comments
EVALUATION The method public StringBuffer insert(int dstOffset, CharSequence s) was added in JDK 5. The -source and -target flags do not know about what previous versions of the libraries were and javac uses the classes on the bootclasspath. The call site StringBuffer.insert(0, StringBuffer buf) would not compile on JKD 1.4 since there is no applicable method in that version of the library so it is expected that the NoSuchMethodError would be thrown on a 1.4.x JDK. To have the compiler be limited to the prior version of the library too, the bootclasspath of the compile just be set appropriately. Closing as not a bug.
11-07-2007