reflect学习

package cn.zen.entity;

import java.util.List;

public class Person {
	public String pubName = "hello frist reflect";
	private int prvAge;
	private String prvEmail;

	public Person() {
		System.out.println("No arguments constructor");
	}

	public Person(int _age, String _email) {
		System.out.println("Age:" + _age + "——>Email:" + _email);
	}

	public Person(String[] _strArr) {
		System.out.println("Array:" + _strArr.toString());
	}

	private Person(List list) {
		System.out.println(list.toString());
	}

	public void method01() {
		System.out.println("this is one no arguments method…");
	}

	public void method02(String _name, int[] _intArr) {
		System.out.println("Name:" + _name + "   array:" + _intArr[0]);
	}

	public void method021(String[] _strArr) {
		System.out.println(_strArr[0]);
	}

	public String[] method03(String _name) {
		return new String[] { _name, "wangbadan" };
	}

	private void prvMethod() {
		System.out.println("this is private method…");
	}

	public static void staticMethod(String _hello) {
		System.out.println(_hello+" this is on static method");
	}
	
	public static void main(String[] args) {
		System.out.println("this is main method…");
	}
}


package cn.zen.reflect;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

import cn.zen.entity.Person;

public class Ex_Chapter01 {

	@Test
	public void reflectNArgCst() throws Exception {
		Class clazz = Class.forName("cn.zen.entity.Person");
		// Constructor c = clazz.getConstructor(null);
		// Person p = (Person) c.newInstance(null);
		// 等同于下面的
		Person p = (Person) clazz.newInstance();
		System.out.println(p.pubName);
	}

	@Test
	public void reflectCst() throws Exception {
		Class clazz = Class.forName("cn.zen.entity.Person");
		Constructor c = clazz.getConstructor(int.class, String.class);
		Person p = (Person) c.newInstance(22, "xixihaha@163.com");
		System.out.println(p.pubName);
	}

	@Test
	public void reflectCst1() throws Exception {
		Class clazz = Class.forName("cn.zen.entity.Person");
		Constructor c = clazz.getConstructor(String[].class);
		Person p = (Person) c.newInstance((Object) new String[] { "zhangsan",
				"lisi" });
		System.out.println(p.pubName);
	}

	@Test
	public void reflectPrvCst() throws Exception {
		List list = new ArrayList();
		list.add("ZhangSan");
		list.add("LiSi");

		Class clazz = Class.forName("cn.zen.entity.Person");
		Constructor c = clazz.getDeclaredConstructor(List.class);
		c.setAccessible(true);// 暴力反射
		Person p = (Person) c.newInstance(list);
	}

	@Test
	public void reflectMethod01() throws Exception {
		Class clazz = Class.forName("cn.zen.entity.Person");
		Person p = (Person) clazz.newInstance();
		Method method = clazz.getMethod("method01", null);
		method.invoke(p, null);
	}

	@Test
	public void reflectMethod02() throws Exception {
		Class clazz = Class.forName("cn.zen.entity.Person");
		Person p = (Person) clazz.newInstance();
		Method method = clazz.getMethod("method02", String.class, int[].class);
		method.invoke(p, "xiaopianzi", new int[] { 123, 12 });

		Method method1 = clazz.getMethod("method021", String[].class);
		method1.invoke(p, (Object) new String[] { "yuncaile", "haha^-^" });
	}

	@Test
	public void reflectMethod03() throws Exception {
		Class clazz = Class.forName("cn.zen.entity.Person");
		Person p = (Person) clazz.newInstance();
		Method method = clazz.getMethod("method03", String.class);
		String[] strArr = (String[]) method.invoke(p, "xiaopianzi");

		for (String str : strArr) {
			System.out.println(str);
		}
	}

	@Test
	public void reflectPrvMethod() throws Exception {
		Class clazz = Class.forName("cn.zen.entity.Person");
		Person p = (Person) clazz.newInstance();

		Method method = clazz.getDeclaredMethod("prvMethod", null);
		method.setAccessible(true);
		method.invoke(p, null);
	}

	@Test
	public void reflectStaticMethod() throws Exception {
		Class clazz = Class.forName("cn.zen.entity.Person");
		Person p = (Person) clazz.newInstance();

		Method method = clazz.getMethod("staticMethod", String.class);
		method.invoke(null, "Hello!");
	}

	@Test
	public void refelctMain() throws Exception {
		Class clazz = Class.forName("cn.zen.entity.Person");
		Person p = (Person) clazz.newInstance();

		Method method = clazz.getMethod("main", String[].class);
		method.invoke(p, (Object) new String[] { "xxx" });
	}

	@Test
	public void reflectField() throws Exception {
		Class clazz = Class.forName("cn.zen.entity.Person");
		Person p = (Person) clazz.newInstance();

		Field nameFld = clazz.getField("pubName");

		Class fldClass = nameFld.getType();

		System.out.println(nameFld.get(p));

		if (fldClass.equals(String.class)) {
			nameFld.set(p, "xiaolizi");
		}
		System.out.println(nameFld.get(p));
	}

	@Test
	public void reflectPrvField() throws Exception {
		Class clazz = Class.forName("cn.zen.entity.Person");
		Person p = (Person) clazz.newInstance();

		Field ageFld = clazz.getDeclaredField("prvAge");
		Field emailFld = clazz.getDeclaredField("prvEmail");

		ageFld.setAccessible(true);
		emailFld.setAccessible(true);

		if (ageFld.getType().equals(int.class)) {
			ageFld.setInt(p, 23);
		}

		if (emailFld.getType().equals(String.class)) {
			emailFld.set(p, "xxxx@163.com");
		}

		System.out.println(ageFld.getInt(p));
		System.out.println(emailFld.get(p));
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值