Search in sources :

Example 51 with Symbol

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

the class ParameterReassignedToCheck method visitAssignmentExpression.

@Override
public void visitAssignmentExpression(AssignmentExpressionTree tree) {
    ExpressionTree variable = tree.variable();
    if (variable.is(Tree.Kind.IDENTIFIER)) {
        IdentifierTree identifier = (IdentifierTree) variable;
        Symbol reference = identifier.symbol();
        if (reference.isVariableSymbol() && variables.contains(reference)) {
            context.reportIssue(this, identifier, "Introduce a new variable instead of reusing the parameter \"" + identifier.name() + "\".");
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 52 with Symbol

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

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

the class MainInServletCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    ClassTree node = (ClassTree) tree;
    Symbol.TypeSymbol symbol = node.symbol();
    if (isServletOrEjb(symbol)) {
        for (Tree member : node.members()) {
            if (member.is(Tree.Kind.METHOD) && ((MethodTreeImpl) member).isMainMethod()) {
                reportIssue(((MethodTree) member).simpleName(), "Remove this unwanted \"main\" method.");
            }
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 54 with Symbol

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

the class VariableReadExtractor method visitIdentifier.

@Override
public void visitIdentifier(IdentifierTree tree) {
    Symbol owner = tree.symbol().owner();
    if (methodSymbol.equals(owner) || (includeFields && isField(tree.symbol(), methodSymbol.owner()))) {
        used.add(tree.symbol());
    }
    super.visitIdentifier(tree);
}
Also used : MethodSymbol(org.sonar.plugins.java.api.semantic.Symbol.MethodSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 55 with Symbol

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

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