泛型反射

import java.lang.reflect.GenericArrayType;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.TypeVariable;
import java.lang.reflect.WildcardType;
import java.util.Scanner;


public class GenericReflectionTest {
	public static void main(String[] args){
		String name;
		if(args.length > 0){
			name = args[0];
		}else{
			Scanner in = new Scanner(System.in);
			System.out.println("Enter class name(e.g. java.util.Date):");
			name = in.next();
		}
		
		try{
			Class<?> cl = Class.forName(name);
			printClass(cl);
			for(Method m:cl.getDeclaredMethods()){
				printMethod(m);
			}
		}catch(ClassNotFoundException e){
			e.printStackTrace();
		}
	}
	
	public static void printClass(Class<?> cl){
		System.out.print(cl);
		printTypes(cl.getTypeParameters(),"<",",",">");
		Type sc = cl.getGenericSuperclass();
		if(sc!=null){
			System.out.print(" extends ");
			printType(sc);
		}
		printTypes(cl.getGenericInterfaces()," implements ",",","");
		System.out.println();
	}
	
	public static void printMethod(Method m){
		String name = m.getName();
		System.out.print(Modifier.toString(m.getModifiers()));
		System.out.print(" ");
		printTypes(m.getTypeParameters(),"<",",",">");
		printType(m.getGenericReturnType());
		System.out.print(" ");
		System.out.print(name);
		System.out.print("(");
		printTypes(m.getGenericParameterTypes(),"",",","");
		System.out.println(")");
	}
	
	public static void printTypes(Type[] types,String pre,String sep,String suf){
		if(types.length > 0) System.out.print(pre);
		for(int i=0;i<types.length;i++){
			if(i>0) System.out.print(sep);
			printType(types[i]);
		}
		if(types.length > 0) System.out.print(suf);
	}
	
	public static void printType(Type type){
		if(type instanceof Class){
			Class<?> t = (Class<?>)type;
			System.out.print(t.getName());
		}else if(type instanceof TypeVariable){
			TypeVariable<?> t = (TypeVariable<?>)type;
			System.out.print(t.getName());
			printTypes(t.getBounds()," extends ","&","");
		}else if(type instanceof WildcardType){
			WildcardType t = (WildcardType) type;
			System.out.print("?");
			printTypes(t.getLowerBounds()," extends "," & ","");
			printTypes(t.getUpperBounds()," super "," & ","");
		}else if(type instanceof ParameterizedType){
			ParameterizedType t = (ParameterizedType) type;
			Type owner = t.getOwnerType();
			if(owner != null){printType(owner);System.out.print(".");}
			printType(t.getRawType());
			printTypes(t.getActualTypeArguments(),"<",",",">");
		}else if(type instanceof GenericArrayType){
			GenericArrayType t = (GenericArrayType) type;
			System.out.print("");
			printType(t.getGenericComponentType());
			System.out.print("[]");
		}
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值