Eclipse fails when javac and IDEA succeed

Consider the following standalone sample:

package bloopers; import java.lang.annotation.Annotation; public final class Blooper5 { interface Converter<T,F> { T convert( F from ); } interface Identifier<T> { } static class ConvertingIdentifier<F,T> implements Identifier<F> { ConvertingIdentifier( Converter<T,F> converter ) { } } static final class AnnotationIdentifier { Identifier<Annotation> I1 = new ConvertingIdentifier<>( a -> a.annotationType() ); Identifier<Annotation> I2 = new ConvertingIdentifier<>( Annotation::annotationType ); //<-- ERROR Identifier<Annotation> I3 = new ConvertingIdentifier<>( (Converter<Class<? extends Annotation>,Annotation>) Annotation::annotationType ); } } 

The above code compiles only under the following conditions:

  • javac from the command line.
  • IntelliJ IDEA configured to use the javac compiler.

But it does not compile with the following:

  • Eclipse
  • IntelliJ IDEA configured to use the Eclipse compiler.

Eclipse cannot compile a string with <-- ERROR by specifying the following message:

The constructor Blooper5.ConvertingIdentifier<Annotation,Class<capture#5-of ? extends Annotation>>(Blooper5.Converter<Class<? extends Annotation>,Annotation>) is undefined

Admittedly, this code really pushes the general possibilities of outputting parameters like a compiler, but still I would like to know exactly what a mismatch is, no matter how small.

Some impact of my methods in case someone can see something wrong that I don’t see:

The command I used to compile with javac was "c:\Program Files\Java\jdk1.8.0_40\bin\javac" Blooper5.java .

I have IntelliJ IDEA version 14.1. In Project Structure/SDKs , I only have "1.8", which points to C:\Program Files\Java\jdk1.8.0_40 , and in Project Structure/Modules specific module is configured to use "Project SDK (1.8)", which is displayed like 1.8 (java version "1.8.0_40") .

As for Eclipse, I am using Eclipse for RCP and RAP Developers - Version: Luna Release (4.4.0) - Build id: 20140612-0600 . In Preferences/Java/Installed JREs , I only have jdk1.8.0_40, and it is by default. In the Execution Environments section, it is also verified as "Java J-1.8" Compatible JRE. And in my Project/Properties/Java Build Path/Libraries "JRE System Library" [jdk1.8.0_40] .

More noteworthy facts:

  • It is not only me; it also fails to install the (very similar) eclipse collective.

  • IntelliJ IDEA says that the expression lambda a -> a.annotationType() can be replaced with a method reference, but if asked to do it, it will not convert it to Annotation::annotationType ; instead, it converts it to (Converter<Class<? extends Annotation>, Annotation>) Annotation:: annotationType .

So the question is:

What causes these discrepancies between Eclipse and others, and what can be done to resolve these discrepancies?

(Obviously, the goal is to eliminate, unfortunately, a scenario that is too common when one developer compiles code that does not compile to another developer IDE.)

EDIT: When I originally posted this question, I thought that IDEA using the Eclipse compiler also compiled fine, but I was wrong. It turns out that it is possible that IDEA will not be able to compile the above code by choosing the Eclipse compiler. However, the question is why there is a discrepancy between eclipse and javac.

+6
source share
1 answer

The answer to “why there is a discrepancy” is simple, but perhaps not very satisfactory: because compilers have errors and, moreover, are open to interpretation of a very complex language specification. Determining whether a bug in javac or Eclipse is a difficult task; I have seen that such discrepancies end up being declared in both directions, sometimes like Eclipse compiler errors, sometimes like javac errors. This definition, especially when it comes to generics and new language features (like lambda), can become quite tedious and mysterious. For example, look at this one, which turned out to be a javac error, but found a related problem in the Eclipse compiler: https://bugs.eclipse.org/bugs/show_bug.cgi?id=456459

It's best to report this as an Eclipse error, like me, and see if the Eclipse compiler / command can track it.

+4
source

Source: https://habr.com/ru/post/984448/


All Articles