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.
-
-
Field Summary
Fields Modifier and Type Field Description private Map<Integer,List<TextBlock>>
clangComments
Map of the C comments indexed on the first line of the comment to a list of comments on that line.private Map<Integer,TextBlock>
cppComments
Map of the C++ comments indexed on the first line of the comment.private Map<Integer,TextBlock>
javadocComments
Map of the Javadoc comments indexed on the last line of the comment.private static Pattern
MATCH_SINGLELINE_COMMENT
Compiled regexp to match a single-line comment line.private static String
MATCH_SINGLELINE_COMMENT_PAT
The pattern to match a single-line comment containing only the comment itself -- no code.private FileText
text
The text.
-
Constructor Summary
Constructors Constructor Description FileContents(FileText text)
Creates a newFileContents
instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description private String[]
extractBlockComment(int startLineNo, int startColNo, int endLineNo, int endColNo)
Returns the specified block comment as a String array.Map<Integer,List<TextBlock>>
getBlockComments()
Returns a map of all block comments.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.private boolean
hasIntersectionWithBlockComment(int startLineNo, int startColNo, int endLineNo, int endColNo)
Checks if the specified position intersects with a block comment.boolean
hasIntersectionWithComment(int startLineNo, int startColNo, int endLineNo, int endColNo)
Checks if the specified position intersects with a comment.private boolean
hasIntersectionWithSingleLineComment(int startLineNo, int startColNo, int endLineNo, int endColNo)
Checks if the specified position intersects with a single-line comment.boolean
inPackageInfo()
Deprecated.useCheckUtil.isPackageInfo(String)
for the same functionality, or useAbstractCheck.getFilePath()
to process your own standards.private String
line(int lineNo)
Get a single-line.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(int startLineNo, int startColNo, int endLineNo, int endColNo)
Report the location of a block comment.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
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.
-
-
-
Field Detail
-
MATCH_SINGLELINE_COMMENT_PAT
private static final String MATCH_SINGLELINE_COMMENT_PAT
The pattern to match a single-line comment containing only the comment itself -- no code.- See Also:
- Constant Field Values
-
MATCH_SINGLELINE_COMMENT
private static final Pattern MATCH_SINGLELINE_COMMENT
Compiled regexp to match a single-line comment line.
-
javadocComments
private final Map<Integer,TextBlock> javadocComments
Map of the Javadoc comments indexed on the last line of the comment. The hack is it assumes that there is only one Javadoc comment per line.
-
cppComments
private final Map<Integer,TextBlock> cppComments
Map of the C++ comments indexed on the first line of the comment.
-
clangComments
private final Map<Integer,List<TextBlock>> clangComments
Map of the C comments indexed on the first line of the comment to a list of comments on that line.
-
-
Constructor Detail
-
FileContents
public FileContents(FileText text)
Creates a newFileContents
instance.- Parameters:
text
- the contents of the file
-
-
Method Detail
-
getText
public FileText getText()
Get the full text of the file.- Returns:
- an object containing the full text of 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
-
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
-
reportBlockComment
public void reportBlockComment(int startLineNo, int startColNo, int endLineNo, int endColNo)
Report the location of a block comment.- Parameters:
startLineNo
- the starting line numberstartColNo
- the starting column numberendLineNo
- the ending line numberendColNo
- the ending column number
-
extractBlockComment
private String[] extractBlockComment(int startLineNo, int startColNo, int endLineNo, int endColNo)
Returns the specified block comment as a String array.- Parameters:
startLineNo
- the starting line numberstartColNo
- the starting column numberendLineNo
- the ending line numberendColNo
- the ending column number- Returns:
- block comment as an array
-
line
private String line(int lineNo)
Get a single-line. For internal use only, as getText().get(lineNo) is just as suitable for external use and avoids method duplication.- Parameters:
lineNo
- the number of the line to get- Returns:
- the corresponding line, without terminator
- Throws:
IndexOutOfBoundsException
- if lineNo is invalid
-
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
-
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.
-
hasIntersectionWithBlockComment
private boolean hasIntersectionWithBlockComment(int startLineNo, int startColNo, int endLineNo, int endColNo)
Checks if the specified position intersects with a block 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 block comment.
-
hasIntersectionWithSingleLineComment
private boolean hasIntersectionWithSingleLineComment(int startLineNo, int startColNo, int endLineNo, int endColNo)
Checks if the specified position intersects with a single-line 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 single-line comment.
-
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
-
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
-
inPackageInfo
@Deprecated(since="10.2") public boolean inPackageInfo()
Deprecated.useCheckUtil.isPackageInfo(String)
for the same functionality, or useAbstractCheck.getFilePath()
to process your own standards.Checks if the current file is a package-info.java file.- Returns:
- true if the package file.
-
-