背景
项目使用方面进行日志记录。详情:
- 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
错误作为错误。
相关
- 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 报告以下错误:
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/