使用运算符“==”的切入点具有特定类型

发布于 2024-12-07 07:16:43 字数 298 浏览 0 评论 0原文

是否可以编写一个与特定类型的特定运算符的使用相匹配的 AspectJ 切入点?

一些背景信息:我正在开发一个项目,我们必须使用遗留的Java库(5.0之前,enum关键字之前),它带有几种“伪枚举”类型,即普通Java定义其自己类型的静态常量的类。这些“感觉”非常像枚举,但是像枚举一样使用它们可能会导致错误。因此,如果可能的话,我希望 AspectJ 将 == 与这些类型之一的对象的使用标记为错误。我用谷歌搜索了这个并查阅了通常非常有用的书AspectJ in action,但到目前为止还没有成功。任何帮助表示赞赏。

is it possible to write an AspectJ pointcut that matches the usage of a specific operator with a specific type?

Some background information: I'm working on a project where we have to use a legacy Java library (pre 5.0, before the enum keyword) which comes with several "pseudo-enum" types, i.e. normal Java classes that define static constants of its own type. Those "feel" very much like enums, but using them like enums can result in an error. Therefore I'd like AspectJ to mark uses of == with objects of one of these types as an error, if that is possible. I have googled this and consulted the normally very helpful book AspectJ in action, but so far without success. Any help is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情泪▽动烟 2024-12-14 07:16:43

== 与 equals() 不同,它是由 JVM 实现的,并且据我所知不能被aspectj 修改。另外,我不认为 AspectJ 是完成这项工作的正确工具。

更好的方法是为您的项目实施 Checkstyle 规则或类似规则,以允许您指出这些点。您可以将它们视为错误或警告。有关详细信息,请参阅为 Checkstyle 编写检查

这需要一些编码,但aspectJ 解决方案也需要一些编码。

Checkstyle 有一个 eclipse 插件和一个 maven 插件,因此当您在 IDE 中工作时可能会出现这些错误。

== is not the same as equals(), and is implemented by the JVM, and as far as I can tell cannot be modified by aspectj. Also, I don't think that AspectJ is the correct tool for this job.

A better approach would be to implement a Checkstyle rule or similar for your project to allow you indicate these points. You could have them as errors or warnings. See Writing Checks for Checkstyle for more information.

This would require some coding, but so would the aspectJ solution.

Checkstyle has an eclipse plugin as well as a maven plugin, so you could have these errors appearing as you work in the IDE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文