Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update AvoidHidingCauseExceptionCheck.java #805

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -192,33 +197,50 @@ private List<DetailAST> makeThrowList(DetailAST parentAST) {
* @return List contains exceptions that wraps the original
* exception object.
*/
private List<String> makeExceptionsList(DetailAST currentCatchAST,
DetailAST parentAST, String currentExcName) {
private List<String> makeExceptionsList(final DetailAST currentCatchAST,
final DetailAST parentAST,
final String currentExcName) {
// Test if the current node is currentCatchAST or of token type ASSIGN
final Predicate<DetailAST> 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<DetailAST> getParentNode = (final DetailAST temp) -> {
return Stream.iterate(temp, DetailAST::getParent)
.filter(Objects::nonNull)
.filter(testIfCurrentAstOrAssign)
.findFirst()
.orElse(temp);
};

final List<String> 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<DetailAST> optAst = Optional.of(currentNode)
.map(getParentNode)
.filter(temp -> temp.getType() == TokenTypes.ASSIGN);

// Find firast identity token
final Optional<DetailAST> 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
Expand Down