jcip 注释的 checkstyle 规则

发布于 2024-09-07 18:13:36 字数 1042 浏览 3 评论 0原文

我想要一个规则来检查字段和类是否使用实践中提供的 java 并发注释进行了正确注释: http://mvnrepository.com/artifact/net.jcip/jcip-annotations

字段必须使用 @GuardedBy 进行注释,类必须使用 @Immutable、@ThreadSafe 或 @NotThreadSafe 之一进行注释。

我当前应用了一条规则,该规则确保 Spring Dao 类使用 @Repository 而不是 @Service 或 @Component 进行注释。

<module name="Regexp">
    <property name="format" 
        value="(@Component|@Service)(.*[\n])*.*class.*Dao.*\{" />
    <property name="message" 
        value="Daos sollten lieber mit @Repository annotiert werden." />
    <property name="illegalPattern" value="true" />
</module>

这种方法的问题是,我只能检查某些注释并告诉,最好使用其他注释。这对我进行 jcip 注释检查没有帮助,因为我无法检查“不存在特定注释”。

对于初学者来说,如果有人知道如何将上面的 Dao 检查转换为仅确保 @Repository 出现在名称以 Dao 结尾的类上的检查,那就太酷了。 然后可以使用该模式来开发 jcip 注释检查。

或者也许不是尝试转换正则表达式检查,也许有某种方法可以通过 checkstyle 的令牌支持来实现 jcip 规则?这可能会使规则更加稳健。

无论如何,我想知道如何确保特定注释必须通过 checkstyle 出现在特定元素上。希望有人知道这一点。 :)

I want a rule that checks that fields and classes are properly annotated with the java concurrency in practice annotations provided by: http://mvnrepository.com/artifact/net.jcip/jcip-annotations

Fields have to be annotated with @GuardedBy and classes have to be annotated with one of @Immutable, @ThreadSafe or @NotThreadSafe.

I have a rule currently applied, which ensures that Spring Dao classes are annotated with @Repository instead of @Service or @Component.

<module name="Regexp">
    <property name="format" 
        value="(@Component|@Service)(.*[\n])*.*class.*Dao.*\{" />
    <property name="message" 
        value="Daos sollten lieber mit @Repository annotiert werden." />
    <property name="illegalPattern" value="true" />
</module>

The Problem with this approach is, that I can only check for some annotation and tell, that the other annotation should better be used instead. This does not help me with the jcip annotation check, because I can't check for "no specific annotation present".

For starters, it would be cool if someone knew how to transform the Dao check above into a check that just ensures, that @Repository is present on classes which names end with Dao.
That pattern could then be used to develop the jcip annotation checks.

Or maybe instead of trying to transform the regexp check, maybe there is some way to implement the jcip rules with the token support of checkstyle? This would maybe make the rule robust.

Anyway, I would like to know how to ensure that a specific annotation has to be present on a specific element via checkstyle. Hopefully someone knows this. :)

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

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

发布评论

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

评论(1

扬花落满肩 2024-09-14 18:13:36

找到解决方案:

<module name="Regexp">
    <property name="format" value="(interface [a-zA-Z0-9 <>,\.]* \{|(@Immutable|@ThreadSafe|@NotThreadSafe)(.*[\n])*.*(class|enum) [a-zA-Z0-9\s<>,\.]* \{)" />
    <property name="message" value="Types must be annotated with @Immutable, @ThreadSafe, or @NotThreadSafe." />
    <property name="illegalPattern" value="false" />
</module>

此外,检查 @GuardedBy 没有任何意义,因为它取决于类使用的同步策略。因此,并不总是需要将 @GuardedBy 添加到字段声明中。对于简单的检查样式规则来说,检查需要的情况实在太复杂了。 :)

编辑:为了保持一致性,我已将规则更新为更强大的版本。

Found the solution:

<module name="Regexp">
    <property name="format" value="(interface [a-zA-Z0-9 <>,\.]* \{|(@Immutable|@ThreadSafe|@NotThreadSafe)(.*[\n])*.*(class|enum) [a-zA-Z0-9\s<>,\.]* \{)" />
    <property name="message" value="Types must be annotated with @Immutable, @ThreadSafe, or @NotThreadSafe." />
    <property name="illegalPattern" value="false" />
</module>

Also, checking for @GuardedBy does not make any sense, because it depends on the synchronization strategy the class uses. Thus it is not always required to add @GuardedBy to the field declaration. And checking for the cases where it is needed is entirely too complex for a simple checkstyle rule. :)

EDIT: Just for consistency, I've updated the rule to a more robust version.

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