如何为ajc指定aop.xml

问题描述 投票:0回答:1

我目前正在尝试使用 AspectJ 在构建时检测 Java 类。为此,我假设

ajc
(https://eclipse.dev/aspectj/doc/released/devguide/ajc-ref.html) 因为 AspectJ 编译器是一个合适的工具。但是,我无法让它在我的代码上运行。

我试图编织的方面如下:https://github.com/kieker-monitoring/kieker/blob/main/monitoring/src/kieker/monitoring/probe/aspectj/operationExecution/OperationExecutionAspectFull.java它与其他方面捆绑在一个罐子中。我在 MooBench 基准测试上执行此操作(https://github.com/kieker-monitoring/moobench/tree/main/tools/benchmark),但任何其他简单的 jar 也会发生同样的情况。

根据文档,我可以指定包含应编织的方面的

-aspectpath
。基于 AspectJs 常规流程,我假设我可以使用
aop.xml
指定方面,在我的例子中看起来像这样(并且适用于使用
-javaagent
进行加载时编织):

<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN" "http://www.aspectj.org/dtd/aspectj_1_5_0.dtd">
<aspectj>
  <weaver options="">
    <include within="moobench.application.*" />
  </weaver>
  <aspects>
    <aspect
      name="kieker.monitoring.probe.aspectj.operationExecution.OperationExecutionAspectFull" />
  </aspects>
</aspectj>

但是,

ajc
始终忽略此文件。我尝试将其直接作为文件夹传递(
-aspectpath "$(pwd)/meta-inf-classpath/:$KIEKERJAR"
,也同样使用
$(pwd)/meta-inf-classpath/META-INF
$(pwd)/meta-inf-classpath/META-INF/aop.xml
),或压缩为jar(
-aspectpath "$(pwd)/meta-inf-classpath.jar:$KIEKERJAR"
),但AspectJ总是开始使用所有方面进行检测,导致像这样的日志:

reichelt@reichelt-desktop:~/nvme/workspaces/kiekerworkspace/moobench/benchmark/lib$ ajc -aspectpath "$(pwd)/meta-inf-classpath.jar:/home/reichelt/.m2/repository/net/kieker-monitoring/kieker/2.0.0-SNAPSHOT/kieker-2.0.0-SNAPSHOT-aspectj.jar" -inpath benchmark.jar 
<Unknown> [warning] Found @DeclareAnnotation while current release does not support it (see 'org.aspectj.weaver.bcel.AtAjAttributes')

kieker/monitoring/probe/aspectj/flow/concurrency/SynchronizedAspect.java [warning] unlock() pointcut designator cannot be used without the option -Xjoinpoints:synchronization

kieker/monitoring/probe/aspectj/flow/concurrency/SynchronizedAspect.java [warning] lock() pointcut designator cannot be used without the option -Xjoinpoints:synchronization

kieker/monitoring/probe/aspectj/flow/concurrency/SynchronizedAspect.java [warning] lock() pointcut designator cannot be used without the option -Xjoinpoints:synchronization

kieker/monitoring/probe/aspectj/jersey/OperationExecutionJerseyClientInterceptor.java [warning] no match for this type name: com.sun.jersey.api.client.ClientResponse [Xlint:invalidAbsoluteTypeName]

kieker/monitoring/probe/aspectj/jersey/OperationExecutionJerseyClientInterceptor.java [warning] no match for this type name: com.sun.jersey.client.apache4.ApacheHttpClient4Handler [Xlint:invalidAbsoluteTypeName]

kieker/monitoring/probe/aspectj/jersey/OperationExecutionJerseyClientInterceptor.java [warning] no match for this type name: com.sun.jersey.api.client.ClientRequest [Xlint:invalidAbsoluteTypeName]

kieker/monitoring/probe/aspectj/jersey/OperationExecutionJerseyServerInterceptor.java [warning] no match for this type name: com.sun.jersey.spi.container.ContainerResponse [Xlint:invalidAbsoluteTypeName]

kieker/monitoring/probe/aspectj/jersey/OperationExecutionJerseyServerInterceptor.java [warning] no match for this type name: com.sun.jersey.server.impl.application.WebApplicationImpl [Xlint:invalidAbsoluteTypeName]

kieker/monitoring/probe/aspectj/jersey/OperationExecutionJerseyServerInterceptor.java [warning] no match for this type name: com.sun.jersey.server.impl.application.WebApplicationContext [Xlint:invalidAbsoluteTypeName]

kieker/monitoring/probe/aspectj/jersey/OperationExecutionJerseyServerInterceptor.java [warning] no match for this type name: com.sun.jersey.spi.container.ContainerRequest [Xlint:invalidAbsoluteTypeName]

kieker/monitoring/probe/aspectj/jersey/OperationExecutionJerseyServerInterceptor.java [warning] no match for this type name: com.sun.jersey.spi.container.ContainerResponse [Xlint:invalidAbsoluteTypeName]

kieker/monitoring/probe/aspectj/operationExecution/AbstractOperationExecutionAspectServlet.java [warning] no match for this type name: javax.servlet.http.HttpServletRequest [Xlint:invalidAbsoluteTypeName]

kieker/monitoring/probe/aspectj/operationExecution/AbstractOperationExecutionAspectServlet.java [warning] no match for this type name: javax.servlet.http.HttpServletResponse [Xlint:invalidAbsoluteTypeName]

kieker/monitoring/probe/aspectj/operationExecution/AbstractOperationExecutionAspectServlet.java [error] bad parameter to pointcut reference

kieker/monitoring/probe/aspectj/spring/RestCommunicationAspect.java [warning] no match for this type name: org.springframework.web.servlet.FrameworkServlet [Xlint:invalidAbsoluteTypeName]

kieker/monitoring/probe/aspectj/spring/RestCommunicationAspect.java [warning] no match for this type name: org.springframework.http.client.support.HttpAccessor [Xlint:invalidAbsoluteTypeName]

它因错误而失败并且不执行我的检测。

另一方面,当我尝试从 jar 中删除我不想要的所有方面时,我仍然收到有关 AspectJ 无法确定两个建议之间的优先级的错误(1053 次):

[...]

/home/reichelt/.m2/repository/net/kieker-monitoring/kieker/2.0.0-SNAPSHOT/kieker-2.0.0-SNAPSHOT-aspectj.jar!kieker/monitoring/probe/aspectj/operationExecution/AbstractOperationExecutionAspect.class [error] circular advice precedence: can't determine precedence between two or more pieces of advice that apply to the same join point: method-execution(void com.beust.jcommander.validators.PositiveInteger.validate(java.lang.String, java.lang.String))

/home/reichelt/.m2/repository/net/kieker-monitoring/kieker/2.0.0-SNAPSHOT/kieker-2.0.0-SNAPSHOT-aspectj.jar!kieker/monitoring/probe/aspectj/operationExecution/AbstractOperationExecutionAspect.class [error] circular advice precedence: can't determine precedence between two or more pieces of advice that apply to the same join point: method-execution(void com.beust.jcommander.validators.PositiveInteger.validate(java.lang.String, java.lang.String))

/home/reichelt/.m2/repository/net/kieker-monitoring/kieker/2.0.0-SNAPSHOT/kieker-2.0.0-SNAPSHOT-aspectj.jar!kieker/monitoring/probe/aspectj/operationExecution/AbstractOperationExecutionAspect.class [error] circular advice precedence: can't determine precedence between two or more pieces of advice that apply to the same join point: method-execution(void com.beust.jcommander.validators.PositiveInteger.validate(java.lang.String, java.lang.String))

如果仅对一个方面和给定的类进行检测,即考虑

aop.xml
,则可能不会发生这种情况。那么有没有办法在
aop.xml
中指定
ajc
的用法?

其他问题(AspectJ 与 aop.xmlaop.xml 名称和位置?)对加载时编织提出相同的要求,但我想在构建时进行编织。

java aspectj
1个回答
0
投票

由于我们之前私下讨论过其他仪器相关的话题,我知道你的部分情况。当从加载时切换到编译时编织时,您可以简单地摆脱 aop.xml 并配置构建工具 - Maven (AspectJ Maven Plugin)、Gradle (Freefair) 等 - 并编译你的单模块项目。对于像 Kieker 这样的多模块项目,您只需确保在编译要检测的模块时将包含方面的模块放在inpath上:AspectJ MavenFreefair

我知道,您正在考虑使用

-xmlConfigured
编译器参数。我不认为这是您需要或应该使用的。你可能会把简单的事情复杂化。我强烈建议依赖构建工具插件,而不是重新发明任何轮子。

© www.soinside.com 2019 - 2024. All rights reserved.