Search in sources :

Example 1 with JavaTree

use of org.sonar.java.model.JavaTree in project sonar-java by SonarSource.

the class ExplodedGraphWalker method execute.

private void execute(MethodTree tree) {
    CFG cfg = CFG.build(tree);
    exitBlock = cfg.exitBlock();
    checkerDispatcher.init(tree, cfg);
    liveVariables = LiveVariables.analyze(cfg);
    explodedGraph = new ExplodedGraph();
    methodTree = tree;
    constraintManager = new ConstraintManager();
    workList = new LinkedList<>();
    // Linked hashSet is required to guarantee order of yields to be generated
    endOfExecutionPath = new LinkedHashSet<>();
    if (DEBUG_MODE_ACTIVATED) {
        LOG.debug("Exploring Exploded Graph for method " + tree.simpleName().name() + " at line " + ((JavaTree) tree).getLine());
    }
    programState = ProgramState.EMPTY_STATE;
    steps = 0;
    for (ProgramState startingState : startingStates(tree, programState)) {
        enqueue(new ProgramPoint(cfg.entry()), startingState);
    }
    while (!workList.isEmpty()) {
        steps++;
        if (steps > maxSteps()) {
            throwMaxSteps(tree);
        }
        // LIFO:
        setNode(workList.removeFirst());
        CFG.Block block = (CFG.Block) programPosition.block;
        if (block.successors().isEmpty()) {
            endOfExecutionPath.add(node);
            continue;
        }
        try {
            Tree terminator = block.terminator();
            if (programPosition.i < block.elements().size()) {
                // process block element
                visit(block.elements().get(programPosition.i), terminator);
            } else if (terminator == null) {
                // process block exit, which is unconditional jump such as goto-statement or return-statement
                handleBlockExit(programPosition);
            } else if (programPosition.i == block.elements().size()) {
                // process block exist, which is conditional jump such as if-statement
                checkerDispatcher.executeCheckPostStatement(terminator);
            } else {
                // process branch
                // process block exist, which is conditional jump such as if-statement
                checkerDispatcher.executeCheckPreStatement(terminator);
                handleBlockExit(programPosition);
            }
        } catch (TooManyNestedBooleanStatesException e) {
            throwTooManyBooleanStates(tree, e);
        } catch (RelationalSymbolicValue.TransitiveRelationExceededException e) {
            throwTooManyTransitiveRelationsException(tree, e);
        }
    }
    handleEndOfExecutionPath(false);
    checkerDispatcher.executeCheckEndOfExecution();
    // Cleanup:
    workList = null;
    node = null;
    programState = null;
    constraintManager = null;
}
Also used : ConstraintManager(org.sonar.java.se.constraint.ConstraintManager) CFG(org.sonar.java.cfg.CFG) RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) JavaTree(org.sonar.java.model.JavaTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) NewArrayTree(org.sonar.plugins.java.api.tree.NewArrayTree) JavaTree(org.sonar.java.model.JavaTree) TypeCastTree(org.sonar.plugins.java.api.tree.TypeCastTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ThrowStatementTree(org.sonar.plugins.java.api.tree.ThrowStatementTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) ForStatementTree(org.sonar.plugins.java.api.tree.ForStatementTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) ConditionalExpressionTree(org.sonar.plugins.java.api.tree.ConditionalExpressionTree) Tree(org.sonar.plugins.java.api.tree.Tree) ArrayAccessExpressionTree(org.sonar.plugins.java.api.tree.ArrayAccessExpressionTree) WhileStatementTree(org.sonar.plugins.java.api.tree.WhileStatementTree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) DoWhileStatementTree(org.sonar.plugins.java.api.tree.DoWhileStatementTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree)

Example 2 with JavaTree

use of org.sonar.java.model.JavaTree in project sonar-java by SonarSource.

the class ClassTreeImplTest method getLine.

@Test
public void getLine() {
    CompilationUnitTree tree = createTree("class A {\n" + "A a = new A() {};" + "\n}");
    ClassTree classTree = (ClassTree) tree.types().get(0);
    assertThat(((JavaTree) classTree).getLine()).isEqualTo(1);
    // get line of anonymous class
    NewClassTree newClassTree = (NewClassTree) ((VariableTree) classTree.members().get(0)).initializer();
    assertThat(((JavaTree) newClassTree.classBody()).getLine()).isEqualTo(2);
}
Also used : CompilationUnitTree(org.sonar.plugins.java.api.tree.CompilationUnitTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) ClassTree(org.sonar.plugins.java.api.tree.ClassTree) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) JavaTree(org.sonar.java.model.JavaTree) Test(org.junit.Test)

