Search in sources :

Example 66 with VariableTree

use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.

the class LiveVariables method processBlockElements.

private void processBlockElements(CFG.Block block, Set<Symbol> blockKill, Set<Symbol> blockGen) {
    // process elements from bottom to top
    Set<Tree> assignmentLHS = new HashSet<>();
    for (Tree element : Lists.reverse(block.elements())) {
        switch(element.kind()) {
            case ASSIGNMENT:
                processAssignment((AssignmentExpressionTree) element, blockKill, blockGen, assignmentLHS);
                break;
            case IDENTIFIER:
                processIdentifier((IdentifierTree) element, blockGen, assignmentLHS);
                break;
            case MEMBER_SELECT:
                processMemberSelect((MemberSelectExpressionTree) element, assignmentLHS, blockGen);
                break;
            case VARIABLE:
                blockKill.add(((VariableTree) element).symbol());
                blockGen.remove(((VariableTree) element).symbol());
                break;
            case LAMBDA_EXPRESSION:
                blockGen.addAll(getUsedVariables(((LambdaExpressionTree) element).body(), cfg.methodSymbol()));
                break;
            case METHOD_REFERENCE:
                blockGen.addAll(getUsedVariables(((MethodReferenceTree) element).expression(), cfg.methodSymbol()));
                break;
            case NEW_CLASS:
                blockGen.addAll(getUsedVariables(((NewClassTree) element).classBody(), cfg.methodSymbol()));
                break;
            default:
        }
    }
}
Also used : LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodReferenceTree(org.sonar.plugins.java.api.tree.MethodReferenceTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) HashSet(java.util.HashSet)

Example 67 with VariableTree

use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.

the class SymbolTableTest method recursive_type_substitution.

