/**
  * Returns a string describing the exception type caught by the given try tree's catch
  * statement(s), defaulting to {@code "Exception"} if more than one exception type is caught.
  */
 private String exceptionToString(TryTree tree) {
   if (tree.getCatches().size() != 1) {
     return "Exception";
   }
   String exceptionType = tree.getCatches().iterator().next().getParameter().getType().toString();
   if (exceptionType.contains("|")) {
     return "Exception";
   }
   return exceptionType;
 }
 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;
 }
 private boolean hasExpectedException(TryTree tree) {
   for (CatchTree catchTree : tree.getCatches()) {
     if (catchTree.getParameter().getName().toString().equals("expected")) {
       return true;
     }
   }
   return false;
 }
 private boolean isInapplicableExceptionType(TryTree tree, VisitorState state) {
   for (CatchTree catchTree : tree.getCatches()) {
     if (INAPPLICABLE_EXCEPTION.matches(catchTree.getParameter(), state)) {
       return true;
     }
   }
   return false;
 }
  @Override
  public Description matchTry(TryTree tree, VisitorState state) {
    if (tryTreeMatches(tree, state)) {
      List<? extends StatementTree> tryStatements = tree.getBlock().getStatements();
      StatementTree lastTryStatement = tryStatements.get(tryStatements.size() - 1);

      String failCall = String.format("%nfail(\"Expected %s\");", exceptionToString(tree));
      SuggestedFix.Builder fixBuilder =
          SuggestedFix.builder().postfixWith(lastTryStatement, failCall);

      // Make sure that when the fail import is added it doesn't conflict with existing ones.
      fixBuilder.removeStaticImport("junit.framework.Assert.fail");
      fixBuilder.removeStaticImport("junit.framework.TestCase.fail");
      fixBuilder.addStaticImport("org.junit.Assert.fail");

      return describeMatch(lastTryStatement, fixBuilder.build());
    } else {
      return Description.NO_MATCH;
    }
  }
 @Override
 protected Iterable<? extends StatementTree> getChildNodes(TryTree tree, VisitorState state) {
   return tree.getBlock().getStatements();
 }
 private boolean hasFinally(TryTree tree) {
   return tree.getFinallyBlock() != null;
 }