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);
}
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());
}
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;
}
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);
}
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);
}
Aggregations