文章目錄
- 如何織入代碼
- 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());
}
}
執(zhí)行下面的命令行,
java -cp <classpath> org.aspectj.tools.ajc.Main -inpath <MainActivity.class path> -aspectpath <MethodTracer.class path>
新生成的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);
}
}
相關(guān)API解讀肿仑,
// org.aspectj.runtime.reflect.Factory.java
public MethodSignature makeMethodSig(
java.lang.String modifiers, // public: 1, private: 2, protected: 4
java.lang.String methodName, // 函數(shù)名: onCreate
java.lang.String declaringType, // 類名: org.sdet.aspectj.MainActivity
java.lang.String[] paramTypes, // 參數(shù)類型: [android.os.Bundle, ]
java.lang.String[] paramNames, // 參數(shù)名: [savedInstanceState,]
java.lang.String[] exceptionTypes, // 異常類型: ""
java.lang.String returnType) // 返回類型: void
)
public JoinPoint.StaticPart makeSJP(
java.lang.String kind, //
Signature sig, //
int l // 代碼行號
)
call和execution的區(qū)別
// 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
假設(shè)方法functionA, functionB都調(diào)用了dummy谈撒,但只想在functionB調(diào)用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方法內(nèi)
@Pointcut("withincode(void org.sdet.aspectj.MainActivity.functionB(..))")
public void invokeFunctionB() {}
// call: 調(diào)用dummy方法
@Pointcut("call(void org.sdet.aspectj.MainActivity.dummy(..))")
public void invokeDummy() {}
// 在functionB內(nèi)調(diào)用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調(diào)用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編譯