Farrukh Chishti
Farrukh Chishti

Reputation: 8437

Checkstyle length check for import statements

How to remove checkstyle violation if it throws a "Line is longer than 80 characters" for an import statement?

Upvotes: 3

Views: 2964

Answers (4)

M. Justin
M. Justin

Reputation: 21239

By default, the Checkstyle LineLength configuration ignores package and import statements. This is accomplished by the default value for the ignorePattern property, which is "^(package|import) .*".

Since your import statement is failing the LineLength check, either it's formatted differently than expected by the default configuration (e.g. it has a leading space), or you've customized the ignorePattern in your project, (inadvertantly) removing the import check configuration in the process.

Import statements can be added back to a customized ignorePattern regex. For instance, if it's currently set to "somePattern", you could configure your ignorePattern as follows:

<module name="LineLength">
  <property name="ignorePattern" value="^import .*|somePattern"/>
</module>

Upvotes: 0

Leonid Dashko
Leonid Dashko

Reputation: 4156

To exclude import and package statements from the check, you can apply following configuration:

<module name="LineLength">
    <property name="max" value="80" />
    <property name="ignorePattern" value="^(package|import) .*"/>
</module>

Source: https://checkstyle.org/config_sizes.html#LineLength_Examples

Or you can apply tags as suggested by Marko (but first option is more proffered):

// CHECKSTYLE:OFF
import ...
// CHECKSTYLE:ON

Upvotes: 1

Domadin
Domadin

Reputation: 577

I want to add some aditional info to Paulius Matulionis's answer. If you are using default Checkstyle rules, you can find checkstyle configuration file by going to your Idea directory:

%HOMEPATH%.IdeaIC2017.1\config\plugins\CheckStyle-IDEA\classes\sun_checks.xml

Folder name ".IdeaIC2017.1" could be different depends on version of Idea you are using.

Also, you can find your configuration file name in Idea's "Settings"

Upvotes: 0

Paulius Matulionis
Paulius Matulionis

Reputation: 23413

Remove:

<module name="LineLength">
    <property name="max" value="80"/>
</module>

from your checkstyle configuration.

EDIT:

Disable it only for imports:

<module name="LineLength">
   <property name="ignorePattern" value="someRegex"/>
</module>

You have to provide the regular expression which finds the line starting with import into the value attribute. I am not so good at regex so you will need work out the regular expression yourself.

The regex like: ...value="^import"... should work. But not tested.

Upvotes: 5

Related Questions