Search in sources :

Example 1 with ArrayAccessExpressionTreeImpl

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

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

the class TreeFactory method newArrayCreatorWithDimension.

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

Aggregations

Optional (com.sonar.sslr.api.typed.Optional)2 InternalSyntaxToken (org.sonar.java.model.InternalSyntaxToken)2 ArrayAccessExpressionTreeImpl (org.sonar.java.model.expression.ArrayAccessExpressionTreeImpl)2 ImmutableList (com.google.common.collect.ImmutableList)1 ArrayDimensionTreeImpl (org.sonar.java.model.ArrayDimensionTreeImpl)1 AnnotationTreeImpl (org.sonar.java.model.declaration.AnnotationTreeImpl)1 IdentifierTreeImpl (org.sonar.java.model.expression.IdentifierTreeImpl)1 MemberSelectExpressionTreeImpl (org.sonar.java.model.expression.MemberSelectExpressionTreeImpl)1 MethodInvocationTreeImpl (org.sonar.java.model.expression.MethodInvocationTreeImpl)1 NewArrayTreeImpl (org.sonar.java.model.expression.NewArrayTreeImpl)1 NewClassTreeImpl (org.sonar.java.model.expression.NewClassTreeImpl)1 TypeArgumentListTreeImpl (org.sonar.java.model.expression.TypeArgumentListTreeImpl)1 ArrayDimensionTree (org.sonar.plugins.java.api.tree.ArrayDimensionTree)1 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)1