Skip to content

Instantly share code, notes, and snippets.

@onelittlenightmusic
Created July 4, 2013 03:25
Show Gist options
  • Save onelittlenightmusic/5924699 to your computer and use it in GitHub Desktop.
Save onelittlenightmusic/5924699 to your computer and use it in GitHub Desktop.
import java.lang.reflect.ParameterizedType;
import java.util.List;
import java.util.Map;
import java.util.Set;
public class Main {
/**
* @param args
*/
private List<String> list;
private Set<Integer> set;
private Map<String, Long> map;
public static void main(String[] args) {
System.out.println("Hello world!");
ParameterizedType listPt;
try {
listPt = (ParameterizedType) Main.class.getDeclaredField("list")
.getGenericType();
System.out.printf("list: %s\n",
listPt.getActualTypeArguments()[0].toString());
ParameterizedType setPt = (ParameterizedType) Main.class
.getDeclaredField("set").getGenericType();
System.out.printf("set: %s\n",
setPt.getActualTypeArguments()[0].toString());
ParameterizedType mapPt = (ParameterizedType) Main.class
.getDeclaredField("map").getGenericType();
System.out.printf("map: %s, %s\n",
mapPt.getActualTypeArguments()[0].toString(),
mapPt.getActualTypeArguments()[1].toString());
} catch (NoSuchFieldException | SecurityException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment