代码之家  ›  专栏  ›  技术社区  ›  Roddy of the Frozen Peas

Aspectj切入点以可选依赖项的注释为目标

  •  1
  • Roddy of the Frozen Peas  · 技术社区  · 6 年前

    我正在使用aspectj以使用第三方注释的方法为目标。然而,我不能保证这个注释在类路径上是可用的。有没有一种方法可以从可选依赖项中定位注释?

    举个例子,我可能想针对JUnit5 @ParameterizedTest 注释。我的aj文件看起来像这样:

    public aspect Example {
      pointcut beforeTest(): @annotation(ParamterizedTest);
    
      before(): beforeTest() {
        System.out.println("This is a Parameterized Test!");
      }
    }
    

    但是,如果我的项目使用JUnit 4,或者不包括JUnit jupiter params库,那么Maven将无法编织,因为它找不到类:

    2019-02-04 16:37:37.649 [ERROR] Failed to execute goal org.codehaus.mojo:aspectj-maven-plugin:1.11:test-compile (default) on project ExampleProject: AJC compiler errors:
    2019-02-04 16:37:37.650 [ERROR] error at (no source information available)
    2019-02-04 16:37:37.656 [ERROR] /jenkins/workspace/exampleProject/src/test/java/com/example/ExampleTest.java:0::0 can't determine annotations of missing type org.junit.jupiter.params.ParameterizedTest
    2019-02-04 16:37:37.657 [ERROR] when weaving type com.example.ExampleTest
    2019-02-04 16:37:37.657 [ERROR] when weaving classes
    2019-02-04 16:37:37.657 [ERROR] when weaving
    2019-02-04 16:37:37.658 [ERROR] when batch building BuildConfig[null] #Files=21 AopXmls=#0
    2019-02-04 16:37:37.658 [ERROR] [Xlint:cantFindType]
    

    我试着把这个库添加到 aspectj-maven-plugin 是的 <dependencies> 像这样的部分:

    <plugin>
      <groupId>org.codehaus.mojo</groupId>
      <artifactId>aspectj-maven-plugin</artifactId>
      <version>1.11</version>
      <configuration>
        <source>1.8</source>
        <target>1.8</target>
        <complianceLevel>1.8</complianceLevel>
        <aspectLibraries>
          <aspectLibrary>
            <groupId>com.example</groupId>
            <artifactId>example-aspects</artifactId>
          </aspectLibrary>
        </aspectLibraries>
      </configuration>
      <executions>
        <execution>
          <goals>
            <goal>test-compile</goal>
          </goals>
        </execution>
      </executions>
      <dependencies>
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjrt</artifactId>
          <version>1.8.13</version>
        </dependency>
        <dependency>
          <groupId>org.aspectj</groupId>
          <artifactId>aspectjtools</artifactId>
          <version>1.8.13</version>
        </dependency>
        <dependency>
          <groupId>org.junit.jupiter</groupId>
          <artifactId>junit-jupiter-params</artifactId>
          <version>5.1.1</version>
        </dependency>
      </dependencies>
    </plugin>
    

    ... 但这没什么区别。

    有没有一种方法可以在不需要添加依赖项的情况下实现这一点?如果有使用第三方注释(如果存在)注释的方法,我非常希望切入点起作用,否则就被忽略。

    (为了实现junit示例的目的,我构建了这个示例,以确认它的工作原理与我真正的问题相同,我的方面库确实声明了对junit jupiter参数的依赖。)

    1 回复  |  直到 6 年前
        1
  •  0
  •   kriegaex    6 年前

    您的方面使用该类,即使您的示例代码没有显示该类,也必须在方面的顶部为其导入。这意味着它肯定是方面库的依赖项,而不是可选依赖项。因此,您必须在库的Maven POM中定义它。其他任何事情都没有意义。定义它到底有什么大不了的?适当的依赖管理是Maven的初衷。


    更新: 谢谢你的澄清评论。

    您可以使用基于注释的语法而不是本机语法,因为它不需要导入。切入点将不匹配,另一个Xlint警告将显示这一点。

    如果您可以选择使用AspectJ编译器编译其他项目,我还建议您研究 @DeclareError @DeclareWarning 。它将帮助您强制执行策略,在编译期间不使用参数化测试,而不是在运行时抛出错误或记录某些内容。AspectJ编译器不会使用该注释编译测试。