package d;
//==========================用于反射的类=====================================
import java.io.InputStream;
import java.util.List;
public class Person {
public int value = 2222;
// ======================构造函数======================
public Person() {
System.out.println("person");
}
public Person(String name) {
System.out.println(name);
}
public Person(String name, int password) {
System.out.println(name + ":" + password);
}
private Person(List list) {
System.out.println(list);
}
// ======================构造方法===========================
public void arg() {
System.out.println("Hello");
}
public void arg(String name, int num) {
System.out.println(name + ":" + num);
}
public Class[] arg(String name, int[] num) {
return new Class[] { String.class, int.class };
}
private void arg(InputStream in) {
System.out.println(in);
}
public static void arg(int num) {
System.out.println(num);
}
}
package d;
//====================反射Person的构造函数============================
import java.lang.reflect.Constructor;
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class reflectConstruct {
// 反射Person类的public Person()构造函数
@Test
public void test() throws Exception {
Class clazz = Class.forName("d.Person");
Constructor c = clazz.getConstructor(null);
Person p = (Person) c.newInstance(null);
System.out.println(p.value);
}
// 反射Person类的public Person(String name)构造函数
@Test
public void test1() throws Exception {
Class clazz = Class.forName("d.Person");
Constructor c = clazz.getConstructor(String.class);
Person p = (Person) c.newInstance("cccccccccc");
System.out.println(p.value);
}
// 反射Person类的public Person(String name, int password)构造函数
@Test
public void test2() throws Exception {
Class clazz = Class.forName("d.Person");
Constructor c = clazz.getConstructor(String.class, int.class);
Person p = (Person) c.newInstance("vvv", 8989);
System.out.println(p.value);
}
// 反射Person类的private Person(List list)构造函数
@Test
public void test3() throws Exception {
Class claxx = Class.forName("d.Person");
Constructor c = claxx.getDeclaredConstructor(List.class);
c.setAccessible(true);
Person p = (Person) c.newInstance(new ArrayList());
System.out.println(p.value);
}
}
package d;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Method;
import org.junit.Test;
public class reflectMethod {
//调用Person类的public void arg() 方法
@Test
public void test1() throws Exception {
Person p = new Person();
Class claxx = Class.forName("d.Person");
Method method = claxx.getMethod("arg", null);
method.invoke(p, null);
}
//调用Person类的public void arg(String name, int num) 方法
@Test
public void test2() throws Exception {
Person p = new Person();
Class clazz = Class.forName("d.Person");
Method method = clazz.getMethod("arg", String.class, int.class);
method.invoke(p, "Congratlation", 520);
}
//调用Person类的public Class[] arg(String name, int[] num) 方法
@Test
public void test3() throws Exception {
Person p = new Person();
Class clazz = Class.forName("d.Person");
Method method = clazz.getMethod("arg", String.class, int[].class);
Class[] clz = (Class[]) method.invoke(p, "loveyou", new int[] { 1, 3,
1, 4 });
System.out.println(clz[0] + "-" + clz[1]);
}
//调用Person类的private void arg(InputStream in) 方法
@Test
public void test4() throws Exception {
Person p = new Person();
Class claxx = Class.forName("d.Person");
Method method = claxx.getDeclaredMethod("arg", InputStream.class);//得到所有方法
method.setAccessible(true);//暴力访问
method.invoke(p, new FileInputStream("c://IO.SYS"));
}
//调用Person类的public static void arg(int num) 方法
@Test
public void test5() throws Exception{
Class clavv=Class.forName("d.Person");
Method method=clavv.getMethod("arg",int.class);
method.invoke(null, 5201314);
}
}
StackTraceElement[] temp=Thread.currentThread().getStackTrace();
StackTraceElement a=(StackTraceElement)temp[2];
System.out.println("----from--"+a.getMethodName()+"--method----------to use-refreshcart--------");//获取哪个方法调用当前方法
System.out.println("----from--"+a.getMethodName()+"--method----------to use-refreshcart--------");//获取哪个类调用当前方法