AspectJ parent method

南楼画角 提交于 2020-01-07 03:02:35

问题


I have a around method which tries to hide keyboard on dispatchTouchEvent method.

@Around("execution(boolean (@com.savaskoc.keyboard.KeyboardHide *).dispatchTouchEvent(android.view.MotionEvent))")

This works well if I override dispatchTouchEvent method from android.app.Activity like that

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    return super.dispatchTouchEvent(ev);
}

Is there any way for intercept parent class' method?

Ps: I tried change "execution" to "call" and it gives;

[warning] advice defined in com.savaskoc.keyboard.KeyboardAspect has not been applied [Xlint:adviceDidNotMatch

I have annotated class named BaseActivity. Around works when I override dispatchTouchEvent like this

@KeyboardHide
public abstract class BaseActivity extends ToolbarActivity {
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        return super.dispatchTouchEvent(ev);
    }
}

but when I remove this method, I'm getting a warning like above


回答1:


Either you intercept calls instead of executions like this:

call(boolean (@com.savaskoc.keyboard.KeyboardHide *).dispatchTouchEvent(android.view.MotionEvent))

Or, if this is not good enough because calls are being made from code outside the control of your AspectJ compiler, you can continue using execution pointcuts but need to weave aspects into the third party library you want to intercept via binary weaving, creating a new, woven version of it.

Update after Hyperion's edit:

The warning advice ... has not been applied [Xlint:adviceDidNotMatch] simply means that the AspectJ compiler has not found any joinpoints to weave your pointcut into. In this case it means just what I mentioned above when I said:

calls are being made from code outside the control of your AspectJ compiler

This means (repeating myself in other words) that either you have to weave your aspect code into the original JAR containing class ToolbarActivity via binary compile-time weaving or via load-time weaving during runtime, making sure that the aspect weaver is loaded before the original JAR so it can intercept it during class-loading. I have no idea if LTW (or Java agents in general) is available on Android JVMs (I would guess no) because I have never used Android in my whole life, but these two are the options you have. The workaround to override methods you want to intercept always works if all else fails.



来源:https://stackoverflow.com/questions/34840802/aspectj-parent-method

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!