java - AspectJ 找不到 .raw 父类(super class)

标签 java mockito gradle-plugin aspect compile-time-weaving

背景

项目使用方面进行日志记录。详情:

  • Java 11
  • AspectJ 1.9.4(运行时、工具、编译器、编译后编织插件)
  • Mockito 核心 2.25.1

build.gradle 文件类似于:

apply plugin: "io.freefair.aspectj.post-compile-weaving"

dependencies {
    compileOnly group: 'javax.servlet', name: 'javax.servlet-api', version: '4.0.1'
    compileOnly group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.12.1'
    compileOnly group: 'org.osgi', name: 'org.osgi.framework', version: '1.9.0'
    compileOnly group: 'org.mockito', name: 'mockito-core', version: '2.25.1'

    inpath project(":company.project.main")
}

问题

编译应用程序时,AspectJ 找不到 MockMethodDispatcher,并报告错误:

.../mockito-core-2.25.1.jar [error] can't determine superclass of missing type org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher
when weaving type org.mockito.internal.creation.bytebuddy.MockMethodAdvice
when weaving classes 
when weaving 
when batch building BuildConfig[null] #Files=0 AopXmls=#0
 [Xlint:cantFindType]
(no source information available)
        [Xlint:cantFindType]
.../org.mockito/mockito-core/2.25.1/e8fa2864b65c0b6fbb20daa436a94853bcd17e5e/mockito-core-2.25.1.jar [warning] can't find type org.mockito.internal.creation.bytebuddy.inject.MockMethodDispatcher whilst determining signatures of call or execution join point for java.util.concurrent.Callable org.mockito.internal.creation.bytebuddy.MockMethodAdvice.handle(java.lang.Object, java.lang.reflect.Method, java.lang.Object[]), this may cause a pointcut to fail to match at this join point 
when weaving type org.mockito.internal.creation.bytebuddy.MockMethodAdvice
when weaving classes 
when weaving 
when batch building BuildConfig[null] #Files=0 AopXmls=#0
 [Xlint:cantFindTypeAffectingJPMatch]

我怀疑这是因为该文件存储为 .raw 文件而不是 .class 文件(根据 issue 845 ):

1778 Mon Jul 08 13:47:02 PDT 2019 org/mockito/internal/creation/bytebuddy/inject/MockMethodDispatcher.raw

问题

您将如何更新 Gradle 文件来指示 post-compile-weaving插件完全忽略编织(或扫描)Mockito 类?

注释

从命令行,编织似乎可以工作:

java -cp aspectjtools-1.9.4.jar:aspectjrt-1.9.4.jar org.aspectj.tools.ajc.Main \
-inpath application.jar \
-aspectpath ../aspects/build/classes/java/main \
-Xlint:warning \
-verbose \
-showWeaveInfo \
-log aop.log \
-outjar woven.jar

尽管 wed.jar 中的输出类应该注入(inject)到 application.jar 中。

附录

注意:

  • 通过使用 !within @Pointcut 注解装饰 @Aspect 注解的类来解决此问题是不可行的。强烈希望将参数传递给 ajc通过插件。
  • cantFindType 从错误降级为警告将是一个令人满意的答案,但并不理想(停留在语法上),因为我希望保留其他 cantFindType 错误作为错误。

相关

尝试

调用compileJava时如下:

compileJava {
    ajc {
        enabled = true
        classpath = configurations.aspectj
        options {
            aspectpath = configurations.aspect
            compilerArgs = [""]
        }
    }
}

Gradle 报告以下错误:

Cannot set the value of read-only property 'classpath' for object of type io.freefair.gradle.plugins.aspectj.AjcAction.

使用:

compileJava {
  ajc {
    options {
      compilerArgs = [""]
    }
  }
}

Gradle 报告:

Could not find method options() for arguments [...] on object of type io.freefair.gradle.plugins.aspectj.AjcAction.

source code在 master 上似乎为其“可配置的东西”公开了不同的名称:

task.getInputs().property("ajcArgs", this.getOptions().getCompilerArgs())

最佳答案

声明的依赖是可传递的:

inpath project(":company.project.main")

这会将 :company.project.main 的完整运行时类路径(由所述项目及其所有依赖项生成的类)传递到 -inpath ajc 的 。 (请参阅 build/tmp/compileJava/ajc.options 文件进行确认。)

为了避免将建议编织到外部类中,请声明 non-transitive dependency如下:

inpath(project(":company.project.main")) { 
    transitive = false
}

根据您的具体要求和项目结构,将 io.freefair.aspectj.post-compile-weaving 插件直接应用于 :company 可能是更好的方法。 project.main 项目。

关于java - AspectJ 找不到 .raw 父类(super class),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58193520/

相关文章:

java - 需要使用mockito测试apache poi excel

gradle-bintray-plugin 插件 [id : 'com.jfrog.bintray' , 版本: '1.+'] 未找到

java - 用括号括起来的对象

java - 使用 Mockito/JUnit 模拟 Java 常量变量(公共(public)静态)

java - 数组中唯一元素的数量

java - JUnit:如何检查一个方法是否调用另一个方法?

gradle - 在gradle中为插件添加选项

android - 找不到参数的方法 classpath() [com.android.tools.build :gradle:3. 4.2]

java - 为 KeyStroke 设置多个掩码

java - 使用RowSorter时如何获取JTable上的数据?