Skip to content

Commit

Permalink
Issue checkstyle#10924: Checks that use getLine() should check code p…
Browse files Browse the repository at this point in the history
…oints for spaces
  • Loading branch information
MUzairS15 committed Nov 23, 2021
1 parent c323c17 commit afaad3e
Showing 1 changed file with 46 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -192,22 +192,23 @@ public void visitToken(DetailAST ast) {
*/
private void processEnd(DetailAST ast) {
final String line = getLine(ast.getLineNo() - 1);
int[] codePoints = line.codePoints().toArray();
final int before = ast.getColumnNo() - 1;
final int after = ast.getColumnNo() + 1;

if (before >= 0 && Character.isWhitespace(line.charAt(before))
&& !containsWhitespaceBefore(before, line)) {
if (before >= 0 && isWhitespace(codePoints, before)
&& !containsWhitespaceBefore(before, codePoints)) {
log(ast, MSG_WS_PRECEDED, CLOSE_ANGLE_BRACKET);
}

if (after < line.length()) {
if (after < codePoints.length) {
// Check if the last Generic, in which case must be a whitespace
// or a '(),[.'.
if (depth == 1) {
processSingleGeneric(ast, line, after);
processSingleGeneric(ast, line, after, codePoints);
}
else {
processNestedGenerics(ast, line, after);
processNestedGenerics(ast, line, after, codePoints);
}
}
}
Expand All @@ -219,7 +220,7 @@ private void processEnd(DetailAST ast) {
* @param line line content
* @param after position after
*/
private void processNestedGenerics(DetailAST ast, String line, int after) {
private void processNestedGenerics(DetailAST ast, String line, int after, int[] codePoints) {
// In a nested Generic type, so can only be a '>' or ',' or '&'

// In case of several extends definitions:
Expand All @@ -228,9 +229,15 @@ private void processNestedGenerics(DetailAST ast, String line, int after) {
// ^
// should be whitespace if followed by & -+
//
final int indexOfAmp = line.indexOf('&', after);
int indexOfAmp = after;
for(int i = after; i < codePoints.length; i++){
if(codePoints[i] == '&') {
indexOfAmp = i;
break;
}
}
if (indexOfAmp >= 1
&& containsWhitespaceBetween(after, indexOfAmp, line)) {
&& containsWhitespaceBetween(after, indexOfAmp, line, codePoints)) {
if (indexOfAmp - after == 0) {
log(ast, MSG_WS_NOT_PRECEDED, "&");
}
Expand All @@ -250,8 +257,8 @@ else if (line.charAt(after) == ' ') {
* @param line line content
* @param after position after
*/
private void processSingleGeneric(DetailAST ast, String line, int after) {
final char charAfter = line.charAt(after);
private void processSingleGeneric(DetailAST ast, String line, int after, int[] codePoints) {
final char charAfter = Character.toChars(codePoints[after])[0];
if (isGenericBeforeMethod(ast) || isGenericBeforeCtor(ast)) {
if (Character.isWhitespace(charAfter)) {
log(ast, MSG_WS_FOLLOWED, CLOSE_ANGLE_BRACKET);
Expand Down Expand Up @@ -304,6 +311,7 @@ private static boolean isAfterMethodReference(DetailAST genericEnd) {
*/
private void processStart(DetailAST ast) {
final String line = getLine(ast.getLineNo() - 1);
int[] codePoints = line.codePoints().toArray();
final int before = ast.getColumnNo() - 1;
final int after = ast.getColumnNo() + 1;

Expand All @@ -321,19 +329,19 @@ private void processStart(DetailAST ast) {
|| grandparent.getType() == TokenTypes.METHOD_DEF
|| isGenericBeforeCtor(ast)) {
// Require whitespace
if (!Character.isWhitespace(line.charAt(before))) {
if (!isWhitespace(codePoints, before)) {
log(ast, MSG_WS_NOT_PRECEDED, OPEN_ANGLE_BRACKET);
}
}
// Whitespace not required
else if (Character.isWhitespace(line.charAt(before))
&& !containsWhitespaceBefore(before, line)) {
else if (isWhitespace(codePoints, before)
&& !containsWhitespaceBefore(before, codePoints)) {
log(ast, MSG_WS_PRECEDED, OPEN_ANGLE_BRACKET);
}
}

if (after < line.length()
&& Character.isWhitespace(line.charAt(after))) {
&& isWhitespace(codePoints, before)) {
log(ast, MSG_WS_FOLLOWED, OPEN_ANGLE_BRACKET);
}
}
Expand All @@ -344,13 +352,13 @@ else if (Character.isWhitespace(line.charAt(before))
*
* @param fromIndex the index to start the search from. Inclusive
* @param toIndex the index to finish the search. Exclusive
* @param line the line to check
* @param codePoints the line to check
* @return whether there are only whitespaces (or nothing)
*/
private static boolean containsWhitespaceBetween(int fromIndex, int toIndex, String line) {
private static boolean containsWhitespaceBetween(int fromIndex, int toIndex, String line, int[] codePoints) {
boolean result = true;
for (int i = fromIndex; i < toIndex; i++) {
if (!Character.isWhitespace(line.charAt(i))) {
if (!isWhitespace(codePoints, i)) {
result = false;
break;
}
Expand All @@ -362,12 +370,30 @@ private static boolean containsWhitespaceBetween(int fromIndex, int toIndex, Str
* Returns whether the specified string contains only whitespace up to specified index.
*
* @param before the index to start the search from. Inclusive
* @param line the index to finish the search. Exclusive
* @param codePoints the index to finish the search. Exclusive
* @return {@code true} if there are only whitespaces,
* false if there is nothing before or some other characters
*/
private static boolean containsWhitespaceBefore(int before, String line) {
return before != 0 && CommonUtil.hasWhitespaceBefore(before, line);
private static boolean containsWhitespaceBefore(int before, int[] codePoints) {
return before != 0 && hasWhitespaceBefore(before, codePoints);
}

public static boolean hasWhitespaceBefore(int index, int[] codePoints) {
boolean result = true;
for (int i = 0; i < index; i++) {
if (!isWhitespace(codePoints, i)) {
result = false;
break;
}
}
return result;
}

private static boolean isWhitespace(int[] codePoints, int index) {
// We only need to check the first member of a surrogate pair to verify that
// it is not whitespace.
final char character = Character.toChars(codePoints[index])[0];
return Character.isWhitespace(character);
}

/**
Expand Down

0 comments on commit afaad3e

Please sign in to comment.