The following code is provided for educational/illustrative purposes only.
This is probably just a clever hack. This is not kosher. To all students of the Java language, please do not start using this in your day-to-day code. Let me be the first to admit that I haven’t used this extensively and I wouldn’t recommend using it for production.
Disclaimers aside, I won’t give any more background into Java generics. For those in need of an introduction, Angelika Langer’s Java Generics FAQ explains a lot of things with more clarity and depth than I ever could.
The basic problem is, given a generic class, we would like to determine the type parameter to that class at run-time..
I’ve found myself needing to do this more than once. The simplest example I can contrive is one when we want the
Base class to perform run-time type-checking of some input parameter. Continue reading