Automatic creation of Type-Tokens: Is it already there?
The use of Class objects as "type-tokens" is a well-established pattern in Java programming.
It just would be so much nicer if the required Class objects could be automatically supplied by the compiler, woudn't it?
Surprisingly, this feature is already present. The following code shows a class which, if instantiated, automagically receives the correct Class object.
publicclass AutoGeneric<T>{
privatefinal Class<T> type;
@SuppressWarnings("unchecked")
public AutoGeneric(T... argv){
this.type = (Class<T>)argv.getClass().getComponentType();
}
public Class<T> getType(){
return type;
}
publicstaticvoid main(String[] args){
System.out.println(new AutoGeneric<String>().getType());
}
}
What do you think: is that a recommendable way of implementing "automatic type tokens"?

