3

I have a code like this:

protected <T> T doSomething(String someParam, Class<T> clazz) {
...
}

which I use in a TestCase class:

Class clazz = MyClass.class;
MyClass MyClass = someObject.doSomething(someString, clazz);

This code gives a warning in eclipse:

Class is a raw type. References to generic type Class should be parameterized

and

Multiple markers at this line
- Type safety: Unchecked invocation doSomething(String, Class) of the generic method doSomething(String, Class) of type MyClass
- Type safety: The expression of type Class needs unchecked conversion to conform to Class

When I run this code (test) in eclipse - everything works perfectly. When I do it through "mvn clean install" through command prompt, I get:

C:\pathToProject\src\test\java\packagesPath\MyTestCase.java:[xx,xxx] incompatible types found :
java.lang.Object
required: com.somePackagePath.MyClass

But when providing:

Class<MyClass> clazz = MyClass.class;
MyClass MyClass = someObject.doSomething(someString, clazz);

I don't get any errors nor warnings. .
I understand that java compiler erases type info, so:
Shouldn't eclipse provide a compiler error instead of warning, or is it the maven-plugin-compiler that creates the problem?
The maven plugin is:

        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <showDeprecation>false</showDeprecation>
                <debug>true</debug>
                <encoding>UTF-8</encoding>
            </configuration>
        </plugin>

Kind Regards,
Despot

despot
  • 7,167
  • 9
  • 44
  • 63

3 Answers3

3

This is a known bug in Eclipse: 333011: [compiler][1.5] Eclipse compiles codes which javac rejects: incompatible types . Feel free to vote for the bug and add your use case to it.


There are situations when javac and Eclipse disagree, and you should report them as bugs. It is preferred to have a standalone test case of as few Java files as possible which can be copy/pasted in Eclipse and compiled using javac from the command line without any dependencies.

Robert Munteanu
  • 67,031
  • 36
  • 206
  • 278
  • I had a discussion with a colegue of mine, who argues that the eclipse compiler is just being "smarter" that the maven compiler and can notice that Class clazz = MyClass.class; is of type MyClass, because on the right side of the expression we have a simple statement. I would say that either java specification should be changed to accommodate this behavior or the eclipse compiler shouldn't do this - it will create problems for other compilers. Right? – despot Apr 12 '11 at 10:21
  • Fact is that the Java Language Specification is set in stone. Once a compiler is written is _must_ obey it. I agree that a smart compiler can infer much more than what javac and eclipsec currently do, but that would make it non-compliant - useless. In this specific instance the eclipse compiler is non-compliant and should be fixed. And another note: it is not the maven compiler, it is the maven-compiler-_plugin_ which is invoking javac under the hood, so the comparison is between eclipsec and javac. – Robert Munteanu Apr 12 '11 at 10:29
0

A workaround is using eclipse compiler in maven as described at Using Eclipse Java Compiler (ecj) in maven builds

Yasser Zamani
  • 2,380
  • 21
  • 18
0

Which compiler are you using? In this case http://code.google.com/p/jclouds/issues/detail?id=461 the openjdk seems to reveal the incompatible types while the sun jdk ignores it and compiles.

Daniele Dellafiore
  • 1,867
  • 1
  • 15
  • 19