Whisper the terrible words type erasure to any moderately experienced Java programmer and you're likely to see:
- fear
- loathing
- soul-crushing sense of defeat
- all of the above
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..."
Comments
Post a Comment