private static List<Type> getTypes(ListTree<TypeTree> superInterfaces) { List<Type> types = new ArrayList<>(superInterfaces.size()); for (TypeTree superInterface : superInterfaces) { types.add(superInterface.symbolType()); } return types; }
private static boolean isCompatibleWithThrows(Type exceptionType, List<TypeTree> throwsClauses) { for (TypeTree typeTree : throwsClauses) { if (exceptionType.isSubtypeOf(typeTree.symbolType())) { return true; } } return false; }
private static List<Type> getCatchedExceptions(List<CatchTree> catches) { List<Type> result = new ArrayList<>(); for (CatchTree element : catches) { result.add(element.parameter().type().symbolType()); } return result; }
private void handleClassTree(ClassTree tree) { tree.superInterfaces().stream() .filter(superInterface -> INTERFACES.stream().anyMatch(superInterface.symbolType()::is)) .forEach(this::reportIssue); TypeTree superClass = tree.superClass(); if (superClass != null && superClass.symbolType().is(GLOBAL_METHOD_SECURITY_CONFIGURATION)) { reportIssue(superClass); } checkAnnotations(tree.modifiers(), JSR_250_ANNOTATIONS); }
private void checkScopeAnnotation(ClassTree tree) { tree.modifiers().annotations().stream() .filter(a -> a.annotationType().symbolType().fullyQualifiedName().equals(SCOPE_ANNOTATION_FQN)) .forEach(a -> reportIssue(a, "Remove this \"@Scope\" annotation.")); }
private static Tree toReportTree(Tree member) { Stream<AnnotationTree> stream = Stream.empty(); if (member.is(Kind.VARIABLE)) { stream = ((VariableTree) member).modifiers().annotations().stream(); } else if (member.is(Kind.METHOD)) { stream = ((MethodTree) member).modifiers().annotations().stream(); } return stream .filter(a -> a.annotationType().symbolType().is("org.springframework.beans.factory.annotation.Autowired")) .findFirst() .orElseThrow(() -> new IllegalStateException("Mapping a tree to something unexpected")); }
private void checkExtendsObject(ClassTree classTree) { TypeTree superClassTree = classTree.superClass(); if (superClassTree != null && superClassTree.symbolType().is("java.lang.Object")) { reportIssue(superClassTree, "\"Object\" should not be explicitly extended."); } }
@Override public void visitNode(Tree tree) { if (!hasSemantic()) { return; } MethodTree methodTree = (MethodTree) tree; TypeTree returnType = methodTree.returnType(); if (returnType.symbolType().isPrimitive()) { findNullableAnnotation(methodTree) .ifPresent(annotation -> reportIssue(returnType, String.format("\"%s\" annotation should not be used on primitive types", annotation))); } }
private void checkConfigurationFactoryExtension(ClassTree tree) { TypeTree superClass = tree.superClass(); if (superClass != null && superClass.symbolType().is("org.apache.logging.log4j.core.config.ConfigurationFactory")) { reportIssue(superClass, MESSAGE); } }
@Override public boolean apply(MethodTree methodTree) { return methodTree.simpleName().name().startsWith(getStartName()) && methodTree.parameters().isEmpty() && !methodTree.returnType().symbolType().is("void"); } };
private void visitInstanceOf(InstanceOfTree instanceOfTree) { ExpressionTree expression = instanceOfTree.expression(); if (expression.is(Tree.Kind.IDENTIFIER) && instanceOfTree.type().symbolType().is("java.io.Serializable")) { testedSymbols.add(((IdentifierTree) expression).symbol()); } }
@Override public void visitClass(ClassTree tree) { TypeTree superClass = tree.superClass(); if (superClass != null && superClass.symbolType().is(JAVA_LANG_THREADGROUP)) { context.reportIssue(this, superClass, MESSAGE); } super.visitClass(tree); }
@Override public void visitVariable(VariableTree tree) { Type varType = tree.type().symbolType(); checkExpression(varType, tree.initializer()); super.visitVariable(tree); }
private static boolean isAllowingNull(MethodTree methodTree) { for (AnnotationTree annotation : methodTree.modifiers().annotations()) { Type type = annotation.annotationType().symbolType(); if (type.is("javax.annotation.Nullable") || type.is("javax.annotation.CheckForNull")) { return true; } } return false; } }
private static boolean isSpringWebHandler(AnnotationTree annotationTree) { Type annotationType = annotationTree.annotationType().symbolType(); return annotationType.is(REQUEST_MAPPING_ANNOTATION) || annotationType.symbol().metadata().isAnnotatedWith(REQUEST_MAPPING_ANNOTATION); } }
@Override public void visitVariable(VariableTree variable) { if (variable.type().symbolType().is(OPTIONAL)) { checkNullableAnnotation(variable.modifiers(), "\"Optional\" variables should not be \"@Nullable\"."); } super.visitVariable(variable); }
@Override public void visitMethod(MethodTree methodTree) { if (methodTree.returnType() != null ) { String returnTypeName = methodTree.returnType().symbolType().fullyQualifiedName(); checkIfDisallowed(returnTypeName, methodTree.returnType()); } super.visitMethod(methodTree); }
@Override public void visitNewClass(NewClassTree tree) { if (isUnorderedMap(tree.symbolType()) && hasEnumKey(tree.identifier().symbolType())) { addIssue(tree); } else { super.visitNewClass(tree); } }
@Override public void visitNode(Tree tree) { InstanceOfTree instanceOfTree = (InstanceOfTree) tree; Type expressionType = instanceOfTree.expression().symbolType(); Type instanceOf = instanceOfTree.type().symbolType(); if (expressionType.isSubtypeOf(instanceOf) && !instanceOfTree.expression().is(Tree.Kind.NULL_LITERAL)) { reportIssue(instanceOfTree.instanceofKeyword(), "Remove this useless \"instanceof\" operator; it will always return \"true\". "); } } }
@Override public void visitForEachStatement(ForEachStatement tree) { scan(tree.variable()); scan(tree.expression()); TypeTree typeTree = tree.variable().type(); if (typeTree.is(Tree.Kind.VAR_TYPE)) { JavaType iteratedObjectType = getIteratedObjectType((JavaType) tree.expression().symbolType()); setInferedType(upwardProjection(iteratedObjectType), (DeferredType) typeTree.symbolType()); } // scan the body only after handling type of variable scan(tree.statement()); }