利用java的放射机制分析类,包括构造函数,方法、域(成员变量)
1.类
Demo.java
package com.wei;
public class Demo
{
private int age;
public String i="null";
private Demo()
{
}
public Demo(int age)
{
this.age = age;
}
public String toString()
{
return "["+this.getClass()+" age="+this.age+" i="+this.i+"]";
}
private void xxx()
{
}
}
ReflectionTest.java
package com.wei;
import java.lang.reflect.*;
import java.util.*;
/*利用反射来分析类*/
public class ReflectionTest
{
/**
* @param args
*/
public static void main(String[] args)
{
// TODO Auto-generated method stub
String name;
if(args.length>0)
{
name = args[0];
}
else
{
System.out.println("输入类的名字,例如:java.lang.String ");
Scanner in = new Scanner(System.in);
name = in.next();
String s = "类名输入完成!以下是类的分析结果:";
System.out.printf("%s\n",s);
}
try
{
Class c1 = Class.forName(name);
Class superc1 = c1.getSuperclass();
//打印 类的修饰符 public private final static...
String modifiers = Modifier.toString(c1.getModifiers());
if(modifiers.length()>0)
{
System.out.print(modifiers+" ");
}
//打印 类名
System.out.print("class "+name);
//打印 类的超类
if(superc1 !=null && superc1 != Object.class)
{
System.out.print(" extends "+superc1.getName());
}
System.out.print("\n{\n");
printConstractors(c1);
System.out.println();
printMethods(c1);
System.out.println();
printFields(c1);
System.out.print("\n}\n");
}
catch (ClassNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
System.exit(0);
}
//方法函数
public static void printMethods(Class cl)
{
Method[] methods = cl.getDeclaredMethods();
for (Method m : methods)
{
Class retType = m.getReturnType();
String name = m.getName();
System.out.print(" ");
// print modifiers, return type and method name
String modifiers = Modifier.toString(m.getModifiers());
if (modifiers.length() > 0)
{
System.out.print(modifiers + " ");
}
System.out.print(retType.getName() + " " + name + "(");
// print parameter types
Class[] paramTypes = m.getParameterTypes();
for (int j = 0; j < paramTypes.length; j++)
{
if (j > 0) System.out.print(", ");
System.out.print(paramTypes[j].getName());
}
System.out.println(");");
}
}
//域
private static void printFields(Class c1)
{
// TODO Auto-generated method stub
// Field[] fields = c1.getFields();
Field[] fields = c1.getDeclaredFields();
for (Field f : fields)
{
//域的类型----int double...
Class type = f.getType();
//域的名字 XXX...
String name = f.getName();
System.out.print(" ");
//打印域的修饰符---public private final static...
String modifier = Modifier.toString(f.getModifiers());
if(modifier.length()>0)
{
System.out.print(modifier+" ");
}
//打印 域的类型和名字 ---int SIZE...
System.out.println(type.getName()+" "+name+";");
}
}
//构造函数
private static void printConstractors(Class c1)
{
// TODO Auto-generated method stub
Constructor[] constructors = c1.getDeclaredConstructors();
for (Constructor c : constructors)
{
String name = c.getName();
System.out.print(" ");
//打印 类的修饰符 public private final static...
String modifier = Modifier.toString(c.getModifiers());
if(modifier.length()>0)
{
System.out.print(modifier+" ");
}
System.out.print(name+"(");
//构造函数的参数
Class[] paramTypes = c.getParameterTypes();
for(int j=0; j<paramTypes.length; j++)
{
if(j>0) System.out.print(", ");
System.out.print(paramTypes[j].getName());
}
System.out.print(");\n");
}
}
}