Yes, for compatibility reasons, generics are really only a compile-time check. By run-time, it's as if you didn't use them at all. That works fine most of the time, and it allowed generics to be added to the Java Language Specification without making big changes to the byte code. Sometimes though, it's a really pain in the butt. You could hack together a solution to your problem by storing the class you need as a member field.
I puzzled over this far longer than I should have, but it's not my nature to give up. And I've found a solution. There's one sacrifice: you *must* instantiate your class as an anonymous class; by doing so, type erasure doesn't happen. Then you may add this method to your generic class and you'll have access to the type.
If anyone can see a way to simplify this (especially with regards to getting the actual short name of the type from the longer, complete class name), I'd be very interested:
No. No. No. No. Changed my mind. Wanna come down. To see this tiny ad: