Bug 36674 - compiler can generate Java 1.4-only bytecode regardless of compatibility settings
Summary: compiler can generate Java 1.4-only bytecode regardless of compatibility sett...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows NT
: P3 normal (vote)
Target Milestone: 3.0 M1   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 24412 (view as bug list)
Depends on:
Blocks:
 
Reported: 2003-04-19 18:38 EDT by vlad CLA
Modified: 2012-10-16 11:39 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description vlad CLA 2003-04-19 18:38:45 EDT
When compiling the following class:

public class Main
{
    public static void main (String [] args)
    {
    	StringBuffer s1 = new StringBuffer ();
        s1.append (new StringBuffer ());
    }

} // end of class

Eclipse 2.1 GA will emit code that uses Java 1.4-only StringBuffer.append
(StringBuffer) method regardless of JDK or .class compliance level settings 
[the following javap dump was obtained when using JDK 1.3 compatibility with 
default compliance settings]:

Compiled from Main.java
public class Main extends java.lang.Object {
    public Main();
    public static void main(java.lang.String[]);
}

Method Main()
   0 aload_0
   1 invokespecial #9 <Method java.lang.Object()>
   4 return

Method void main(java.lang.String[])
   0 new #17 <Class java.lang.StringBuffer>
   3 dup
   4 invokespecial #18 <Method java.lang.StringBuffer()>
   7 astore_1
   8 aload_1
   9 new #17 <Class java.lang.StringBuffer>
  12 dup
  13 invokespecial #18 <Method java.lang.StringBuffer()>
  16 invokevirtual #22 <Method java.lang.StringBuffer append
(java.lang.StringBuffer)>
  19 pop
  20 return

Note that even though the major/minor version of the emitted .class file is 
45/3 (i.e., JVM 1.1-compliant), the class can only run in JVM 1.4 and up 
despite the fact that the source is Java 1.1-compatible.

The correct behavior for emitting JDK 1.3-compatible code would be to use 
StringBuffer.append(Object) method.
Comment 1 Olivier Thomann CLA 2003-04-21 10:41:57 EDT
You are compiling your code against the JDK 1.4 libraries. This is why the 1.4 
method is used. If you compile the same code against 1.3 libraries, the append
(Object) method is used.
The compiler uses the method available in the libraries you use to compile. If 
you don't want 1.4 API to be used, why do you compile against them? You can 
compile against 1.3 libraries and run against 1.4 without any problem. You 
simply need to install more than one JRE in your workspace and select the one 
you want for the runtime.
So I don't see any bug. The compliance level is used to set the way the 
compiler should perform nameloopuk,... Some of them change between 1.3 and 1.4, 
but this compliance level has no impact on the API used in the compiled code.
Comment 2 vlad CLA 2003-04-21 11:21:35 EDT
Eclipse generates no warnings about 1.4 libraries being used when Java 1.3 
compliance is requested. In the above case most users will not know that they 
are in fact not generating 1.3-compatible code.
Comment 3 Olivier Thomann CLA 2003-04-21 12:58:44 EDT
The problem is that there is no way to distinguish a 1.3 libraries from 1.4 
libraries. It is up to the user to know what libraries he/she is compiling 
against. This has nothing to do with the compliance setting.
Comment 4 Olivier Thomann CLA 2003-04-21 15:15:32 EDT
We can detect such a case. This could definitely be improved. We might simply
specify what is the compliance level.
Comment 5 Philipe Mulet CLA 2003-04-21 21:27:30 EDT
Isn't the target JDK meant to deal with these ? 
Comment 6 Philipe Mulet CLA 2003-04-22 05:44:48 EDT
Javac will complain if trying to use 1.4 libraries for a 1.3 level compiler, 
arguing about wrong major/minor versions for the libraries. We should allow to 
compile, but still issue some notification under the same circumstances.
Comment 7 Philipe Mulet CLA 2003-04-24 02:38:13 EDT
We should detect the case where 1.4 libraries are used in 1.3 compliant mode, 
and issue a classpath problem marker.

Comment 8 Philipe Mulet CLA 2003-05-22 12:41:19 EDT
Added optional problem to diagnose this issue:
Added JavaCore optional problem to detect incompatible required binaries, so as 
to flag situations where some prerequisite binaries are required a JRE level 
higher than the project target platform; i.e. compiling against 1.4 libraries 
when deploying for 1.1 platform is likely unwanted. 
* JAVACORE / Reporting Incompatible JDK Level for Required Binaries
*    Indicate the severity of the problem reported when a project prerequisites 
another project 
*    or library with an incompatible target JDK level (e.g. project targeting 
1.1 vm, but compiled against 1.4 libraries).
*     - option id:         "org.eclipse.jdt.core.incompatibleJDKLevel"
*     - possible values:   { "error", "warning", "ignore" }
*     - default:           "ignore"

It will likely be located amongst compiler preferences, under buildpath tab, 
and will default to 'ignore' for now.
Comment 9 David Audel CLA 2003-06-06 06:12:17 EDT
Verified.
Comment 10 Philipe Mulet CLA 2003-10-30 04:50:35 EST
*** Bug 24412 has been marked as a duplicate of this bug. ***