checkstyle / checkstyle/checkstyle

Dynamic variables support for ImportControl check (property expansion)

Open
#6,791 4 comments 0 reactions 0 assignees View on GitHub
approved
Dominant language
Java
Stars
9.6k
Forks
4.2k
Avg merge
22h 23m
Merged PRs (30d)
232

Description

```
/var/tmp $ javac Test.java
# no output produced, compiles properly

/var/tmp $ cat config.xml





/var/tmp $ cat import-control.xml

/var/tmp $ cat Test.java
package foo.bar;

import java.util.ArrayList;
import java.util.Collection;

class Test {
Collection collection = new ArrayList();
}

/var/tmp $ java -Duser.language=en -Duser.country=US -Dpackage_name=bar \
-jar checkstyle-8.21-all.jar -c config.xml TestImportControl.java
Starting audit...
[ERROR] /Test.java:1:1: Import control file does not handle this package. [ImportControl]
Audit done.
Checkstyle ends with 1 errors.

```

---------------

Expected result:
```
Starting audit...
[ERROR] /Test.java:3:1: Disallowed import - java.util.ArrayList. [ImportControl]
[ERROR] /Test.java:4:1: Disallowed import - java.util.Collection. [ImportControl]
Audit done.
Checkstyle ends with 2 errors.
```

Property expansion is not implemented for import control check.

--------------

Problem: cannot find a way to create generic import-control config to allow import for some subpackages only from the same modules - really useful for multimodule project with the same module structures and rules.

From the current available options to resolve this issue the most suitable one is probably property expansion.
Currently it is not possible to use property expansion functionality for ImportControl configuration, as it is always must be a separate config file linked by root config, for instance:
```





```
While it is possible to declare variable properties using `${prop_name}` syntax in the snippet above, one cannot do the same with `import-control.xml` file.

With this support it must be allowed to write something like this.
```



```

Any ideas of how to resolve the problem with different approaches are really appreciated.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.