从头捋了一遍 JDK动态代理,收获颇丰!
共 13577字,需浏览 28分钟
·
2021-06-18 02:44
阅读本文大概需要 5 分钟。
来自:cnblogs.com/dreamroute/p/5273888.html
public interface Car {
void drive(String driverName, String carName);
}
public class Audi implements Car {
@Override
public void drive(String driverName, String carName) {
System.err.println("Audi is driving... " + "driverName: " + driverName + ", carName" + carName);
}
}
public class CarHandler implements InvocationHandler {
private Car car;
public CarHandler(Car car) {
this.car = car;
}
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.err.println("before");
method.invoke(car, args);
System.err.println("after");
return null;
}
}
public class ProxyTest {
@Test
public void proxyTest() throws Exception {
Car audi = (Car) Proxy.newProxyInstance(Car.class.getClassLoader(), new Class<?>[] {Car.class}, new CarHandler(new Audi()));
audi.drive("name1", "audi");
}
}
before
Audi is driving... driverName: name1, carNameaudi
after
public static Object newProxyInstance(ClassLoader loader,
Class<?>[] interfaces,
InvocationHandler h)
throws IllegalArgumentException
{
if (h == null) {
throw new NullPointerException();
}
/*
* Look up or generate the designated proxy class.
*/
Class<?> cl = getProxyClass(loader, interfaces);
/*
* Invoke its constructor with the designated invocation handler.
*/
try {
Constructor cons = cl.getConstructor(constructorParams);
return cons.newInstance(new Object[] { h });
} catch (NoSuchMethodException e) {
throw new InternalError(e.toString());
} catch (IllegalAccessException e) {
throw new InternalError(e.toString());
} catch (InstantiationException e) {
throw new InternalError(e.toString());
} catch (InvocationTargetException e) {
throw new InternalError(e.toString());
}
}
// 创建代理类
Class<?> cl = getProxyClass(loader, interfaces);
// 实例化代理对象
Constructor cons = cl.getConstructor(constructorParams);
Class<?> cl = getProxyClass(loader, interfaces);
byte[] proxyClassFile = ProxyGenerator.generateProxyClass(
proxyName, interfaces);
public class ProxyTest {
@SuppressWarnings("resource")
@Test
public void proxyTest() throws Exception {
byte[] bs = ProxyGenerator.generateProxyClass("AudiImpl", new Class<?>[] {Car.class});
new FileOutputStream(new File("d:/AudiImpl.class")).write(bs);
}
}
public final class AudiImpl extends Proxy implements Car {
private static final long serialVersionUID = 5351158173626517207L;
private static Method m1;
private static Method m3;
private static Method m0;
private static Method m2;
public AudiImpl(InvocationHandler paramInvocationHandler) {
super(paramInvocationHandler);
}
public final boolean equals(Object paramObject) {
try {
return ((Boolean) this.h.invoke(this, m1, new Object[] { paramObject })).booleanValue();
} catch (Error | RuntimeException localError) {
throw localError;
} catch (Throwable localThrowable) {
throw new UndeclaredThrowableException(localThrowable);
}
}
public final void drive(String paramString1, String paramString2) {
try {
this.h.invoke(this, m3, new Object[] { paramString1, paramString2 });
return;
} catch (Error | RuntimeException localError) {
throw localError;
} catch (Throwable localThrowable) {
throw new UndeclaredThrowableException(localThrowable);
}
}
public final int hashCode() {
try {
return ((Integer) this.h.invoke(this, m0, null)).intValue();
} catch (Error | RuntimeException localError) {
throw localError;
} catch (Throwable localThrowable) {
throw new UndeclaredThrowableException(localThrowable);
}
}
public final String toString() {
try {
return (String) this.h.invoke(this, m2, null);
} catch (Error | RuntimeException localError) {
throw localError;
} catch (Throwable localThrowable) {
throw new UndeclaredThrowableException(localThrowable);
}
}
static {
try {
m1 = Class.forName("java.lang.Object").getMethod("equals",
new Class[] { Class.forName("java.lang.Object") });
m3 = Class.forName("com.mook.core.service.Car").getMethod("drive",
new Class[] { Class.forName("java.lang.String"), Class.forName("java.lang.String") });
m0 = Class.forName("java.lang.Object").getMethod("hashCode", new Class[0]);
m2 = Class.forName("java.lang.Object").getMethod("toString", new Class[0]);
} catch (NoSuchMethodException localNoSuchMethodException) {
throw new NoSuchMethodError(localNoSuchMethodException.getMessage());
} catch (ClassNotFoundException localClassNotFoundException) {
throw new NoClassDefFoundError(localClassNotFoundException.getMessage());
}
}
}
推荐阅读:
微信扫描二维码,关注我的公众号
朕已阅