use of com.sun.source.tree.StatementTree in project j2objc by google.
the class TreeConverter method convertBlock.
private TreeNode convertBlock(JCTree.JCBlock node) {
Block newNode = new Block();
for (StatementTree stmt : node.getStatements()) {
TreeNode tree = convert(stmt);
if (tree instanceof AbstractTypeDeclaration) {
tree = new TypeDeclarationStatement().setDeclaration((AbstractTypeDeclaration) tree);
}
newNode.addStatement((Statement) tree);
}
return newNode;
}
use of com.sun.source.tree.StatementTree in project error-prone by google.
the class TryFailThrowable method tryTreeMatches.
private static MatchResult tryTreeMatches(TryTree tryTree, VisitorState state) {
BlockTree tryBlock = tryTree.getBlock();
List<? extends StatementTree> statements = tryBlock.getStatements();
if (statements.isEmpty()) {
return doesNotMatch();
}
// Check if any of the statements is a fail or assert* method (i.e. any
// method that can throw an AssertionFailedError)
StatementTree failStatement = null;
for (StatementTree statement : statements) {
if (!(statement instanceof ExpressionStatementTree)) {
continue;
}
if (failOrAssert.matches(((ExpressionStatementTree) statement).getExpression(), state)) {
failStatement = statement;
break;
}
}
if (failStatement == null) {
return doesNotMatch();
}
// Verify that the only catch clause catches Throwable
List<? extends CatchTree> catches = tryTree.getCatches();
if (catches.size() != 1) {
// to be checked - it would either be Throwable or Error.
return doesNotMatch();
}
CatchTree catchTree = catches.get(0);
VariableTree catchType = catchTree.getParameter();
boolean catchesThrowable = javaLangThrowable.matches(catchType, state);
boolean catchesError = javaLangError.matches(catchType, state);
boolean catchesOtherError = someAssertionFailure.matches(catchType, state);
if (!catchesThrowable && !catchesError && !catchesOtherError) {
return doesNotMatch();
}
// Verify that the catch block is empty or contains only comments.
List<? extends StatementTree> catchStatements = catchTree.getBlock().getStatements();
for (StatementTree catchStatement : catchStatements) {
// or a list of empty statements.
if (!Matchers.<Tree>kindIs(EMPTY_STATEMENT).matches(catchStatement, state)) {
return doesNotMatch();
}
}
return matches(failStatement, catchesThrowable ? JAVA_LANG_THROWABLE : catchesError ? JAVA_LANG_ERROR : SOME_ASSERTION_FAILURE);
}
use of com.sun.source.tree.StatementTree in project error-prone by google.
the class FilesLinesLeak method matchMethodInvocation.
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!MATCHER.matches(tree, state)) {
return NO_MATCH;
}
if (inTWR(state)) {
return NO_MATCH;
}
Description.Builder description = buildDescription(tree);
Tree parent = state.getPath().getParentPath().getLeaf();
if (parent instanceof MemberSelectTree) {
MemberSelectTree select = (MemberSelectTree) parent;
StatementTree statement = state.findEnclosing(StatementTree.class);
SuggestedFix.Builder fix = SuggestedFix.builder();
if (statement instanceof VariableTree) {
VariableTree var = (VariableTree) statement;
int pos = ((JCTree) var).getStartPosition();
int initPos = ((JCTree) var.getInitializer()).getStartPosition();
int eqPos = pos + state.getSourceForNode(var).substring(0, initPos - pos).lastIndexOf('=');
fix.replace(eqPos, initPos, String.format(";\ntry (Stream<String> stream = %s) {\n%s =", state.getSourceForNode(tree), var.getName().toString()));
} else {
fix.prefixWith(statement, String.format("try (Stream<String> stream = %s) {\n", state.getSourceForNode(tree)));
fix.replace(select.getExpression(), "stream");
}
fix.replace(tree, "stream");
fix.postfixWith(statement, "}");
fix.addImport("java.util.stream.Stream");
description.addFix(fix.build());
}
return description.build();
}
use of com.sun.source.tree.StatementTree in project error-prone by google.
the class DivZero method matchDivZero.
private Description matchDivZero(Tree tree, ExpressionTree operand, VisitorState state) {
if (!anyOf(kindIs(Kind.DIVIDE), kindIs(Kind.DIVIDE_ASSIGNMENT)).matches(tree, state)) {
return Description.NO_MATCH;
}
if (!kindIs(Kind.INT_LITERAL).matches(operand, state)) {
return Description.NO_MATCH;
}
LiteralTree rightOperand = (LiteralTree) operand;
if (((Integer) rightOperand.getValue()) != 0) {
return Description.NO_MATCH;
}
// Find and replace enclosing Statement.
StatementTree enclosingStmt = ASTHelpers.findEnclosingNode(state.getPath(), StatementTree.class);
return (enclosingStmt != null) ? describeMatch(tree, SuggestedFix.replace(enclosingStmt, "throw new ArithmeticException(\"/ by zero\");")) : describeMatch(tree);
}
use of com.sun.source.tree.StatementTree in project error-prone by google.
the class NextStatement method matches.
@Override
public boolean matches(T statement, VisitorState state) {
List<? extends StatementTree> blockStatements = state.findEnclosing(BlockTree.class).getStatements();
int statementIndex = blockStatements.indexOf(statement);
if (statementIndex == -1) {
// return a; isn't contained directly in a BlockTree :-|
return false;
}
// find next statement
statementIndex++;
StatementTree nextStmt = null;
if (statementIndex < blockStatements.size()) {
nextStmt = blockStatements.get(statementIndex);
}
// TODO(glorioso): return false always instead of allowing the matcher to fail to match null?
return matcher.matches(nextStmt, state);
}
Aggregations