首页 > 代码库 > java反射机制reflect 分析Object对象

java反射机制reflect 分析Object对象

直接看一个实例吧

</pre><pre name="code" class="java">package reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

class Behavior {
	private long distance;
	private long amount;

	public long getDistance() {
		return distance;
	}

	public void setDistance(long distance) {
		this.distance = distance;
	}

	public long getAmount() {
		return amount;
	}

	public void setAmount(long amount) {
		this.amount = amount;
	}

	public String walk() {
		return "走了" + distance + "公里";
	}

	public String eat() {
		return "吃了" + amount + "碗饭";
	}

}

class Person extends Behavior {
	private String username;
	private int age;
	static char sex;
	protected final double high = 0;
	public String[] hobby;
	public Behavior be;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public char getSex() {
		return sex;
	}

	public static void setSex(char sex) {
		Person.sex = sex;
	}

	public double getHigh() {
		return high;
	}

	public String[] getHobby() {
		return hobby;
	}

	public void setHobby(String[] hobby) {
		this.hobby = hobby;
	}

	public Behavior getBe() {
		return be;
	}

	public void setBe(Behavior be) {
		this.be = be;
	}

	public Person() {

	}

	public Person(String username, int age, String[] hobby, Behavior be) {
		super();
		this.username = username;
		this.age = age;
		this.hobby = hobby;
		this.be = be;
	}

	public String fun2(int a, int b, int c, String d, double e, float f, char g) {
		return a + b + c + d + e + f + g;
	}

}

public class ObjectAnalyzer {

	public void printFields(Field[] fields) {
		for (int i = 0; i < fields.length; i++) {
			Field f = fields[i];
			int m = f.getModifiers();
			String name = f.getName();
			Class<?> clazz = f.getType();
			System.out.println(Modifier.toString(m) + " " + clazz.getName()
					+ " " + name);
		}
	}

	public void printConstructor(Constructor<?>[] c) {
		for (int i = 0; i < c.length; i++) {
			Constructor<?> con = c[i];
			String name = con.getName();
			int m = con.getModifiers();
			Class<?>[] clazz = con.getParameterTypes();
			System.out.print(Modifier.toString(m) + " " + name + "(");
			for (int j = 0; j < clazz.length; j++) {
				System.out.print(clazz[j].getName() + " ");
			}
			System.out.println(")");
		}
	}

	public void printMethod(Method[] methods) {
		for (int i = 0; i < methods.length; i++) {
			Method m = methods[i];
			int modi = m.getModifiers();
			String name = m.getName();
			Class<?> returnType = m.getReturnType();
			Class<?>[] paramType = m.getParameterTypes();
			System.out.print(Modifier.toString(modi) + " "
					+ returnType.getName() + " " + name + "(");
			for (int j = 0; j < paramType.length; j++) {
				System.out.print(paramType[j].getName() + " ");
			}
			System.out.println(")");
		}
	}

	public void analyzer(Object obj) {
		Class<?> clazz = obj.getClass();
		Field[] fields = clazz.getDeclaredFields();
		Constructor<?>[] c = clazz.getDeclaredConstructors();
		Method[] method = clazz.getDeclaredMethods();
		//Method[] method = clazz.getMethods();
		System.out.println("=====属性变量的分析=====");
		System.out.println("Fields长度:" + fields.length + " 变量如下");
		printFields(fields);
		System.out.println("=====构造方法分析=====");
		printConstructor(c);
		System.out.println("=====方法分析=====");
		printMethod(method);

	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Behavior be = new Behavior();
		Person p = new Person("chiwei", 26, new String[] { "1", "2" }, be);
		new ObjectAnalyzer().analyzer(p);
	}

}

运行结果如下:

=====属性变量的分析=====
Fields长度:6 变量如下
private java.lang.String username
private int age
static char sex
protected final double high
public [Ljava.lang.String; hobby
public reflect.Behavior be
=====构造方法分析=====
public reflect.Person()
public reflect.Person(java.lang.String int [Ljava.lang.String; reflect.Behavior )
=====方法分析=====
public void setUsername(java.lang.String )
public int getAge()
public void setAge(int )
public char getSex()
public static void setSex(char )
public double getHigh()
public [Ljava.lang.String; getHobby()
public void setHobby([Ljava.lang.String; )
public reflect.Behavior getBe()
public void setBe(reflect.Behavior )
public java.lang.String fun2(int int int java.lang.String double float char )
public java.lang.String getUsername()