Search in sources :

Example 21 with Symbol

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

the class BehaviorCacheTest method test_peek.

@Test
public void test_peek() throws Exception {
    Set<String> testedPre = new HashSet<>();
    Set<String> testedPost = new HashSet<>();
    SECheck check = new SECheck() {

        @Override
        public ProgramState checkPreStatement(CheckerContext context, Tree syntaxNode) {
            if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
                Symbol.MethodSymbol symbol = (Symbol.MethodSymbol) ((MethodInvocationTree) syntaxNode).symbol();
                MethodBehavior peekMethodBehavior = ((CheckerDispatcher) context).peekMethodBehavior(symbol);
                assertThat(peekMethodBehavior).isNull();
                testedPre.add(symbol.name());
            }
            return context.getState();
        }

        @Override
        public ProgramState checkPostStatement(CheckerContext context, Tree syntaxNode) {
            if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
                Symbol.MethodSymbol symbol = (Symbol.MethodSymbol) ((MethodInvocationTree) syntaxNode).symbol();
                String methodName = symbol.name();
                MethodBehavior peekMethodBehavior = ((CheckerDispatcher) context).peekMethodBehavior(symbol);
                assertThat(peekMethodBehavior).isNotNull();
                if ("foo".equals(methodName) || "isBlank".equals(methodName)) {
                    // foo should have been computed
                    assertThat(peekMethodBehavior.isComplete()).isTrue();
                } else if ("bar".equals(methodName)) {
                    assertThat(peekMethodBehavior.isComplete()).isFalse();
                }
                testedPost.add(methodName);
            }
            return super.checkPostStatement(context, syntaxNode);
        }
    };
    SymbolicExecutionVisitor sev = createSymbolicExecutionVisitor("src/test/files/se/BehaviorCachePeek.java", check);
    assertThat(sev.behaviorCache.peek("org.apache.commons.lang.StringUtils#isBlank(Ljava/lang/String;)Z").isComplete()).isTrue();
    assertThat(sev.behaviorCache.peek("org.foo.A#foo()Z").isComplete()).isTrue();
    assertThat(sev.behaviorCache.peek("org.foo.A#bar()Z").isComplete()).isFalse();
    assertThat(sev.behaviorCache.peek("org.foo.A#unknownMethod()Z")).isNull();
    assertThat(sev.behaviorCache.behaviors.keySet()).containsOnly("org.foo.A#foo()Z");
    assertThat(testedPre).containsOnly("foo", "bar", "isBlank");
    assertThat(testedPost).containsOnly("foo", "bar", "isBlank");
}
Also used : SECheck(org.sonar.java.se.checks.SECheck) Symbol(org.sonar.plugins.java.api.semantic.Symbol) SETestUtils.getMethodBehavior(org.sonar.java.se.SETestUtils.getMethodBehavior) MethodBehavior(org.sonar.java.se.xproc.MethodBehavior) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) SETestUtils.createSymbolicExecutionVisitor(org.sonar.java.se.SETestUtils.createSymbolicExecutionVisitor) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 22 with Symbol

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

the class NullableAnnotationUtilsTest method testIsAnnotatedNullable.

@Test
public void testIsAnnotatedNullable() {
    Symbol foo = getSymbol("foo");
    assertThat(isAnnotatedNullable(foo)).isFalse();
    getSymbols("nullable").forEach(s -> {
        assertThat(isAnnotatedNullable(s)).as(s + " should be recognized as Nullable.").isTrue();
        assertThat(isAnnotatedNonNull(s)).as(s + " should NOT be recognized as Nonnull.").isFalse();
    });
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) Test(org.junit.Test)

Example 23 with Symbol

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

the class VariableDeclarationScopeCheck method check.

private void check(VariableTree variable, List<StatementTree> body, int bodySize, int next) {
    Symbol symbol = variable.symbol();
    ReferenceVisitor referenceVisitor = new ReferenceVisitor(symbol);
    for (int i = next; i < bodySize; i++) {
        referenceVisitor.visit(body.get(i));
        if (referenceVisitor.referenceSymbol) {
            return;
        } else if (referenceVisitor.hasBreakingStatement) {
            reportIssue(variable.simpleName(), "Move the declaration of \"" + symbol.name() + "\" closer to the code that uses it.");
            return;
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 24 with Symbol

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

the class UnusedLocalVariableCheck method checkVariableUsages.

private void checkVariableUsages() {
    for (VariableTree variableTree : variables) {
        Symbol symbol = variableTree.symbol();
        if (symbol.usages().size() == assignments.get(symbol).size()) {
            IdentifierTree simpleName = variableTree.simpleName();
            reportIssue(simpleName, "Remove this unused \"" + simpleName + "\" local variable.");
        }
    }
}
Also used : Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

Example 25 with Symbol

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

the class UnusedMethodParameterCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    if (methodTree.block() == null || isExcluded(methodTree)) {
        return;
    }
    Set<String> documentedParameters = documentedParameters(methodTree);
    boolean overridableMethod = overridableMethod(methodTree.symbol());
    List<IdentifierTree> unused = Lists.newArrayList();
    for (VariableTree var : methodTree.parameters()) {
        Symbol symbol = var.symbol();
        if (symbol.usages().isEmpty() && !symbol.metadata().isAnnotatedWith(AUTHORIZED_ANNOTATION) && !isStrutsActionParameter(var) && (!overridableMethod || !documentedParameters.contains(symbol.name()))) {
            unused.add(var.simpleName());
        }
    }
    Set<String> unresolvedIdentifierNames = unresolvedIdentifierNames(methodTree.block());
    // kill the noise regarding unresolved identifiers, and remove the one with matching names from the list of unused
    unused = unused.stream().filter(id -> !unresolvedIdentifierNames.contains(id.name())).collect(Collectors.toList());
    if (!unused.isEmpty()) {
        reportUnusedParameters(unused);
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MethodSymbol(org.sonar.plugins.java.api.semantic.Symbol.MethodSymbol) Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree)

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