logo!


Some Little Generics Suggestion

Posted in java, technology by dabar on the June 3rd, 2008

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.

6 Responses to 'Some Little Generics Suggestion'

Subscribe to comments with RSS or TrackBack to 'Some Little Generics Suggestion'.


  1. on June 4th, 2008 at 3:08 am

    Wow! Nice idea…

    I’ve been struggling to understand/make best of the Java Generics feature for some time now and I still have mixed feelings about it (although I tend to think more often that they are a good thing with not so good implementation).

    However, what you are describing here may pertain to a ‘more general’ issue, closely related to code generation based on annotations…


  2. on June 4th, 2008 at 7:29 am

    [...] This is our own view of the Java Generics and how maybe it can be better [...]

  3. prashant said,

    on June 4th, 2008 at 12:40 pm

    nice thought…

    Thanks
    Prashant Jalasutram
    http://prashantjalasutram.blogspot.com/

  4. Sakuraba said,

    on June 5th, 2008 at 1:31 am

    Uhum, did you ever had a chance to look at the Guice framework? This provide you that kind of injection facilities. Besides, if you dont understand Generics completely, just have a look at frameworks that use it extensively and successfully at the same time (just like Guice).

  5. dabar said,

    on June 5th, 2008 at 4:55 am

    Guice is a framework. Java is a Language, Generics is a Java Language Feature. I am talking about what we will be able to do in core Java without relying on Guice. Meanwhile I prefer Spring to Guice.


  6. on June 5th, 2008 at 8:28 am

    I personally have an issue with both Generics AND Annotations in Java: And combined they have the potential to create one hell of a mess.

    Nevertheless, you’re solution certainly has merit…but it’s really just a workaround for the fact that type info isn’t reified in Generics under Java. If that were the case you wouldn’t have to go through this at all.

    A far better solution would be for Sun (or whomever) to implement reification, thereby giving you the flexibility that Generics were meant to have.

Leave a Reply