Created
October 15, 2013 16:53
-
-
Save archena/6994793 to your computer and use it in GitHub Desktop.
Reified generics in Java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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