Java -> Code Style -> Code Templates -> Comments -> Methods -> Edit
I have removed whitespace in above template and save the setting but space wasn't removed when I press Ctrl+Shift+F for Eclipse autoformat.
Checkstyle is complaining me about this space and I can't remove manually in every java file. Do I need to use regex to replace instead?
To remove leading and trailing spaces in Java, use the trim() method. This method returns a copy of this string with leading and trailing white space removed, or this string if it has no leading or trailing white space.
Go to Preferences and then search for Compare. There should be a setting to ignore whitespace. It will be in Preferences->General->Compare/Patch. Check the "Ignore White space" option.
Whitespace is not always insignificant. In some cases, trailing whitespace can significantly change the meaning of a line of code or data. In most cases whitespace is there to format the code for human readers.
One possibility is to configure the Java editor save actions to remove trailing spaces.
This is configured in 'Preferences > Java > Editor > Save Actions'. Check 'Addition actions' and click 'Configure...'. On the 'Code Organizing' tab you can configure the 'Remove trailing whitespace' option.
When I try it in Eclipse Juno SR2, greg-449's suggestion to use save actions works for me. Be sure to select the "all lines" radio button, though. An empty line of Javadoc is considered an empty line despite the single asterisk, so "ignore empty lines" is not a good setting for you.
There is also the "Clean up" feature in Eclipse, which allows you to batch run this on all your files in order to save time. What "Clean up" does can be configured like so (note "all lines" is selected again):
Last but not least, if you just want to stop Checkstyle from complaining, you can reconfigure the check so that it does not flag a single trailing space in an otherwise empty line of Javadoc. Configure the RegexpSingleline check like this (explanation of the regex):
<module name="RegexpSingleline">
<property name="format" value="^(?!\s+\* $).*?\s+$"/>
<property name="message" value="Line has trailing spaces."/>
</module>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With