Search in sources :

Example 1 with TRY_STATEMENT

use of org.sonar.plugins.java.api.tree.Tree.Kind.TRY_STATEMENT in project sonar-java by SonarSource.

the class FileLinesVisitor method computeExecutableLines.

private void computeExecutableLines(List<? extends Tree> trees) {
    if (trees.isEmpty()) {
        return;
    }
    // rely on cfg to get every instructions and get most of the token.
    CFG cfg = CFG.buildCFG(trees);
    cfg.blocks().stream().flatMap(b -> b.elements().stream()).forEach(t -> {
        if (t.is(NEW_CLASS)) {
            NewClassTree newClassTree = (NewClassTree) t;
            new ExecutableLinesTokenVisitor().scanTree(newClassTree.identifier());
            executableLines.add(newClassTree.newKeyword().line());
        } else if (t.is(TRY_STATEMENT)) {
            // add last token of try statements
            executableLines.add(t.lastToken().line());
        } else {
            executableLines.add(t.firstToken().line());
        }
    });
}
Also used : NEW_CLASS(org.sonar.plugins.java.api.tree.Tree.Kind.NEW_CLASS) SyntaxTrivia(org.sonar.plugins.java.api.tree.SyntaxTrivia) FOR_EACH_STATEMENT(org.sonar.plugins.java.api.tree.Tree.Kind.FOR_EACH_STATEMENT) STATIC_INITIALIZER(org.sonar.plugins.java.api.tree.Tree.Kind.STATIC_INITIALIZER) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) CATCH(org.sonar.plugins.java.api.tree.Tree.Kind.CATCH) LAMBDA_EXPRESSION(org.sonar.plugins.java.api.tree.Tree.Kind.LAMBDA_EXPRESSION) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) FileLinesContext(org.sonar.api.measures.FileLinesContext) HashSet(java.util.HashSet) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) DO_STATEMENT(org.sonar.plugins.java.api.tree.Tree.Kind.DO_STATEMENT) TOKEN(org.sonar.plugins.java.api.tree.Tree.Kind.TOKEN) VARIABLE(org.sonar.plugins.java.api.tree.Tree.Kind.VARIABLE) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) SonarComponents(org.sonar.java.SonarComponents) FLOAT_LITERAL(org.sonar.plugins.java.api.tree.Tree.Kind.FLOAT_LITERAL) Modifier(org.sonar.plugins.java.api.tree.Modifier) STRING_LITERAL(org.sonar.plugins.java.api.tree.Tree.Kind.STRING_LITERAL) TypeTree(org.sonar.plugins.java.api.tree.TypeTree) INITIALIZER(org.sonar.plugins.java.api.tree.Tree.Kind.INITIALIZER) Set(java.util.Set) INT_LITERAL(org.sonar.plugins.java.api.tree.Tree.Kind.INT_LITERAL) Tree(org.sonar.plugins.java.api.tree.Tree) LONG_LITERAL(org.sonar.plugins.java.api.tree.Tree.Kind.LONG_LITERAL) METHOD(org.sonar.plugins.java.api.tree.Tree.Kind.METHOD) ModifiersUtils(org.sonar.java.model.ModifiersUtils) FOR_STATEMENT(org.sonar.plugins.java.api.tree.Tree.Kind.FOR_STATEMENT) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) CoreMetrics(org.sonar.api.measures.CoreMetrics) File(java.io.File) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree) LambdaExpressionTree(org.sonar.plugins.java.api.tree.LambdaExpressionTree) NULL_LITERAL(org.sonar.plugins.java.api.tree.Tree.Kind.NULL_LITERAL) List(java.util.List) CFG(org.sonar.java.cfg.CFG) DOUBLE_LITERAL(org.sonar.plugins.java.api.tree.Tree.Kind.DOUBLE_LITERAL) BOOLEAN_LITERAL(org.sonar.plugins.java.api.tree.Tree.Kind.BOOLEAN_LITERAL) CONSTRUCTOR(org.sonar.plugins.java.api.tree.Tree.Kind.CONSTRUCTOR) BLOCK(org.sonar.plugins.java.api.tree.Tree.Kind.BLOCK) CHAR_LITERAL(org.sonar.plugins.java.api.tree.Tree.Kind.CHAR_LITERAL) WHILE_STATEMENT(org.sonar.plugins.java.api.tree.Tree.Kind.WHILE_STATEMENT) Collections(java.util.Collections) TRY_STATEMENT(org.sonar.plugins.java.api.tree.Tree.Kind.TRY_STATEMENT) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) CFG(org.sonar.java.cfg.CFG) NewClassTree(org.sonar.plugins.java.api.tree.NewClassTree)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 Lists (com.google.common.collect.Lists)1 File (java.io.File)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 CoreMetrics (org.sonar.api.measures.CoreMetrics)1 FileLinesContext (org.sonar.api.measures.FileLinesContext)1 SonarComponents (org.sonar.java.SonarComponents)1 CFG (org.sonar.java.cfg.CFG)1 ModifiersUtils (org.sonar.java.model.ModifiersUtils)1 JavaFileScannerContext (org.sonar.plugins.java.api.JavaFileScannerContext)1 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)1 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)1 LambdaExpressionTree (org.sonar.plugins.java.api.tree.LambdaExpressionTree)1 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)1 Modifier (org.sonar.plugins.java.api.tree.Modifier)1 NewClassTree (org.sonar.plugins.java.api.tree.NewClassTree)1 SyntaxToken (org.sonar.plugins.java.api.tree.SyntaxToken)1