AspectInj is an AOP solution for Java via Annotation-Processing.This is especially useful on Android.
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.
The jar file can be compiled by module aspectinj
@JoinPoints
public class MainActivity extends FragmentActivity {
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;
}
});
}
AspectInjector.weave(new DemoPointCut());
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) {
}
}
}
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.
onCreate in Demo
"com.windning.demo.Demo.onCreate(Object)"
init in Demo.DemoModel
"com.windning.demo.Demo.DemoModel.init(int)"
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.