34

Is there a way to have an IDE fix Checkstyle errors automatically without having to fix each manually?

Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
ivotron
  • 1,471
  • 1
  • 17
  • 21

6 Answers6

20

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).

Freedom_Ben
  • 11,247
  • 10
  • 69
  • 89
  • Docs on shortcut keys: https://www.jetbrains.com/help/idea/using-code-editor.html#reformat_rearrange_code – Daniel Jun 07 '18 at 16:06
  • 1
    Additionally, make sure that if you have a .editorconfig file that you delete it or comment out any conflicting styles. The .editorconfig will take precedence and prevent the reformat command from using the checkstyle settings. – Adam Wise Mar 29 '19 at 18:08
9

If you're using Eclipse, yes. You can't correct all problems though. There are two ways:

  1. Right click on the java file in Package Explorer or whatever, and select 'Apply Checkstyle Corrections'.
  2. Click on the error in the problems view, and select 'Quick fix'. This corrects the problem.
Matthew Farwell
  • 60,889
  • 18
  • 128
  • 171
3

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.

Brady
  • 31
  • 3
3

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.

Mike
  • 7,994
  • 5
  • 35
  • 44
  • I figured that my question is rather stupid, I should have checked for the tons of plugins at the Checkstyle site. You're right, my question doesn't make sense. I edited it. Thanks a lot for your input though! – ivotron Dec 13 '11 at 07:26
1

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/

Stefan
  • 10,010
  • 7
  • 61
  • 117
0

I used jalopy. Create Tool to fix the Check style issues.

Jabeer
  • 879
  • 1
  • 8
  • 13