简体   繁体   English

如何将 aspectJ 切入点与动态选择器一起使用?

[英]How can aspectJ pointcuts be used with dynamic selector?

I'm working on a small project that determines code coverage when testing a java application.我正在开发一个小项目,该项目在测试 Java 应用程序时确定代码覆盖率。 It basically consists of a plugin for an IDE which finds all the classes and methods in the project and saves them in a database, and an agent with aspectJ pointcuts that weave around all of these methods to log their execution.它基本上由一个用于 IDE 的插件组成,该插件可查找项目中的所有类和方法并将它们保存在数据库中,以及一个具有 aspectJ 切入点的代理,该切入点围绕所有这些方法编织以记录它们的执行情况。

The problem I have is that I only want to log the methods that are actually written by the developers of that very project and not those of underlying libraries.我遇到的问题是,我只想记录该项目的开发人员实际编写的方法,而不是底层库的方法。 So the pointcuts need to be defined in a way that only methods of classes in the actual project packages are woven.所以切入点需要以一种方式定义,即只编织实际项目包中的类的方法。 On the other hand, since the agent is to be used with all sorts of projects, I can't hardcode those packages.另一方面,由于代理要用于各种项目,我无法对这些包进行硬编码。

My attempt so far was to read all the package names from the database and build a string from that.到目前为止,我的尝试是从数据库中读取所有包名称并从中构建一个字符串。 Basically what it looks like is this:基本上它的样子是这样的:

private static final String POINTCUT_STRING = AspectUtil.buildPointcutString();

And then, when defining the pointcut:然后,在定义切入点时:

@Pointcut(POINTCUT_STRING)

Thing is, this doesn't work because apparently when defining a Pointcut, the问题是,这不起作用,因为显然在定义切入点时,

Attribute value needs to be a constant.属性值需要是一个常量。

So, how can I make it so that i can only weave methods in classes in the packages that I have in my database?那么,我如何才能使我只能在数据库中的包中的类中编织方法?

Thanks in advance, have a good one!预先感谢,祝你好运!

I don't think a dynamic aspect approach is going to work as aspectj does not expose the weaver to any state management or changes.我不认为动态方面方法会起作用,因为 aspectj 不会将 weaver 暴露给任何状态管理或更改。 Although this would be theoretically possible at runtime it's definitely not possible at compile time (and you have the option to add your aspects at compile time).尽管理论上这在运行时是可能的,但在编译时绝对不可能(并且您可以选择在编译时添加方面)。

But to your issue...但是对于你的问题...

What weave strategy are you using?你使用什么编织策略? compile or runtime?编译还是运行时? I've found compile to work very well and I'm not sure how to use runtime with aspectj.我发现 compile 工作得很好,但我不确定如何将运行时与 aspectj 一起使用。 But what I can say is that if you use compile you'll only be weaving the application classes in any case as that is all you'll have access to.但是我可以说的是,如果您使用 compile,那么无论如何您都只会编织应用程序类,因为这是您可以访问的全部内容。

Another comment to make is if you want to do something dynamic you'd be better off putting the condition on whether to monitor that method for code coverage downstream of the aspect.另一个评论是,如果您想做一些动态的事情,最好将条件放在是否监视该方法的方面下游代码覆盖率上。 So when the aspect is executed the first thing it will do is decide if this class/method call should be monitored for coverage and then go on from there...因此,当执行方面时,它要做的第一件事就是决定是否应该监视此类/方法调用的覆盖范围,然后从那里继续……

When I asked you:当我问你:

What do you mean by "runtime weaving"? “运行时编织”是什么意思? Load-time weaving (LTW) maybe?加载时编织(LTW)可能吗? Ie you are using aop.xml ?即您正在使用aop.xml I am asking for a specific reason.我要求一个特定的原因。

You replied:你回复:

Yes, LTW.是的,LTW。 I am using an aop.xml file.我正在使用aop.xml文件。

In that case you have the option of specifying pointcut definitions in aop.xml which is read during JVM start-up when the weaving agent is activated.在这种情况下,您可以选择在aop.xml中指定切入点定义,当编织代理被激活时,在 JVM 启动期间读取该切入点定义。 For refererence, please read the AspectJ Developers Guide, there is a chapter on LTW .作为参考,请阅读 AspectJ 开发人员指南,其中有一 章是关于 LTW 的 You will find sample code and sample XML definitions there, showing how you can extend an abstract aspect with an abstract pointcut in the XML file and specify a concrete pointut for a concrete subclass.您将在那里找到示例代码和示例 XML 定义,展示如何使用 XML 文件中的抽象切入点扩展抽象方面,并为具体子类指定具体切入点。 That should give you the options you need to keep your pointcut out of the Java code, for whatever reason you think that's a good thing and you need it.这应该为您提供将切入点排除在 Java 代码之外所需的选项,无论出于何种原因,您认为这是一件好事并且您需要它。

Please note that you cannot expect to modify aop.xml during runtime and re-load it, possibly re-applying the aspect dynamically to all classes.请注意,您不能期望在运行时修改aop.xml并重新加载它,可能会动态地将方面重新应用于所有类。 AspectJ LTW works in connection with class-loading, ie you only have one shot at JVM start-up before all application classes are loaded. AspectJ LTW 与类加载一起工作,即在加载所有应用程序类之前,您只有一次 JVM 启动机会。 This is not an AspectJ limitation but just how bytecode instrumentation in the JVM works.这不是 AspectJ 的限制,而是 JVM 中字节码检测的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM