Some Little Generics Suggestion
There has been a lot of issues about Generics generally. Personally my dislike for Generics was rekindled again when my favourite web framework (Wicket) almost had issues taking us the generics way.
Please, I am no JVM engineer but with my little knowledge of Java, I kind of use my imagination to think about ways of making things simpler.
Now, For instance take a look at this simple errornoues implementation of a newbie Generics user:
public class SomeGenericType<T> {
public Class<T> getGenericsTypeClass(){
return T.class; //off course this is not possible. I have taken this step once. Thank God for modern IDEs
}
}
Now after learning the right thing, the code above will now become something like this:
public class SomeGenericsType<T> {
private Class<T> genericTypeClass;
public SomeGenericsType(Class<T> clazz){
this.genericTypeClass = clazz;
}
public Class<T> getGenericsTypeClass(){
return genericTypeClass; //now this will compile
}
}
because of this implementation, we are suffering from too much repitition like this
SomeGenericsType<MyType> someGenericType = new SomeGenericType<MyType>(MyType.class);
and can be harsh on API designers and users.
look at this
SomeOtherTypes<MyKey,MyType> someOtherTypes = new SomeOtherTypes<MyKey,MyType>(MyKey.class, MyType.class); //I think this is not fun
However, JDK5.0 introduced a particular feature called Annotation and from my little knowledge, I know that certain annotations like @SuppressWarning gives messages to the compiler.
Now this is what my little imagination is messing with.
Why will it be impossible or why is it not technically sound or feasible or unreasonable to have this little enhancement to Java giving us a little assistance from the compiler with Generics.
public class SomeGenericsType<T>{
@InjectType //a compiler annotation
private Class<T> genericsTypeClass;
public SomeGenericsType(){
//no need to pass Class as constructor argument again
}
public Class<T> getGenericsTypeClass(){
return genericTypeClass;
}
}
Now during compilation, the compiler will read the annotation @InjectType and will inject the Parameter Types. It will simply help us reengineer the code so that Class<T> genericsTypeClass changes to Class genericTypeClass = MyType.class;
I think like this, we can have a little hint of our generics Parameter Types at runtime.
Thats all my suggestion. Am sure you can understand where I am going with this.
OK, bury the little idea,
but I think Generics is pretty driving some community crazy and something needs to be done about this.
Thank You for your time.
