Skip to main content



      Home
Home » Language IDEs » Java Development Tools (JDT) » Compiler compliance 1.4 compiles String.contains
Compiler compliance 1.4 compiles String.contains [message #520171] Thu, 11 March 2010 07:58 Go to next message
Eclipse UserFriend
Hi,

Not sure but I think this is a bug. I'm running Eclipse with a 1.6 JRE but if I set the compiler compliance to 1.4 it will still compile something like:

String s = "Hello";
boolean b = s.contains("He");


Although String#contains is only available since 1.5.

Not really a blocking problem but it shouldn't compile in this case.
Re: Compiler compliance 1.4 compiles String.contains [message #520177 is a reply to message #520171] Thu, 11 March 2010 08:14 Go to previous messageGo to next message
Eclipse UserFriend
Maybe it is a compiler bug.
I run the test in the command line using the following command:
javac -source 1.4 -target 1.4 Test.java

I'm using JDK 6 and the file compiled normally.
Then I tried to run it using JVM 1.42 and I got this error:

Exception in thread "main" java.lang.NoSuchMethodError: java.lang.String.contains(Ljava/lang/CharSequence;)Z
at Teste.main(Teste.java:5)

I think that maybe the compiler don't have a way to identify which method was available in each version and the String class that it has available at compile time is the version from JDK 6. So I think that the only thing it verify is basic syntax and other stuff.

For example, I added the following line in my code:
List<String> teste = new ArrayList<String>();
After that it won't compile, I got the following message using the same command as above:
Test.java:8: generics are not supported in -source 1.4
(use -source 5 or higher to enable generics)
List<String> teste = new ArrayList<String>();

I think that you will have to be careful with things like that. Or download and use the SDK 1.4. I didn't test it, but I think you can find it here: http://java.sun.com/products/archive/
Re: Compiler compliance 1.4 compiles String.contains [message #520187 is a reply to message #520177] Thu, 11 March 2010 08:53 Go to previous messageGo to next message
Eclipse UserFriend
I didn't try compiling from the command line but this clearly looks like an issue with the compiler and not Eclipse. I run a my Tomcat server with 1.4.2 and I got the same error so I guess I'll just have to be careful with that. Although I may see about opening a bug with Sun/Oracle.

Thanks for your response
Re: Compiler compliance 1.4 compiles String.contains [message #520241 is a reply to message #520187] Thu, 11 March 2010 11:36 Go to previous messageGo to next message
Eclipse UserFriend
"Matt Rossner" <matt.rossner@gmail.com> wrote in message
news:hnasln$k29$1@build.eclipse.org...
>I didn't try compiling from the command line but this clearly looks like an
>issue with the compiler and not Eclipse. I run a my Tomcat server with
>1.4.2 and I got the same error so I guess I'll just have to be careful with
>that. Although I may see about opening a bug with Sun/Oracle.
> Thanks for your response

You are confusing the compiler (Java Language) with the runtime libraries.
The compiler knows Java syntax and how to convert it into class files. It
has no knowledge of which classes/methods pertain to which release of Java.
When you compile your code, the compiler will use the classes/methods
available to it for validation. If you set compiler compliance to 1.4 but
compile against a 6.x runtime, the compile will allow you to reference the
6.x members.
Re: Compiler compliance 1.4 compiles String.contains [message #520270 is a reply to message #520241] Thu, 11 March 2010 13:54 Go to previous messageGo to next message
Eclipse UserFriend
"David Wegener" <dpwegener@gmail.com> wrote in message
news:hnb66b$lfp$1@build.eclipse.org...
>
> "Matt Rossner" <matt.rossner@gmail.com> wrote in message
> news:hnasln$k29$1@build.eclipse.org...
>>I didn't try compiling from the command line but this clearly looks like
>>an issue with the compiler and not Eclipse. I run a my Tomcat server with
>>1.4.2 and I got the same error so I guess I'll just have to be careful
>>with that. Although I may see about opening a bug with Sun/Oracle.
>> Thanks for your response
>
> You are confusing the compiler (Java Language) with the runtime libraries.
> The compiler knows Java syntax and how to convert it into class files. It
> has no knowledge of which classes/methods pertain to which release of
> Java. When you compile your code, the compiler will use the
> classes/methods available to it for validation. If you set compiler
> compliance to 1.4 but compile against a 6.x runtime, the compile will
> allow you to reference the 6.x members.

What David is saying is that you need to set your project's JRE (in the Java
build path) to be a 1.4 JRE.
Re: Compiler compliance 1.4 compiles String.contains [message #520278 is a reply to message #520270] Thu, 11 March 2010 14:39 Go to previous messageGo to next message
Eclipse UserFriend
Seems like you have a environment just like mine!

My server uses Tomcat 4.1 with JRE 1.42 so I have to simulate exactly the same environment for tests.

First step is to install Java 1.42 in your computer (you can download it from Sun's Archive, linked above). After that, add it to the Installed JREs (inside Preferences > Java > Installed JREs).

Then, add a new server runtime inside Preferences > Server > Runtime Environments and point it to use the new installed JRE.

When you add a server in the Server view using that server runtime, it will use JRE 1.4 and the Tomcat you pointed to. That way you'll reproduce the environment you need exactly as it is in the server and will be able to find bugs like the one you've noticed here in development phase, not production.
Re: Compiler compliance 1.4 compiles String.contains [message #526099 is a reply to message #520171] Thu, 08 April 2010 12:50 Go to previous message
Eclipse UserFriend
This is not a bug. The compiler is using the libraries on the classpath to find out if a method/field/type can be used. It doesn't have any information about when the method was added.
You can use API Tooling to get this kind of problem. If you set up your project as a bundle project (PDE), then you can enable API Tooling and set the preferences for invalid references inside libraries as error.
Compliance 1.4 only means that the compiler will comply to javac 1.4 in term of name lookup and other settings specific to the version of javac.

Hope this help.

Olivier
Previous Topic:Inserting AST Nodes at every Statements
Next Topic:Discouraged access
Goto Forum:
  


Current Time: Wed Mar 19 20:56:35 EDT 2025

Powered by FUDForum. Page generated in 0.06637 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top