Z
z-man
Let me say: generics type erasure is just a misfeature that's
embarrassingly hiped as a "feature".
Is there any wizard that can solve this annoying problem (dynamic
instantiation from a type variable using its default constructor -- see
example below)?
Dropping all the type information at compile time is definitely a shame.
protected <T,TSuper extends MySuperType<T>> void setEntry(
String key,
T value
)
{
TSuper entry = myMap.get(key));
if(entry == null)
{
// Owing to that crappy type erasure, neither this works...
myMap.put(key,entry = new TSuper());
// ...nor this one (no 'class' member available).
myMap.put(key,entry = TSuper.class.newInstance());
}
entry.setValue(value);
}
embarrassingly hiped as a "feature".
Is there any wizard that can solve this annoying problem (dynamic
instantiation from a type variable using its default constructor -- see
example below)?
Dropping all the type information at compile time is definitely a shame.
protected <T,TSuper extends MySuperType<T>> void setEntry(
String key,
T value
)
{
TSuper entry = myMap.get(key));
if(entry == null)
{
// Owing to that crappy type erasure, neither this works...
myMap.put(key,entry = new TSuper());
// ...nor this one (no 'class' member available).
myMap.put(key,entry = TSuper.class.newInstance());
}
entry.setValue(value);
}