private static Optional<StatementTree> firstAndOnlyStatement(MethodTree methodTree) { return Optional.ofNullable(methodTree.block()) .filter(b -> b.body().size() == 1) .map(b -> b.body().get(0)); } }
@Override public void visitNode(Tree tree) { MethodTree methodTree = (MethodTree) tree; if (methodTree.block() != null) { CFG cfg = CFG.build(methodTree); cfg.blocks().forEach(this::checkBlock); } }
private static boolean hasTryWithResourceInBody(MethodTree methodTree) { BlockTree block = methodTree.block(); if (block == null) { return false; } TryWithResourcesVisitor visitor = new TryWithResourcesVisitor(); block.accept(visitor); return visitor.hasTryWithResource; }
private void checkMethodDefinition(MethodTree tree) { BlockTree blockTree = tree.block(); if (VERIFY_METHOD_MATCHER.matches(tree) && blockTree != null) { checkBlock(blockTree); } }
private static boolean isDefaultConstructorForJpa(MethodTree methodTree) { if (!methodTree.block().body().isEmpty()) { // Constructor does something. return false; } SymbolMetadata symbolMetadata = ((ClassTree) methodTree.parent()).symbol().metadata(); return Stream.of(JPA_ANNOTATIONS).anyMatch(symbolMetadata::isAnnotatedWith); }
@Override public void visitMethod(MethodTree tree) { if (tree.block() != null) { blame.add(tree.simpleName().identifierToken()); } super.visitMethod(tree); }
@Nullable private static Set<Type> thrownExceptionsFromBody(MethodTree methodTree) { BlockTree block = methodTree.block(); if (block != null) { MethodInvocationVisitor visitor = new MethodInvocationVisitor(); block.accept(visitor); return visitor.thrownExceptions(); } return null; }
private static boolean isCloneMethod(Tree tree) { if (!tree.is(Kind.METHOD)) { return false; } MethodTree methodTree = (MethodTree) tree; return "clone".equals(methodTree.simpleName().name()) && methodTree.parameters().isEmpty() && methodTree.block() != null; }
private static boolean hasOneAssignementStatement(MethodTree methodTree, ClassTree classTree) { List<StatementTree> body = methodTree.block().body(); return body.size() == 1 && body.get(0).is(Tree.Kind.EXPRESSION_STATEMENT) && ((ExpressionStatementTree) body.get(0)).expression().is(Tree.Kind.ASSIGNMENT) && referencePrivateProperty((AssignmentExpressionTree) ((ExpressionStatementTree) body.get(0)).expression(), classTree); }
@Override public void visitMethod(MethodTree tree) { if (tree.block() == null || tree.is(Tree.Kind.CONSTRUCTOR)) { // skip everything for abstract methods (from interfaces or abstract class) and constructors return; } tree.parameters().stream() .filter(p -> IS_ZIP_ENTRY.test(p.symbol().type())) .forEach(p -> context.reportIssue(this, p, ISSUE_MESSAGE)); super.visitMethod(tree); }
@Override public void visitNode(Tree tree) { MethodTree methodTree = (MethodTree) tree; BlockTree block = methodTree.block(); if (block != null && isRunMethod(methodTree)) { block.accept(new AssertionsVisitor()); } }
@Override public void visitNode(Tree tree) { MethodTree methodTree = (MethodTree) tree; if (!hasSemantic()) { return; } if (methodTree.block() != null && isHasNextMethod(methodTree)) { hasNextBodyVisitor.setHasNextOwner(methodTree.symbol().owner()); methodTree.block().accept(hasNextBodyVisitor); } }
private static boolean isLastStatement(MethodTree methodTree, MethodInvocationTree lastStatementTree) { BlockTree blockTree = methodTree.block(); if (blockTree != null && blockTree.body().stream().anyMatch(statement -> statement.is(Kind.TRY_STATEMENT) && isLastStatement(((TryStatementTree) statement).finallyBlock(), lastStatementTree))) { return true; } return isLastStatement(blockTree, lastStatementTree); }
private static boolean isSetter(MethodTree methodTree) { TypeTree returnType = methodTree.returnType(); BlockTree block = methodTree.block(); boolean returnsVoid = returnType.is(Tree.Kind.PRIMITIVE_TYPE) && "void".equals(((PrimitiveTypeTree) returnType).keyword().text()); boolean hasAtLeastOneStatement = block == null || !block.body().isEmpty(); return methodTree.simpleName().name().startsWith("set") && methodTree.parameters().size() == 1 && returnsVoid && hasAtLeastOneStatement; } }
@Override public void visitMethod(MethodTree tree) { BlockTree blockTree = tree.block(); if (blockTree != null) { checkTokens(getLastTokenFromSignature(tree), blockTree.openBraceToken()); } super.visitMethod(tree); }
@Override public void visitNode(Tree tree) { if (!hasSemantic()) { return; } MethodTree methodTree = (MethodTree) tree; methodTree.block().accept(new ConstructorBodyVisitor(methodTree.symbol().owner())); }
@Override public void visitNode(Tree tree) { MethodTree methodTree = (MethodTree) tree; Symbol.MethodSymbol methodSymbol = methodTree.symbol(); boolean isMethodInJunit3 = isWithinJunit3TestCase(methodSymbol) && isSetUpOrTearDown(methodSymbol); if (isMethodInJunit3 && requiresSuperCall(methodSymbol) && !callSuperOnOverride(methodTree.block(), methodSymbol)) { reportIssue(methodTree.simpleName(), String.format("Add a \"super.%s()\" call to this method.", methodSymbol.name())); } }
@Override public void visitNode(Tree tree) { MethodTree methodTree = (MethodTree) tree; if (hasSemantic() && isIteratorNextMethod(methodTree.symbol()) && methodTree.block() != null) { NextMethodBodyVisitor visitor = new NextMethodBodyVisitor(); tree.accept(visitor); if (!visitor.foundThrow) { reportIssue(methodTree.simpleName(), "Add a \"NoSuchElementException\" for iteration beyond the end of the collection."); } } }
@Override public void visitMethod(MethodTree tree) { //skip return type, and throw clauses : visited in second pass. scan(tree.modifiers()); completeMetadata((JavaSymbol.MethodJavaSymbol) tree.symbol(), tree.modifiers().annotations()); scan(tree.typeParameters()); // revisits the parameters to resolve their annotations. scan(tree.parameters()); scan(tree.defaultValue()); scan(tree.block()); }
@Override public void visitMethod(MethodTree tree) { scan(tree.modifiers()); scan(tree.typeParameters()); scan(tree.returnType()); // skip the simple name scan(tree.parameters()); scan(tree.defaultValue()); scan(tree.throwsClauses()); scan(tree.block()); } }