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..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,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,33 +197,50 @@ private List makeThrowList(DetailAST parentAST) { * @return List contains exceptions that wraps the original * exception object. */ - private List makeExceptionsList(DetailAST currentCatchAST, - DetailAST parentAST, String currentExcName) { + 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); + }; + + // 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<>(); + // Iterate through child nodes 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()); - } - } + + // 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); } if (currentNode.getType() != TokenTypes.PARAMETER_DEF