反射,用于动态获取类的信息以及动态的调用对象的方法.
- 在运行时判断任意一个对象所属的类
- 在运行时构造任意一个对象
- 在运行时判断任意一个类所具有的成员变量和方法
- 在运行时调用任意一个对象的方法.在
在JDK中, 主要由以下类来实现Java反射机制,这些类都位于java.lang.reflect包中
1. Class类: 代表一个类
2. Field类:代表类的成员变量
3. Method类:代表类的方法
4. Constructor类:代表类的构造方法
5. Array类:提供了动态创建数组,以及访问数组的元素的静态方法.
获取Class对象的方法
1. Class.forName(“类的全名”);
//example:1
Class<?> classType = Class.forName("java.lang.String");
- String.class–类.class
//example:2
Class<?> classType = String.class;
3.String str = “”;
str.getClass(); –对象.getClass()
//example:3
String str = "";
Class<?> classType = str.getClass();
获取类的实例的两种方法
Class<?> classType = Class.forName("java.lang.String");
//1 适用于没有参数时
Object obj = classType.newInstance();
//2 有没有参数都可以
Constructor cons = classType.getConstructor(new Class[]{String.class, int.class});
Object obj2 = cons.newInstance(new Object[]{"tom",12});
Method对象例子:
package com.reflect.test1;
import java.lang.reflect.Method;
public class InvokeTest
{
public int add(int param1, int param2)
{
return param1+param2;
}
public String hello(String msg1, String msg2)
{
return msg1+msg2;
}
public static void main(String[] args) throws Exception
{
Class<?> classType = InvokeTest.class;
Object obj = classType.newInstance();
Method method = classType.getMethod("add",new Class[]{int.class,int.class});
int sum = (int) method.invoke(obj,new Object[]{4,1});
System.out.println(sum);
System.out.println("--------------------------");
Method method2 = classType.getMethod("hello", new Class[]{String.class, String.class});
String hello = (String) method2.invoke(obj,new Object[]{"hello:", "Tom"});
System.out.println(hello);
}
}
Field
//这个getDelclaredFields()是获得所有的Field,包括public protected default 和 private
Field[] fields = classType.getDeclaredFields();
例子:通过反射复制copy一个对象
package com.reflect.test1;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
public class ObjectCopy
{
public static void main(String[] args) throws Exception
{
User user = new User("小刚","123",15);
User objCopy =(User)ObjectCopy.Copy(user);
System.out.println(objCopy.getUsername());
}
//通过反射copy对象
public static Object Copy(Object obj) throws Exception
{
//获取Class对象objClass
Class<?> objClass = obj.getClass();
//获取objClass的一个实例
Object objCopy = objClass.newInstance();
//获取类中的所有Feild
Field[] fields = objClass.getDeclaredFields();
for(Field field: fields)
{
String name = field.getName();
String firstLetter = name.substring(0,1).toUpperCase();
String getMethodName = "get" + firstLetter + name.substring(1);
String setMethodName = "set" + firstLetter + name.substring(1);
Method getMethod = objClass.getDeclaredMethod(getMethodName,new Class[]{});
Method setMethod = objClass.getDeclaredMethod(setMethodName, new Class[]{field.getType()});
Object value = getMethod.invoke(obj, new Object[]{});
setMethod.invoke(objCopy, new Object[]{value});
}
return objCopy;
}
}
调用私有方法时要注意,先压制java的方法检查
Method method = classType.getDeclaredMethod("sayHello", new Class[]{});
method.setAccessible(true);
method.invoke(obj, new Object[]{});
通过反射修改private String name;
package com.reflect.test1;
import java.lang.reflect.Field;
public class Private2
{
public static void main(String[] args)throws Exception
{
Person p = new Person();
Class<?> classType = p.getClass();
Field field = classType.getDeclaredField("name");
field.setAccessible(true);
field.set(p, "lisi");
System.out.println(p.getName());
}
}
class Person{
private String name = "zhangsan";
public String getName()
{
return this.name;
}
}