Skip to content

Commit

Permalink
Issue sevntu-checkstyle#743: fix compiling issues with checkstyle 8.21
Browse files Browse the repository at this point in the history
  • Loading branch information
rnveach authored and romani committed May 8, 2019
1 parent 984c6fe commit 356b897
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 19 deletions.
Expand Up @@ -83,7 +83,7 @@ public void visitToken(DetailAST variableDefNode) {
&& newNode.findFirstToken(TokenTypes.ARRAY_DECLARATOR) == null) {
final DetailAST typeArgs = getFirstTypeArgumentsToken(newNode);

if (varDefArguments.equalsTree(typeArgs)) {
if (typeArgs != null && isTreeEqual(varDefArguments, typeArgs)) {
log(typeArgs, MSG_KEY);
}
}
Expand Down Expand Up @@ -113,4 +113,46 @@ private static DetailAST getFirstTypeArgumentsToken(DetailAST rootToken) {
return resultNode;
}

/**
* Checks if the 2 given trees have the same children, type, and text.
* @param left One of the trees to compare.
* @param right The other tree to compare.
* @return {@code true} if the trees are equal.
*/
private static boolean isTreeEqual(DetailAST left, DetailAST right) {
boolean result;

if (isAstEqual(left, right)) {
result = true;

DetailAST leftChild = left.getFirstChild();
DetailAST rightChild = right.getFirstChild();

while (leftChild != rightChild) {
if (!isTreeEqual(leftChild, rightChild)) {
result = false;
break;
}

leftChild = leftChild.getNextSibling();
rightChild = rightChild.getNextSibling();
}
}
else {
result = false;
}

return result;
}

/**
* Checks if the 2 given ASTs have the same type and text.
* @param left One of the ASTs to compare.
* @param right The other AST to compare.
* @return {@code true} if the ASTs are equal.
*/
private static boolean isAstEqual(DetailAST left, DetailAST right) {
return left.getType() == right.getType() && left.getText().equals(right.getText());
}

}
Expand Up @@ -41,13 +41,10 @@ public class ForbidCCommentsInMethodsCheck extends AbstractCheck {
*/
public static final String MSG_KEY = "forbid.c.comments.in.the.method.body";

/** AST to use for null checks because {@link Deque} doesn't allow null elements. */
private static final DetailAST NULL_AST = new DetailAST();

/** Method stack. */
private final Deque<DetailAST> scopeStack = new ArrayDeque<>();

/** Reference to current method. */
/** Reference to current token being tracked. */
private DetailAST methodAst;

@Override
Expand Down Expand Up @@ -76,7 +73,7 @@ public boolean isCommentNodesRequired() {

@Override
public void beginTree(DetailAST rootAST) {
methodAst = NULL_AST;
methodAst = rootAST;
}

@Override
Expand All @@ -85,13 +82,11 @@ public void visitToken(DetailAST ast) {

switch (ast.getType()) {
case TokenTypes.METHOD_DEF:
methodAst = ast;
break;
case TokenTypes.OBJBLOCK:
methodAst = NULL_AST;
methodAst = ast;
break;
default:
if (methodAst != NULL_AST) {
if (methodAst.getType() == TokenTypes.METHOD_DEF) {
final DetailAST lcurly = methodAst.findFirstToken(TokenTypes.SLIST);
if (lcurly != null
&& (ast.getLineNo() > lcurly.getLineNo()
Expand Down
Expand Up @@ -25,7 +25,6 @@
import java.util.Set;
import java.util.regex.Pattern;

import antlr.collections.AST;
import com.puppycrawl.tools.checkstyle.api.AbstractCheck;
import com.puppycrawl.tools.checkstyle.api.DetailAST;
import com.puppycrawl.tools.checkstyle.api.TokenTypes;
Expand Down Expand Up @@ -305,9 +304,9 @@ private static boolean isInsideInterfaceDefinition(DetailAST methodDefAst) {
* @return the set of modifier Strings for aMethodDefAST
*/
private static Set<String> getModifiers(DetailAST methodDefAst) {
final AST modifiersAst = methodDefAst.getFirstChild();
final DetailAST modifiersAst = methodDefAst.getFirstChild();
final Set<String> modifiersSet = new HashSet<>();
AST modifierAst = modifiersAst.getFirstChild();
DetailAST modifierAst = modifiersAst.getFirstChild();
while (modifierAst != null) {
modifiersSet.add(modifierAst.getText());
modifierAst = modifierAst.getNextSibling();
Expand Down
Expand Up @@ -60,10 +60,10 @@ public int compare(Object s1, Object s2) {
private transient Map<Method, String> shadowMatchCache = new ConcurrentHashMap<Method, String>(32);

private Predicate<File> inputCsvFilesFilter = new Predicate<File>() {
public boolean apply(File input)
{
return false;
}
public boolean apply(File input)
{
return false;
}
};

String str = new String("");
Expand Down Expand Up @@ -91,6 +91,12 @@ public void doSmth() {
java.util.Date date = new java.util.Date();
static java.util.Map<List<String>, String> myMap2 = new java.util.TreeMap<List<String>, String>();
static java.util.Map<List<String>, String> myMap3 = new TreeMap<List<String>, String>();

static List<String> myMap4 = new InputDiamondOperatorForVariableDefinitionCheck.Inner();
static H006_ComplexConstructors<Integer> instance = new <String>H006_ComplexConstructors<Integer>(0, "");

private static class Inner extends ArrayList<String> {}
private static class H006_ComplexConstructors<T> {
public <V> H006_ComplexConstructors(T t, V v) {}
}
}


0 comments on commit 356b897

Please sign in to comment.