Giter Club home page Giter Club logo

aspectinj's Introduction

AspectInj

AspectInj is an AOP solution for Java via Annotation-Processing.This is especially useful on Android.


What can AspectInj Do

Imagine there's a set of existing Java code, and some aspect logic needs to be added.The most direct way is to add static before and after methods arround each method.Obviously it's a much too intrusive solution, but AspectInj can help you to add these join point entries, without any changes to your method code.What you need to do is just adding an annotation to your class.

How to Use

1.Add AspectInj's jar file

The jar file can be compiled by module aspectinj

2.Add JoinPoints annotation to the target class:

@JoinPoints
public class MainActivity extends FragmentActivity {

3.Register point cuts by implementing PointCutRegistry:

public class DemoPointCut implements PointCutRegistry {
    @Override
    public void register(PointCutEntry entry) {
        entry.add("demo.windning.view.MainActivity.onCreate.class#0.onClick(View)",
        new PointCut(){
            @Override
            public boolean onBefore(Object self, Object[] args) {
            	...
                return true;
            }
        });
    }

4.Invoke weave before any main logic:

AspectInjector.weave(new DemoPointCut());

Guides

Assuming there is a class Demo like this:

package com.windning.demo
//demo class
public class Demo {
    //member function
    public void onCreate(Object inst) {
        ...
        //anonymous class
        listener = new OnClickListener() {
            //anonymous class's method
            @Override
            public void onClick(View target) {

            }
        }
    }

    //inner class
    private class DemoModel{
        //inner class's method
        private void init(int arg) {

        }
    }
}

* How to add PointCut around some specific method

public class [UserOwnRegistry] implements PointCutRegistry {
    @Override
    public void register(PointCutEntry entry) {
        entry.add([method position], [customized point cut function]);
    }

The point is to register correct method position(a String) and customize point cut function.

1.method position of normal method:

onCreate in Demo

"com.windning.demo.Demo.onCreate(Object)"

2.method position of inner class's method:

init in Demo.DemoModel

"com.windning.demo.Demo.DemoModel.init(int)"

3.method position of anonymous class's method:

onClick in Demo.onCreate

"com.windning.demo.Demo.onCreate.class#0.onClick(View)"

Note that an anonymous class is identified by "class#" prefix and an index.The index is the order defined in current class/method block counted from 0.

aspectinj's People

Stargazers

Jeremy  avatar  avatar

Watchers

 avatar

aspectinj's Issues

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.