Search in sources :

Example 86 with Symbol

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

the class CollectionCallingItselfCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        MethodInvocationTree methodInvocationTree = (MethodInvocationTree) tree;
        Symbol symbolReference = null;
        Symbol method = null;
        String reportedName = "";
        if (methodInvocationTree.methodSelect().is(Tree.Kind.MEMBER_SELECT)) {
            MemberSelectExpressionTree mse = (MemberSelectExpressionTree) methodInvocationTree.methodSelect();
            IdentifierTree identifier = mse.identifier();
            reportedName = identifier.name();
            method = identifier.symbol();
            if (mse.expression().is(Tree.Kind.IDENTIFIER)) {
                symbolReference = ((IdentifierTree) mse.expression()).symbol();
            }
        }
        if (symbolReference != null && isMethodFromCollection(method)) {
            reportIssueForParameters(methodInvocationTree, symbolReference, reportedName);
        }
    }
}
Also used : MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) 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 87 with Symbol

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

the class ClassWithoutHashCodeInHashStructureCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    Type type = ((NewClassTree) tree).symbolType();
    if (type instanceof ParametrizedTypeJavaType && useHashDataStructure(type)) {
        ParametrizedTypeJavaType ptt = (ParametrizedTypeJavaType) type;
        Symbol.TypeSymbol symbol = ptt.substitution(ptt.typeParameters().get(0)).symbol();
        if (implementsEquals(symbol) && !implementsHashCode(symbol)) {
            reportIssue(tree, "Add a \"hashCode()\" method to \"" + symbol.name() + "\" or remove it from this hash.");
        }
    }
}
Also used : ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) Type(org.sonar.plugins.java.api.semantic.Type) ParametrizedTypeJavaType(org.sonar.java.resolve.ParametrizedTypeJavaType) Symbol(org.sonar.plugins.java.api.semantic.Symbol) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Example 88 with Symbol

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

the class CallToDeprecatedMethodCheck method checkIdentifierIssue.

private void checkIdentifierIssue(IdentifierTree identifierTree) {
    if (isSimpleNameOfVariableTreeOrVariableIsDeprecated(identifierTree)) {
        return;
    }
    Symbol symbol = identifierTree.symbol();
    if (isDeprecated(symbol)) {
        String name;
        if (isConstructor(symbol)) {
            name = symbol.owner().name();
        } else {
            name = symbol.name();
        }
        reportIssue(identifierTree, "Remove this use of \"" + name + "\"; it is deprecated.");
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 89 with Symbol

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

the class AssertsOnParametersOfPublicMethodCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    if (!methodTree.symbol().isPublic()) {
        return;
    }
    methodTree.parameters().stream().map(VariableTree::symbol).map(Symbol::usages).flatMap(List::stream).forEach(parameter -> checkUsage(parameter, methodTree));
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 90 with Symbol

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

the class ChildClassShadowFieldCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    TypeTree superClass = ((ClassTree) tree).superClass();
    if (superClass != null) {
        Symbol.TypeSymbol superclassSymbol = superClass.symbolType().symbol();
        ((ClassTree) tree).members().stream().filter(m -> m.is(Kind.VARIABLE)).map(VariableTree.class::cast).map(VariableTree::simpleName).forEach(fieldSimpleName -> {
            if (!IGNORED_FIELDS.contains(fieldSimpleName.name())) {
                checkForIssue(superclassSymbol, fieldSimpleName);
            }
        });
    }
}
Also used : TypeTree(org.sonar.plugins.java.api.tree.TypeTree) ImmutableSet(com.google.common.collect.ImmutableSet) Kind(org.sonar.plugins.java.api.tree.Tree.Kind) Set(java.util.Set) Tree(org.sonar.plugins.java.api.tree.Tree) Type(org.sonar.plugins.java.api.semantic.Type) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) CheckForNull(javax.annotation.CheckForNull) Symbol(org.sonar.plugins.java.api.semantic.Symbol) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree)

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