generics3


stáhnout snippet
zobrazení: SyntaxHighlighter | GeSHi | Holý text
import java.lang.reflect.Field;
import java.lang.reflect.GenericDeclaration;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.util.ArrayList;
import java.util.List;

public class Main<T extends Number> {

	
	public List<T> entries = new ArrayList<T>();
	
	
	public static void main(String[] args) throws Exception {
		
		Main<Float> main = new Main<Float>();
		
		// How to get the Float type at runtime - can't be done, erased
		
		
		// I can only play with class information - upper bound Number is know only 
		
		Field field = getField();
		ParameterizedType genericType = (ParameterizedType) field.getGenericType();
		for(Type type : genericType.getActualTypeArguments()) {
			TypeVariable tv = (TypeVariable) type;
			GenericDeclaration genericDeclaration = tv.getGenericDeclaration();
			Type[] bounds = tv.getBounds();
			String name = tv.getName();
			System.out.println();
		}
	}
	
	
	public static Field getField() throws Exception {
		Class clazz = Main.class;
		return clazz.getField("entries");
	}
	
}


java 43 řádků | 2011-10-06 20:11:33