use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAnonymousClassExpr_basic.
@Test
public void writeAnonymousClassExpr_basic() {
ConcreteReference ref = ConcreteReference.withClazz(Runnable.class);
TypeNode type = TypeNode.withReference(ref);
AssignmentExpr assignmentExpr = createAssignmentExpr("foobar", "false", TypeNode.BOOLEAN);
ExprStatement statement = ExprStatement.withExpr(assignmentExpr);
MethodDefinition method = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("run").setIsOverride(true).setBody(Arrays.asList(statement)).build();
AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setMethods(Arrays.asList(method)).build();
anonymousClassExpr.accept(writerVisitor);
assertEquals(LineFormatter.lines("new Runnable() {\n", "@Override\n", "public void run() {\n", "boolean foobar = false;\n}\n\n}"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAssignmentExpr_varToVar.
@Test
public void writeAssignmentExpr_varToVar() {
Variable variable = Variable.builder().setName("foobar").setType(TypeNode.INT).build();
VariableExpr variableExpr = VariableExpr.builder().setVariable(variable).setIsStatic(true).setIsFinal(true).setScope(ScopeNode.PRIVATE).setIsDecl(true).build();
Variable anotherVariable = Variable.builder().setName("y").setType(TypeNode.INT).build();
Expr valueExpr = VariableExpr.builder().setVariable(anotherVariable).build();
AssignmentExpr assignExpr = AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(valueExpr).build();
assignExpr.accept(writerVisitor);
assertEquals("private static final int foobar = y", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeGeneralForStatement_basicIsNotDecl.
@Test
public void writeGeneralForStatement_basicIsNotDecl() {
AssignmentExpr assignExpr = createAssignmentExpr("x", "3", TypeNode.INT);
Statement assignExprStatement = ExprStatement.withExpr(assignExpr);
List<Statement> body = Arrays.asList(assignExprStatement, assignExprStatement);
VariableExpr localVarExpr = createVariableExpr("i", TypeNode.INT);
ValueExpr initValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setValue("1").setType(TypeNode.INT).build());
ValueExpr maxValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setValue("10").setType(TypeNode.INT).build());
GeneralForStatement forStatement = GeneralForStatement.incrementWith(localVarExpr, initValueExpr, maxValueExpr, body);
forStatement.accept(writerVisitor);
assertEquals(String.format("%s%s%s%s", "for (i = 1; i < 10; i++) {\n", "int x = 3;\n", "int x = 3;\n", "}\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAssignmentExpr_nullObjectValueReferenceType.
@Test
public void writeAssignmentExpr_nullObjectValueReferenceType() {
Variable variable = Variable.builder().setName("x").setType(TypeNode.STRING).build();
VariableExpr variableExpr = VariableExpr.builder().setVariable(variable).setIsDecl(true).build();
Value value = NullObjectValue.create();
Expr valueExpr = ValueExpr.builder().setValue(value).build();
AssignmentExpr assignExpr = AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(valueExpr).build();
assignExpr.accept(writerVisitor);
assertEquals("String x = null", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createOperationsClientAssignExprs.
private List<AssignmentExpr> createOperationsClientAssignExprs(Expr thisExpr, VariableExpr stubVarExpr) {
List<AssignmentExpr> operationsClientAssignExprs = new ArrayList<>();
Iterator<TypeNode> opClientTypesIt = getTransportContext().operationsClientTypes().iterator();
Iterator<String> opClientNamesIt = getTransportContext().operationsClientNames().iterator();
Iterator<String> opStubNamesIt = getTransportContext().transportOperationsStubNames().iterator();
while (opClientTypesIt.hasNext() && opClientNamesIt.hasNext() && opStubNamesIt.hasNext()) {
TypeNode operationsClientType = opClientTypesIt.next();
String opClientName = opClientNamesIt.next();
String opStubName = opStubNamesIt.next();
VariableExpr operationsClientVarExpr = VariableExpr.withVariable(Variable.builder().setType(operationsClientType).setName(opClientName).build());
String operationsStubGetterName = String.format("get%s", JavaStyle.toUpperCamelCase(opStubName));
Expr clientArgExpr = MethodInvocationExpr.builder().setExprReferenceExpr(stubVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setMethodName(operationsStubGetterName).build();
AssignmentExpr operationsClientAssignExpr = AssignmentExpr.builder().setVariableExpr(operationsClientVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(operationsClientType).setMethodName("create").setArguments(clientArgExpr).setReturnType(operationsClientVarExpr.type()).build()).build();
operationsClientAssignExprs.add(operationsClientAssignExpr);
}
return operationsClientAssignExprs;
}
Aggregations