use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method createAddBookToShelf.
private MethodDefinition createAddBookToShelf() {
Variable bookVar = createVarFromVaporRef(bookClassRef, "book");
AnonymousClassExpr anonymousBookClassExpr = createAnonymousClass();
AssignmentExpr createNewBook = createAssignmentExpr(createVarDeclExpr(bookVar), anonymousBookClassExpr);
return MethodDefinition.builder().setHeaderCommentStatements(Arrays.asList(createPreMethodLineComment("Private helper."))).setName("addBookToShelf").setReturnType(TypeNode.withReference(bookClassRef)).setArguments(Arrays.asList(createVarDeclExpr(bookKindVar), createVarDeclExpr(shelfVar))).setScope(ScopeNode.PRIVATE).setBody(Arrays.asList(ExprStatement.withExpr(createNewBook))).setReturnExpr(VariableExpr.withVariable(bookVar)).build();
}
use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method createLibServiceCtor.
private MethodDefinition createLibServiceCtor() {
VaporReference libraryServiceStubRef = createVaporReference("com.google.exmaple.library.core", "LibraryServiceStub");
NewObjectExpr arrayList = createNewObjectExpr(ArrayList.class);
NewObjectExpr hashMap = createNewObjectExpr(HashMap.class);
ReferenceConstructorExpr superExpr = ReferenceConstructorExpr.superBuilder().setType(TypeNode.withReference(libraryServiceStubRef)).build();
ValueExpr thisValueExpr = ValueExpr.withValue(ThisObjectValue.withType(TypeNode.withReference(libraryServiceStubRef)));
VariableExpr thisVariableExpr = createVarExprFromRefVarExpr(shelfListVar, thisValueExpr);
AssignmentExpr shelfListAssignmentExpr = createAssignmentExpr(thisVariableExpr, arrayList);
AssignmentExpr shelfMapAssignmentExpr = createAssignmentExpr(VariableExpr.withVariable(shelfMapVar), hashMap);
return MethodDefinition.constructorBuilder().setBody(Arrays.asList(ExprStatement.withExpr(superExpr), ExprStatement.withExpr(shelfListAssignmentExpr), ExprStatement.withExpr(shelfMapAssignmentExpr))).setReturnType(TypeNode.withReference(libraryServiceStubRef)).setScope(ScopeNode.PUBLIC).build();
}
use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAnonymousClassExpr_withStatementsMethods.
@Test
public void writeAnonymousClassExpr_withStatementsMethods() {
ConcreteReference ref = ConcreteReference.withClazz(Runnable.class);
TypeNode type = TypeNode.withReference(ref);
// [Constructing] private static final String s = "foo";
Variable variable = createVariable("s", TypeNode.STRING);
VariableExpr variableExpr = VariableExpr.builder().setScope(ScopeNode.PRIVATE).setIsDecl(true).setIsFinal(true).setIsStatic(true).setVariable(variable).build();
ValueExpr valueExpr = ValueExpr.builder().setValue(StringObjectValue.withValue("foo")).build();
AssignmentExpr assignmentExpr = AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(valueExpr).build();
ExprStatement exprStatement = ExprStatement.withExpr(assignmentExpr);
MethodDefinition methodDefinition = MethodDefinition.builder().setName("run").setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))).build();
AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setStatements(Arrays.asList(exprStatement)).setMethods(Arrays.asList(methodDefinition)).build();
anonymousClassExpr.accept(writerVisitor);
String expected = LineFormatter.lines("new Runnable() {\n", "private static final String s = \"foo\";\n", "@Override\n", "public void run() {\n", "int x = 3;\n}\n\n}");
assertEquals(expected, writerVisitor.write());
}
use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAssignmentExpr_stringObjectValue.
@Test
public void writeAssignmentExpr_stringObjectValue() {
Variable variable = Variable.builder().setName("x").setType(TypeNode.STRING).build();
VariableExpr variableExpr = VariableExpr.builder().setVariable(variable).setIsDecl(true).build();
Value value = StringObjectValue.withValue("Hi! World. \n");
Expr valueExpr = ValueExpr.builder().setValue(value).build();
AssignmentExpr assignExpr = AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(valueExpr).build();
assignExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("String x = \"Hi! World. \\n\"");
}
use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeIfStatement_simple.
@Test
public void writeIfStatement_simple() {
AssignmentExpr assignExpr = createAssignmentExpr("x", "3", TypeNode.INT);
Statement assignExprStatement = ExprStatement.withExpr(assignExpr);
List<Statement> ifBody = Arrays.asList(assignExprStatement, assignExprStatement);
VariableExpr condExpr = createVariableExpr("condition", TypeNode.BOOLEAN);
IfStatement ifStatement = IfStatement.builder().setConditionExpr(condExpr).setBody(ifBody).build();
ifStatement.accept(writerVisitor);
assertEquals(LineFormatter.lines("if (condition) {\n", "int x = 3;\n", "int x = 3;\n", "}\n"), writerVisitor.write());
}
Aggregations