反射(Reflection)
- 在运行时判断任意一个对象所属的类
- 在运行时构造任意一个类的对象
- 在运行时判断任意一个类所具有的成员变量和方法
- 在运行时调用任意一个对象的方法
反射的常用类
- Class类,代表一个类。由JVM在加载类的时候生成
1.获取某个类对应Class的三种方式
Class<?> classType1 = Class.forName("javaSEtest.Person"); //获取字节码
Class<?> classType2 = Person.class;
Class<?> classType3 = new Person().getClass();
- Field类,代表类的成员变量
- Method类,代表类的方法
- Constructor 类,代表类的构造方法
- Array类,提供了动态创建数组,以及访问数组元素的静态方法
反射的入口是一个class(字节码)
反射初探
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
public class test {
public static void main(String[] args) throws Exception{
Class classType = Class.forName("javaSEtest.Person"); //获取字节码
Method m = classType.getMethod("sayHello", String.class);//获取方法
Constructor c =classType.getDeclaredConstructor(String.class);//获取构造方法
Object o = c.newInstance("铜驼");//获取相应的对象
System.out.println(m.invoke(o, "tongliya"));//方法调用
}
}
class Person{
String name;
Person(){
this.name = "爱好";
}
Person(String name){
this.name = name;
}
public String sayHello(String s){
return name +" say hello:To" + s;
}
}
对象的复制
public class test {
public static Object copy(Object o) throws Exception {
Class<?> copyClass = o.getClass();
Constructor<?> constructor = copyClass.getConstructor();
Object copyObject = constructor.newInstance();
Field[] fields = copyClass.getDeclaredFields();
for (Field f : fields) {
String fieldName = f.getName();
String firstNameUpper = fieldName.substring(0, 1).toUpperCase();
String getM = "get" + firstNameUpper + fieldName.substring(1);
String setM = "set" + firstNameUpper + fieldName.substring(1);
Method getMethod = copyClass.getMethod(getM);
Method setMethod = copyClass.getMethod(setM, f.getType());
Object value = getMethod.invoke(o);
setMethod.invoke(copyObject, value);
}
return copyObject;
}
public static void main(String[] args) throws Exception {
Person p = new Person("liyang", 26, 100);
Person copyP = (Person) copy(p);
System.out.println(copyP);
}
}
class Person {
String name;
int age;
int core;
public Person() {
}
public Person(String name, int age, int core) {
super();
this.name = name;
this.age = age;
this.core = core;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public int getCore() {
return core;
}
public void setCore(int core) {
this.core = core;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", core=" + core + "]";
}
}
java.lang.reflect.Array初探
//一维
Object a = Array.newInstance(String.class, 10);
Array.set(a, 1, "liy");
System.out.println(Array.get(a, 1));
//多维
String[][][][][] s = (String[][][][][])Array.newInstance(String.class, 2,3,4,5,6);
s[1][2][3][4][5] = "liyang";
System.out.println(s[1][2][3][4][5]);
私有变量和方法的访问
public class test {
public static void main(String[] args) throws Exception {
Class<?> classa = Person.class;
// Method m = classa.getMethod("sayHello"); //只能访问public的
Method m1 = classa.getDeclaredMethod("sayHello");// 没有限制都可以访问
m1.setAccessible(true);// 破坏掉原生的java访问限制,设置为true就可以访问私有的了,
Person p = new Person();
System.out.println(m1.invoke(p));
Field f = classa.getDeclaredField("name");
f.setAccessible(true);//设置私有可访问
System.out.println(f.get(p));
f.set(p, "liyang");
System.out.println(f.get(p));
}
}
class Person {
private String name = "limengxia";
private String sayHello() {
return "Hello";
}
}
网友评论