AspectJ的注解基于切入点不会被调用 [英] AspectJ annotated based pointcuts not being invoked

查看:1046
本文介绍了AspectJ的注解基于切入点不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图创建一个检查,如果互联网是在执行具有我定义了自定义注释的方法之前提供一个Android库。我使用AspectJ来做到这一点。

我的注解如下:

  @Target({METHOD})@Retention(运行时)
公共@interface InternetRequired {}

现在我的方面:

  @Aspect
公共类CilantroAspect
{    私有静态最后弦乐POINTCUT_METHOD =执行(@ com.cilantro.service.InternetRequired *(..));
    私有静态最后弦乐POINTCUT_METHOD2 =@注解(com.cilantro.service.InternetRequired);
    ;    @Pointcut(POINTCUT_METHOD2)
    公共无效internetAnnotatedMethod(){
    }    @Around(internetAnnotatedMethod())
    公共无效checkInternetConnectivity(ProceedingJoinPoint连接点)抛出的Throwable
        Log.v(看点,建议被触发);
        如果(Cilantro.isConnected()){
            joinPoint.proceed();
        }其他{
            Cilantro.Toast(网络不可用);
        }
    }
}

我与注解的方法活动的片段。

  ....
    Cilantro.init(本);
    测试();
}@InternetRequired
公共无效测试()
{
    Toast.makeText(这一点,试验方法,Toast.LENGTH_LONG).show();
}

当我运行我的Andr​​oid应用程序的around建议不被触发。我试着用POINTCUT_METHOD和POINTCUT_METHOD2。仍然没有运气。

我的Andr​​oid应用程序被配置为使用看点Ĵ,所以我知道,因为如果我的切入点定义内作出了错误,这detected..but只是要确定让我分享这不是问题。

主要构建脚本

  buildscript {
    库{
        jcenter()
    }
    依赖{
        类路径'com.android.tools.build:gradle:1.2.3
        类路径'com.uphyca.gradle:gradle这个-Android的AspectJ的插件:0.9 +'
}
....

模块含有方面

 应用插件:'com.android.library
应用插件:Android系统的AspectJ
....


解决方案

环绕通知没有被触发,因为我用的是与从应用层面的看点注解,而方面是包含在库中。对于各方面被编织成在编译时应用模块,我不得不只公布库Maven的(本地测试和Maven中央分配),然后包括库中包含AspectJ织一个gradle这个插件项目依赖关系然后tasks.The插件应用到应用程序的模块。

下面是我的插件,它是写在Groovy中的一个片段。添加我库,包含我的方面,然后运行该应用程序的模块上的编织任务。

  project.dependencies {
    编译com.github.jd亚历山大:FLENDER运行时:1.0
    // TODO这应该是及物动词
    编译org.aspectj:aspectjrt:1.8.5
}variants.all {变种 - >    variant.dex.doFirst {
        字串[] args = [
                -showWeaveInfo
                -1.5
                -inpath,javaCompile.destinationDir.toString(),
                -aspectpath,javaCompile.classpath.asPath,
                -d,javaCompile.destinationDir.toString()
                -classpath,javaCompile.classpath.asPath,
                -bootclasspath,project.android.bootClasspath.join(File.pathSeparator)
        ]
        log.debugAJC ARGS:+ Arrays.toString(参数)        的MessageHandler处理=新的MessageHandler(真);
        新的Main()跑(参数,处理程序);

有关的如何做到这一点,你可以检查我的出库在github上 HTTPS一个完整的例子:// github上.COM / JD-亚历山大/ FLENDER

I am attempting to create an android library that checks if internet is available before executing a method that has a custom annotation I have defined. I'm using AspectJ to accomplish this.

My annotation is as follows :

@Target({METHOD}) @Retention(RUNTIME)
public @interface InternetRequired  {

}

Now for my aspect:

@Aspect
public class CilantroAspect
{

    private static final String POINTCUT_METHOD = "execution(@com.cilantro.service.InternetRequired * *(..))";
    private static final String POINTCUT_METHOD2 ="@annotation(com.cilantro.service.InternetRequired)";
    ;

    @Pointcut(POINTCUT_METHOD2)
    public void internetAnnotatedMethod() {
    }

    @Around("internetAnnotatedMethod()")
    public void checkInternetConnectivity(ProceedingJoinPoint joinPoint) throws Throwable {
        Log.v("Aspect","advice being triggered");
        if (Cilantro.isConnected()) {
            joinPoint.proceed();
        } else {
            Cilantro.Toast("Internet not available");
        }
    }
}

Snippet of my activity with the annotated method.

    ....
    Cilantro.init(this);
    test();
}

@InternetRequired
public void test()
{
    Toast.makeText(this,"Test method",Toast.LENGTH_LONG).show();
}

When I run my android app the around advice is not being triggered. I tried using POINTCUT_METHOD and POINTCUT_METHOD2. Still no luck.

My android app is configured to use Aspect J so I know that's not the problem because if I make errors within the pointcut definitions it's detected..but just to be sure let me share.

Main build script

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.2.3'
        classpath 'com.uphyca.gradle:gradle-android-aspectj-plugin:0.9.+'
}
....

module containing Aspects

apply plugin: 'com.android.library'
apply plugin: 'android-aspectj' 
....

解决方案

The around advice wasn't being triggered because I was using annotations related to the Aspect from the app level while the aspect was contained in the library. For the aspects to be weaved into the app module at compile time I had to simply publish the library to maven (local for testing and maven central for distribution) and then include the library as a project dependency in a gradle plugin which contains the AspectJ weaving tasks.The plugin is then applied to the app's module.

Here's a snippet of my plugin that's written in groovy. I add my library that contains my Aspects and then run the weaving tasks on the app's module.

project.dependencies {
    compile 'com.github.jd-alexander:flender-runtime:1.0'
    // TODO this should come transitively
    compile 'org.aspectj:aspectjrt:1.8.5'
}

variants.all { variant ->

    variant.dex.doFirst {
        String[] args = [
                "-showWeaveInfo",
                "-1.5",
                "-inpath", javaCompile.destinationDir.toString(),
                "-aspectpath", javaCompile.classpath.asPath,
                "-d", javaCompile.destinationDir.toString(),
                "-classpath", javaCompile.classpath.asPath,
                "-bootclasspath", project.android.bootClasspath.join(File.pathSeparator)
        ]
        log.debug "ajc args: " + Arrays.toString(args)

        MessageHandler handler = new MessageHandler(true);
        new Main().run(args, handler);

For a complete example of how this is done you can check my library out on github https://github.com/jd-alexander/flender

这篇关于AspectJ的注解基于切入点不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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