Search code examples
genericscompiler-errorsjava-8compiler-bug

Generified static method invocation not compiling in Java 8


Long story short, following code is not compiling in Java 8 but was compiling and executing well in Java 7:

public static void main(final String[] args) {
    final Class instance = null;
    meth(instance); // compiler error here
}

private static <K, T extends Enum<T> & IAliased<K>> void meth(final Class<T> clazz) {

}

The error occurs at mentioned line with message: The method meth(Class<T>) in the type AnotherSpike is not applicable for the arguments (Class).

While I totally understand that such code is not 100% typesafe, I need very similar invocation working in production code in Java 8 (and it was compiling with warnings and working well in Java 7).

The interesting thing is that above code compiles fine (with warnings, but this is ok) in following cases:

  1. if & IAliased<K> is removed from method signature:

    private static <K, T extends Enum<T>> void meth(final Class<T> clazz)
    
  2. if & IAliased<K> is replaced with & IAliased in signature:

    private static <K, T extends Enum<T> & IAliased> void meth(final Class<T> clazz)
    

Two above observations has lead me to the thought that this is more like a compiler bug in Java 8 rather than thoughtful enhancement to the Java 8 compiler, though I may be mistaken.

Anyway, can somebody advise on how can I pass raw instance of Class to meth method with such signature without compiler errors?

Thanks in advance for any help!


Solution

  • This seems to be a bug in the IDE's internal compiler.

    With Eclipse I get the same error you are describing. However, when using the JDK compiler I still get only the warnings and it compiles.