https://github.com/lamspace/newproxy
A tool to generate dynamic proxy for interfaces and class. following the concept of JDK's built in Proxy.
https://github.com/lamspace/newproxy
bcel proxy
Last synced: about 1 year ago
JSON representation
A tool to generate dynamic proxy for interfaces and class. following the concept of JDK's built in Proxy.
- Host: GitHub
- URL: https://github.com/lamspace/newproxy
- Owner: LamSpace
- License: apache-2.0
- Created: 2024-05-05T04:08:40.000Z (about 2 years ago)
- Default Branch: master
- Last Pushed: 2024-06-01T11:50:18.000Z (about 2 years ago)
- Last Synced: 2025-06-13T19:03:16.373Z (about 1 year ago)
- Topics: bcel, proxy
- Language: Java
- Homepage:
- Size: 221 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.MD
Awesome Lists containing this project
README
# NewProxy: New Proxy for Java
[中文](./README_CN.md)

[](https://www.apache.org/licenses/LICENSE-2.0.html)





---
## What is it?
> **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](https://github.com/LamSpace/newproxy-samples)
or [Gitee](https://gitee.com/LamTong/newproxy-samples).
---
## What does it do?
**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.
---
## Quick Start
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.
```xml
io.github.lamspace
newproxy
${latest.version}
```
### Case 1: Generate dynamic proxy class for only interface(s)
```java
public interface Foo {
void foo();
}
```
Then you need to do as follows:
```java
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();
}
```
---
### Case 2: Generate dynamic proxy class for class with non-parameter constructor
```java
public class Bar {
public void bar() {
// ...
}
}
```
```java
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();
}
```
### Case 3: Generate dynamic proxy class for class with parameterized constructor
```java
public class Bar {
private final String s;
public Bar(Strin s) {
this.s = s;
}
public void bar() {
//...
}
}
```
```java
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();
}
```
### Case 4: Generate dynamic proxy class for interfaces and class
...
---
## Underlying Support
**NewProxy** is built on top of the **Byte Code Engineering Library**
(simply called [BCEL](https://commons.apache.org/proper/commons-bcel/)).
More details can be found in the [BCEL](https://commons.apache.org/proper/commons-bcel/) official website.
---
## Prerequisites
At least JDK 8 is required.
---
## Contributing
Contributions are welcome! Do what you want to do with **Apache License 2.0**!
Please check [CONTRIBUTING](./CONTRIBUTING.md).
*Cheers!*
---