Reputation: 1511
Is there a way to have an IDE fix Checkstyle errors automatically without having to fix each manually?
Upvotes: 38
Views: 96325
Reputation: 3140
For those who're looking for the way to fix checkstyle violations using the checkstyle plugin itself (build plugin I mean, either maven or gradle) - currently, no way. There's an old and very heavily asked issue about it, but there is no out-of-the-box maven goal/gradle task to actually fix the violations. So, yeah, it sucks.
Upvotes: 0
Reputation: 31
There is a way to do this every time you save your file but not all warnings in Checkstyle can be fixed automatically.
You can have Checkstyle generate a formatter for you by right clicking on your package in the Package/Project explorer and selecting Checkstyle->Create Formatter-Profile. This will create a new formatter called eclipse-cs [your-package-name] based off of your Checkstyle configuration. You'll find it in the "Active profile" drop-down in the formatter preferences of Eclipse.
You can then set the formatter to run each save by going to Preferences->[Language of Choice]->Editor->Save Actions. Check the "Perform the selected actions on save" and "Format source code" boxes.
Upvotes: 3
Reputation: 11923
If you are using IntelliJ, you can install the CheckStyle IDEA plugin.
Then import the CheckStyle settings into the editor, and Reformat like normal (Ctrl+Alt+L is default shortcut key).
Upvotes: 23
Reputation: 12232
I created a feature request for the Eclipse checkstyle plugin to also support quick fixes from within the code editor: https://sourceforge.net/p/eclipse-cs/feature-requests/150/
Upvotes: 1
Reputation: 61695
If you're using Eclipse, yes. You can't correct all problems though. There are two ways:
Upvotes: 9
Reputation: 8100
Your question is rather vague, but I think what you're asking is if CheckStyle can automatically reformat code that it's checking to fix code layout problems that it finds.
The direct answer here is "no."
However, there are a number of "Java source code formatters" (google that, choose one) that will do what I believe you're asking for.
Upvotes: 3