public class NewlineAtEndOfFileCheck extends AbstractFileSetCheck
Checks whether files end with a line separator.
Rationale: Any source files and text files in general should end with a line separator to let other easily add new content at the end of file and "diff" command does not show previous lines as changed.
Example (line 36 should not be in diff):
@@ -32,4 +32,5 @@ ForbidWildcardAsReturnTypeCheck.returnTypeClassNamesIgnoreRegex PublicReferenceToPrivateTypeCheck.name = Public Reference To Private Type StaticMethodCandidateCheck.name = Static Method Candidate -StaticMethodCandidateCheck.desc = Checks whether private methods should be declared as static. \ No newline at end of file +StaticMethodCandidateCheck.desc = Checks whether private methods should be declared as static. +StaticMethodCandidateCheck.skippedMethods = Method names to skip during the check.
It can also trick the VCS to report the wrong owner for such lines. An engineer who has added nothing but a newline character becomes the last known author for the entire line. As a result, a mate can ask him a question to which he will not give the correct answer.
Old Rationale: CVS source control management systems will even print a warning when it encounters a file that doesn't end with a line separator.
Attention: property fileExtensions works with files that are passed by similar property for at Checker. Please make sure required file extensions are mentioned at Checker's fileExtensions property.
This will check against the platform-specific default line separator.
It is also possible to enforce the use of a specific line-separator across
platforms, with the lineSeparator
property.
lineSeparator
- Specify the type of line separator.
Type is com.puppycrawl.tools.checkstyle.checks.LineSeparatorOption
.
Default value is lf_cr_crlf
.
fileExtensions
- Specify the file type extension of the files to check.
Type is java.lang.String[]
.
Default value is all files
.
To configure the check:
<module name="NewlineAtEndOfFile"/>
Example:
// File ending with a new line public class Test {⤶ ⤶ }⤶ // ok Note: The comment // ok is a virtual, not actually present in the file // File ending without a new line public class Test1 {⤶ ⤶ } // violation, the file does not end with a new line
To configure the check to always use Unix-style line separators:
<module name="NewlineAtEndOfFile"> <property name="lineSeparator" value="lf"/> </module>
Example:
// File ending with a new line public class Test {⤶ ⤶ }⤶ // ok Note: The comment // ok is a virtual, not actually present in the file // File ending without a new line public class Test1 {⤶ ⤶ }␍⤶ // violation, expected line ending for file is LF(\n), but CRLF(\r\n) is detected
To configure the check to work only on Java, XML and Python files:
<module name="NewlineAtEndOfFile"> <property name="fileExtensions" value="java, xml, py"/> </module>
Example:
// Any java file public class Test {⤶ } // violation, file should end with a new line. // Any py file print("Hello World") // violation, file should end with a new line. // Any txt file This is a sample text file. // ok, this file is not specified in the config.
Parent is com.puppycrawl.tools.checkstyle.Checker
Violation Message Keys:
noNewlineAtEOF
unable.open
wrong.line.end
AutomaticBean.OutputStreamOptions
Modifier and Type | Field and Description |
---|---|
static String |
MSG_KEY_NO_NEWLINE_EOF
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_KEY_UNABLE_OPEN
A key is pointing to the warning message text in "messages.properties"
file.
|
static String |
MSG_KEY_WRONG_ENDING
A key is pointing to the warning message text in "messages.properties"
file.
|
Constructor and Description |
---|
NewlineAtEndOfFileCheck() |
Modifier and Type | Method and Description |
---|---|
protected void |
processFiltered(File file,
FileText fileText)
Called to process a file that matches the specified file extensions.
|
void |
setLineSeparator(String lineSeparatorParam)
Setter to specify the type of line separator.
|
addMessages, beginProcessing, destroy, finishProcessing, fireErrors, getFileContents, getFileExtensions, getMessageDispatcher, getMessages, getTabWidth, init, log, log, process, setFileContents, setFileExtensions, setMessageDispatcher, setTabWidth
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
configure, contextualize, getConfiguration, setupChild
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
configure
contextualize
public static final String MSG_KEY_UNABLE_OPEN
public static final String MSG_KEY_NO_NEWLINE_EOF
public static final String MSG_KEY_WRONG_ENDING
public NewlineAtEndOfFileCheck()
protected void processFiltered(File file, FileText fileText)
AbstractFileSetCheck
processFiltered
in class AbstractFileSetCheck
file
- the file to be processedfileText
- the contents of the file.public void setLineSeparator(String lineSeparatorParam)
lineSeparatorParam
- The line separator to setIllegalArgumentException
- If the specified line separator is not
one of 'crlf', 'lf', 'cr', 'lf_cr_crlf' or 'system'Copyright © 2001–2020. All rights reserved.