Search in sources :

Example 11 with InternalSyntaxToken

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

the class SymbolicValueFactoryTest method testFactory.

@Test
public void testFactory() {
    final IdentifierTree tree = new IdentifierTreeImpl(new InternalSyntaxToken(1, 1, "id", Collections.<SyntaxTrivia>emptyList(), 0, 0, false));
    final ConstraintManager manager = new ConstraintManager();
    SymbolicValue symbolicValue = manager.createSymbolicValue(tree);
    assertThat(symbolicValue.getClass()).as("Created without factory").isSameAs(SymbolicValue.class);
    manager.setValueFactory(new TestSymbolicValueFactory());
    symbolicValue = manager.createSymbolicValue(tree);
    assertThat(symbolicValue.getClass()).as("Created with factory").isSameAs(TestSymbolicValue.class);
    assertThat(symbolicValue.references(symbolicValue)).isFalse();
    manager.setValueFactory(new TestSymbolicValueFactory());
    try {
        manager.setValueFactory(new TestSymbolicValueFactory());
        fail("Able to add a second factory to the contraints manager");
    } catch (IllegalStateException e) {
        assertThat(e.getMessage()).as("Exception message").isEqualTo("The symbolic value factory has already been defined by another checker!");
    }
    symbolicValue = manager.createSymbolicValue(tree);
    assertThat(symbolicValue.getClass()).as("Created with first factory").isSameAs(TestSymbolicValue.class);
    symbolicValue = manager.createSymbolicValue(tree);
    assertThat(symbolicValue.getClass()).as("Created after factory usage").isSameAs(SymbolicValue.class);
}
Also used : ConstraintManager(org.sonar.java.se.constraint.ConstraintManager) IdentifierTreeImpl(org.sonar.java.model.expression.IdentifierTreeImpl) SyntaxTrivia(org.sonar.plugins.java.api.tree.SyntaxTrivia) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Test(org.junit.Test)

Example 12 with InternalSyntaxToken

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

the class TreeFactory method newVariableDeclarators.

public VariableDeclaratorListTreeImpl newVariableDeclarators(VariableTreeImpl variable, Optional<List<Tuple<InternalSyntaxToken, VariableTreeImpl>>> rests) {
    ImmutableList.Builder<VariableTreeImpl> variables = ImmutableList.builder();
    variables.add(variable);
    if (rests.isPresent()) {
        VariableTreeImpl previousVariable = variable;
        for (Tuple<InternalSyntaxToken, VariableTreeImpl> rest : rests.get()) {
            VariableTreeImpl newVariable = rest.second();
            InternalSyntaxToken separator = rest.first();
            variables.add(newVariable);
            // store the separator
            previousVariable.setEndToken(separator);
            previousVariable = newVariable;
        }
    }
    return new VariableDeclaratorListTreeImpl(variables.build());
}
Also used : ImmutableList(com.google.common.collect.ImmutableList) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken)

Example 13 with InternalSyntaxToken

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

the class TreeFactory method assignmentExpression.

// End of statements
// Expressions
public ExpressionTree assignmentExpression(ExpressionTree expression, Optional<List<OperatorAndOperand>> operatorAndOperands) {
    if (!operatorAndOperands.isPresent()) {
        return expression;
    }
    ExpressionTree result = null;
    InternalSyntaxToken lastOperator = null;
    for (OperatorAndOperand operatorAndOperand : Lists.reverse(operatorAndOperands.get())) {
        if (lastOperator == null) {
            result = operatorAndOperand.operand();
        } else {
            result = new AssignmentExpressionTreeImpl(kindMaps.getAssignmentOperator((JavaPunctuator) lastOperator.getGrammarRuleKey()), operatorAndOperand.operand(), lastOperator, result);
        }
        lastOperator = operatorAndOperand.operator();
    }
    result = new AssignmentExpressionTreeImpl(kindMaps.getAssignmentOperator((JavaPunctuator) lastOperator.getGrammarRuleKey()), expression, lastOperator, result);
    return result;
}
Also used : ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) AssignmentExpressionTreeImpl(org.sonar.java.model.expression.AssignmentExpressionTreeImpl) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken)

Example 14 with InternalSyntaxToken

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

the class TreeFactory method newArrayTypeTreeWithAnnotations.

private static ArrayTypeTreeImpl newArrayTypeTreeWithAnnotations(TypeTree type, Tuple<Optional<List<AnnotationTreeImpl>>, Tuple<InternalSyntaxToken, InternalSyntaxToken>> dim) {
    List<AnnotationTreeImpl> annotations = dim.first().or(ImmutableList.<AnnotationTreeImpl>of());
    InternalSyntaxToken openBracketToken = dim.second().first();
    InternalSyntaxToken closeBracketToken = dim.second().second();
    return new ArrayTypeTreeImpl(type, annotations, openBracketToken, closeBracketToken);
}
Also used : AnnotationTreeImpl(org.sonar.java.model.declaration.AnnotationTreeImpl) ArrayTypeTreeImpl(org.sonar.java.model.JavaTree.ArrayTypeTreeImpl) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken)

Example 15 with InternalSyntaxToken

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

the class TreeFactory method newInferedParameters.

public LambdaParameterListTreeImpl newInferedParameters(InternalSyntaxToken openParenToken, Optional<Tuple<VariableTreeImpl, Optional<List<Tuple<InternalSyntaxToken, VariableTreeImpl>>>>> identifiersOpt, InternalSyntaxToken closeParenToken) {
    ImmutableList.Builder<VariableTreeImpl> params = ImmutableList.builder();
    if (identifiersOpt.isPresent()) {
        Tuple<VariableTreeImpl, Optional<List<Tuple<InternalSyntaxToken, VariableTreeImpl>>>> identifiers = identifiersOpt.get();
        VariableTreeImpl variable = identifiers.first();
        params.add(variable);
        VariableTreeImpl previousVariable = variable;
        if (identifiers.second().isPresent()) {
            for (Tuple<InternalSyntaxToken, VariableTreeImpl> identifier : identifiers.second().get()) {
                variable = identifier.second();
                params.add(variable);
                InternalSyntaxToken comma = identifier.first();
                previousVariable.setEndToken(comma);
                previousVariable = variable;
            }
        }
    }
    return new LambdaParameterListTreeImpl(openParenToken, params.build(), closeParenToken);
}
Also used : Optional(com.sonar.sslr.api.typed.Optional) ImmutableList(com.google.common.collect.ImmutableList) VariableTreeImpl(org.sonar.java.model.declaration.VariableTreeImpl) InternalSyntaxToken(org.sonar.java.model.InternalSyntaxToken)

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