Posted on 06 March 2010

Recently, I had to look back at some of the pieces I coded a few years back and
found a generics trick I had forgotten. Thought of sharing with everyone.

Most of the time when we are working with Dependency Injection, Caching, Helper
methods etc, we are not aware of the type of object a method may return. Perfect
examples to the same could be the use of HibernateTemplate, EHCache,
ApplicationContext.getBean, and alike. What we do at such hour is simply, define
the return type of the method as Object and then cast back to the required original such as,

publicconsume(){Objectobjct=myMethod(...);Listlist=(List)object;}publicObjectmyMethod(arguments...){// some code}

This approach induces some amount of boiler plate code. We always have to get an
object and then cast it to the required type. In today’s era, when you send it for
code review, a surely comment is to check for the type before casting leading to
some more boiler plate.

Well, starting Java 5 there is this neat generics trick which you may employ to
reduce all such boiler plate code. Consider an example where we want to pick up
a bean from the ApplicationContext and return back.