Code Monkey home page Code Monkey logo

newproxy's Introduction

NewProxy: New Proxy for Java

δΈ­ζ–‡

Gitter License Static Badge GitHub Repo stars GitHub forks GitHub Release Maven Central Version


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 or Gitee.


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.

<dependency>
    <groupId>io.github.lamspace</groupId>
    <artifactId>newproxy</artifactId>
    <version>${latest.version}</version>
</dependency>

Case 1: Generate dynamic proxy class for only interface(s)

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();
}

Case 2: Generate dynamic proxy class for class with non-parameter constructor

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();
}

Case 3: Generate dynamic proxy class for class with parameterized constructor

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();
}

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). More details can be found in the 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.

Cheers!


newproxy's People

Contributors

lamspace avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.