Domanda

How can I create an ImportControl module, which disallows the import of some defined classes within several src-folders? - The classes are defined in an own project while they are not allowed to be used within productive code, merly in other unit-tests.

Is that even possible? First I defined a module:

<module name="ImportControl">
  <property name="severity" value="error"/>
  <property name="file" value="${config_loc}/import_control.xml"/>
</module>

Now I need a control importer like this:

<!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=toset>
    ...
</import-control>

My first approach was to set the disallowed tag with the help of a regex, so that selected files can't be imported. The problem is that I can't define the pkg as "src".

Is there a way to handle this problem with just the import-control.xml?

È stato utile?

Soluzione

I'm afraid that's not possible using ImportControl.

The reason is that anything you can specify in an import-control.xml refers to packages, but not to source folders or projects. Checkstyle does not know source folders or projects because it is IDE agnostic, and those are Eclipse constructs. import-control.xml also cannot refer to absolute file names, because Checkstyle does not know the absolute file names of the referenced classes.

One way out of this problem is to choose the forbidden packages so that they all contain a common component (like com.foo.FORBIDDEN.pkg). You could then forbid the use of the thusly tagged packages.

Also, you could write an Eclipse-aware Checkstyle check of your own. I did that once, and succeeded, but I can tell you it will be several days of work to do it right, even if you know what you're doing.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top