Search in sources :

Example 6 with AnnotationTree

use of org.sonar.plugins.java.api.tree.AnnotationTree in project sonar-java by SonarSource.

the class ClassVariableVisibilityCheck method visitVariable.

@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);
}
Also used : ModifiersTree(org.sonar.plugins.java.api.tree.ModifiersTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree)

Example 7 with AnnotationTree

use of org.sonar.plugins.java.api.tree.AnnotationTree in project sonar-java by SonarSource.

the class TreeFactory method newAnnotatedParameterizedIdentifier.

public ExpressionTree newAnnotatedParameterizedIdentifier(Optional<List<AnnotationTreeImpl>> annotations, InternalSyntaxToken identifierToken, Optional<TypeArgumentListTreeImpl> typeArguments) {
    List<AnnotationTree> annotationList = ImmutableList.copyOf(annotations.or(ImmutableList.of()));
    ExpressionTree result = new IdentifierTreeImpl(identifierToken);
    if (typeArguments.isPresent()) {
        result = new ParameterizedTypeTreeImpl((TypeTree) result, typeArguments.get()).complete(annotationList);
    } else {
        result = ((IdentifierTreeImpl) result).complete(annotationList);
    }
    return result;
}
Also used : IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) ParameterizedTypeTreeImpl(org.sonar.java.model.JavaTree.ParameterizedTypeTreeImpl)

Example 8 with AnnotationTree

use of org.sonar.plugins.java.api.tree.AnnotationTree in project sonar-java by SonarSource.

the class AnnotationDefaultArgumentCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    AnnotationTree annotationTree = (AnnotationTree) tree;
    TypeSymbol annotationSymbol = annotationTree.symbolType().symbol();
    if (annotationSymbol.isUnknown()) {
        return;
    }
    Map<String, Object> defaultValueByName = annotationSymbol.memberSymbols().stream().filter(Symbol::isMethodSymbol).map(s -> (JavaSymbol.MethodJavaSymbol) s).filter(s -> s.defaultValue() != null).collect(Collectors.toMap(Symbol::name, JavaSymbol.MethodJavaSymbol::defaultValue));
    for (ExpressionTree argument : annotationTree.arguments()) {
        Tree valueSet = argument;
        // Single element annotation : JLS8 9.7.3 : one param must be named value.
        String paramName = "value";
        if (argument.is(Tree.Kind.ASSIGNMENT)) {
            AssignmentExpressionTree assignmentTree = (AssignmentExpressionTree) argument;
            IdentifierTree nameTree = (IdentifierTree) assignmentTree.variable();
            paramName = nameTree.name();
            valueSet = assignmentTree.expression();
        }
        if (setValueIsSameAsDefaultValue(defaultValueByName.get(paramName), valueSet)) {
            reportIssue(argument, String.format("Remove this default value assigned to parameter \"%s\".", paramName));
        }
    }
}
Also used : TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LiteralUtils(org.sonar.java.model.LiteralUtils) Map(java.util.Map) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) Nullable(javax.annotation.Nullable) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) JavaSymbol(org.sonar.java.resolve.JavaSymbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) TypeSymbol(org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol)

Example 9 with AnnotationTree

use of org.sonar.plugins.java.api.tree.AnnotationTree in project sonar-java by SonarSource.

the class ChangeMethodContractCheck method checkContractChange.

private void checkContractChange(MethodTreeImpl methodTree, Symbol.MethodSymbol overridee) {
    if (methodTree.isEqualsMethod() && methodTree.parameters().get(0).symbol().metadata().isAnnotatedWith(JAVAX_ANNOTATION_NONNULL)) {
        reportIssue(methodTree.parameters().get(0), "Equals method should accept null parameters and return false.");
        return;
    }
    for (int i = 0; i < methodTree.parameters().size(); i++) {
        VariableTree parameter = methodTree.parameters().get(i);
        Symbol overrideeParamSymbol = ((JavaSymbol.MethodJavaSymbol) overridee).getParameters().scopeSymbols().get(i);
        checkParameter(parameter, overrideeParamSymbol);
    }
    if (nonNullVsNull(overridee, methodTree.symbol())) {
        for (AnnotationTree annotationTree : methodTree.modifiers().annotations()) {
            if (annotationTree.symbolType().is(JAVAX_ANNOTATION_NULLABLE) || annotationTree.symbolType().is(JAVAX_ANNOTATION_CHECK_FOR_NULL)) {
                reportIssue(annotationTree, "Remove this \"" + annotationTree.symbolType().name() + "\" annotation to honor the overridden method's contract.");
            }
        }
    }
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree)

Example 10 with AnnotationTree

use of org.sonar.plugins.java.api.tree.AnnotationTree in project sonar-java by SonarSource.

the class SuppressWarningsCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    AnnotationTree annotationTree = (AnnotationTree) tree;
    List<String> ruleWarnings = getAllowedWarnings();
    if (isJavaLangSuppressWarnings(annotationTree)) {
        if (ruleWarnings.isEmpty()) {
            reportIssue(annotationTree.annotationType(), "Suppressing warnings is not allowed");
        } else {
            List<String> suppressedWarnings = getSuppressedWarnings(annotationTree.arguments().get(0));
            List<String> issues = suppressedWarnings.stream().filter(currentWarning -> !ruleWarnings.contains(currentWarning)).collect(Collectors.toList());
            if (!issues.isEmpty()) {
                StringBuilder sb = new StringBuilder("Suppressing the '").append(Joiner.on(", ").join(issues)).append("' warning").append(issues.size() > 1 ? "s" : "").append(" is not allowed");
                reportIssue(annotationTree.annotationType(), sb.toString());
            }
        }
    }
}
Also used : LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) StringUtils(org.apache.commons.lang.StringUtils) Kind(org.sonar.plugins.java.api.tree.Tree.Kind) RuleProperty(org.sonar.check.RuleProperty) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) List(java.util.List) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) LiteralUtils(org.sonar.java.model.LiteralUtils) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) Splitter(com.google.common.base.Splitter) Joiner(com.google.common.base.Joiner) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree)

Aggregations

AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)13 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)5 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)5 Symbol (org.sonar.plugins.java.api.semantic.Symbol)3 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)3 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)3 ImmutableList (com.google.common.collect.ImmutableList)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Test (org.junit.Test)2 Rule (org.sonar.check.Rule)2 LiteralUtils (org.sonar.java.model.LiteralUtils)2 JavaSymbol (org.sonar.java.resolve.JavaSymbol)2 IssuableSubscriptionVisitor (org.sonar.plugins.java.api.IssuableSubscriptionVisitor)2 TypeSymbol (org.sonar.plugins.java.api.semantic.Symbol.TypeSymbol)2 LiteralTree (org.sonar.plugins.java.api.tree.LiteralTree)2 NewArrayTree (org.sonar.plugins.java.api.tree.NewArrayTree)2 Tree (org.sonar.plugins.java.api.tree.Tree)2 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)2 Joiner (com.google.common.base.Joiner)1