@Override public void visitNode(Tree tree) { MethodTree methodTree = (MethodTree) tree; if (isNotOverriden(methodTree) && !pattern.matcher(methodTree.simpleName().name()).matches()) { reportIssue(methodTree.simpleName(), "Rename this method name to match the regular expression '" + format + "'."); } }
private static boolean isExcluded(MethodTree methodTree) { String name = methodTree.simpleName().name(); if ("equals".equals(name)) { return methodTree.parameters().size() == 1; } else if ("hashCode".equals(name)) { return methodTree.parameters().isEmpty(); } return false; }
@Override public void leaveNode(Tree tree) { if (isCloneMethod(tree) && !foundSuperClone) { reportIssue(((MethodTree) tree).simpleName(), "Use super.clone() to create and seed the cloned instance to be returned."); } }
private void report(MethodInvariantContext methodInvariantContext) { Flow.Builder flowBuilder = Flow.builder(); methodInvariantContext.returnStatementTrees.stream().map(r -> new JavaFileScannerContext.Location("", r)).forEach(flowBuilder::add); reportIssue( methodInvariantContext.methodTree.simpleName(), "Refactor this method to not always return the same value.", Collections.singleton(flowBuilder.build())); }
@Override public void visitNode(Tree tree) { if (!hasSemantic()) { return; } MethodTree methodTree = (MethodTree) tree; List<AnnotationTree> annotations = methodTree.modifiers().annotations(); annotations.stream().filter(SpringEndpointsCheck::isSpringWebHandler).forEach(annotationTree -> reportIssue(methodTree.simpleName(), MESSAGE)); }
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 void reportIssueOnMethod(@Nullable MethodTree declaration, Symbol.TypeSymbol classSymbol) { if (declaration != null) { String message = "Move this method into "; if (classSymbol.name().isEmpty()) { message += "the anonymous class declared at line " + ((JavaTree) classSymbol.declaration()).getLine()+"."; } else { message += "\"" + classSymbol.name() + "\"."; } reportIssue(declaration.simpleName(), message); } }
private static boolean isFinalize(MethodTree methodTree) { if ("finalize".equals(methodTree.simpleName().name())) { Tree returnType = methodTree.returnType(); if (returnType != null && returnType.is(Tree.Kind.PRIMITIVE_TYPE)) { return "void".equals(((PrimitiveTypeTree) returnType).keyword().text()); } } return false; }
@Override public void visitMethod(MethodTree tree) { visitNode(tree, tree.simpleName(), tree.symbol().metadata()); super.visitMethod(tree); currentParents.pop(); }
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 boolean apply(MethodTree methodTree) { return methodTree.simpleName().name().startsWith(getStartName()) && methodTree.parameters().isEmpty() && !methodTree.returnType().symbolType().is("void"); } };
private void checkVariable(CheckerContext context, MethodTree tree, final Symbol symbol) { String nonNullAnnotation = nonNullAnnotation(symbol); if (nonNullAnnotation == null || symbol.isStatic()) { return; } if (isUndefinedOrNull(context, symbol)) { context.reportIssue(tree.simpleName(), this, MessageFormat.format("\"{0}\" is marked \"{1}\" but is not initialized in this constructor.", symbol.name(), nonNullAnnotation)); } }
private void checkMethod(MethodTree methodTree) { BlockTree block = methodTree.block(); if (block != null && isEmpty(block) && !containsComment(block)) { reportIssue(methodTree.simpleName(), "Add a nested comment explaining why this method is empty, throw an UnsupportedOperationException or complete the implementation."); } }
private void checkNotStatic(MethodTree methodTree) { Symbol.MethodSymbol methodSymbol = methodTree.symbol(); if (methodSymbol.isStatic()) { reportIssue(methodTree.simpleName(), "The \"static\" modifier should not be applied to \"" + methodSymbol.name() + "\"."); } }
private void checkPrivate(MethodTree methodTree) { Symbol.MethodSymbol methodSymbol = methodTree.symbol(); if (!methodSymbol.isPrivate()) { reportIssue(methodTree.simpleName(), "Make \"" + methodSymbol.name() + "\" \"private\"."); } }
@Override public void visitNode(Tree tree) { ClassTree node = (ClassTree) tree; Symbol.TypeSymbol symbol = node.symbol(); if (isServletOrEjb(symbol)) { for (Tree member : node.members()) { if (member.is(Tree.Kind.METHOD) && ((MethodTreeImpl) member).isMainMethod()) { reportIssue(((MethodTree) member).simpleName(), "Remove this unwanted \"main\" method."); } } } }
@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) { List<IdentifierTree> usages = tree.symbol().usages(); createSymbol(tree.simpleName(), usages); for (TypeParameterTree typeParameterTree : tree.typeParameters()) { createSymbol(typeParameterTree.identifier(), typeParameterTree); } super.visitMethod(tree); }
private static int getDeclarationLine(Symbol symbol) { if (symbol.declaration() == null) { return -1; } if (symbol.isVariableSymbol()) { return ((Symbol.VariableSymbol) symbol).declaration().simpleName().identifierToken().line(); } return ((Symbol.MethodSymbol) symbol).declaration().simpleName().identifierToken().line(); }
@Override public void visitMethod(MethodTree tree) { scan(tree.modifiers()); scan(tree.typeParameters()); scan(tree.returnType()); scan(tree.simpleName()); scan(tree.parameters()); scan(tree.defaultValue()); scan(tree.throwsClauses()); scan(tree.block()); }