其他
从头捋了一遍 JDK 动态代理,此次之后,永生难忘!
>>号外:关注“Java精选”公众号,回复“面试资料”,免费领取资料!“Java精选面试题”小程序,3000+ 道面试题在线刷,最新、最全 Java 面试题!
void drive(String driverName, String carName);
}
@Override
public void drive(String driverName, String carName) {
System.err.println("Audi is driving... " + "driverName: " + driverName + ", carName" + carName);
}
}
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;
}
}
@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");
}
}
Audi is driving... driverName: name1, carNameaudi
after
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);
proxyName, interfaces);
@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);
}
}
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());
}
}
}
作者:神一样的存在
cnblogs.com/dreamroute/p/5273888.html
源码分析 Mybatis 事务管理,将颠覆你心中目前对事务的理解!
Java 中如何优雅的根治 null 值引起的 Bug 问题,速看分析!
Spring Cloud 微服务架构,小团队到底适不适合,如何应用?
写了 10 年代码,做过的项目都下线了,程序员的意义在哪里?
半路出家的菜鸡程序员,北漂五年,给刚入行朋友的一些忠告,发自肺腑