From 5c038071b7d0ec074aa0e130b07ed47ac42d9c03 Mon Sep 17 00:00:00 2001 From: yajna pandith Date: Fri, 6 Mar 2020 00:13:45 +0530 Subject: [PATCH 1/3] Update AvoidHidingCauseExceptionCheck.java While I have been able to reduce the complexity of the code, I am not very satisfied with the verbosity introduced by checkstyle's unnecessary braces and sonar's complaint. I will post the PR. I would like you to look into sonar errors with respect to unnecessary braces in lambda and suggest me if I should tinker checkstyle configuration for the same. --- .../AvoidHidingCauseExceptionCheck.java | 89 ++++++++++++------- 1 file changed, 55 insertions(+), 34 deletions(-) diff --git a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/AvoidHidingCauseExceptionCheck.java b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/AvoidHidingCauseExceptionCheck.java index 754438be0c..d97bd83b31 100644 --- a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/AvoidHidingCauseExceptionCheck.java +++ b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/AvoidHidingCauseExceptionCheck.java @@ -21,6 +21,11 @@ import java.util.LinkedList; import java.util.List; +import java.util.Objects; +import java.util.Optional; +import java.util.function.Predicate; +import java.util.function.UnaryOperator; +import java.util.stream.Stream; import com.puppycrawl.tools.checkstyle.api.AbstractCheck; import com.puppycrawl.tools.checkstyle.api.DetailAST; @@ -192,44 +197,60 @@ private List makeThrowList(DetailAST parentAST) { * @return List contains exceptions that wraps the original * exception object. */ - private List makeExceptionsList(DetailAST currentCatchAST, - DetailAST parentAST, String currentExcName) { - final List wrapExcNames = new LinkedList<>(); + private List makeExceptionsList(final DetailAST currentCatchAST, + final DetailAST parentAST, + final String currentExcName) { + // Test if the current node is currentCatchAST or of token type ASSIGN + final Predicate testIfCurrentAstOrAssign = node -> { + return currentCatchAST.equals(node) + || Objects.equals(node.getType(), TokenTypes.ASSIGN); + }; - for (DetailAST currentNode : getChildNodes(parentAST)) { - if (currentNode.getType() == TokenTypes.IDENT - && currentNode.getText().equals(currentExcName) - && currentNode.getParent().getType() != TokenTypes.DOT) { - DetailAST temp = currentNode; - - while (!temp.equals(currentCatchAST) - && temp.getType() != TokenTypes.ASSIGN) { - temp = temp.getParent(); - } - - if (temp.getType() == TokenTypes.ASSIGN) { - DetailAST convertedExc = null; - if (temp.getParent().getType() == TokenTypes.VARIABLE_DEF) { - convertedExc = temp.getParent().findFirstToken(TokenTypes.IDENT); - } - else { - convertedExc = temp.findFirstToken(TokenTypes.IDENT); - } - if (convertedExc != null) { - wrapExcNames.add(convertedExc.getText()); - } - } - } + // Iterate through a node and its parent till testIfCurrentAstOrAssign passes once + final UnaryOperator getParentNode = (final DetailAST temp) -> { + return Stream.iterate(temp, DetailAST::getParent) + .filter(Objects::nonNull) + .filter(testIfCurrentAstOrAssign) + .findFirst() + .orElse(temp); + }; - if (currentNode.getType() != TokenTypes.PARAMETER_DEF - && currentNode.getFirstChild() != null) { - wrapExcNames.addAll(makeExceptionsList(currentCatchAST, - currentNode, currentExcName)); - } + final List wrapExcNames = new LinkedList<>(); + + // Iterate through child nodes + for (DetailAST currentNode : getChildNodes(parentAST)) { + if (currentNode.getType() == TokenTypes.IDENT + && currentNode.getText().equals(currentExcName) + && currentNode.getParent().getType() != TokenTypes.DOT) { + + // Assignable T\token type node + final Optional optAst = Optional.of(currentNode) + .map(getParentNode) + .filter(temp -> temp.getType() == TokenTypes.ASSIGN); + + // Find firast identity token + final Optional optResult = optAst.map(temp -> { + final DetailAST convertedExc = Optional.of(temp) + .filter(node -> { + return node.getParent().getType() == TokenTypes.VARIABLE_DEF; + }) + .map(DetailAST::getParent) + .orElse(temp); + return convertedExc.findFirstToken(TokenTypes.IDENT); + }); + + // Add to exception names + optResult.map(DetailAST::getText).ifPresent(wrapExcNames::add); } - return wrapExcNames; - } + if (currentNode.getType() != TokenTypes.PARAMETER_DEF + && currentNode.getFirstChild() != null) { + wrapExcNames.addAll(makeExceptionsList(currentCatchAST, + currentNode, currentExcName)); + } + } + return wrapExcNames; +} /** * Gets all the children one level below on the current parent node. * @param node Current parent node. From 21bb39dd7b2c48fd7ca71ea7263e683926b8a617 Mon Sep 17 00:00:00 2001 From: yajna pandith Date: Fri, 6 Mar 2020 00:39:12 +0530 Subject: [PATCH 2/3] Update AvoidHidingCauseExceptionCheck.java --- .../AvoidHidingCauseExceptionCheck.java | 89 ++++++++++--------- 1 file changed, 45 insertions(+), 44 deletions(-) diff --git a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/AvoidHidingCauseExceptionCheck.java b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/AvoidHidingCauseExceptionCheck.java index d97bd83b31..4b882f49a1 100644 --- a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/AvoidHidingCauseExceptionCheck.java +++ b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/AvoidHidingCauseExceptionCheck.java @@ -198,59 +198,60 @@ private List makeThrowList(DetailAST parentAST) { * exception object. */ private List makeExceptionsList(final DetailAST currentCatchAST, - final DetailAST parentAST, - final String currentExcName) { - // Test if the current node is currentCatchAST or of token type ASSIGN - final Predicate testIfCurrentAstOrAssign = node -> { - return currentCatchAST.equals(node) - || Objects.equals(node.getType(), TokenTypes.ASSIGN); - }; + final DetailAST parentAST, + final String currentExcName) { + // Test if the current node is currentCatchAST or of token type ASSIGN + final Predicate testIfCurrentAstOrAssign = node -> { + return currentCatchAST.equals(node) + || Objects.equals(node.getType(), TokenTypes.ASSIGN); + }; - // Iterate through a node and its parent till testIfCurrentAstOrAssign passes once - final UnaryOperator getParentNode = (final DetailAST temp) -> { - return Stream.iterate(temp, DetailAST::getParent) - .filter(Objects::nonNull) - .filter(testIfCurrentAstOrAssign) - .findFirst() - .orElse(temp); - }; + // Iterate through a node and its parent till testIfCurrentAstOrAssign passes once + final UnaryOperator getParentNode = (final DetailAST temp) -> { + return Stream.iterate(temp, DetailAST::getParent) + .filter(Objects::nonNull) + .filter(testIfCurrentAstOrAssign) + .findFirst() + .orElse(temp); + }; - final List wrapExcNames = new LinkedList<>(); + final List wrapExcNames = new LinkedList<>(); - // Iterate through child nodes - for (DetailAST currentNode : getChildNodes(parentAST)) { - if (currentNode.getType() == TokenTypes.IDENT - && currentNode.getText().equals(currentExcName) - && currentNode.getParent().getType() != TokenTypes.DOT) { + // Iterate through child nodes + for (DetailAST currentNode : getChildNodes(parentAST)) { + if (currentNode.getType() == TokenTypes.IDENT + && currentNode.getText().equals(currentExcName) + && currentNode.getParent().getType() != TokenTypes.DOT) { - // Assignable T\token type node - final Optional optAst = Optional.of(currentNode) - .map(getParentNode) - .filter(temp -> temp.getType() == TokenTypes.ASSIGN); + // Assignable T\token type node + final Optional optAst = Optional.of(currentNode) + .map(getParentNode) + .filter(temp -> temp.getType() == TokenTypes.ASSIGN); - // Find firast identity token - final Optional optResult = optAst.map(temp -> { - final DetailAST convertedExc = Optional.of(temp) - .filter(node -> { - return node.getParent().getType() == TokenTypes.VARIABLE_DEF; - }) - .map(DetailAST::getParent) - .orElse(temp); - return convertedExc.findFirstToken(TokenTypes.IDENT); - }); + // Find firast identity token + final Optional optResult = optAst.map(temp -> { + final DetailAST convertedExc = Optional.of(temp) + .filter(node -> { + return node.getParent().getType() == TokenTypes.VARIABLE_DEF; + }) + .map(DetailAST::getParent) + .orElse(temp); + return convertedExc.findFirstToken(TokenTypes.IDENT); + }); - // Add to exception names - optResult.map(DetailAST::getText).ifPresent(wrapExcNames::add); - } + // Add to exception names + optResult.map(DetailAST::getText).ifPresent(wrapExcNames::add); + } - if (currentNode.getType() != TokenTypes.PARAMETER_DEF - && currentNode.getFirstChild() != null) { - wrapExcNames.addAll(makeExceptionsList(currentCatchAST, - currentNode, currentExcName)); + if (currentNode.getType() != TokenTypes.PARAMETER_DEF + && currentNode.getFirstChild() != null) { + wrapExcNames.addAll(makeExceptionsList(currentCatchAST, + currentNode, currentExcName)); + } } + return wrapExcNames; } - return wrapExcNames; -} + /** * Gets all the children one level below on the current parent node. * @param node Current parent node. From ab00a6852741ed911fc72cabb1e95d02d483f763 Mon Sep 17 00:00:00 2001 From: yajna pandith Date: Fri, 6 Mar 2020 00:42:34 +0530 Subject: [PATCH 3/3] Update AvoidHidingCauseExceptionCheck.java --- .../checks/coding/AvoidHidingCauseExceptionCheck.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/AvoidHidingCauseExceptionCheck.java b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/AvoidHidingCauseExceptionCheck.java index 4b882f49a1..5ecb91d15d 100644 --- a/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/AvoidHidingCauseExceptionCheck.java +++ b/sevntu-checks/src/main/java/com/github/sevntu/checkstyle/checks/coding/AvoidHidingCauseExceptionCheck.java @@ -21,7 +21,7 @@ import java.util.LinkedList; import java.util.List; -import java.util.Objects; +import java.util.Objects; import java.util.Optional; import java.util.function.Predicate; import java.util.function.UnaryOperator;