FastProxy is a library to create dynamic proxy class for Java 8 and above using org.ow2.asm:asm
.
<dependency>
<groupId>com.alphalxy</groupId>
<artifactId>fast-proxy</artifactId>
<version>0.0.1</version>
</dependency>
import com.alphalxy.FastProxy;
Just invoke newProxyInstance
method.
Foo target = new Foo() {
// ...
};
MethodInterceptor interceptor = new MethodInterceptor() {
@Override
public Object intercept(Object proxy, Method method, MethodInvoker invoker, Object[] args) throws Throwable {
// do with method
return invoker.invoke(target, args);
}
};
Foo foo = (Foo)FastProxy.newProxyInstance(Foo.class.getClassLoader(), new Class<?>[] {Foo.class}, interceptor);
Or use lambda expression.
Foo target = new Foo() {
// ...
};
MethodInterceptor interceptor = (proxy, method, invoker, args) -> invoker.invoke(target, args);
Foo foo = (Foo)FastProxy.newProxyInstance(Foo.class.getClassLoader(), new Class<?>[] {Foo.class}, interceptor);
FastProxy.isProxyInstance(f);
FastProxy.isProxyClass(Foo.class);
Use lambda express as MethodInvoker
for each method.
For any interface
public interface Foo {
String bar(String string);
void baz(String string);
}
generate a $Proxy
class using org.ow2.asm:asm
.
import com.alphalxy.MethodInterceptor;
import java.lang.reflect.Method;
public final class $Proxy implements Foo {
private final MethodInterceptor interceptor;
private static final Method M_0 = Foo.class.getMethod("bar", String.class);
private static final Method M_1 = Foo.class.getMethod("baz", String.class);
public $Proxy(MethodInterceptor interceptor) {
this.interceptor = interceptor;
}
public String bar(String string) {
return (String)interceptor.intercept(this, M_0, (target, args) -> {
return ((Foo)target).bar((String)args[0]);
}, new Object[] {string});
}
public void baz(String string) {
interceptor.intercept(this, M_1, (target, args) -> {
((Foo)target).baz((String)args[0]);
return null;
}, new Object[] {string});
}
}