Hi,

We have intensive usages of java generics in the code base including SPI model classes and extensiblity interfaces. But I noticed that there are some inconsistencies which mix the legacy and parameterized types (either known type or unknown wildcard).

Assuming we have an interface or class MyType<T>, the following method styles will produce different results as illustrated in the sample test below.

Style 1: MyType getMyType(...); // Legacy, all the generic information will not be honored
Style 2: MyType<?> getMyType(...); // unknown parameter type
Style 3 : MyType<String> getMyType(...); // know parameter type

Please read the following example to understand the differencies.

Thanks,
Raymond

package test;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
public class TestGenerics {
/**
* @param args
*/
public static void main(String[] args) {
   Collection<?> a = new ArrayList<String>();
   Collection b = new ArrayList();
   Collection<Object> c = new ArrayList<Object>();
   String value = "ABC";
   a.add(value); // Illegal
   b.add(value); // legal but with a warning
   c.add(value); // legal
   c = a; // illegal
   c = b;// legal but with a warning
   b = a; // legal
   a = c; // legal

   Test<String> t1 = new Test<String>();
   Test t2 = t1;
   Test<?> t3 = t1;
   Collection result = null;
   result = t1.getMap().get(value); // returns Collection<String>
   result = t2.getMap().get(value); // illegal, returns Object
   result = t3.getMap().get(value); // returns Collection<?>
}

public static class Test<T> {
   public Map<String, Collection<T>> getMap() {
       return new HashMap<String, Collection<T>>();
   }
}
}

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to