Skip to content

Instantly share code, notes, and snippets.

@archena
Created October 15, 2013 16:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save archena/6994793 to your computer and use it in GitHub Desktop.
Save archena/6994793 to your computer and use it in GitHub Desktop.
Reified generics in Java
class ReifiableType<T> {
final T value;
final Class<T> type;
public ReifiableType(T value, Class<T> type) {
this.value = value;
this.type = type;
}
}
class SomeGenericClass<T extends ReifiableType> {
public SomeGenericClass(T param) {
System.out.println("I was constructed with a parameter of type " + param.type + "!");
System.out.println("...where is your CLR now?");
}
}
class ReifiableTest {
public static void main(String[] args) {
ReifiableType<String> p = new ReifiableType<>("A string", String.class); // We assume the caller is honest
new SomeGenericClass<ReifiableType<String>>(p);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment