Android AspectJ实例讲解

文章目录

  • 如何织入代码
  • call vs execution
  • withincode
  • 碰到的问题

如何织入代码

MethedTrace.java是AspectJ代码,需要织入切面代码到MainActivity.java

// MainActivity.java
package org.sdet.aspectj;

public class MainActivity extends Activity {
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
  }  
}
// MethodTracer.java
package org.sdet.aspectj;

import android.util.Log;

@Aspect
public class MethodTracer {

  private static final String TAG = MethodTracer.class.getSimpleName();

  @Before("execution (protected void org.sdet.aspectj.MainActivity.onCreate(android.os.Bundle))")
  public void adviceOnCreate(JoinPoint joinPoint) {
    Log.v(TAG, joinPoint.toString());
  }
}

执行下面的命令行,

java -cp  org.aspectj.tools.ajc.Main -inpath  -aspectpath 

新生成的MainActivity.class,用Java bytecode decompile工具打开,如下。

import org.sdet.aspectj.MethodTracer;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.JoinPoint.StaticPart;
import org.aspectj.runtime.reflect.Factory;

public class MainActivity extends Activity {

  private static final JoinPoint.StaticPart ajc$tjp_0;

  private static void ajc$preClinit() {
    Factory localFactory = new Factory("MainActivity.java", MainActivity.class);
    ajc$tjp_0 = localFactory.makeSJP("method-execution", localFactory.makeMethodSig("4", "onCreate", "org.sdet.aspectj.MainActivity", "android.os.Bundle", "savedInstanceState", "", "void"), 13);   
  }

  protected void onCreate(Bundle savedInstanceState) {
    Bundle localBundle = savedInstanceState;
    // BEGIN - 织入的代码
    JoinPoint localJoinPoint = Factory.makeJP(ajc$tjp_0, this, this, localBundle);
    MethodTracer.aspectOf().adviceOnCreate(localJoinPoint);
    // END - 织入的代码
    super.onCreate(savedInstanceState);
    setContentView(2130903040);
  }
}

相关API解读,

//  org.aspectj.runtime.reflect.Factory.java
public MethodSignature makeMethodSig(
  java.lang.String modifiers,        // public: 1, private: 2, protected: 4
  java.lang.String   methodName,     // 函数名: onCreate
  java.lang.String   declaringType,  // 类名: org.sdet.aspectj.MainActivity
  java.lang.String[] paramTypes,     // 参数类型: [android.os.Bundle, ]
  java.lang.String[] paramNames,     // 参数名: [savedInstanceState,]
  java.lang.String[] exceptionTypes, // 异常类型: ""
  java.lang.String   returnType)     // 返回类型: void
)

public JoinPoint.StaticPart makeSJP(
  java.lang.String kind,           //
  Signature sig,                   //
  int l                            // 代码行号
 )

call和execution的区别

Android AspectJ实例讲解_第1张图片
Paste_Image.png
// MethodTracer.java
@Aspect
public class MethodTracer {

  @Before("execution (private void org.sdet.aspectj.MainActivity.method4Execution(..))")
  public void adviceOnExecution(JoinPoint joinPoint) {
    Log.v(TAG, joinPoint.toString());
  }

  @Before("call (private void org.sdet.aspectj.MainActivity.method4Call(..))")
  public void adviceOnCall(JoinPoint joinPoint) {
    Log.v(TAG, joinPoint.toString());
  }
}

public class MainActivity extends Activity {

  private static final JoinPoint.StaticPart ajc$tjp_0;
  private static final JoinPoint.StaticPart ajc$tjp_1;
 
  private static void ajc$preClinit() {
    Factory localFactory = new Factory("MainActivity.java", MainActivity.class);
    ajc$tjp_0 = localFactory.makeSJP("method-call", localFactory.makeMethodSig("2", "method4Call", "org.sdet.aspectj.MainActivity", "", "", "", "void"), 16);
    ajc$tjp_1 = localFactory.makeSJP("method-execution", localFactory.makeMethodSig("2", "method4Execution", "org.sdet.aspectj.MainActivity", "", "", "", "void"), 42);
  }

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(2130903040);
    method4Execution();
    // call织入的位置
    MainActivity localMainActivity = this;
    JoinPoint localJoinPoint = Factory.makeJP(ajc$tjp_0, this, localMainActivity);
    MethodTracer.aspectOf().adviceOnCall(localJoinPoint);
    localMainActivity.method4Call();
  }

  private void method4Execution() {
    // execution织入的位置
    JoinPoint localJoinPoint = Factory.makeJP(ajc$tjp_1, this, this);
    MethodTracer.aspectOf().adviceOnExecution(localJoinPoint);
    System.out.println("in method method4Execution");
  }
 
  private void method4Call() {
    System.out.println("in method method4Call");
  }
}

withincode

假设方法functionA, functionB都调用了dummy,但只想在functionB调用dummy时织入代码。

//  MainActivity.java
public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    functionA();
    functionB();
  }

  private void functionA() { dummy(); }
  private void functionB() { dummy(); }
  private void dummy() { }
}
// MethodTracer.java
@Aspect
public class MethodTracer {
  // withincode: 在functionB方法内
  @Pointcut("withincode(void org.sdet.aspectj.MainActivity.functionB(..))")
  public void invokeFunctionB() {}

  // call: 调用dummy方法
  @Pointcut("call(void org.sdet.aspectj.MainActivity.dummy(..))")
  public void invokeDummy() {}

  // 在functionB内调用dummy方法
  @Pointcut("invokeDummy() && invokeFunctionB()")
  public void invokeDummyInsideFunctionB() {}

  @Before("invokeDummyInsideFunctionB()")
  public void beforeInvokeDummyInsideFunctionB(JoinPoint joinPoint) {
    System.out.printf("Before.InvokeDummyInsideFunctionB.advice() called on '%s'", joinPoint);
  }
}
// MainActivity.java

public class MainActivity extends Activity {

  private static final JoinPoint.StaticPart ajc$tjp_0;

  private static void ajc$preClinit() {
    Factory localFactory = new Factory("MainActivity.java", MainActivity.class);
    ajc$tjp_0 = localFactory.makeSJP("method-call", localFactory.makeMethodSig("2", "dummy", "org.sdet.aspectj.MainActivity", "", "", "", "void"), 56);
  }

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(2130903040);    
    functionA();
    functionB();
  }

  private void functionA() {
    dummy();
  }

  // 只有functionB调用dummy时才会织入代码
  private void functionB() {
    MainActivity localMainActivity = this;
    JoinPoint localJoinPoint = Factory.makeJP(ajc$tjp_0, this, localMainActivity);
    MethodTracer.aspectOf().beforeInvokeDummyInsideFunctionB(localJoinPoint);
    localMainActivity.dummy();
  }

around

AspectJ “around” and “proceed” with “before / after”

http://stackoverflow.com/questions/18016503/aspectj-around-and-proceed-with-before-after

碰到的问题

  • com.android.dx.cf.iface.ParseException: bad class file magic (cafebabe) or version (0034.0000)
    原因:Set compliance level to 1.8时会碰到这个异常

  • java.lang.NoSuchMethodError: org.sdet.aspectj.MethodTracer.aspectOf
    原因:AspectJ代码用了javac编译

你可能感兴趣的:(Android AspectJ实例讲解)