Search in sources :

Example 1 with InternalSyntaxToken

use of org.sonar.java.model.InternalSyntaxToken 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 2 with InternalSyntaxToken

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

the class TreeFactory method applySelectors.

private static ExpressionTree applySelectors(ExpressionTree primary, Optional<List<Tuple<Optional<InternalSyntaxToken>, ExpressionTree>>> selectors) {
    ExpressionTree result = primary;
    if (selectors.isPresent()) {
        for (Tuple<Optional<InternalSyntaxToken>, ExpressionTree> tuple : selectors.get()) {
            Optional<InternalSyntaxToken> dotTokenOptional = tuple.first();
            ExpressionTree selector = tuple.second();
            if (dotTokenOptional.isPresent()) {
                InternalSyntaxToken dotToken = dotTokenOptional.get();
                if (selector.is(Kind.IDENTIFIER)) {
                    IdentifierTreeImpl identifier = (IdentifierTreeImpl) selector;
                    result = new MemberSelectExpressionTreeImpl(result, dotToken, identifier);
                } else {
                    MethodInvocationTreeImpl methodInvocation = (MethodInvocationTreeImpl) selector;
                    IdentifierTreeImpl identifier = (IdentifierTreeImpl) methodInvocation.methodSelect();
                    MemberSelectExpressionTreeImpl memberSelect = new MemberSelectExpressionTreeImpl(result, dotToken, identifier);
                    result = new MethodInvocationTreeImpl(memberSelect, methodInvocation.typeArguments(), (ArgumentListTreeImpl) methodInvocation.arguments());
                }
            } else if (selector.is(Kind.NEW_CLASS)) {
                NewClassTreeImpl newClass = (NewClassTreeImpl) selector;
                result = newClass.completeWithEnclosingExpression(result);
            } else if (selector.is(Kind.ARRAY_ACCESS_EXPRESSION)) {
                ArrayAccessExpressionTreeImpl arrayAccess = (ArrayAccessExpressionTreeImpl) selector;
                result = arrayAccess.complete(result);
            } else if (selector.is(Kind.MEMBER_SELECT)) {
                MemberSelectExpressionTreeImpl memberSelect = (MemberSelectExpressionTreeImpl) selector;
                result = memberSelect.completeWithExpression(result);
            } else {
                throw new IllegalStateException();
            }
        }
    }
    return result;
}
Also used : Optional(com.sonar.sslr.api.typed.Optional) IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) NewClassTreeImpl(org.sonar.java.model.expression.NewClassTreeImpl) ArrayAccessExpressionTreeImpl(org.sonar.java.model.expression.ArrayAccessExpressionTreeImpl) TypeArgumentListTreeImpl(org.sonar.java.model.expression.TypeArgumentListTreeImpl) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) MemberSelectExpressionTreeImpl(org.sonar.java.model.expression.MemberSelectExpressionTreeImpl) MethodInvocationTreeImpl(org.sonar.java.model.expression.MethodInvocationTreeImpl)

Example 3 with InternalSyntaxToken

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

the class TreeFactory method prependNewFormalParameter.

public FormalParametersListTreeImpl prependNewFormalParameter(VariableTreeImpl variable, Optional<Tuple<InternalSyntaxToken, FormalParametersListTreeImpl>> rest) {
    if (rest.isPresent()) {
        InternalSyntaxToken comma = rest.get().first();
        FormalParametersListTreeImpl partial = rest.get().second();
        partial.add(0, variable);
        // store the comma as endToken for the variable
        variable.setEndToken(comma);
        return partial;
    } else {
        return new FormalParametersListTreeImpl(variable);
    }
}
Also used : InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken)

Example 4 with InternalSyntaxToken

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

the class TreeFactory method newArrayCreatorWithInitializer.

