背景

一个项目使用Aspects进行记录。细节:


Java 11
AspectJ 1.9.4(运行时,工具,编译器,编译后编织的插件)
Mockito Core 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


尽管应该将woven.jar中的输出类注入到application.jar中。

附录

注意:


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


有关


Gradle and AspectJ - avoid weaving my own packages during compile time
How to set "-aspectpath" for the FreeFair AspectJ Gradle Plugin?
AspectJ + Gradle configuration


尝试次数

如下调用compileJava时:

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


Gradle报告以下错误:


  无法为类型io.freefair.gradle.plugins.aspectj.AjcAction的对象设置只读属性'classpath'的值。


使用:

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


摇篮报告:


  在类型io.freefair.gradle.plugins.aspectj.AjcAction的对象上找不到参数[...]的方法options()。


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

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

最佳答案

声明的依赖项是可传递的:

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


这会将:company.project.main的完整运行时类路径(由所述项目生成的类及其所有依赖项)传递到ajc的-inpath中。 (请参见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/

10-12 00:36