Thursday, November 26, 2009

K ?

Whisper the terrible words type erasure to any moderately experienced Java programmer and you're likely to see:
  1. fear
  2. loathing
  3. soul-crushing sense of defeat
  4. all of the above
Now if you are a moderately experienced Java programmer, but you're thinking to yourself "what's type erasure ?" my advice is this:

Stop reading now !

Sometimes ignorance really is bliss.

My own rule of thumb, when confronted by the need to query the identity of K, or V, or T or any of their evil friends, in the innards of a class with generic type parameters, is to run away. Sadly, there are circumstances when that's just not an option. Although I deeply empathise with those who equate Java reflection with incest and folk dancing there are times when it's black magic or nothing. But where to find the magic ?

Enter Richard Gomes and his article: Using TypeTokens to retrieve generic parameters. This demonstrates the seemingly impossible feat of pulling erased rabbits out of Java's hat.

I'm off to try it now... "Nothing up my sleeve..."


No comments:

Post a Comment