java - How to get the type for a List<aType>? -
i have defined object takes type parameter in constructor :
public dynparameterdaoimpl(class<t1> type) { super(); ... }
the instanciation works doing
dynparameterdaoimpl myvar = new dynparameterdaoimpl(atype.class);
is there way type being list. following not work..
dynparameterdaoimpl myvar = new dynparameterdaoimpl(list<atype>.class);
no.
list<atype>
type not class because type erasure ensures list<x>
represented same class list<y>
reference types x
, y
.
for example,
list<string> strings = new arraylist<string>(); // legal boolean b = strings instanceof list<number>; // compiler rejects
the compiler rejects instanceof
check because there isn't enough information available virtual machine distinguish between list<string>
, list<number>
.
if possible class value non-class type list<string>
, reflective equivalent
class<? extends list<string>> stringlistclass = strings.getclass(); // illegal class<? extends list<number>> numberlistclass = list<number>.class; // illegal boolean b = numberlistclass.isassignablefrom(stringlistclass);
would pass silent violation of type-safety.
Comments
Post a Comment