Search in sources :

Example 31 with VariableTree

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

the class LeastSpecificTypeCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    Symbol.MethodSymbol methodSymbol = methodTree.symbol();
    if (!methodSymbol.isPublic() || !Boolean.FALSE.equals(methodTree.isOverriding()) || isOverloaded(methodSymbol)) {
        return;
    }
    boolean springInjectionAnnotated = isSpringInjectionAnnotated(methodSymbol.metadata());
    methodTree.parameters().stream().map(VariableTree::symbol).filter(p -> p.type().isClass() && !p.type().symbol().isEnum() && !p.type().is("java.lang.String")).filter(p -> !(springInjectionAnnotated && p.type().is("java.util.Collection"))).forEach(p -> handleParameter(p, springInjectionAnnotated));
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Iterator(java.util.Iterator) Set(java.util.Set) Tree(org.sonar.plugins.java.api.tree.Tree) Type(org.sonar.plugins.java.api.semantic.Type) Collectors(java.util.stream.Collectors) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) BooleanUtils(org.apache.commons.lang.BooleanUtils) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) JavaType(org.sonar.java.resolve.JavaType) Lists(com.google.common.collect.Lists) ClassJavaType(org.sonar.java.resolve.ClassJavaType) SymbolMetadata(org.sonar.plugins.java.api.semantic.SymbolMetadata) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Optional(java.util.Optional) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) Comparator(java.util.Comparator) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Collections(java.util.Collections) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 32 with VariableTree

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

the class PublicApiCheckerTest method getPublicApiVisitor.

private SubscriptionVisitor getPublicApiVisitor() {
    return new SubscriptionVisitor() {

        private final Deque<ClassTree> classTrees = Lists.newLinkedList();

        private final Deque<MethodTree> methodTrees = Lists.newLinkedList();

        @Override
        public List<Tree.Kind> nodesToVisit() {
            return Arrays.asList(Tree.Kind.values());
        }

        @Override
        public void visitNode(Tree tree) {
            if (tree.is(Tree.Kind.VARIABLE)) {
                VariableTree variableTree = (VariableTree) tree;
                String name = variableTree.simpleName().name();
                Tree parent = classTrees.peek();
                if (!methodTrees.isEmpty()) {
                    parent = methodTrees.peek();
                }
                assertThat(PublicApiChecker.isPublicApi(parent, tree)).as(name).isEqualTo(name.endsWith("Public"));
            } else if (tree.is(PublicApiChecker.methodKinds())) {
                MethodTree methodTree = (MethodTree) tree;
                methodTrees.push(methodTree);
                String name = methodTree.simpleName().name();
                // getters and setters are included in the public API
                assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).as(name).isEqualTo(name.endsWith("Public") || name.contains("GetSet"));
            } else if (tree.is(PublicApiChecker.classKinds())) {
                IdentifierTree className = ((ClassTree) tree).simpleName();
                if (className == null) {
                    assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).isFalse();
                } else {
                    assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).as(className.name()).isEqualTo(className != null && className.name().endsWith("Public"));
                }
                classTrees.push((ClassTree) tree);
            } else {
                assertThat(PublicApiChecker.isPublicApi(classTrees.peek(), tree)).isFalse();
            }
        }

        @Override
        public void leaveNode(Tree tree) {
            if (tree.is(PublicApiChecker.classKinds())) {
                classTrees.pop();
            } else if (tree.is(PublicApiChecker.methodKinds())) {
                methodTrees.pop();
            }
        }
    };
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Deque(java.util.Deque)

Example 33 with VariableTree

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

the class CompareToResultTestCheck method isIdentifierContainingCompareToResult.

private static boolean isIdentifierContainingCompareToResult(IdentifierTree identifier) {
    Symbol variableSymbol = identifier.symbol();
    if (!variableSymbol.isVariableSymbol()) {
        return false;
    }
    VariableTree variableDefinition = ((Symbol.VariableSymbol) variableSymbol).declaration();
    if (variableDefinition != null) {
        ExpressionTree initializer = variableDefinition.initializer();
        if (initializer != null && initializer.is(Tree.Kind.METHOD_INVOCATION) && variableSymbol.owner().isMethodSymbol()) {
            MethodTree method = ((Symbol.MethodSymbol) variableSymbol.owner()).declaration();
            return method != null && COMPARE_TO.matches((MethodInvocationTree) initializer) && !isReassigned(variableSymbol, method);
        }
    }
    return false;
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) UnaryExpressionTree(org.sonar.plugins.java.api.tree.UnaryExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree)

Example 34 with VariableTree

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

the class CollectionMethodsWithLinearComplexityCheck method findAssignedTypes.

private static Set<String> findAssignedTypes(Symbol symbol) {
    Set<String> types = new HashSet<>();
    Tree declaration = symbol.declaration();
    if (declaration != null && declaration.is(Tree.Kind.VARIABLE)) {
        ExpressionTree initializer = ((VariableTree) declaration).initializer();
        if (initializer != null) {
            types.add(initializer.symbolType().fullyQualifiedName());
        }
    }
    symbol.usages().stream().flatMap(CollectionMethodsWithLinearComplexityCheck::usageInAssignment).map(assignment -> assignment.expression().symbolType().fullyQualifiedName()).forEach(types::add);
    return types;
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) TypeCriteria(org.sonar.java.matcher.TypeCriteria) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) ExpressionUtils(org.sonar.java.model.ExpressionUtils) Tree(org.sonar.plugins.java.api.tree.Tree) Type(org.sonar.plugins.java.api.semantic.Type) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) HashSet(java.util.HashSet) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) Stream(java.util.stream.Stream) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) Map(java.util.Map) MethodMatcher(org.sonar.java.matcher.MethodMatcher) 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) Collections(java.util.Collections) CheckForNull(javax.annotation.CheckForNull) Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) HashSet(java.util.HashSet)

Example 35 with VariableTree

use of org.sonar.plugins.java.api.tree.VariableTree 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)

Aggregations

VariableTree (org.sonar.plugins.java.api.tree.VariableTree)86 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)32 Tree (org.sonar.plugins.java.api.tree.Tree)32 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)29 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)27 Test (org.junit.Test)25 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)22 Symbol (org.sonar.plugins.java.api.semantic.Symbol)18 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)18 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)17 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)16 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)13 List (java.util.List)12 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)12 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)12 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)11 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)11 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)10 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9