AspectJ父方法 [英] AspectJ parent method

查看:207
本文介绍了AspectJ父方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个around方法,试图隐藏dispatchTouchEvent方法上的键盘.

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

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

如果我像这样从android.app.Activity覆盖dispatchTouchEvent方法,此方法效果很好

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:我尝试将执行"更改为通话",它给出了;

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

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

我有一个名为BaseActivity的带注释的类.当我这样覆盖dispatchTouchEvent

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

推荐答案

您可以拦截呼叫,而不是像这样执行代码:

Either you intercept calls instead of executions like this:

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

或者,如果这还不够好,因为调用是从AspectJ编译器无法控制的代码中进行的,则可以继续使用执行切入点,但需要将方面编织到要通过二进制编织来拦截的第三方库中,创建新的编织版本.

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:

警告advice ... has not been applied [Xlint:adviceDidNotMatch]仅表示AspectJ编译器未找到任何将切入点编织到的联接点.在这种情况下,这意味着我刚才说的内容:

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:

调用是由AspectJ编译器无法控制的代码进行的

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

这意味着(换句话说,就是重复我自己),您必须通过二进制编译时编织或在运行时通过加载时编织将方面代码编织到包含类ToolbarActivity的原始JAR中. weaver在原始JAR之前加载,因此它可以在类加载期间对其进行拦截.我不知道LTW(或一般的Java代理)是否可以在Android JVM上使用(我想不会),因为我一生中从未使用过Android,但是您可以选择这两个.如果所有其他方法均失败,则覆盖要拦截的方法的解决方法始终有效.

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.

这篇关于AspectJ父方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