NewProxy is an extension of Proxy, It is similar to CGLIB. But it is as fast as CGLIB as possible, even faster, simpler and smaller than CGLIB.
More details see GitHub or Gitee.
NewProxy is a tool to generate dynamic proxy for interfaces, and class also.
It follows the concept of JDK's built-in Proxy class for generating dynamic proxy classes, which involves creating a proxy class for the target interfaces and then invoking its methods, returning the results to the caller. In contrast to the JDK's Proxy class, dynamic proxy class generated by NewProxy does not extend NewProxy itself; instead, it merely implements the specified interfaces (including extra InvocationDispatcher interface) and extend class, significantly reducing the complexity of the generated proxy classes.
Besides, NewProxy also supports generating dynamic proxy class for one class at most like CGLIB does.
NewProxy provides the following features:
- Generate dynamic proxy classes for public interfaces and class with public final modifiers.
- Generate dynamic proxy classes for non-public interfaces and class with final modifier.
- Check whether the target class if a dynamic proxy class or not.
- Check if the target object is an instance of a dynamic proxy class or not.
- Acquire the invocation interceptor instance of the target object if its class is a dynamic proxy class.
It's straightforward to use NewProxy to generate dynamic proxy classes as it is to use Proxy.
First of all, you need to import the NewProxy library into your project.
<dependency>
<groupId>io.github.lamspace</groupId>
<artifactId>newproxy</artifactId>
<version>${latest.version}</version>
</dependency>
public interface Foo {
void foo();
}
Then you need to do as follows:
import io.github.lamspace.newproxy.InvocationInterceptor;
import io.github.lamspace.newproxy.MethodDecorator;
import io.github.lamspace.newproxy.NewProxy;
public static void main(String[] args) {
InvocationInterceptor interceptor = new InvocationInterceptor() {
@Override
public Object intercept(Object proxy, MethodDecorator method, Object[] args) {
return method.invoke(proxy, fooImpl, args);
}
};
Foo foo = (Foo) NewProxy.newProxyInstance(Foo.class.getClassLoader(), interceptor, null, null, Foo.class);
foo.foo();
}
public class Bar {
public void bar() {
// ...
}
}
import io.github.lamspace.newproxy.InvocationInterceptor;
import io.github.lamspace.newproxy.MethodDecorator;
import io.github.lamspace.newproxy.NewProxy;
public static void main(String[] args) {
InvocationInterceptor interceptor = new InvocationInterceptor() {
@Override
public Object intercept(Object proxy, MethodDecorator method, Object[] args) throws Throwable {
return method.invoke(proxy, null, args);
}
};
Bar bar = (Bar) NewProxy.newProxyInstance(Bar.class.getClassLoader(), interceptor, null, null, Bar.class);
bar.bar();
}
public class Bar {
private final String s;
public Bar(Strin s) {
this.s = s;
}
public void bar() {
//...
}
}
import io.github.lamspace.newproxy.InvocationInterceptor;
import io.github.lamspace.newproxy.MethodDecorator;
public static void main(String[] args) {
InvocationInterceptor interceptor = new InvocationInterceptor() {
@Override
public Object intercept(Object proxy, MethodDecorator method, Object[] args) throws Throwable {
return method.invoke(proxy, null, args);
}
};
Bar bar = (Bar) NewProxy.newProxyInstance(Bar.class.getClassLoader(), interceptor, new Class<?>[]{String.class}, new Object[]{"Hello World!"}, Bar.class);
bar.bar();
}
...
NewProxy is built on top of the Byte Code Engineering Library (simply called BCEL). More details can be found in the BCEL official website.
At least JDK 8 is required.
Contributions are welcome! Do what you want to do with Apache License 2.0! Please check CONTRIBUTING.
Cheers!