@Override public Result visitCatch(CatchTree node, BreakContext cxt) { return node.getBlock().accept(this, cxt); }
private boolean anyCatchBlockMatches(TryTree tree, VisitorState state, Matcher<Tree> matcher) { for (CatchTree catchTree : tree.getCatches()) { if (matcher.matches(catchTree.getBlock(), state)) { return true; } } return false; }
@Override public Description matchCatch(CatchTree tree, VisitorState state) { List<? extends StatementTree> statements = tree.getBlock().getStatements(); if (statements.size() != 1) { return NO_MATCH; } StatementTree statement = Iterables.getOnlyElement(statements); if (!MATCHER.matches(statement, state)) { return NO_MATCH; } return describeMatch(statement); } }
@Override public Description matchTry(TryTree tree, VisitorState state) { if (tree.getCatches().isEmpty()) { return NO_MATCH; } // Find catch blocks that contain only a call to fail, and that ignore the caught exception. ImmutableList<CatchTree> catchBlocks = tree.getCatches().stream() .filter( c -> c.getBlock().getStatements().size() == 1 && FAIL_METHOD.matches(getOnlyElement(c.getBlock().getStatements()), state)) .filter(c -> !catchVariableIsUsed(c)) .collect(toImmutableList()); if (catchBlocks.isEmpty()) { return NO_MATCH; } Description.Builder description = buildDescription(tree); rethrowFix(catchBlocks, state).ifPresent(description::addFix); deleteFix(tree, catchBlocks, state).ifPresent(description::addFix); return description.build(); }
@Override @Nullable public Choice<Unifier> visitCatch(CatchTree node, @Nullable Unifier unifier) { return getParameter() .unify(node.getParameter(), unifier) .thenChoose(unifications(getBlock(), node.getBlock())); } }
private boolean catchVariableIsUsed(CatchTree c) { VarSymbol sym = ASTHelpers.getSymbol(c.getParameter()); boolean[] found = {false}; c.getBlock() .accept( new TreeScanner<Void, Void>() { @Override public Void visitIdentifier(IdentifierTree node, Void aVoid) { if (Objects.equals(sym, ASTHelpers.getSymbol(node))) { found[0] = true; } return super.visitIdentifier(node, aVoid); } }, null); return found[0]; }
@Override public UCatch visitCatch(CatchTree tree, Void v) { return UCatch.create( visitVariable(tree.getParameter(), null), visitBlock(tree.getBlock(), null)); }
@Override public Choice<State<JCCatch>> visitCatch(final CatchTree node, State<?> state) { return chooseSubtrees( state, s -> unifyStatement(node.getBlock(), s), block -> maker().Catch((JCVariableDecl) node.getParameter(), (JCBlock) block)); }
private static Fix fixWithReturn( TryTree tryTree, StatementTree failStatement, VisitorState state) { SuggestedFix.Builder builder = SuggestedFix.builder(); builder.delete(failStatement); builder.replace(getOnlyCatch(tryTree).getBlock(), "{ return; }"); // TODO(cpovirk): Use the file's preferred assertion API. String messageSnippet = getMessageSnippet(failStatement, state, HasOtherParameters.FALSE); builder.postfixWith(tryTree, format("fail(%s);", messageSnippet)); return builder.build(); }
private Optional<Fix> rethrowFix(ImmutableList<CatchTree> catchBlocks, VisitorState state) { SuggestedFix.Builder fix = SuggestedFix.builder(); catchBlocks.forEach( c -> { // e.g. // fail("message") -> throw new AssertionError("message", cause); // assertWithMessage("message format %s", 42) -> // throw new AssertionError(String.format("message format %s", 42), cause); StatementTree statementTree = getOnlyElement(c.getBlock().getStatements()); MethodInvocationTree methodInvocationTree = (MethodInvocationTree) ((ExpressionStatementTree) statementTree).getExpression(); String message = null; if (message == null && !methodInvocationTree.getArguments().isEmpty()) { message = getMessageOrFormat(methodInvocationTree, state); } if (message != null) { // only catch and rethrow to add additional context, not for raw `fail()` calls fix.replace( statementTree, String.format( "throw new AssertionError(%s, %s);", message, c.getParameter().getName())); } }); return fix.isEmpty() ? Optional.empty() : Optional.of(fix.build()); }
private static Fix fixWithBoolean( TryTree tryTree, StatementTree failStatement, VisitorState state) { SuggestedFix.Builder builder = SuggestedFix.builder(); builder.delete(failStatement); builder.prefixWith(tryTree, "boolean threw = false;"); builder.replace(getOnlyCatch(tryTree).getBlock(), "{ threw = true; }"); // TODO(cpovirk): Use the file's preferred assertion API. String messageSnippet = getMessageSnippet(failStatement, state, HasOtherParameters.TRUE); builder.postfixWith(tryTree, format("assertTrue(%sthrew);", messageSnippet)); return builder.build(); }
return Optional.empty(); List<? extends StatementTree> catchStatements = catchTree.getBlock().getStatements(); fix.addStaticImport("org.junit.Assert.assertThrows"); if (!catchStatements.isEmpty()) {
@Override public Boolean visitTry(TryTree that, Void unused) { boolean completes = scan(that.getBlock()); // assume all catch blocks are reachable; javac has already rejected unreachable // checked exception handlers for (CatchTree catchTree : that.getCatches()) { completes |= scan(catchTree.getBlock()); } if (that.getFinallyBlock() != null && !scan(that.getFinallyBlock())) { completes = false; } return completes; } }
uniq.add(state.getSourceForNode(ct.getBlock()));
List<? extends StatementTree> catchStatements = catchTree.getBlock().getStatements(); for (StatementTree catchStatement : catchStatements) {
if (ASTHelpers.isCastable(typeSym, interruptedType, state)) { if (tree.getBlock().getStatements().stream() .allMatch(s -> s instanceof EmptyStatementTree)) { SuggestedFix.Builder fix = SuggestedFix.builder();
@Override public Void visitCatch(CatchTree expected, Tree actual) { Optional<CatchTree> other = checkTypeAndCast(expected, actual); if (!other.isPresent()) { addTypeMismatch(expected, actual); return null; } scan(expected.getParameter(), other.get().getParameter()); scan(expected.getBlock(), other.get().getBlock()); return null; }
@Override @Nullable public Choice<Unifier> visitCatch(CatchTree node, @Nullable Unifier unifier) { return getParameter() .unify(node.getParameter(), unifier) .thenChoose(unifications(getBlock(), node.getBlock())); } }
@Override public Tree visitCatch(CatchTree tree, Void p) { CatchTree n = make.Catch(tree.getParameter(), tree.getBlock()); model.setType(n, model.getType(tree)); comments.copyComments(tree, n); model.setPos(n, model.getPos(tree)); return n; }