首页 > 代码库 > JAVA 反射

JAVA 反射

Person.java

 

 

package reflect;public class Person {	private String name;	private int age;		public Person() {			}		public Person(String name) {		this.name = name;	}		public Person(int age) {		this.age = age;	}		public Person(String name, int age) {		this.name = name;		this.age = age;	}		public void setName(String name) {		this.name = name;	}		public void setAge(int age) {		this.age = age;	}		public String getName() {		return name;	}		public int getAge() { 		return age;	}		@Override	public String toString() {		return "[name = " + name + ", age = " + age + "]";	}	}

 ReflectTest.java

package reflect;import java.lang.reflect.*;public class ReflectTest {	/**	 * @param args	 */	public static void main(String[] args) {		Class<?> cl = null;		try {			cl = Class.forName("reflect.Person");			String modifiers = Modifier.toString(cl.getModifiers());			if (modifiers.length() > 0)				System.out.print(modifiers + " ");			System.out.println(cl.getName() + "{");		}		catch (Exception e) {			e.printStackTrace();		}		printFields(cl);		System.out.println();		printConstructors(cl);		System.out.println();		printMethods(cl);		System.out.println("}");	}	public static void printConstructors(Class<?> cl){		Constructor<?>[] c = cl.getConstructors();		for (int i = 0; i < c.length; i++) {			String modifiers = Modifier.toString(c[i].getModifiers());			if (modifiers.length() > 0)				System.out.print(modifiers + " ");			String name = c[i].getName();			System.out.print(name + "(");			Class<?>[] paramTypes = c[i].getParameterTypes();			for (int j = 0; j < paramTypes.length-1; j++) 				System.out.print(paramTypes[j].getName() + ",");			if (paramTypes.length > 0)				System.out.print(paramTypes[paramTypes.length-1]);			System.out.println(");");		}	}		public static void printFields(Class<?> cl) {		Field[] fields = cl.getDeclaredFields();		for (Field f : fields) {			String modifiers = Modifier.toString(f.getModifiers());			if (modifiers.length() > 0)				System.out.print(modifiers + " ");			String name = f.getName();			System.out.println(name + ";");		}	}		public static void printMethods(Class<?> cl) {		Method[] methods = cl.getDeclaredMethods();		for (Method m : methods) {			String modifiers = Modifier.toString(m.getModifiers());			if (modifiers.length() > 0)				System.out.print(modifiers + " "); 			Class<?> retType = m.getReturnType();			String name = m.getName();			System.out.print(retType.getName() + " " + name + "(");			Class<?>[] paramTypes = m.getParameterTypes(); 			for (int i = 0; i < paramTypes.length; i++) {				if (i > 0)					System.out.print(", ");				System.out.print(paramTypes[i]);			}			System.out.println(");");		}	}}

 打印结果:

public reflect.Person{private name;private age;public reflect.Person(class java.lang.String);public reflect.Person(int);public reflect.Person(java.lang.String,int);public reflect.Person();public java.lang.String toString();public java.lang.String getName();public void setName(class java.lang.String);public void setAge(int);public int getAge();}

 

JAVA 反射