Checkstyle规则限制根包之间的交互(使用ImportControl?) [英] Checkstyle rule to limit interactions between root packages (with ImportControl?)

查看:770
本文介绍了Checkstyle规则限制根包之间的交互(使用ImportControl?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建Checkstyle规则以限制不同根包之间的交互?

How can I create a Checkstyle rule to limit interactions between different root packages?

我有以下3个根包:


  • models

  • views

  • 控制器

  • models
  • views
  • controllers

(他们不是类似 com.mycompany.myproject.models 。他们是根包。)

(They are not something like com.mycompany.myproject.models. They are root packages.)

我想禁止从模型访问视图并从 views models (以及其他一些)。

I wanted to disallow access from models to views and from views to models (and some others).

I尝试使用Checkstyle的 ImportControl-Checker

I try to use the ImportControl-Checker from Checkstyle:


  • 尝试1:使用一个 import-control.xml 。问题:我只能提供一个Root-XML-Element(< import-control pkg =models> ),这只包含一个包(但我想要有多个)。

  • 尝试2:使用几个 import-control.xml 。问题:如果我在 checkstyle-config.xml 中导入多个,则两者似乎都不起作用(没有错误,它看起来我没有定义)。我在 import-control.xml中的定义

  • Try 1: Use one single import-control.xml. Problem: I can provide only one Root-XML-Element (<import-control pkg="models">) and this contains only one Package (but I want to have more than one).
  • Try 2: Use several import-control.xml. Problem: If I import more than one in checkstyle-config.xml, neither seems to work (there is no error, it just looks like I didn't define neither). My definition in import-control.xml:

<module name="ImportControl">
  <property name="id" value="ImportControlViews"/>
  <property name="file" value="${basedir}/project/import-control/views.xml"/>
</module>
<module name="ImportControl">
  <property name="id" value="ImportControlModels"/>
  <property name="file" value="${basedir}/project/import-control/models.xml"/>
</module>


推荐答案

不幸的是,你想要的东西很难用ImportControl开箱即用。
这就是原因:

Unfortunately, what you want is very hard to do using the ImportControl check out of the box.
Here's why:

你已经找到了为什么你的选择1不能工作:只能有一个根包。

You already found out why your option 1 cannot work: There can only be one root package.

选项2是可能的,但很费力。让我深入一点。我使用了以下两个导入控制文件,它们禁止从视图视图中使用模型 / code>来自 models

Option 2 is possible, but laborious. Let me go into some depth. I used the following two import control files, which disallow using models from views and views from models:

<!DOCTYPE import-control PUBLIC "-//Puppy Crawl//DTD Import Control 1.1//EN"
    "http://www.puppycrawl.com/dtds/import_control_1_1.dtd">
<import-control pkg="views">
    <allow pkg="views" />
    <disallow pkg="models" />
</import-control>



<!DOCTYPE import-control PUBLIC "-//Puppy Crawl//DTD Import Control 1.1//EN"
    "http://www.puppycrawl.com/dtds/import_control_1_1.dtd">
<import-control pkg="models">
    <allow pkg="models" />
    <disallow pkg="views" />
</import-control>

在我的测试设置中,这基本上有效,但有一个缺点:每个班级都会收到一个Checkstyle警告导入控制文件不处理此包。这是因为ImportControl检查要求所有包都驻留在公共根目录下(通过查看Checkstyle 5.6源代码进行验证)。因此,在 models 包中,您将从为 views 包配置的检查实例收到警告,反之亦然。

还有一个问题,即ImportControl检查仅适用于import语句,但没有找到代码中直接使用的完全限定引用。

In my test setup, this basically worked, but there is a drawback: Every class gets a Checkstyle warning that the Import control file does not handle this package. This is because the ImportControl check expects all packages to reside under a common root (verified by looking at the Checkstyle 5.6 sources). So in the models package, you get the warning from the check instance configured for the views package, and vice versa.
There is also the added problem that the ImportControl check only works on the import statements, but does not find fully qualified references used directly in the code.

那么,你能做什么?


  • 更改你的应用程序,以便拥有一个共同的根目录。这是最佳实践,通常也是一个好主意。

  • 将自定义检查实现为 ImportControlCheck ,它添加了启用/禁用导入的选项控制文件不处理此包消息,否则请使用您的选项2。

  • 如果您使用的是Eclipse,还有第三种解决方案。您可以使用Checkstyle Eclipse插件提供的高级配置对话框,以将ImportControl实例限制为各自的文件。这也将消除导入控制文件不处理此包消息。

  • Change your app so that you have a common root. This is best practice and generally a good idea.
  • Implement a custom check as a subclass of ImportControlCheck which adds an option for enabling/disabling the "Import control file does not handle this package" message, and otherwise go with your option 2.
  • If you are using Eclipse, there is also a third solution. You could use the advanced configuration dialog that the Checkstyle Eclipse plugin provides in order to restrict the ImportControl instances to their respective files. This would also eliminate the "Import control file does not handle this package" messages.

这篇关于Checkstyle规则限制根包之间的交互(使用ImportControl?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