Example 3 with JavaTree

use of org.sonar.java.model.JavaTree in project sonar-java by SonarSource.

the class TreeFactory method annotationIdentifier.

public TypeTree annotationIdentifier(InternalSyntaxToken firstIdentifier, Optional<List<Tuple<InternalSyntaxToken, InternalSyntaxToken>>> rests) {
    List<InternalSyntaxToken> children = Lists.newArrayList();
    children.add(firstIdentifier);
    if (rests.isPresent()) {
        for (Tuple<InternalSyntaxToken, InternalSyntaxToken> rest : rests.get()) {
            children.add(rest.first());
            children.add(rest.second());
        }
    }
    JavaTree result = null;
    InternalSyntaxToken dotToken = null;
    for (InternalSyntaxToken child : children) {
        if (!child.getGrammarRuleKey().equals(JavaTokenType.IDENTIFIER)) {
            dotToken = child;
        } else {
            InternalSyntaxToken identifierToken = child;
            if (result == null) {
                result = new IdentifierTreeImpl(identifierToken);
            } else {
                IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
                result = new MemberSelectExpressionTreeImpl((ExpressionTree) result, dotToken, identifier);
            }
        }
    }
    return (TypeTree) result;
}
Also used : ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) MemberSelectExpressionTreeImpl(org.sonar.java.model.expression.MemberSelectExpressionTreeImpl) JavaTree(org.sonar.java.model.JavaTree)

Example 4 with JavaTree

use of org.sonar.java.model.JavaTree in project sonar-java by SonarSource.

the class TreeFactory method newCompilationUnit.

