JDK动态代理
JDK的动态代理是 动态的根据指定的接口生成一个类的字节码,该类实现接口的方法。
通过自己实现模拟代理,以便于容易理解动态代理的原理。
接口及实现类
实际使用时,一般有实现类的话,作用就是功能增强。如果没有实现类,就会根据业务规则,做共通的处理。
为了测试方便,暂时使用实现类。
public interface TestInterface {
void hello();
}
public class TestClass implements TestInterface{
@Override
public void hello() {
System.out.println("hello word");
}
}
动态代理 - 回调接口
仿JDK的InvocationHandler。
public interface MyInvocationHandler {
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable;
}
动态代理 - 源码生成、编译、加载
仿JDK的Proxy。
1、动态生成源代码.java文件
2、把生成的.java文件编译成.class文件
3、编译生成的.class文件加载到JVM中来
4、反射新的代理对象。
public class MyProxy {
public static final String ln = "\r\n";
public static Object newProxyInstance(MyClassLoader classLoader, Class<?>[] interfaces, MyInvocationHandler h) {
try {
//动态生成源代码.java文件
String src = generateSrc(interfaces);
//Java文件输出磁盘
String filePath = MyProxy.class.getResource("").getPath();
File f = new File(filePath + "$Proxy0.java");
FileWriter fw = new FileWriter(f);
fw.write(src);
fw.flush();
fw.close();
//把生成的.java文件编译成.class文件
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager manage = compiler.getStandardFileManager(null, null, null);
Iterable iterable = manage.getJavaFileObjects(f);
JavaCompiler.CompilationTask task = compiler.getTask(null, manage, null, null, null, iterable);
task.call();
manage.close();
//编译生成的.class文件加载到JVM中来
Class proxyClass = classLoader.findClass("$Proxy0");
Constructor c = proxyClass.getConstructor(MyInvocationHandler.class);
f.delete();
//返回字节码重组以后的新的代理对象
return c.newInstance(h);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private static String generateSrc(Class<?>[] interfaces) {
StringBuffer sb = new StringBuffer();
sb.append("package com.qgbihc.proxy;" + ln);
sb.append("import " + interfaces[0].getName() + ";" + ln);
sb.append("import java.lang.reflect.*;" + ln);
sb.append("public class $Proxy0 implements " + interfaces[0].getName() + "{" + ln);
sb.append("MyInvocationHandler h;" + ln);
sb.append("public $Proxy0(MyInvocationHandler h) { " + ln);
sb.append("this.h = h;");
sb.append("}" + ln);
for (Method m : interfaces[0].getMethods()) {
Class<?>[] params = m.getParameterTypes();
StringBuffer paramNames = new StringBuffer();
StringBuffer paramValues = new StringBuffer();
StringBuffer paramClasses = new StringBuffer();
for (int i = 0; i < params.length; i++) {
Class clazz = params[i];
String type = clazz.getName();
String paramName = toLowerFirstCase(clazz.getSimpleName());
paramNames.append(type + " " + paramName);
paramValues.append(paramName);
paramClasses.append(clazz.getName() + ".class");
if (i > 0 && i < params.length - 1) {
paramNames.append(",");
paramClasses.append(",");
paramValues.append(",");
}
}
sb.append("public " + m.getReturnType().getName() + " " + m.getName() + "(" + paramNames.toString() + ") {" + ln);
sb.append("try{" + ln);
sb.append("Method m = " + interfaces[0].getName() + ".class.getMethod(\"" + m.getName() + "\",new Class[]{" + paramClasses.toString() + "});" + ln);
sb.append((hasReturnValue(m.getReturnType()) ? "return " : "") + getCaseCode("this.h.invoke(this,m,new Object[]{" + paramValues + "})", m.getReturnType()) + ";" + ln);
sb.append("}catch(Error _ex) { }");
sb.append("catch(Throwable e){" + ln);
sb.append("throw new UndeclaredThrowableException(e);" + ln);
sb.append("}");
sb.append(getReturnEmptyCode(m.getReturnType()));
sb.append("}");
}
sb.append("}" + ln);
return sb.toString();
}
private static Map<Class, Class> mappings = new HashMap<Class, Class>();
static {
mappings.put(int.class, Integer.class);
}
private static String getReturnEmptyCode(Class<?> returnClass) {
if (mappings.containsKey(returnClass)) {
return "return 0;";
} else if (returnClass == void.class) {
return "";
} else {
return "return null;";
}
}
private static String getCaseCode(String code, Class<?> returnClass) {
if (mappings.containsKey(returnClass)) {
return "((" + mappings.get(returnClass).getName() + ")" + code + ")." + returnClass.getSimpleName() + "Value()";
}
return code;
}
private static boolean hasReturnValue(Class<?> clazz) {
return clazz != void.class;
}
private static String toLowerFirstCase(String src) {
char[] chars = src.toCharArray();
chars[0] += 32;
return String.valueOf(chars);
}
}
动态生成的Java文件如下:
package com.qgbihc.proxy.test;
import com.qgbihc.proxy.MyInvocationHandler;
import com.qgbihc.proxy.test.TestInterface;
import java.lang.reflect.*;
public class $Proxy0 implements TestInterface {
MyInvocationHandler h;
public $Proxy0(MyInvocationHandler h) {
this.h = h;
}
public void hello() {
try {
Method m = TestInterface.class.getMethod("hello", new Class[]{});
this.h.invoke(this, m, new Object[]{});
} catch (Error _ex) {
} catch (Throwable e) {
throw new UndeclaredThrowableException(e);
}
}
}
动态代理 - 类加载器
继承ClassLoader,实现自定义类加载器。通过defineClass把class文件的byte转为Class。
public class MyClassLoader extends ClassLoader {
private File classPathFile;
public MyClassLoader(){
String classPath = MyClassLoader.class.getResource("").getPath();
this.classPathFile = new File(classPath);
}
@Override
protected Class<?> findClass(String name) throws ClassNotFoundException {
String className = MyClassLoader.class.getPackage().getName() + "." + name;
if(classPathFile != null){
File classFile = new File(classPathFile,name.replaceAll("\\.","/") + ".class");
if(classFile.exists()){
FileInputStream in = null;
ByteArrayOutputStream out = null;
try{
in = new FileInputStream(classFile);
out = new ByteArrayOutputStream();
byte [] buff = new byte[1024];
int len;
while ((len = in.read(buff)) != -1){
out.write(buff,0,len);
}
return defineClass(className,out.toByteArray(),0,out.size());
}catch (Exception e){
e.printStackTrace();
}
}
}
return null;
}
}
编写Invocation
继承MyInvocationHandler,编写TestInvocation。实现被代理类的功能增强。
在原方法执行前,和执行后增加处理。
public class TestInvocation implements MyInvocationHandler {
private Object target;
public Object getInstance(Object target) throws Exception{
this.target = target;
Class<?> clazz = target.getClass();
return MyProxy.newProxyInstance(new MyClassLoader(),clazz.getInterfaces(),this);
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
before();
// 根据规则处理逻辑。可以直接反射,或特定的处理,比如RPC
Object obj = method.invoke(this.target,args);
after();
return obj;
}
private void before(){
System.out.println("被代理对象的方法执行前。");
}
private void after(){
System.out.println("被代理对象的方法执行后。");
}
}
测试
测试TestClass的hello方法。可以看到在原来的方法前后,打出了LOG。
public class MyProxyTest {
public static void main(String[] args) {
try {
TestInterface obj = (TestInterface) new TestInvocation().getInstance(new TestClass());
System.out.println(obj.getClass());
obj.hello();
}catch (Exception e){
e.printStackTrace();
}
}
}
输出
class com.qgbihc.proxy.$Proxy0
被代理对象的方法执行前。
hello word
被代理对象的方法执行后。