Search in sources :

Example 26 with Symbol

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

the class UnusedPrivateFieldCheck method checkIfUnused.

public void checkIfUnused(VariableTree tree) {
    if (hasNoAnnotation(tree)) {
        Symbol symbol = tree.symbol();
        String name = symbol.name();
        if (symbol.isPrivate() && onlyUsedInVariableAssignment(symbol) && !"serialVersionUID".equals(name) && !unknownIdentifiers.contains(name)) {
            reportIssue(tree.simpleName(), "Remove this unused \"" + name + "\" private field.");
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 27 with Symbol

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

the class UnusedTypeParameterCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (hasSemantic()) {
        TypeParameters typeParameters;
        String messageEnd;
        if (tree.is(Tree.Kind.METHOD)) {
            typeParameters = ((MethodTree) tree).typeParameters();
            messageEnd = "method.";
        } else {
            typeParameters = ((ClassTree) tree).typeParameters();
            messageEnd = "class.";
            if (tree.is(Tree.Kind.INTERFACE)) {
                messageEnd = "interface.";
            }
        }
        for (TypeParameterTree typeParameter : typeParameters) {
            Symbol symbol = semanticModel.getSymbol(typeParameter);
            if (symbol.usages().isEmpty()) {
                String message = new StringBuilder(typeParameter.identifier().name()).append(" is not used in the ").append(messageEnd).toString();
                reportIssue(typeParameter.identifier(), message);
            }
        }
    }
}
Also used : TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) TypeParameters(org.sonar.plugins.java.api.tree.TypeParameters) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 28 with Symbol

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

the class FieldNameMatchingTypeNameCheck method visitClass.

@Override
public void visitClass(ClassTree tree) {
    IdentifierTree simpleName = tree.simpleName();
    if (simpleName != null) {
        Symbol.TypeSymbol classSymbol = tree.symbol();
        Collection<Symbol> members = classSymbol.memberSymbols();
        for (Symbol sym : members) {
            if (sym.isVariableSymbol() && !staticFieldSameType(classSymbol, sym)) {
                // Exclude static fields of the same type.
                fields.add(((Symbol.VariableSymbol) sym).declaration());
            }
        }
        currentClassName = simpleName.name();
    }
    super.visitClass(tree);
    currentClassName = "";
    fields.clear();
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 29 with Symbol

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

the class ExplodedGraphWalker method executeAssignment.

private void executeAssignment(AssignmentExpressionTree tree) {
    ProgramState.Pop unstack;
    SymbolicValue value;
    if (tree.is(Tree.Kind.ASSIGNMENT)) {
        unstack = ExpressionUtils.isSimpleAssignment(tree) ? programState.unstackValue(1) : programState.unstackValue(2);
        value = unstack.values.get(0);
    } else {
        unstack = programState.unstackValue(2);
        value = constraintManager.createSymbolicValue(tree);
    }
    programState = unstack.state;
    Symbol symbol = null;
    if (tree.variable().is(Tree.Kind.IDENTIFIER) || ExpressionUtils.isSelectOnThisOrSuper(tree)) {
        symbol = ExpressionUtils.extractIdentifier(tree).symbol();
        programState = programState.put(symbol, value);
    }
    programState = programState.stackValue(value, symbol);
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

Example 30 with Symbol

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

the class ExplodedGraphWalker method executeLogicalAssignment.

private void executeLogicalAssignment(AssignmentExpressionTree tree) {
    ExpressionTree variable = tree.variable();
    // FIXME handle also assignments with this SONARJAVA-2242
    if (variable.is(Tree.Kind.IDENTIFIER)) {
        ProgramState.Pop unstack = programState.unstackValue(2);
        ProgramState.SymbolicValueSymbol assignedTo = unstack.valuesAndSymbols.get(1);
        ProgramState.SymbolicValueSymbol value = unstack.valuesAndSymbols.get(0);
        programState = unstack.state;
        SymbolicValue symbolicValue = constraintManager.createBinarySymbolicValue(tree, ImmutableList.of(assignedTo, value));
        Symbol symbol = ((IdentifierTree) variable).symbol();
        programState = programState.stackValue(symbolicValue, symbol);
        programState = programState.put(symbol, symbolicValue);
    }
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)

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