1、
/**
* 被代理的接口
*/
public interface IBiz {
void fun();
}
2、
/**
* 被代理的接口实现
*/
public class BizImpl implements IBiz{
public void fun() {
System.out.println(">>>>BizImpl fun()");
}
}
3、
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
/**
* 生成代理类的类
*/
public class ProxyBuilder implements InvocationHandler {
/**
* 被代理对象
*/
Object target;
private ProxyBuilder(Object target) {
this.target = target;
}
/**
* 创建代理对象
*/
public static Object getProxy(Object target){
return Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), new ProxyBuilder(target));
}
/**
* 代理处理逻辑
* @param proxy
* @param method
* @param args
* @return
* @throws Throwable
*/
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result = null;
System.out.println(">>>>start:" + System.currentTimeMillis());
//执行被代理对象的方法
result = method.invoke(target, args);
System.out.println(">>>>end:" + System.currentTimeMillis());
return result;
}
}
4、
/**
* 使用动态代理
*/
public class Test {
public static void main(String[] args) {
IBiz biz = (IBiz) ProxyBuilder.getProxy(new BizImpl());
biz.fun();
}
}
5、
/**
* 将生成的代理类写到class文件
*/
public static void createProxyClassFile() {
String name = "BizProxy";
byte[] data = ProxyGenerator.generateProxyClass(name, new Class[]{IBiz.class});
try {
FileOutputStream out = new FileOutputStream(name + ".class");
out.write(data);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
6、生成的代理类
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.lang.reflect.UndeclaredThrowableException;
import proxy.IBiz;
public final class BizProxy extends Proxy implements IBiz {
private static Method m1;
private static Method m3;
private static Method m0;
private static Method m2;
public BizProxy(InvocationHandler var1) throws {
super(var1);
}
public final boolean equals(Object var1) throws {
try {
return ((Boolean)super.h.invoke(this, m1, new Object[]{var1})).booleanValue();
} catch (RuntimeException | Error var3) {
throw var3;
} catch (Throwable var4) {
throw new UndeclaredThrowableException(var4);
}
}
public final void fun() throws {
try {
super.h.invoke(this, m3, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final int hashCode() throws {
try {
return ((Integer)super.h.invoke(this, m0, (Object[])null)).intValue();
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
public final String toString() throws {
try {
return (String)super.h.invoke(this, m2, (Object[])null);
} catch (RuntimeException | Error var2) {
throw var2;
} catch (Throwable var3) {
throw new UndeclaredThrowableException(var3);
}
}
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals", new Class[]{Class.forName("java.lang.Object")});
m3 = Class.forName("proxy.IBiz").getMethod("fun", new Class[0]);
m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
} catch (NoSuchMethodException var2) {
throw new NoSuchMethodError(var2.getMessage());
} catch (ClassNotFoundException var3) {
throw new NoClassDefFoundError(var3.getMessage());
}
}
}