Skip to content

Commit

Permalink
Support for '&' in nested generics
Browse files Browse the repository at this point in the history
  • Loading branch information
dkomanov committed Aug 17, 2013
1 parent c6edc74 commit e76ae6e
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,27 @@ else if (!Character.isWhitespace(charAfter)
}
}
else {
// In a nested Generic type, so can only be a '>' or ','
if ((line.charAt(after) != '>') && (line.charAt(after) != ','))
// In a nested Generic type, so can only be a '>' or ',' or '&'

// In case of several extends definitions:
//
// class IntEnumValueType<E extends Enum<E> & IntEnum>
// ^
// should be whitespace if followed by & -+
//
int indexOfAmp = line.indexOf('&', after);
if ((indexOfAmp != -1) && whitespaceBetween(after, indexOfAmp, line))
{
if (indexOfAmp - after == 0)
{
log(aAST.getLineNo(), after, "ws.notPreceded", "&");
}
else if (indexOfAmp - after != 1)
{
log(aAST.getLineNo(), after, "ws.followed", ">");
}
}
else if ((line.charAt(after) != '>') && (line.charAt(after) != ','))
{
log(aAST.getLineNo(), after, "ws.followed", ">");
}
Expand Down Expand Up @@ -158,4 +177,25 @@ else if (Character.isWhitespace(line.charAt(before))
log(aAST.getLineNo(), after, "ws.followed", "<");
}
}

/**
* Returns whether the specified string contains only whitespace between
* specified indices.
*
* @param fromIndex the index to start the search from. Inclusive
* @param toIndex the index to finish the search. Exclusive
* @param aLine the line to check
* @return whether there are only whitespaces (or nothing)
*/
private static boolean whitespaceBetween(int fromIndex, int toIndex, String aLine)
{
for (int i = fromIndex; i < toIndex; i++)
{
if (!Character.isWhitespace(aLine.charAt(i)))
{
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,16 @@ <T> InputGenericWhitespaceCheck(List<T> things, int i)
public <T> InputGenericWhitespaceCheck(List<T> things)
{
}

public interface IntEnum {
}

public static class IntEnumValueType<E extends Enum<E> & IntEnum> {
}

public static class IntEnumValueType<E extends Enum<E>& IntEnum> {
}

public static class IntEnumValueType<E extends Enum<E> & IntEnum> {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public void testDefault() throws Exception
"30:21: '>' is followed by an illegal character.",
"42:21: '<' is preceded with whitespace.",
"42:30: '>' is followed by whitespace.",
"60:59: '&' is not preceded with whitespace.",
"63:59: '>' is followed by whitespace.",
};
verify(mCheckConfig,
getPath("whitespace/InputGenericWhitespaceCheck.java"),
Expand Down

0 comments on commit e76ae6e

Please sign in to comment.