private void checkAnnotations(ModifiersTree modifiers, List<String> annotations) { modifiers.annotations().stream() .filter(annotationTree -> annotations.stream().anyMatch(annotationTree.symbolType()::is)) .forEach(this::reportIssue); }
private static Optional<AnnotationTree> findRequestMappingAnnotation(ModifiersTree modifiers) { return modifiers.annotations().stream() .filter(annotation -> annotation.symbolType().is(REQUEST_MAPPING_CLASS)) .findFirst(); }
private static boolean classHasNoAutoValueOrImmutableAnnotation(ClassTree tree) { return tree.modifiers().annotations().stream() .map(AnnotationTree::annotationType) .map(TypeTree::symbolType) .noneMatch(type -> type.is("com.google.auto.value.AutoValue") || type.is("org.immutables.value.Value$Immutable")); } }
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")); }
public boolean isAnnotatedOverride() { for (AnnotationTree annotationTree : modifiers.annotations()) { if (isJavaLangOverride(annotationTree.annotationType())) { return true; } } return false; }
@Override public void visitNode(Tree tree) { if (!hasSemantic()) { return; } ((ClassTree) tree).modifiers().annotations().stream() .filter(SpringAutoConfigurationCheck::isAutoConfiguration) .filter(annotation -> !hasExclude(annotation.arguments())) .forEach(annotation -> reportIssue(annotation, "Exclude from the auto-configuration mechanism the beans you don't need.")); }
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 boolean hasTransactionalAnnotation(MethodTree method) { for (AnnotationTree annotation : method.modifiers().annotations()) { if (annotation.symbolType().is("org.springframework.transaction.annotation.Transactional")) { return true; } } return false; }
@Override public void visitClass(ClassTree tree) { handleSuppressWarning(tree.modifiers().annotations(), tree); super.visitClass(tree); }
@Override public void visitVariable(VariableTree tree) { handleSuppressWarning(tree.modifiers().annotations(), tree); super.visitVariable(tree); }
private static boolean hasOverrideAnnotation(MethodTree method) { for (AnnotationTree annotationTree : method.modifiers().annotations()) { Tree annotationType = annotationTree.annotationType(); if (annotationType.is(Tree.Kind.IDENTIFIER) && "Override".equals(((IdentifierTree) annotationType).name())) { return true; } } return false; }
private void checkNullableAnnotation(ModifiersTree modifiers, String message) { for (AnnotationTree annotation : modifiers.annotations()) { Type type = annotation.annotationType().symbolType(); if (type.is(NULLABLE)) { context.reportIssue(this, annotation, message); } } }
@Override public void visitEnumConstant(EnumConstantTree tree) { // JLS-8.9.3 int flags = Flags.PUBLIC | Flags.STATIC | Flags.FINAL | Flags.ENUM; if (hasDeprecatedAnnotation(tree.modifiers().annotations())) { flags |= Flags.DEPRECATED; } declareVariable(flags, tree.simpleName(), (VariableTreeImpl) tree); super.visitEnumConstant(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 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; } }
@Override public void visitVariable(VariableTree tree) { ModifiersTree modifiers = tree.modifiers(); List<AnnotationTree> annotations = modifiers.annotations(); if (isClass() && isPublic(modifiers) && !(isFinal(modifiers) || !annotations.isEmpty())) { context.reportIssue(this, tree.simpleName(), "Make " + tree.simpleName() + " a static final constant or non-public and provide accessors if needed."); } super.visitVariable(tree); }
private void checkParameter(VariableTree parameter, Symbol overrideeParamSymbol) { Tree reportTree = parameter; if (nonNullVsNull(parameter.symbol(), overrideeParamSymbol)) { for (AnnotationTree annotationTree : parameter.modifiers().annotations()) { if(annotationTree.symbolType().is(JAVAX_ANNOTATION_NONNULL)) { reportTree = annotationTree; } } reportIssue(reportTree, "Remove this \"Nonnull\" annotation to honor the overridden method's contract."); } }
@Override public void visitNode(Tree tree) { MethodTree methodTree = (MethodTree) tree; if (isSingleStatementMethod(methodTree) && isUselessSuperCall(methodTree) && !hasAnnotationDifferentFromOverride(methodTree.modifiers().annotations()) && !isFinalObjectMethod(methodTree)) { addIssue(methodTree, "Remove this method to simply inherit it."); } }
@Override public void visitClass(ClassTree tree) { //skip superclass and interfaces : visited in second pass. scan(tree.modifiers()); completeMetadata((JavaSymbol) tree.symbol(), tree.modifiers().annotations()); scan(tree.typeParameters()); scan(tree.members()); }
@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()); }