I recently started writing a generic object template for a project and came across something that I don't quite understand. Given the following:
public class G<X> { public G(Class<X> c) { } public void m(X x) { } public static <T> G<T> create(Class<T> c) { return new G<T>(c); } public static void main(String[] args) { Object o = "";
I get the following compilation error:
m(capture#402 of ?) in G<capture#402 of ?> cannot be applied to (java.lang.Object)
I cannot figure out how to properly drop t to do this compilation. What am I missing? Using JDK 1.6.
EDIT :
This is not an academic question. I am trying to write a mapper from sleep mode objects to the corresponding DTO, which will be passed at the REST level. It is assumed that for each Foo ORM object, there may be a FooDTO class that has a constructor that takes an instance of Foo as a parameter. The generic class that maps Foo to FooDTO encapsulates this assumption and throws appropriate exceptions if FooDTO does not exist or does not have a corresponding constructor:
class Mapper<Foo,FooDTO> { private final Constructor<FooDTO> dtoConstructor; Mapper(Class<Foo> fooClass, Class<FooDTO> fooDTOClass){
It seems to break if I pass a generic class of objects to create .
Please note that in my actual implementation, all FooDTO classes FooDTO distributed from a common superclass, i.e. The Mapper signature is actually something like Mapper<Foo,DTO<Foo>> . I do not think this is relevant here.
EDIT 2 :
Actually the proposal to change the line G<?> t = create(o.getClass()); on G<Object> t = (G<Object>) create(o.getClass()); worked in this context.
Unfortunately, I did not understand that the fact that my class is more complex actually has an effect. Here is a more complete example (I apologize for the phased question):
public class Y<T> { } public class G<X, Z extends Y<X>> { public G(Class<X> c, Class<Z> s) { } public void m(X x) { } public static <T, S extends Y<T>> G<T, S> create(Class<T> c) { Class<S> s = null;
In this case, the Class<S> object is created using reflection and some common location for objects of this type. This part works great and should not be relevant to this discussion. The error I'm getting now is this:
inconvertible types found : G<capture#155 of ? extends java.lang.Object,Y<capture#155 of ? extends java.lang.Object>> required: G<java.lang.Object,Y<java.lang.Object>>
And if I change the incriminated line to:
G<Object, Y<Object>> t = (G<Object, Y<Object>>) create(o.getClass());
I get a similar error:
java: inconvertible types required: G<java.lang.Object,Y<java.lang.Object>> found: G<capture#1 of ? extends java.lang.Object,Y<capture#1 of ? extends java.lang.Object>>
Once again, I apologize for the piecemeal information. I understand this while I'm writing.