Search in sources :

Example 6 with JavaTree

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

the class ReturnOfBooleanExpressionsCheck method getStatementTree.

private static StatementTree getStatementTree(IfStatementTree ifStatementTree) {
    StatementTree elseStatementOrNextStatement = ifStatementTree.elseStatement();
    if (elseStatementOrNextStatement == null) {
        JavaTree parent = (JavaTree) ifStatementTree.parent();
        List<Tree> children = parent.getChildren();
        int indexOfIf = children.indexOf(ifStatementTree);
        if (indexOfIf < children.size() - 1) {
            // Defensive, this condition should always be true as if necessarily followed by a statement or a token.
            Tree next = children.get(indexOfIf + 1);
            if (!next.is(Kind.TOKEN)) {
                elseStatementOrNextStatement = (StatementTree) next;
            }
        }
    }
    return elseStatementOrNextStatement;
}
Also used : IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) IfStatementTree(org.sonar.plugins.java.api.tree.IfStatementTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree) JavaTree(org.sonar.java.model.JavaTree) StatementTree(org.sonar.plugins.java.api.tree.StatementTree) Tree(org.sonar.plugins.java.api.tree.Tree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) JavaTree(org.sonar.java.model.JavaTree)

Example 7 with JavaTree

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

the class TreeFactory method newEnumDeclaration.

public ClassTreeImpl newEnumDeclaration(InternalSyntaxToken enumToken, InternalSyntaxToken identifierToken, Optional<Tuple<InternalSyntaxToken, QualifiedIdentifierListTreeImpl>> implementsClause, InternalSyntaxToken openBraceToken, Optional<List<EnumConstantTreeImpl>> enumConstants, Optional<InternalSyntaxToken> semicolonToken, Optional<List<JavaTree>> enumDeclarations, InternalSyntaxToken closeBraceToken) {
    List<JavaTree> members = Lists.newLinkedList();
    EnumConstantTreeImpl lastEnumConstant = null;
    if (enumConstants.isPresent()) {
        for (EnumConstantTreeImpl enumConstant : enumConstants.get()) {
            members.add(enumConstant);
            lastEnumConstant = enumConstant;
        }
    }
    if (semicolonToken.isPresent()) {
        InternalSyntaxToken semicolon = semicolonToken.get();
        // add the semicolon as endToken of the last enumConstant, or as empty statement in the enum members
        if (lastEnumConstant != null) {
            lastEnumConstant.setEndToken(semicolon);
        } else {
            members.add(newEmptyMember(semicolon));
        }
    }
    if (enumDeclarations.isPresent()) {
        for (JavaTree enumDeclaration : enumDeclarations.get()) {
            members.add(enumDeclaration);
        }
    }
    ClassTreeImpl result = newClassBody(Kind.ENUM, openBraceToken, Optional.of((List<JavaTree>) ImmutableList.<JavaTree>builder().addAll(members).build()), closeBraceToken);
    result.completeDeclarationKeyword(enumToken);
    IdentifierTreeImpl identifier = new IdentifierTreeImpl(identifierToken);
    result.completeIdentifier(identifier);
    if (implementsClause.isPresent()) {
        InternalSyntaxToken implementsKeyword = implementsClause.get().first();
        QualifiedIdentifierListTreeImpl interfaces = implementsClause.get().second();
        result.completeInterfaces(implementsKeyword, interfaces);
    }
    return result;
}
Also used : ClassTreeImpl(org.sonar.java.model.declaration.ClassTreeImpl) NewClassTreeImpl(org.sonar.java.model.expression.NewClassTreeImpl) IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) EnumConstantTreeImpl(org.sonar.java.model.declaration.EnumConstantTreeImpl) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) JavaTree(org.sonar.java.model.JavaTree)

Example 8 with JavaTree

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

the class JavaParser method createParentLink.

private Tree createParentLink(JavaTree topParent) {
    parentList.push(topParent);
    while (!parentList.isEmpty()) {
        JavaTree parent = parentList.pop();
        if (!parent.isLeaf()) {
            for (Tree nextTree : parent.getChildren()) {
                JavaTree next = (JavaTree) nextTree;
                if (next != null) {
                    next.setParent(parent);
                    parentList.push(next);
                }
            }
        }
    }
    return topParent;
}
Also used : JavaTree(org.sonar.java.model.JavaTree) Tree(org.sonar.plugins.java.api.tree.Tree) 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