public NewArrayTreeImpl newArrayCreatorWithInitializer(InternalSyntaxToken openBracketToken, InternalSyntaxToken closeBracketToken, Optional<List<Tuple<Optional<List<AnnotationTreeImpl>>, Tuple<InternalSyntaxToken, InternalSyntaxToken>>>> dimensions, NewArrayTreeImpl partial) {
    ImmutableList.Builder<ArrayDimensionTree> dDimensionsBuilder = ImmutableList.builder();
    dDimensionsBuilder.add(new ArrayDimensionTreeImpl(openBracketToken, null, closeBracketToken));
    if (dimensions.isPresent()) {
        for (Tuple<Optional<List<AnnotationTreeImpl>>, Tuple<InternalSyntaxToken, InternalSyntaxToken>> dim : dimensions.get()) {
            List<AnnotationTreeImpl> annotations = dim.first().or(ImmutableList.<AnnotationTreeImpl>of());
            Tuple<InternalSyntaxToken, InternalSyntaxToken> brackets = dim.second();
            dDimensionsBuilder.add(new ArrayDimensionTreeImpl(annotations, brackets.first(), null, brackets.second()));
        }
    }
    return partial.completeDimensions(dDimensionsBuilder.build());
}
Also used : AnnotationTreeImpl(org.sonar.java.model.declaration.AnnotationTreeImpl) Optional(com.sonar.sslr.api.typed.Optional) ImmutableList(com.google.common.collect.ImmutableList) ArrayDimensionTree(org.sonar.plugins.java.api.tree.ArrayDimensionTree) ArrayDimensionTreeImpl(org.sonar.java.model.ArrayDimensionTreeImpl) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken)

Example 5 with InternalSyntaxToken

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

the class TreeFactory method newStatementExpressions.

public StatementExpressionListTreeImpl newStatementExpressions(ExpressionTree expression, Optional<List<Tuple<InternalSyntaxToken, ExpressionTree>>> rests) {
    ImmutableList.Builder<StatementTree> statements = ImmutableList.builder();
    statements.add(new ExpressionStatementTreeImpl(expression, null));
    ImmutableList.Builder<SyntaxToken> separators = ImmutableList.builder();
    if (rests.isPresent()) {
        for (Tuple<InternalSyntaxToken, ExpressionTree> rest : rests.get()) {
            separators.add(rest.first());
            statements.add(new ExpressionStatementTreeImpl(rest.second(), null));
        }
    }
    return new StatementExpressionListTreeImpl(statements.build(), separators.build());
}
Also used : StatementTree(org.sonar.plugins.java.api.tree.StatementTree) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) ImmutableList(com.google.common.collect.ImmutableList) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) ExpressionStatementTreeImpl(org.sonar.java.model.statement.ExpressionStatementTreeImpl)

Aggregations

InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)26 IdentifierTreeImpl (org.sonar.java.model.expression.IdentifierTreeImpl)9 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)8 ImmutableList (com.google.common.collect.ImmutableList)7 Optional (com.sonar.sslr.api.typed.Optional)5 AnnotationTreeImpl (org.sonar.java.model.declaration.AnnotationTreeImpl)4 ArrayList (java.util.ArrayList)3 ArrayTypeTreeImpl (org.sonar.java.model.JavaTree.ArrayTypeTreeImpl)3 MemberSelectExpressionTreeImpl (org.sonar.java.model.expression.MemberSelectExpressionTreeImpl)3 TypeArgumentListTreeImpl (org.sonar.java.model.expression.TypeArgumentListTreeImpl)3 ArrayDimensionTree (org.sonar.plugins.java.api.tree.ArrayDimensionTree)3 SyntaxToken (org.sonar.plugins.java.api.tree.SyntaxToken)3 ArrayDimensionTreeImpl (org.sonar.java.model.ArrayDimensionTreeImpl)2 JavaTree (org.sonar.java.model.JavaTree)2 MethodTreeImpl (org.sonar.java.model.declaration.MethodTreeImpl)2 ModuleNameListTreeImpl (org.sonar.java.model.declaration.ModuleNameListTreeImpl)2 VariableTreeImpl (org.sonar.java.model.declaration.VariableTreeImpl)2 ArrayAccessExpressionTreeImpl (org.sonar.java.model.expression.ArrayAccessExpressionTreeImpl)2 NewArrayTreeImpl (org.sonar.java.model.expression.NewArrayTreeImpl)2 NewClassTreeImpl (org.sonar.java.model.expression.NewClassTreeImpl)2