use of com.sun.source.tree.StatementTree in project error-prone by google.
the class IncrementInForLoopAndHeader method matchForLoop.
@Override
public Description matchForLoop(ForLoopTree forLoopTree, VisitorState visitorState) {
List<? extends ExpressionStatementTree> updates = forLoopTree.getUpdate();
// keep track of all the symbols that are updated in the for loop header
final Set<Symbol> incrementedSymbols = updates.stream().filter(expStateTree -> expStateTree.getExpression() instanceof UnaryTree).map(expStateTree -> ASTHelpers.getSymbol(((UnaryTree) expStateTree.getExpression()).getExpression())).collect(Collectors.toCollection(HashSet::new));
// track if they are updated in the body without a conditional surrounding them
StatementTree body = forLoopTree.getStatement();
List<? extends StatementTree> statementTrees = body instanceof BlockTree ? ((BlockTree) body).getStatements() : ImmutableList.of(body);
for (StatementTree s : statementTrees) {
if (!CONDITIONALS.contains(s.getKind())) {
Optional<Symbol> opSymbol = returnUnarySym(s);
if (opSymbol.isPresent() && incrementedSymbols.contains(opSymbol.get())) {
// both ++ and --
return describeMatch(forLoopTree);
}
}
}
return Description.NO_MATCH;
}
use of com.sun.source.tree.StatementTree in project error-prone by google.
the class EmptyIfStatement method matchEmptyStatement.
/**
* Match empty statement if: - Parent statement is an if - The then part of the parent if is an
* empty statement, and - The else part of the parent if does not exist
*/
@Override
public Description matchEmptyStatement(EmptyStatementTree tree, VisitorState state) {
boolean matches = false;
Tree parent = state.getPath().getParentPath().getLeaf();
if (parent.getKind() == IF) {
IfTree parentAsIf = (IfTree) parent;
matches = (parentAsIf.getThenStatement() instanceof EmptyStatementTree) && (parentAsIf.getElseStatement() == null);
}
if (!matches) {
return Description.NO_MATCH;
}
/*
* We suggest different fixes depending on what follows the parent if statement.
* If there is no statement following the if, then suggest deleting the whole
* if statement. If the next statement is a block, then suggest deleting the
* empty then part of the if. If the next statement is not a block, then also
* suggest deleting the empty then part of the if.
*/
boolean nextStmtIsNull = parentNode(nextStatement(Matchers.<StatementTree>isSame(null))).matches(tree, state);
assert (state.getPath().getParentPath().getLeaf().getKind() == IF);
IfTree ifParent = (IfTree) state.getPath().getParentPath().getLeaf();
if (nextStmtIsNull) {
// No following statements. Delete whole if.
return describeMatch(parent, SuggestedFix.delete(parent));
} else {
// There are more statements. Delete the empty then part of the if.
return describeMatch(ifParent.getThenStatement(), SuggestedFix.delete(ifParent.getThenStatement()));
}
}
use of com.sun.source.tree.StatementTree in project error-prone by google.
the class InputStreamSlowMultibyteRead method maybeMatchReadByte.
private Description maybeMatchReadByte(MethodTree readByteMethod, VisitorState state) {
if (readByteMethod.getBody() != null) {
// Null-check for native/abstract overrides of read()
List<? extends StatementTree> statements = readByteMethod.getBody().getStatements();
if (statements.size() == 1) {
Tree tree = statements.get(0);
if (tree.getKind() == Kind.RETURN && ASTHelpers.constValue(((ReturnTree) tree).getExpression()) != null) {
return Description.NO_MATCH;
}
}
}
// Streams within JUnit test cases are likely to be OK as well.
TreePath enclosingPath = ASTHelpers.findPathFromEnclosingNodeToTopLevel(state.getPath(), ClassTree.class);
while (enclosingPath != null) {
ClassTree klazz = (ClassTree) enclosingPath.getLeaf();
if (JUnitMatchers.isTestCaseDescendant.matches(klazz, state) || hasAnnotation(JUnitMatchers.JUNIT4_RUN_WITH_ANNOTATION).matches(klazz, state)) {
return Description.NO_MATCH;
}
enclosingPath = ASTHelpers.findPathFromEnclosingNodeToTopLevel(enclosingPath, ClassTree.class);
}
return describeMatch(readByteMethod);
}
use of com.sun.source.tree.StatementTree in project bazel by bazelbuild.
the class TreeUtils method containsThisConstructorInvocation.
/**
* @return true if the first statement in the body is a self constructor
* invocation within a constructor
*/
public static final boolean containsThisConstructorInvocation(MethodTree node) {
if (!TreeUtils.isConstructor(node) || node.getBody().getStatements().isEmpty())
return false;
StatementTree st = node.getBody().getStatements().get(0);
if (!(st instanceof ExpressionStatementTree) || !(((ExpressionStatementTree) st).getExpression() instanceof MethodInvocationTree))
return false;
MethodInvocationTree invocation = (MethodInvocationTree) ((ExpressionStatementTree) st).getExpression();
return "this".contentEquals(TreeUtils.methodName(invocation));
}
use of com.sun.source.tree.StatementTree in project error-prone by google.
the class MissingFail method matchTry.
@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;
}
}
Aggregations