Search in sources :

Example 61 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class ConfusingOverloadCheck method checkMethod.

private boolean checkMethod(Tree reportTree, Symbol.MethodSymbol methodSymbol, Type superClass) {
    boolean reportStaticIssue = false;
    for (Symbol methodWithSameName : superClass.symbol().lookupSymbols(methodSymbol.name())) {
        if (methodWithSameName.isMethodSymbol()) {
            if (hideStaticMethod(methodSymbol, superClass, methodWithSameName)) {
                reportIssue(reportTree, "Rename this method or make it \"static\".");
                reportStaticIssue = true;
            } else if (confusingOverload(methodSymbol, (Symbol.MethodSymbol) methodWithSameName)) {
                reportIssue(reportTree, getMessage(methodWithSameName));
            }
        }
    }
    return reportStaticIssue;
}
Also used : MethodJavaSymbol(org.sonar.java.resolve.JavaSymbol.MethodJavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 62 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol 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 63 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class CollectionMethodsWithLinearComplexityCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    MethodInvocationTree mit = (MethodInvocationTree) tree;
    matcherActualTypeMap.forEach((methodMatcher, actualTypes) -> {
        if (methodMatcher.matches(mit) && invocationInMethod(mit)) {
            Symbol target = invocationTarget(mit);
            if (target != null && isField(target) && matchesActualType(target, actualTypes)) {
                IdentifierTree methodName = ExpressionUtils.methodName(mit);
                reportIssue(methodName, "This call to \"" + methodName.name() + "()\" may be a performance hot spot if the collection is large.");
            }
        }
    });
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 64 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol 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 65 with Symbol

use of org.sonar.plugins.java.api.semantic.Symbol in project sonar-java by SonarSource.

the class CallSuperInTestCaseCheck method requiresSuperCall.

private static boolean requiresSuperCall(Symbol.MethodSymbol methodSymbol) {
    Type superType = methodSymbol.owner().type().symbol().superClass();
    Collection<Symbol> symbols = Lists.newArrayList();
    while (superType != null && !superType.is(JUNIT_FRAMEWORK_TEST_CASE) && symbols.isEmpty()) {
        symbols = superType.symbol().lookupSymbols(methodSymbol.name());
        superType = superType.symbol().superClass();
    }
    return !symbols.isEmpty() && !symbols.iterator().next().owner().type().is(JUNIT_FRAMEWORK_TEST_CASE);
}
Also used : Type(org.sonar.plugins.java.api.semantic.Type) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Aggregations

Symbol (org.sonar.plugins.java.api.semantic.Symbol)140 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)47 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)41 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)33 Tree (org.sonar.plugins.java.api.tree.Tree)32 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)30 Test (org.junit.Test)29 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)28 JavaSymbol (org.sonar.java.resolve.JavaSymbol)27 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)26 Type (org.sonar.plugins.java.api.semantic.Type)24 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)24 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)23 List (java.util.List)19 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)17 Collectors (java.util.stream.Collectors)14 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)13 Set (java.util.Set)12 ImmutableList (com.google.common.collect.ImmutableList)11 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)11