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));
}
}