反射
动态加载类
//Class.forName通过类名返回一个类的元类型
Class clz = Class.forName("User");
实例化类
访问类的成员
Object obj = clz.newInstance();//调用默认的构造方法
Class objArgs[] = {int.class,String.class,int.class};
Constructor objCons = clz.getDeclaredConstructor(objArgs);//调用指定参数类型的构造方法
Object obj1 = objCons.newInstance(2,"Pang niu",26);
--示例
--Bean类
public class User {
private int id;
private int age;
private String name;
public User(){}
public User(int id,String name,int age){
this.id = id;
this.name = name;
this.age = age;
}
public int getAge() {
return age;
}
private void setAge(int age) {
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return "ID:"+this.id+" NAME:"+this.name+" Age:"+this.age;
}
}
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Main {
/**
* @param args
* @throws ClassNotFoundException 类不存在的异常
* @throws IllegalAccessException 非法访问异常
* @throws InstantiationException 实例化异常,比如构造函数为PRIVATE
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalArgumentException 非法参数异常
*/
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException {
//动态加载类
//Class.forName通过类名返回一个类的元类型
Class clz = Class.forName("User");
Object obj = clz.newInstance();//调用默认的构造方法
Method mSetId = clz.getMethod("setId", int.class);//必须指定方法的参数类型,得到唯一的方法。只能访问Public的方法
mSetId.invoke(obj, 1);
Method mSetName = clz.getMethod("setName", String.class);
mSetName.invoke(obj, "Anker");
// Method mSetAge = clz.getMethod("setAge", int.class);//方法为private,无法访问,只能访问public修饰
Method mSetAge = clz.getDeclaredMethod("setAge", int.class);//
mSetAge.setAccessible(true);//针对缺少可视性的方法,设置为可以访问
mSetAge.invoke(obj, 27);
System.out.println(obj.toString());
Class objArgs[] = {int.class,String.class,int.class};
Constructor objCons = clz.getDeclaredConstructor(objArgs);
Object obj1 = objCons.newInstance(2,"Pang niu",26);
System.out.println(obj1);
}
}
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
public class Main2 {
/**
* @param args
* @throws ClassNotFoundException 类不存在的异常
* @throws IllegalAccessException 非法访问异常
* @throws InstantiationException 实例化异常,比如构造函数为PRIVATE
* @throws NoSuchMethodException
* @throws SecurityException
* @throws InvocationTargetException
* @throws IllegalArgumentException 非法参数异常
* @throws NoSuchFieldException
*/
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
//动态加载类
//Class.forName通过类名返回一个类的元类型
Class clz = Class.forName("User");
Object obj = clz.newInstance();
// Field fId = clz.getField("id"); //只能访问public的属性,否者报错Java.Lang.NoSuchFileException
// Field fName = clz.getField("name");
Field fId1 = clz.getDeclaredField("id"); //只能访问public的属性,除非setAccessible来访问protected或者private的权限
Field fName1 = clz.getDeclaredField("setAge");
fId1.setAccessible(true);
fName1.setAccessible(true);
fId1.set(obj, 1);
fName1.set(obj, "Anker");
System.out.println(obj);
}
}
一些重要的反射API
public static void main(String[] args) throws ClassNotFoundException, InstantiationException, IllegalAccessException, SecurityException, NoSuchMethodException, IllegalArgumentException, InvocationTargetException, NoSuchFieldException {
//动态加载类
//Class.forName通过类名返回一个类的元类型
Class clz = Class.forName("User");
Method[] mthods = clz.getMethods();//获得所有的方法,包括从父类继承的方法
for(Method m:mthods){
if(m.getDeclaringClass() == clz){ //查询本类定义的方法
System.out.println(m);
if(Modifier.isStatic(m.getModifiers())){//判断方法是否是static
System.out.println();//
}
System.out.println(m.getModifiers());//以整型代表方法的修饰符
}
}
}
反射与动态代码
Java 提供的动态代码的实现,为代理类与实现类继承同一个接口,针对接口要实现的方法可以实现动态代理。
package DymaticProxy;
public interface IWork {
public void run();
}
package DymaticProxy;
public class Worker implements IWork{
public void run() {
System.out.println("in work run");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
package DymaticProxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class LoggerProxy implements InvocationHandler {
Object target = null;
public Object newProxyInstance(Object targetObject) {
target = targetObject;
return Proxy.newProxyInstance(targetObject.getClass().getClassLoader(),
target.getClass().getInterfaces(), this);
}
@Override
public Object invoke(Object proxy, Method method, Object[] args)
throws Throwable {
System.out.println("start-->>" + method.getName());
try {
method.invoke(target, args);
System.out.println("success-->" + method.getName());
} catch (Exception ex) {
ex.printStackTrace();
System.out.println("error-->>" + method.getName());
throw ex;
}
return null;
}
}
JDK的动态代理机制只能代理实现了接口的类,而不能实现接口的类就不能实现JDK的动态代理,
CGlib 实现代理类与实现类可以不继承相同接口,通过创建继承代理类,来实现动态代理,
它的原理是对指定的目标类生成一个子类,并覆盖其中方法实现增强,
但因为采用的是继承,所以不能对final修饰的类进行代理。
具体查看附件。