GithubHelp home page GithubHelp logo

pressurekai / cglib-for-android Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bluesky466/cglib-for-android

0.0 0.0 0.0 1.18 MB

CGLib-for-Android (Dex code Generation Library for Android) is high level API to dynamicall generate sub class, its implementation based on DexMaker. This library use for AOP, method intercept, data access authorization authentication on Android.

Java 100.00%

cglib-for-android's Introduction

CGLib-for-Android

fork from https://github.com/leo-ouyang/CGLib-for-Android and fix some bugs

CGLib-for-Android (Dex code Generation Library for Android) is high level API to dynamicall generate sub class, its implementation based on DexMaker. This library use for AOP, method intercept, data access authorization authentication on Android.

Usage

The usage of CGLib-for-Android is similar with CGLib, but CGLib only supports java byte code, CGLib-for-Android designed for dex code which runs on Android.


1 Download cglib-for-android.jar to your project * [cglib-for-android.aar](https://github.com/bluesky466/CGLib-for-Android/blob/master/output/cglib-for-android.aar) (Right-click and Save as)
2 implementation cglib-for-android and dexmaker:
implementation files("libs/cglib-for-android.aar")
implementation 'com.linkedin.dexmaker:dexmaker:2.28.3'

3 Define the bussiness class which will be proxied in future ```Java public class Printer {
public void print() {
    Logger.d("Hello, world!");
}

}


<br>
4 Define proxy class, need to implement MethodInterceptor
```Java
public class MyProxy implements MethodInterceptor {
    
    private Context context;
    
    public MyProxy(Context context) {
        this.context = context;
    }
    
    public Object getProxy(Class cls) {
        Enhancer e = new Enhancer(context);
        e.setSuperclass(cls);
        e.setInterceptor(this);
        return e.create();
    }

    @Override
    public Object intercept(Object object, Object[] args, MethodProxy methodProxy) throws Exception {
        Logger.d("begin print");
        Object result = methodProxy.invokeSuper(object, args);
        Logger.d("end print");
        return result;
    }

}

5 Get printer and run ```Java Printer printer = (Printer) new MyProxy(this).getProxy(Printer.class); printer.print(); ```

Logcat output
[main:MyProxy.java:26 intercept]begin print
[main:Printer.java:6 print]Hello, world!
[main:MyProxy.java:28 intercept]end print

cglib-for-android's People

Contributors

leo-ouyang 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.