4

I have a method defined like this:

public <T> T methodName(Class<T> clazz)

I my unit tests I erroneously test it like this:

Class clazz = Tag.class;
Tag tag = methodName(clazz);

This should produce a compile time error (and when I try to compile it with javac it produces a compile time error), but eclipse allows it to compile.

I really do not understand why eclipse allows this code to compile, can someone please provide some info why this would compile in eclipse?

The eclipse version is: Eclipse Java EE IDE for Web Developers. Version: Luna Service Release 2 (4.4.2) Build id: 20150219-0600

The java version used is: (Oracle) jdk 1.7.0_51.

1 Answers1

0

Eclipse uses a different compiler than the JDK - Eclipse Compiler for Java (ECJ) vs. Oracle's (previously Sun's) javac. There might be slight differences in the compilation schema, interpretation of the Java Language Specification and in the generated bytecode, as well as, naturally, different bugs. It seems like you just ran into one of those discrepancies.

Community
  • 1
  • 1
Adam Michalik
  • 9,678
  • 13
  • 71
  • 102