Class NewlineAtEndOfFileCheck
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
-
- com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
-
- com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
-
- com.puppycrawl.tools.checkstyle.checks.NewlineAtEndOfFileCheck
-
- All Implemented Interfaces:
Configurable
,Contextualizable
,FileSetCheck
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 (the line with 'No newline at end of file' should not be in the 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.-
Property
fileExtensions
- Specify the file extensions of the files to process. Type isjava.lang.String[]
. Default value is""
. -
Property
lineSeparator
- Specify the type of line separator. Type iscom.puppycrawl.tools.checkstyle.checks.LineSeparatorOption
. Default value islf_cr_crlf
.
Parent is
com.puppycrawl.tools.checkstyle.Checker
Violation Message Keys:
-
noNewlineAtEOF
-
unable.open
-
wrong.line.end
- Since:
- 3.1
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
AbstractAutomaticBean.OutputStreamOptions
-
-
Field Summary
Fields Modifier and Type Field Description private LineSeparatorOption
lineSeparator
Specify the type of line separator.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 Summary
Constructors Constructor Description NewlineAtEndOfFileCheck()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static boolean
endsWithNewline(RandomAccessFile file, LineSeparatorOption separator)
Checks whether the content provided by the Reader ends with the platform specific line separator.protected void
processFiltered(File file, FileText fileText)
Called to process a file that matches the specified file extensions.private void
readAndCheckFile(File file)
Reads the file provided and checks line separators.void
setLineSeparator(String lineSeparatorParam)
Setter to specify the type of line separator.-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck
addViolations, beginProcessing, destroy, finishProcessing, fireErrors, getFileContents, getFileExtensions, getMessageDispatcher, getTabWidth, getViolations, init, log, log, process, setFileContents, setFileExtensions, setMessageDispatcher, setTabWidth
-
Methods inherited from class com.puppycrawl.tools.checkstyle.api.AbstractViolationReporter
finishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverity
-
Methods inherited from class com.puppycrawl.tools.checkstyle.AbstractAutomaticBean
configure, contextualize, getConfiguration, setupChild
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.puppycrawl.tools.checkstyle.api.Configurable
configure
-
Methods inherited from interface com.puppycrawl.tools.checkstyle.api.Contextualizable
contextualize
-
-
-
-
Field Detail
-
MSG_KEY_UNABLE_OPEN
public static final String MSG_KEY_UNABLE_OPEN
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_NO_NEWLINE_EOF
public static final String MSG_KEY_NO_NEWLINE_EOF
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
MSG_KEY_WRONG_ENDING
public static final String MSG_KEY_WRONG_ENDING
A key is pointing to the warning message text in "messages.properties" file.- See Also:
- Constant Field Values
-
lineSeparator
private LineSeparatorOption lineSeparator
Specify the type of line separator.
-
-
Constructor Detail
-
NewlineAtEndOfFileCheck
public NewlineAtEndOfFileCheck()
-
-
Method Detail
-
processFiltered
protected void processFiltered(File file, FileText fileText)
Description copied from class:AbstractFileSetCheck
Called to process a file that matches the specified file extensions.- Specified by:
processFiltered
in classAbstractFileSetCheck
- Parameters:
file
- the file to be processedfileText
- the contents of the file.
-
setLineSeparator
public void setLineSeparator(String lineSeparatorParam)
Setter to specify the type of line separator.- Parameters:
lineSeparatorParam
- The line separator to set- Throws:
IllegalArgumentException
- If the specified line separator is not one of 'crlf', 'lf', 'cr', 'lf_cr_crlf' or 'system'- Since:
- 3.1
-
readAndCheckFile
private void readAndCheckFile(File file) throws IOException
Reads the file provided and checks line separators.- Parameters:
file
- the file to be processed- Throws:
IOException
- When an IO error occurred while reading from the file provided
-
endsWithNewline
private static boolean endsWithNewline(RandomAccessFile file, LineSeparatorOption separator) throws IOException
Checks whether the content provided by the Reader ends with the platform specific line separator.- Parameters:
file
- The reader for the content to checkseparator
- The line separator- Returns:
- boolean Whether the content ends with a line separator
- Throws:
IOException
- When an IO error occurred while reading from the provided reader
-
-