Class FileContents
- java.lang.Object
-
- com.puppycrawl.tools.checkstyle.api.FileContents
-
- All Implemented Interfaces:
CommentListener
public final class FileContents extends Object implements CommentListener
Represents the contents of a file.
-
-
Constructor Summary
Constructors Constructor Description FileContents(FileText text)
Creates a newFileContents
instance.FileContents(String filename, String... lines)
Deprecated.UseFileContents(FileText)
instead in order to preserve the original line breaks where possible.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description Map<Integer,List<TextBlock>>
getBlockComments()
Returns a map of all block comments.Map<Integer,List<TextBlock>>
getCComments()
Deprecated.UsegetBlockComments()
instead.Map<Integer,TextBlock>
getCppComments()
Deprecated.UsegetSingleLineComments()
instead.String
getFileName()
Gets the name of the file.TextBlock
getJavadocBefore(int lineNoBefore)
Returns the Javadoc comment before the specified line.String
getLine(int index)
Get the line from text of the file.String[]
getLines()
Gets the lines in the file.Map<Integer,TextBlock>
getSingleLineComments()
Returns a map of all the single line comments.FileText
getText()
Get the full text of the file.boolean
hasIntersectionWithComment(int startLineNo, int startColNo, int endLineNo, int endColNo)
Checks if the specified position intersects with a comment.boolean
inPackageInfo()
Checks if the current file is a package-info.java file.boolean
lineIsBlank(int lineNo)
Checks if the specified line is blank.boolean
lineIsComment(int lineNo)
Checks if the specified line is a single-line comment without code.void
reportBlockComment(String type, int startLineNo, int startColNo, int endLineNo, int endColNo)
Report the location of a block comment that can span multiple lines.void
reportCComment(int startLineNo, int startColNo, int endLineNo, int endColNo)
Deprecated.UsereportBlockComment(int, int, int, int)
instead.void
reportCppComment(int startLineNo, int startColNo)
Deprecated.UsereportSingleLineComment(int, int)
instead.void
reportSingleLineComment(int startLineNo, int startColNo)
Report the location of a single line comment.void
reportSingleLineComment(String type, int startLineNo, int startColNo)
Report the location of a single line comment that extends from the given point to the end of the line.
-
-
-
Constructor Detail
-
FileContents
@Deprecated public FileContents(String filename, String... lines)
Deprecated.UseFileContents(FileText)
instead in order to preserve the original line breaks where possible.Creates a newFileContents
instance.- Parameters:
filename
- name of the filelines
- the contents of the file
-
FileContents
public FileContents(FileText text)
Creates a newFileContents
instance.- Parameters:
text
- the contents of the file
-
-
Method Detail
-
reportSingleLineComment
public void reportSingleLineComment(String type, int startLineNo, int startColNo)
Description copied from interface:CommentListener
Report the location of a single line comment that extends from the given point to the end of the line. The type of comment is identified by a String whose value depends on the language being parsed, but would typically be the delimiter for the comment.- Specified by:
reportSingleLineComment
in interfaceCommentListener
- Parameters:
type
- an identifier for what type of comment it is.startLineNo
- the starting line numberstartColNo
- the starting column number
-
reportSingleLineComment
public void reportSingleLineComment(int startLineNo, int startColNo)
Report the location of a single line comment.- Parameters:
startLineNo
- the starting line numberstartColNo
- the starting column number
-
reportBlockComment
public void reportBlockComment(String type, int startLineNo, int startColNo, int endLineNo, int endColNo)
Description copied from interface:CommentListener
Report the location of a block comment that can span multiple lines. The type of comment is identified by a String whose value depends on the language being parsed, but would typically be the delimiter for the comment.- Specified by:
reportBlockComment
in interfaceCommentListener
- Parameters:
type
- an identifier for what type of comment it is.startLineNo
- the starting line numberstartColNo
- the starting column numberendLineNo
- the ending line numberendColNo
- the ending column number
-
reportCppComment
@Deprecated public void reportCppComment(int startLineNo, int startColNo)
Deprecated.UsereportSingleLineComment(int, int)
instead.Report the location of a C++ style comment.- Parameters:
startLineNo
- the starting line numberstartColNo
- the starting column number
-
getCppComments
@Deprecated public Map<Integer,TextBlock> getCppComments()
Deprecated.UsegetSingleLineComments()
instead.Returns a map of all the C++ style comments. The key is a line number, the value is the commentTextBlock
at the line.- Returns:
- the Map of comments
-
getSingleLineComments
public Map<Integer,TextBlock> getSingleLineComments()
Returns a map of all the single line comments. The key is a line number, the value is the commentTextBlock
at the line.- Returns:
- the Map of comments
-
reportCComment
@Deprecated public void reportCComment(int startLineNo, int startColNo, int endLineNo, int endColNo)
Deprecated.UsereportBlockComment(int, int, int, int)
instead.Report the location of a C-style comment.- Parameters:
startLineNo
- the starting line numberstartColNo
- the starting column numberendLineNo
- the ending line numberendColNo
- the ending column number
-
getCComments
@Deprecated public Map<Integer,List<TextBlock>> getCComments()
Deprecated.UsegetBlockComments()
instead.Returns a map of all C style comments. The key is the line number, the value is aList
of C style commentTextBlock
s that start at that line.- Returns:
- the map of comments
-
getBlockComments
public Map<Integer,List<TextBlock>> getBlockComments()
Returns a map of all block comments. The key is the line number, the value is aList
of block commentTextBlock
s that start at that line.- Returns:
- the map of comments
-
getJavadocBefore
public TextBlock getJavadocBefore(int lineNoBefore)
Returns the Javadoc comment before the specified line. A return value ofnull
means there is no such comment.- Parameters:
lineNoBefore
- the line number to check before- Returns:
- the Javadoc comment, or
null
if none
-
getText
public FileText getText()
Get the full text of the file.- Returns:
- an object containing the full text of the file
-
getLines
public String[] getLines()
Gets the lines in the file.- Returns:
- the lines in the file
-
getLine
public String getLine(int index)
Get the line from text of the file.- Parameters:
index
- index of the line- Returns:
- line from text of the file
-
getFileName
public String getFileName()
Gets the name of the file.- Returns:
- the name of the file
-
lineIsBlank
public boolean lineIsBlank(int lineNo)
Checks if the specified line is blank.- Parameters:
lineNo
- the line number to check- Returns:
- if the specified line consists only of tabs and spaces.
-
lineIsComment
public boolean lineIsComment(int lineNo)
Checks if the specified line is a single-line comment without code.- Parameters:
lineNo
- the line number to check- Returns:
- if the specified line consists of only a single line comment without code.
-
hasIntersectionWithComment
public boolean hasIntersectionWithComment(int startLineNo, int startColNo, int endLineNo, int endColNo)
Checks if the specified position intersects with a comment.- Parameters:
startLineNo
- the starting line numberstartColNo
- the starting column numberendLineNo
- the ending line numberendColNo
- the ending column number- Returns:
- true if the positions intersects with a comment.
-
inPackageInfo
public boolean inPackageInfo()
Checks if the current file is a package-info.java file.- Returns:
- true if the package file.
-
-