Page History
...
Most major IDEs include plugins that support Checkstyle configurations. The plugin usually let you import an existing "checkstyle.xml" configuration to configure your IDE to use and/or validate against that style.
IntelliJ IDEA
...
Fixing the codebase
Cleanup via IDEA
...
- Install checkstyle plugin: https://plugins.jetbrains.com/plugin/1065-checkstyle-idea
- File → Settings → Plugins
- Restart IDE
- Configure it to use our custom
checkstyle.xml for DSpace
- File → Settings → Checkstyle
- In older versions of IntelliJ, this menu may be under File → Settings → Other Settings → Checkstyle
- Add a "Configuration File" (press the
+
in the table). Select ourcheckstyle.xml
- Make it "Active"
- Click Apply
- File → Settings → Checkstyle
- Update IDEA's Code Style to obey the Checkstyle Rules (See also https://stackoverflow.com/a/35273850/3750035)
- File → Settings → Editor → Code Style
- Click the small gear icon next to "Scheme", Select "Import Scheme" → CheckStyle Configuration
- Select our our
checkstyle.xml
- Click OK
- Click Apply
- Optionally, you also may wish to change some default IntelliJ settings, to allow the IDE to automatically reformat code for you (when making bulk edits). This is especially important if you plan to do any bulk cleanup of existing code using IntelliJ tools
- First, fix the default order import statements per our CheckStyle rules (these
- don't seem to
- be auto updated by the Checkstyle plugin at this time)
- First, order import statements per our rules
- File → Settings → Editor → Code Style → Java → Imports
- In the "Import Layout" section, ensure the settings are in this order
- "import static all other imports"
- <blank line>
- "import java.*"
- "import javax.*"
- <blank line>
- "import all other imports"
- Once reordered, click OK
- Then, update to auto-wrap lines at the right margin
- File → Settings → Editor → Code Style → Java → Wrapping and Braces
- CHECK "Ensure right margin is not exceeded"
- Click OK
- Then, OPTIONALLY, for better looking bulk reformatting (i.e. results in more human-readable code), you may want to tweak which types of statements are set to "Wrap if long" (NOTE: this is very opinion-based, so choose something that looks good for you, based on the examples given)
- (NOTE: Without these settings, IDEA's bulk "Reformat Code" option sometimes will generate line breaks wherever the line ends, which can result in somewhat odd looking code. These settings cleanup some of the more odd line wrappings that may occur.)
- File → Settings → Editor → Code Style → Java → Wrapping and Braces
- Optionally, set all of the following to "Wrap if long":
- Extends/implements list (also check "align when multiline")
- Extends/implements keyword
- Throws list
- Throws keyword
- Method declaration parameters (also check "align when multiline")
- Method call arguments (also check "align when multiline")
- Chained method calls (also check "align when multiline")
- "for()" statement (also check "align when multiline")
- "try-with-resources" (also check "align when multiline")
- Click OK
- Update IDEA's Javadoc settings
- to not
- insert "<p>" on blank Javadoc lines (as this
- seems to cause IDEA to change all our DSpace license headings improperly during bulk editing)
- File → Settings → Editor → Code Style → Java → JavaDoc
- UNCHECK "Generate <p> on empty lines"
- Make sure "Keep empty lines" is checked
- Click OK
Eclipse
Use Eclipse Checkstyle plugin: http://eclipse-cs.sourceforge.net/
Tested and works to validate code. To format you must generate a formatter style following the steps here. It seems impossible to force eclipse to use braces on sinle-line if statement
(Please help us enhance these instructions to provide a step-by-step configuration for Eclipse)
NetBeans
Use NetBeans Checkstyle plugin: http://plugins.netbeans.org/plugin/3413/checkstyle-beans
(Please help us enhance these instructions to provide a step-by-step configuration for NetBeans)
Fixing existing code / PRs
Cleanup via IDEA
Use IDEA to perform bulk cleanup
- First, you MUST follow all the configurations in the IntelliJ configuration section above. Especially pay attention to the settings marked optional, unless you are doing bulk edits. Since you want to do bulk edits, those settings are now required.
- Now open up any Java source file. You'll see Checkstyle warnings appear (if any) in RED.
- This highlighting is enabled via the checkstyle plugin.
- Bulk Reformat (an entire module): You can also reformat code in bulk in IDEA: https://www.jetbrains.com/help/idea/editor-basics.html#reformat_rearrange_code
- Right click on a single directory / module
- Select "Reformat Code"
- Check "Optimize imports" (This will remove any import statements with an asterisk)
- Leave "Rearrange entries" unchecked (This will rearrange methods/variables based on arrangement settings in IDEA. We don't need this)
- Under "Filters" → Check "File Masks" and enter in "
*.java, *.xml
" (As we'll only bulk reformat Java and XML files) - Click Run
Finally, run Checkstyle against the module. There likely will still be a few failures to fix manually.
Code Block # mvn install (Only necessary if checkstyle:check below complains about missing SNAPSHOT dependencies) mvn -U checkstyle:check
Also double check license headers are still accurate. Sometimes these get "munged" during bulk reformat (as IDEA occasionally tries to merge nearby comments into one larger comment)
Code Block mvn license:check