jcip 注释的 checkstyle 规则
我想要一个规则来检查字段和类是否使用实践中提供的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
找到解决方案:
此外,检查
@GuardedBy
没有任何意义,因为它取决于类使用的同步策略。因此,并不总是需要将 @GuardedBy 添加到字段声明中。对于简单的检查样式规则来说,检查需要的情况实在太复杂了。 :)编辑:为了保持一致性,我已将规则更新为更强大的版本。
Found the solution:
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.