java 반사 메커니즘 reflect 분석 Object 객체

4835 단어
그냥 실례를 하나 볼게요.
</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 protecteted final doubling username private int age static char sex protecteted final double high public[Ljang.ang.String; behavior be======public reflect.person() public javavalang.lang.lang.string.string.String.string;;;puring puring pring pring ustrint int int int int int int int int int int agage int int ag() 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()

좋은 웹페이지 즐겨찾기