Search in sources :

Example 46 with Symbol

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

the class MembersDifferOnlyByCapitalizationCheck method invalidMethodAndVariable.

private static boolean invalidMethodAndVariable(Symbol currentMember, Symbol knownMember) {
    if (!sameVisibilityNotPrivate(currentMember, knownMember)) {
        return false;
    }
    Symbol methodSymbol = currentMember.isMethodSymbol() ? currentMember : knownMember;
    Symbol variableSymbol = methodSymbol == currentMember ? knownMember : currentMember;
    return !methodReturningVariableWithSameName(methodSymbol, variableSymbol) && !isBuilderPattern(methodSymbol, variableSymbol);
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 47 with Symbol

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

the class RedundantCloseCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    TryStatementTree tryStatementTree = (TryStatementTree) tree;
    Set<Symbol> resourceSymbols = tryStatementTree.resourceList().stream().map(RedundantCloseCheck::resourceSymbol).filter(s -> !s.isUnknown()).collect(Collectors.toSet());
    if (resourceSymbols.isEmpty()) {
        return;
    }
    tryStatementTree.block().accept(new CloseVisitor(resourceSymbols));
}
Also used : TypeCriteria(org.sonar.java.matcher.TypeCriteria) BaseTreeVisitor(org.sonar.plugins.java.api.tree.BaseTreeVisitor) Set(java.util.Set) Tree(org.sonar.plugins.java.api.tree.Tree) Collectors(java.util.stream.Collectors) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) List(java.util.List) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) MethodMatcher(org.sonar.java.matcher.MethodMatcher) IssuableSubscriptionVisitor(org.sonar.plugins.java.api.IssuableSubscriptionVisitor) Rule(org.sonar.check.Rule) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Collections(java.util.Collections) Symbol(org.sonar.plugins.java.api.semantic.Symbol) TryStatementTree(org.sonar.plugins.java.api.tree.TryStatementTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 48 with Symbol

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

the class OverwrittenKeyCheck method isMapPut.

@CheckForNull
private static CollectionAndKey isMapPut(StatementTree statementTree) {
    if (statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
        ExpressionTree expression = ((ExpressionStatementTree) statementTree).expression();
        if (expression.is(Tree.Kind.METHOD_INVOCATION) && MAP_PUT.matches((MethodInvocationTree) expression)) {
            MethodInvocationTree mapPut = (MethodInvocationTree) expression;
            Symbol collection = mapPut.methodSelect().is(Tree.Kind.MEMBER_SELECT) ? symbolFromIdentifier(((MemberSelectExpressionTree) mapPut.methodSelect()).expression()) : null;
            ExpressionTree keyTree = mapPut.arguments().get(0);
            Object key = extractKey(keyTree);
            if (collection != null && key != null) {
                return new CollectionAndKey(collection, keyTree, key, false, null);
            }
        }
    }
    return null;
}
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) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) CheckForNull(javax.annotation.CheckForNull)

Example 49 with Symbol

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

the class OverwrittenKeyCheck method isArrayAssignment.

@CheckForNull
private static CollectionAndKey isArrayAssignment(StatementTree statementTree) {
    if (statementTree.is(Tree.Kind.EXPRESSION_STATEMENT)) {
        ExpressionTree expression = ((ExpressionStatementTree) statementTree).expression();
        if (expression.is(Tree.Kind.ASSIGNMENT)) {
            AssignmentExpressionTree assignment = (AssignmentExpressionTree) expression;
            ExpressionTree variable = assignment.variable();
            if (variable.is(Tree.Kind.ARRAY_ACCESS_EXPRESSION)) {
                ArrayAccessExpressionTree aaet = (ArrayAccessExpressionTree) variable;
                Symbol collection = symbolFromIdentifier(aaet.expression());
                ExpressionTree keyTree = aaet.dimension().expression();
                Object key = extractKey(keyTree);
                if (collection != null && key != null) {
                    return new CollectionAndKey(collection, keyTree, key, true, assignment.expression());
                }
            }
        }
    }
    return null;
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionStatementTree(org.sonar.plugins.java.api.tree.ExpressionStatementTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) CheckForNull(javax.annotation.CheckForNull)

Example 50 with Symbol

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

the class ParameterReassignedToCheck method visitForEachStatement.

@Override
public void visitForEachStatement(ForEachStatement tree) {
    CFG cfg = CFG.buildCFG(Collections.singletonList(tree), true);
    Symbol var = tree.variable().symbol();
    boolean liveVar = true;
    if (var.owner().isMethodSymbol()) {
        cfg.setMethodSymbol((Symbol.MethodSymbol) var.owner());
        LiveVariables analyze = LiveVariables.analyze(cfg);
        Set<Symbol> live = analyze.getOut(cfg.reversedBlocks().get(1));
        liveVar = live.contains(var);
    }
    if (!liveVar) {
        variables.add(var);
    }
    super.visitForEachStatement(tree);
    if (!liveVar) {
        variables.remove(var);
    }
}
Also used : CFG(org.sonar.java.cfg.CFG) LiveVariables(org.sonar.java.cfg.LiveVariables) 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