diff --git a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/CustomDeclarationOrderCheck.java b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/CustomDeclarationOrderCheck.java index 77489197bc..08c8a45d41 100644 --- a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/CustomDeclarationOrderCheck.java +++ b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/CustomDeclarationOrderCheck.java @@ -596,37 +596,27 @@ private int getPositionInOrderDeclaration(final DetailAST ast) if (currentRule.getClassMember() == ast.getType() && currentRule.getRegexp().matcher(modifiers).find()) { - if (currentRule.hasRule(ANNON_CLASS_FIELD_MACRO)) { - if (isAnonymousClassField(ast)) { - result = index; - break; - } - } - else if (currentRule.hasRule(GETTER_SETTER_MACRO)) { + if (currentRule.hasRule(ANNON_CLASS_FIELD_MACRO) + || currentRule.hasRule(GETTER_SETTER_MACRO) + || currentRule.hasRule(MAIN_METHOD_MACRO)) { + final String methodName = getIdentifier(ast); final ClassDetail classDetail = classDetails.peek(); - if (classDetail.containsGetter(methodName) - || classDetail.containsSetter(methodName)) - { - result = index; - break; - } - } - else if (currentRule.hasRule(MAIN_METHOD_MACRO)) { - if (isMainMethod(ast)) { + + if (isAnonymousClassField(ast) + || classDetail.containsGetter(methodName) + || classDetail.containsSetter(methodName) + || isMainMethod(ast)) { result = index; break; } } else { // if more than one rule matches current AST node, then keep first one - result = (result == -1) ? index : result; - if (ast.getType() == TokenTypes.METHOD_DEF - || ast.getType() == TokenTypes.VARIABLE_DEF) + result = result == -1 ? index : result; + if (ast.getType() != TokenTypes.METHOD_DEF + && ast.getType() != TokenTypes.VARIABLE_DEF) { - // continue to find more specific rule - continue; - } else { break; } } diff --git a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/DiamondOperatorForVariableDefinitionCheck.java b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/DiamondOperatorForVariableDefinitionCheck.java index d71a2a3ac4..614690eca2 100644 --- a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/DiamondOperatorForVariableDefinitionCheck.java +++ b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/DiamondOperatorForVariableDefinitionCheck.java @@ -89,23 +89,17 @@ public void visitToken(DetailAST variableDefNode) { * AST subtree to process. */ private static DetailAST getFirstChildTokenOfType(DetailAST rootToken, int tokenType) { - - DetailAST resultNode = null; - DetailAST currentNode = rootToken.getFirstChild(); - while (currentNode != null) { - if (currentNode.getType() == tokenType) { - resultNode = currentNode; - break; - } - DetailAST childNode = getFirstChildTokenOfType(currentNode, tokenType); + DetailAST resultNode = rootToken.getFirstChild(); + + if (resultNode != null + && resultNode.getType() != tokenType) { + DetailAST childNode = getFirstChildTokenOfType(resultNode, tokenType); + if (childNode == null) { - currentNode = currentNode.getNextSibling(); - } - else { - resultNode = childNode; - break; + resultNode = resultNode.getNextSibling(); } } + return resultNode; } diff --git a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/EitherLogOrThrowCheck.java b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/EitherLogOrThrowCheck.java index 6b0eb2240d..58974fe21e 100644 --- a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/EitherLogOrThrowCheck.java +++ b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/EitherLogOrThrowCheck.java @@ -555,22 +555,17 @@ private static boolean containsExceptionParameter( { boolean result = false; DetailAST parameterAst = parametersAst.getFirstChild(); + while (parameterAst != null) { - if (exceptionVariableName.equals(getIdentifier(parameterAst))) - { + if (exceptionVariableName.equals(getIdentifier(parameterAst)) + || isInstanceMethodCall(exceptionVariableName, + parameterAst.getFirstChild())) { result = true; - break; + parameterAst = null; } else { - final DetailAST methodCallAst = parameterAst.getFirstChild(); - if (isInstanceMethodCall(exceptionVariableName, - methodCallAst)) - { - result = true; - break; - } + parameterAst = parameterAst.getNextSibling(); } - parameterAst = parameterAst.getNextSibling(); } return result; } diff --git a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/ForbidInstantiationCheck.java b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/ForbidInstantiationCheck.java index a1ba7f0ebf..cbf842a57b 100644 --- a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/ForbidInstantiationCheck.java +++ b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/ForbidInstantiationCheck.java @@ -164,26 +164,57 @@ private boolean addedUsingForbiddenImport(final String className, boolean result = false; for (String importText : importsList) { - if (importText.endsWith("*")) { - final String importTextWithoutAsterisk = - importText.substring(0, importText.length() - 1); - if (forbiddenClassNameAndPath.equals( - importTextWithoutAsterisk + className)) - { - result = true; - break; - } - } - else if (importText.equals(forbiddenClassNameAndPath) - && importText.endsWith(className)) - { + if (isWildcardForbiddenImported(importText, forbiddenClassNameAndPath, className) + || isForbiddenImported(importText, forbiddenClassNameAndPath, className)) { result = true; break; } } + return result; } + /** + * Tests if the class with given className is imported with the forbidden + * import and false otherwise. + * + * @param importText + * - String representation of imports from the processed class. + * @param className + * - the name of the class to check. + * @param forbiddenClassNameAndPath + * - full name&path of the given forbidden class. + * @return true if the class with given className is imported with the + * forbidden import and false otherwise. + */ + private boolean isWildcardForbiddenImported(String importText, String forbiddenClassNameAndPath, + String className) { + final String importTextWithoutAsterisk = + importText.substring(0, importText.length() - 1); + + return importText.endsWith("*") + && forbiddenClassNameAndPath.equals(importTextWithoutAsterisk + className); + } + + /** + * Tests if the class with given className is imported with the forbidden + * import and false otherwise. + * + * @param importText + * - String representation of imports from the processed class. + * @param className + * - the name of the class to check. + * @param forbiddenClassNameAndPath + * - full name&path of the given forbidden class. + * @return true if the class with given className is imported with the + * forbidden import and false otherwise. + */ + private boolean isForbiddenImported(String importText, String forbiddenClassNameAndPath, + String className) { + return importText.equals(forbiddenClassNameAndPath) + && importText.endsWith(className); + } + /** * Gets the class name from full (dotted) classPath. * @param classNameAndPath diff --git a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/OverridableMethodInConstructorCheck.java b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/OverridableMethodInConstructorCheck.java index 991ebbe3ee..a47b38da2e 100644 --- a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/OverridableMethodInConstructorCheck.java +++ b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/OverridableMethodInConstructorCheck.java @@ -378,39 +378,31 @@ private boolean isOverridableMethodCall(final DetailAST methodCallAST) visitedMethodCalls.add(methodCallAST); final String methodName = getMethodName(methodCallAST); + final DetailAST methodDef = getMethodDef(methodCallAST); - if (methodName != null) { - final DetailAST methodDef = getMethodDef(methodCallAST); - if (methodDef != null) { - - if (hasModifier(methodDef, TokenTypes.LITERAL_STATIC)) { - // do nothing - } - else if (hasModifier(methodDef, TokenTypes.LITERAL_PRIVATE) - || hasModifier(methodDef, TokenTypes.FINAL)) - { - final List methodCallsList = getMethodCallsList( - methodDef); - for (DetailAST curNode : methodCallsList) { - if (visitedMethodCalls.contains(curNode)) { - result = false; - break; - } - else if (isOverridableMethodCall(curNode)) { - result = true; - break; - } + if (methodName != null + && methodDef != null) + { + if (hasModifier(methodDef, TokenTypes.LITERAL_STATIC)) { + // do nothing + } + else if (hasModifier(methodDef, TokenTypes.LITERAL_PRIVATE) + || hasModifier(methodDef, TokenTypes.FINAL)) + { + final List methodCallsList = getMethodCallsList( + methodDef); + for (DetailAST curNode : methodCallsList) { + if (!visitedMethodCalls.contains(curNode) + && isOverridableMethodCall(curNode)) { + result = true; + break; } } - else { - curOverridableMetName = methodName; - result = true; - } } - } - else - { - result = false; + else { + curOverridableMetName = methodName; + result = true; + } } return result; } @@ -890,7 +882,7 @@ private static DetailAST getClassDef(DetailAST rootNode, String className) while (curNode != null) { DetailAST toVisit = curNode.getFirstChild(); - while ((curNode != null) && (toVisit == null)) { + while (curNode != null && toVisit == null) { toVisit = curNode.getNextSibling(); if (toVisit == null) { curNode = curNode.getParent(); @@ -899,11 +891,8 @@ private static DetailAST getClassDef(DetailAST rootNode, String className) curNode = toVisit; - if (curNode == null) { - break; - } - - if (curNode.getType() == TokenTypes.CLASS_DEF + if (curNode != null + && curNode.getType() == TokenTypes.CLASS_DEF && curNode.findFirstToken(TokenTypes.IDENT).getText() .equals(className)) {