Coder Social home page Coder Social logo

tiny-gao / proxy2 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from forax/proxy2

0.0 2.0 0.0 1.43 MB

Better / faster proxy generator than java.lang.reflect.Proxy for Java (require 1.7)

License: Apache License 2.0

Java 99.62% JavaScript 0.38%

proxy2's Introduction

Proxy 2.0

Better / faster proxy generator than java.lang.reflect.Proxy for Java (require 1.7)

Getting Started

The Proxy2 API is composed of only 2 main classes, Proxy2 and MethodBuilder.

The class Proxy2 that defines several static methods named createAnonymousProxyFactory which as their names indicate return a factory of proxy instances (objects). To create a proxy, you need to pass an interface that will be the interface implemented by the proxy and a ProxyHandler that will define the implementation of all the methods of the proxy class.

The method bootstrap of the ProxyHandler is somewhat lazy and will be called once by method the first time the implementation of a method is needed. Because the method is called once, the bootstrap method has no access to the arguments of the method but have access the method of the interface that should be implemented (as a reflection Method object) and the signature of the proxy implementation (as a java.lang.invoke.MethodType object). These two values are encapsulated in the ProxyContext object. The result of the bootstrap method is a callsite (a java.lang.invoke.CallSite objet) that allows to change the implementation of the method at runtime (by calling setTarget()) if needed.

The second class of the API, MethodBuilder is a builder that helps to describe the implementation of a proxy method. While this class is not strictly necessary, the MethodHandles API is not an easy API to master and I believe that the class MethodBuilder provide a good entry level API.

Let's take a simple example,

public interface Hello {
  public String message(String message, String user);
  
  public static void main(String[] args) {
    ProxyFactory<Hello> factory = Proxy2.createAnonymousProxyFactory(Hello.class, new ProxyHandler.Default() { 
      @Override
      public CallSite bootstrap(ProxyContext context) throws Throwable {
        System.out.println("bootstrap method " + context.method());
        System.out.println("bootstrap type " + context.type());
        MethodHandle target =
            MethodBuilder.methodBuilder(context.type())
              .dropFirstParameter()
              .thenCall(MethodHandles.publicLookup(), String.class.getMethod("concat", String.class));
        return new ConstantCallSite(target);
      }
    });
    
    Hello simple = factory.create();
    System.out.println(simple.message("hello ", "proxy"));
    System.out.println(simple.message("hello ", "proxy 2"));
  }
}

The code above create a proxy factory on the interface Hello which has a single method message. The first time the method message is called, it called the method bootstrap of the ProxyHandler, which use a MethodBuilder to create to the implementation.

Here is the output of the code above

bootstrap method public abstract java.lang.String Hello.message(java.lang.String,java.lang.String)
bootstrap type (Object,String,String)String
hello proxy
hello proxy 2

In order to implement the method message, the method bootstrap has to provide a MethodMethod (a glorified function pointer) that takes an object and two strings and return a string. The first object, correspond to the proxy object, the two strings to the two arguments of message and string returned correspond to the return type of message.

Here, the implementation call String.concat, for that the proxy object is not needed, that's why dropFirstParameter() is called on the builder to remove the proxy object from the stack before asking to call String.concat.

As you can see, the method bootstrap is called once even if the method message is called twice, because the method bootstrap acts as a linker between the abstract method defined in the interface and its implementation.

proxy2's People

Contributors

forax avatar

Watchers

James Cloos avatar tiny gao 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.