// End of literals
// Compilation unit
public CompilationUnitTreeImpl newCompilationUnit(JavaTree spacing, Optional<PackageDeclarationTree> packageDeclaration, Optional<List<ImportClauseTree>> importDeclarations, Optional<ModuleDeclarationTree> moduleDeclaration, Optional<List<Tree>> typeDeclarations, InternalSyntaxToken eof) {
    ImmutableList.Builder<ImportClauseTree> imports = ImmutableList.builder();
    if (importDeclarations.isPresent()) {
        for (ImportClauseTree child : importDeclarations.get()) {
            imports.add(child);
        }
    }
    ImmutableList.Builder<Tree> types = ImmutableList.builder();
    if (typeDeclarations.isPresent()) {
        for (Tree child : typeDeclarations.get()) {
            types.add(child);
        }
    }
    return new CompilationUnitTreeImpl(packageDeclaration.orNull(), imports.build(), types.build(), moduleDeclaration.orNull(), eof);
}
Also used : CompilationUnitTreeImpl(org.sonar.java.model.JavaTree.CompilationUnitTreeImpl) ImportClauseTree(org.sonar.plugins.java.api.tree.ImportClauseTree) ImmutableList(com.google.common.collect.ImmutableList) ImportClauseTree(org.sonar.plugins.java.api.tree.ImportClauseTree) ModuleNameTree(org.sonar.plugins.java.api.tree.ModuleNameTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) Tree(org.sonar.plugins.java.api.tree.Tree) ListTree(org.sonar.plugins.java.api.tree.ListTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) JavaTree(org.sonar.java.model.JavaTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ModifierTree(org.sonar.plugins.java.api.tree.ModifierTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) PackageDeclarationTree(org.sonar.plugins.java.api.tree.PackageDeclarationTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) ModuleDirectiveTree(org.sonar.plugins.java.api.tree.ModuleDirectiveTree) ModuleDeclarationTree(org.sonar.plugins.java.api.tree.ModuleDeclarationTree)

Example 5 with JavaTree

use of org.sonar.java.model.JavaTree in project sonar-java by SonarSource.

the class TreeFactory method newAnnotationType.

public ClassTreeImpl newAnnotationType(InternalSyntaxToken openBraceToken, Optional<List<JavaTree>> annotationTypeElementDeclarations, InternalSyntaxToken closeBraceToken) {
    // TODO
    ModifiersTreeImpl emptyModifiers = ModifiersTreeImpl.emptyModifiers();
    ImmutableList.Builder<Tree> members = ImmutableList.builder();
    if (annotationTypeElementDeclarations.isPresent()) {
        for (JavaTree annotationTypeElementDeclaration : annotationTypeElementDeclarations.get()) {
            if (annotationTypeElementDeclaration.getGrammarRuleKey().equals(JavaLexer.VARIABLE_DECLARATORS)) {
                for (VariableTreeImpl variable : (VariableDeclaratorListTreeImpl) annotationTypeElementDeclaration) {
                    members.add(variable);
                }
            } else if (!annotationTypeElementDeclaration.is(Kind.TOKEN)) {
                members.add(annotationTypeElementDeclaration);
            }
        }
    }
    return new ClassTreeImpl(emptyModifiers, openBraceToken, members.build(), closeBraceToken);
}
Also used : ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) NewClassTreeImpl(org.sonar.java.model.expression.NewClassTreeImpl) ImmutableList(com.google.common.collect.ImmutableList) ImportClauseTree(org.sonar.plugins.java.api.tree.ImportClauseTree) ModuleNameTree(org.sonar.plugins.java.api.tree.ModuleNameTree) ParameterizedTypeTree(org.sonar.plugins.java.api.tree.ParameterizedTypeTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) Tree(org.sonar.plugins.java.api.tree.Tree) ListTree(org.sonar.plugins.java.api.tree.ListTree) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) JavaTree(org.sonar.java.model.JavaTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ModifierTree(org.sonar.plugins.java.api.tree.ModifierTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) PackageDeclarationTree(org.sonar.plugins.java.api.tree.PackageDeclarationTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) TypeParameterTree(org.sonar.plugins.java.api.tree.TypeParameterTree) ModuleDirectiveTree(org.sonar.plugins.java.api.tree.ModuleDirectiveTree) ModuleDeclarationTree(org.sonar.plugins.java.api.tree.ModuleDeclarationTree) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl) ModifiersTreeImpl(org.sonar.java.model.declaration.ModifiersTreeImpl) JavaTree(org.sonar.java.model.JavaTree)

Aggregations

JavaTree (org.sonar.java.model.JavaTree)8 Tree (org.sonar.plugins.java.api.tree.Tree)5 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)4 ImmutableList (com.google.common.collect.ImmutableList)3 ArrayDimensionTree (org.sonar.plugins.java.api.tree.ArrayDimensionTree)3 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)3 ParameterizedTypeTree (org.sonar.plugins.java.api.tree.ParameterizedTypeTree)3 StatementTree (org.sonar.plugins.java.api.tree.StatementTree)3 TypeTree (org.sonar.plugins.java.api.tree.TypeTree)3 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)3 InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)2 ClassTreeImpl (org.sonar.java.model.declaration.ClassTreeImpl)2 IdentifierTreeImpl (org.sonar.java.model.expression.IdentifierTreeImpl)2 NewClassTreeImpl (org.sonar.java.model.expression.NewClassTreeImpl)2 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)2 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)2 IfStatementTree (org.sonar.plugins.java.api.tree.IfStatementTree)2 ImportClauseTree (org.sonar.plugins.java.api.tree.ImportClauseTree)2 ListTree (org.sonar.plugins.java.api.tree.ListTree)2 ModifierTree (org.sonar.plugins.java.api.tree.ModifierTree)2