public class GenericWhitespaceCheck extends AbstractCheck
Checks that the whitespace around the Generic tokens (angle brackets) "<" and ">" are correct to the typical convention. The convention is not configurable.
Left angle bracket ("<"):
Right angle bracket (">"):
To configure the check:
<module name="GenericWhitespace"/>
Examples with correct spacing:
// Generic methods definitions
public void <K, V extends Number> boolean foo(K, V) {}
// Generic type definition
class name<T1, T2, ..., Tn> {}
// Generic type reference
OrderedPair<String, Box<Integer>> p;
// Generic preceded method name
boolean same = Util.<Integer, String>compare(p1, p2);
// Diamond operator
Pair<Integer, String> p1 = new Pair<>(1, "apple");
// Method reference
List<T> list = ImmutableList.Builder<T>::new;
// Method reference
sort(list, Comparable::<String>compareTo);
// Constructor call
MyClass obj = new <String>MyClass();
Examples with incorrect spacing:
List< String> l; // violation, "<" followed by whitespace
Box b = Box. <String>of("foo"); // violation, "<" preceded with whitespace
public<T> void foo() {} // violation, "<" not preceded with whitespace
List a = new ArrayList<> (); // violation, ">" followed by whitespace
Map<Integer, String>m; // violation, ">" not followed by whitespace
Pair<Integer, Integer > p; // violation, ">" preceded with whitespace
Parent is com.puppycrawl.tools.checkstyle.TreeWalker
Violation Message Keys:
ws.followed
ws.illegalFollow
ws.notPreceded
ws.preceded
AutomaticBean.OutputStreamOptions| Modifier and Type | Field and Description |
|---|---|
private static java.lang.String |
CLOSE_ANGLE_BRACKET
Close angle bracket literal.
|
private int |
depth
Used to count the depth of a Generic expression.
|
static java.lang.String |
MSG_WS_FOLLOWED
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_WS_ILLEGAL_FOLLOW
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_WS_NOT_PRECEDED
A key is pointing to the warning message text in "messages.properties"
file.
|
static java.lang.String |
MSG_WS_PRECEDED
A key is pointing to the warning message text in "messages.properties"
file.
|
private static java.lang.String |
OPEN_ANGLE_BRACKET
Open angle bracket literal.
|
| Constructor and Description |
|---|
GenericWhitespaceCheck() |
| Modifier and Type | Method and Description |
|---|---|
void |
beginTree(DetailAST rootAST)
Called before the starting to process a tree.
|
private static boolean |
containsWhitespaceBefore(int before,
java.lang.String line)
Returns whether the specified string contains only whitespace up to specified index.
|
private static boolean |
containsWhitespaceBetween(int fromIndex,
int toIndex,
java.lang.String line)
Returns whether the specified string contains only whitespace between
specified indices.
|
int[] |
getAcceptableTokens()
The configurable token set.
|
int[] |
getDefaultTokens()
Returns the default token a check is interested in.
|
int[] |
getRequiredTokens()
The tokens that this check must be registered for.
|
private static boolean |
isAfterMethodReference(DetailAST genericEnd)
Checks if current generic end ('>') is located after
method reference operator. |
private static boolean |
isCharacterValidAfterGenericEnd(char charAfter)
Checks whether given character is valid to be right after generic ends.
|
private static boolean |
isGenericBeforeCtor(DetailAST ast)
Checks if generic is before constructor invocation.
|
private static boolean |
isGenericBeforeMethod(DetailAST ast)
Is generic before method reference.
|
private void |
processEnd(DetailAST ast)
Checks the token for the end of Generics.
|
private void |
processNestedGenerics(DetailAST ast,
java.lang.String line,
int after)
Process Nested generics.
|
private void |
processSingleGeneric(DetailAST ast,
java.lang.String line,
int after)
Process Single-generic.
|
private void |
processStart(DetailAST ast)
Checks the token for the start of Generics.
|
void |
visitToken(DetailAST ast)
Called to process a token.
|
clearViolations, destroy, finishTree, getFileContents, getLine, getLineCodePoints, getLines, getTabWidth, getTokenNames, getViolations, init, isCommentNodesRequired, leaveToken, log, log, log, setFileContents, setTabWidth, setTokensfinishLocalSetup, getCustomMessages, getId, getMessageBundle, getSeverity, getSeverityLevel, setId, setSeverityconfigure, contextualize, getConfiguration, setupChildpublic static final java.lang.String MSG_WS_PRECEDED
public static final java.lang.String MSG_WS_FOLLOWED
public static final java.lang.String MSG_WS_NOT_PRECEDED
public static final java.lang.String MSG_WS_ILLEGAL_FOLLOW
private static final java.lang.String OPEN_ANGLE_BRACKET
private static final java.lang.String CLOSE_ANGLE_BRACKET
private int depth
public GenericWhitespaceCheck()
public int[] getDefaultTokens()
AbstractCheckgetDefaultTokens in class AbstractCheckTokenTypespublic int[] getAcceptableTokens()
AbstractCheckgetAcceptableTokens in class AbstractCheckTokenTypespublic int[] getRequiredTokens()
AbstractCheckgetRequiredTokens in class AbstractCheckTokenTypespublic void beginTree(DetailAST rootAST)
AbstractCheckbeginTree in class AbstractCheckrootAST - the root of the treepublic void visitToken(DetailAST ast)
AbstractCheckvisitToken in class AbstractCheckast - the token to processprivate void processEnd(DetailAST ast)
ast - the token to checkprivate void processNestedGenerics(DetailAST ast, java.lang.String line, int after)
ast - tokenline - line contentafter - position afterprivate void processSingleGeneric(DetailAST ast, java.lang.String line, int after)
ast - tokenline - line contentafter - position afterprivate static boolean isGenericBeforeCtor(DetailAST ast)
ast - astprivate static boolean isGenericBeforeMethod(DetailAST ast)
ast - astprivate static boolean isAfterMethodReference(DetailAST genericEnd)
method reference operator.genericEnd - TokenTypes.GENERIC_ENDprivate void processStart(DetailAST ast)
ast - the token to checkprivate static boolean containsWhitespaceBetween(int fromIndex, int toIndex, java.lang.String line)
fromIndex - the index to start the search from. InclusivetoIndex - the index to finish the search. Exclusiveline - the line to checkprivate static boolean containsWhitespaceBefore(int before, java.lang.String line)
before - the index to start the search from. Inclusiveline - the index to finish the search. Exclusivetrue if there are only whitespaces,
false if there is nothing before or some other charactersprivate static boolean isCharacterValidAfterGenericEnd(char charAfter)
charAfter - character to checkCopyright © 2001-2022. All Rights Reserved.