java反射实例

java反射实例:
import java.lang.reflect.*;
import java.util.*;

public class GenericReflectionTest
{
public static void main(String[] args)
{
// read class name from command-line args or user input
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
{
// print generic info for class and public methods
Class cl = Class.forName(name);
printClass(cl);
for (Method m : cl.getDeclaredMethods())
printMethod(m);
printFiles(cl);
}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
}

private static void printFiles(Class cl) {
// TODO Auto-generated method stub
Field[] files=cl.getDeclaredFields();
for (Field field : files) {
field.setAccessible(true);
System.out.print(Modifier.toString(field.getModifiers())+ " ");
printType(field.getGenericType());
System.out.print(" "+field.getName()+" \n");
}
}

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("[]");
}

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值