@Test
public void recursive_type_substitution() {
    Result result = Result.createFor("Generics");
    MethodTree ddt_method = (MethodTree) result.symbol("ddt_method").declaration();
    VariableTree variable = (VariableTree) ddt_method.block().body().get(0);
    assertThat(variable.initializer().symbolType().name()).isEqualTo("String");
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Test(org.junit.Test)

Example 68 with VariableTree

use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.

the class SymbolTableTest method Generics.

@Test
public void Generics() {
    Result result = Result.createFor("Generics");
    JavaSymbol.TypeJavaSymbol typeSymbol = (JavaSymbol.TypeJavaSymbol) result.symbol("A");
    JavaSymbol symbolA1 = result.symbol("a1");
    assertThat(symbolA1.type.symbol).isSameAs(typeSymbol);
    JavaSymbol symbolA2 = result.symbol("a1");
    assertThat(symbolA2.type.symbol).isSameAs(typeSymbol);
    assertThat(symbolA2.type).isSameAs(symbolA1.type);
    JavaSymbol symbolA3 = result.symbol("a1");
    assertThat(symbolA3.type.symbol).isSameAs(typeSymbol);
    assertThat(result.reference(12, 5)).isSameAs(result.symbol("foo", 8));
    assertThat(result.reference(13, 5)).isSameAs(result.symbol("foo", 9));
    // Check erasure
    TypeVariableJavaType STypeVariableType = (TypeVariableJavaType) typeSymbol.typeParameters.lookup("S").get(0).type;
    assertThat(STypeVariableType.erasure().getSymbol().getName()).isEqualTo("CharSequence");
    JavaType arrayErasure = typeSymbol.members().lookup("arrayErasure").get(0).type;
    assertThat(arrayErasure.isTagged(JavaType.ARRAY)).isTrue();
    assertThat(arrayErasure.erasure().isTagged(JavaType.ARRAY)).isTrue();
    assertThat(((ArrayJavaType) arrayErasure.erasure()).elementType().symbol.getName()).isEqualTo("CharSequence");
    IdentifierTree tree = result.referenceTree(20, 7);
    JavaType symbolType = (JavaType) tree.symbolType();
    assertThat(symbolType).isInstanceOf(ParametrizedTypeJavaType.class);
    ParametrizedTypeJavaType ptt = (ParametrizedTypeJavaType) symbolType;
    assertThat(ptt.symbol.getName()).isEqualTo("C");
    assertThat(ptt.typeSubstitution.size()).isEqualTo(1);
    assertThat(ptt.typeSubstitution.substitutedType(ptt.typeSubstitution.typeVariables().iterator().next()).symbol.getName()).isEqualTo("String");
    JavaSymbol.MethodJavaSymbol method1 = (JavaSymbol.MethodJavaSymbol) typeSymbol.members().lookup("method1").get(0);
    assertThat(((MethodJavaType) method1.type).resultType).isSameAs(STypeVariableType);
    JavaSymbol.MethodJavaSymbol method2 = (JavaSymbol.MethodJavaSymbol) typeSymbol.members().lookup("method2").get(0);
    TypeVariableJavaType PTypeVariableType = (TypeVariableJavaType) method2.typeParameters().lookup("P").get(0).type;
    assertThat(method2.getReturnType().type).isSameAs(PTypeVariableType);
    assertThat(method2.parameterTypes().get(0)).isSameAs(PTypeVariableType);
    // Type parameter defined in outer class
    JavaSymbol.TypeJavaSymbol classCSymbol = (JavaSymbol.TypeJavaSymbol) typeSymbol.members().lookup("C").get(0);
    JavaSymbol innerClassField = classCSymbol.members().lookup("innerClassField").get(0);
    assertThat(innerClassField.type).isSameAs(STypeVariableType);
    // Unknown parametrized type should be tagged as unknown
    MethodTree methodTree = (MethodTree) result.symbol("unknownSymbol").declaration();
    VariableTree variableTree = (VariableTree) methodTree.block().body().get(0);
    assertThat(variableTree.type().symbolType().isUnknown()).isTrue();
    // Inner class referenced as type parameter in super class/interface
    assertThat(result.reference(68, 53)).isSameAs(result.symbol("B", 69));
    JavaSymbol applyMethod = result.symbol("apply");
    assertThat(result.reference(83, 12)).isSameAs(applyMethod);
    assertThat(result.reference(89, 61)).isSameAs(applyMethod);
    // this method does not compile but is resolved whereas it should not.
    assertThat(result.reference(85, 12)).isSameAs(applyMethod);
    assertThat(applyMethod.usages()).hasSize(3);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Test(org.junit.Test)

Example 69 with VariableTree

use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.

the class ComplexityVisitorTest method lambda_complexity.

@Test
public void lambda_complexity() throws Exception {
    CompilationUnitTree cut = (CompilationUnitTree) p.parse("class A { Function f = s -> {if(s.isEmpty()) return s; return new MyClass(){ void foo(){if(a) return;} };};}");
    ExpressionTree lambda = ((VariableTree) ((ClassTree) cut.types().get(0)).members().get(0)).initializer();
    List<Tree> nodes = new ComplexityVisitor().getNodes(lambda);
    assertThat(nodes).hasSize(2);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) Test(org.junit.Test)

Example 70 with VariableTree

use of org.sonar.plugins.java.api.tree.VariableTree in project sonar-java by SonarSource.

the class PublicApiCheckerTest method retrieveJavadoc.

@Test
public void retrieveJavadoc() {
    new SubscriptionVisitor() {

        @Override
        public List<Tree.Kind> nodesToVisit() {
            return Arrays.asList(Tree.Kind.values());
        }

        @Override
        public void visitNode(Tree tree) {
            if (tree.is(Tree.Kind.VARIABLE)) {
                VariableTree variableTree = (VariableTree) tree;
                checkApi(tree, variableTree.simpleName().name());
            } else if (tree.is(Tree.Kind.METHOD, Tree.Kind.CONSTRUCTOR)) {
                MethodTree methodTree = (MethodTree) tree;
                checkApi(tree, methodTree.simpleName().name());
            } else if (tree.is(Tree.Kind.CLASS, Tree.Kind.ENUM, Tree.Kind.INTERFACE, Tree.Kind.ANNOTATION_TYPE)) {
                IdentifierTree idTree = ((ClassTree) tree).simpleName();
                checkApi(tree, idTree == null ? "" : idTree.name());
            } else {
                checkApi(tree, "");
            }
        }
    }.scanTree(cut);
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) Tree(org.sonar.plugins.java.api.tree.Tree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) List(java.util.List) Test(org.junit.Test)

Aggregations

VariableTree (org.sonar.plugins.java.api.tree.VariableTree)86 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)32 Tree (org.sonar.plugins.java.api.tree.Tree)32 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)29 ClassTree (org.sonar.plugins.java.api.tree.ClassTree)27 Test (org.junit.Test)25 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)22 Symbol (org.sonar.plugins.java.api.semantic.Symbol)18 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)18 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)17 AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)16 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)13 List (java.util.List)12 CompilationUnitTree (org.sonar.plugins.java.api.tree.CompilationUnitTree)12 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)12 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)11 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)11 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)10 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)10 Type (org.sonar.plugins.java.api.semantic.Type)9