Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
[platform-dev] generics: different behaviour between Eclipse 3.2 and Sun's javac

I have the problem that Eclipse 3.2 and Sun's javac behave differently for Java generics. Eclipse accepts more, or Sun's javac (up to Java 6 Beta 2) is more strict, or one of them is buggy.

Example 1:

   public <T> Class<? extends T> fooGetClass(final T object) {
       return object.getClass();
   }

Eclipse reports no warnings. javac says:

smood/GetClassTest.java:6: incompatible types
found   : java.lang.Class<capture of ? extends java.lang.Object>
required: java.lang.Class<? extends T>
               return object.getClass();

I think I've read that this is an exception in the JLS, which Eclipse supports correctly, but I'm not sure. If I use a cast

   public <T> Class<? extends T> fooGetClass(final T object) {
       return (Class<? extends T>) object.getClass();
   }
javac doesn't complain (it compiles the code), but Eclipse reports a necessary cast.

Example 2:

public static <T extends Comparable<T>> T readComparableParameter(ComparableParameter<T> parameter {
   // some code
}

using it in

for (ComparableParameter<? extends Comparable<?>> parameter: parameterSet)
       {
          readComparableParameter(parameter);
       }

leads to a pattern matching problem in javac.

I have many of things strange behaviours, specially when using wildcards.

I'm using Java generics since more than 1 year, but still I have sometimes problems. Mostly with erasure. BTW, who invented the bullshit that

   ArrayList<Object> x=new ArrayList<String>();

doesn't work but

   ArrayList<? extends Object> x=new ArrayList<String>();

does? This distinction is absolut useless, and against all OOP principles! It just enforces you to write "? extends" everywhere!

Or that the wildcard is sometimes higher that Object!

Why can't I have the class of Map as Class<Map<?,?>>, but only Class<Map>, even if I can't have Class<Map<K,V>> because of erasure? How can I avoid raw types, as there's a new warning in Eclipse 3.2, when this doesn't work. But you can create:

Map<?,?>[] mapArray=new Map<?,?>[5];

Sorry for mixing but reports with design problems and listening to my frustrations. But I'm sometimes forced to go back to raw types as I can't figure out how to use generics. Transforming a project of 40'000 LOC in Java to Java 5 is more complicated if you have strange restrictions. Or bugs like http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6218229.

greetings

   Bojan



Back to the top