use of com.google.api.generator.engine.ast.NewObjectExpr 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.NewObjectExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeNewObjectExpr_withMethodExprArgs.
@Test
public void writeNewObjectExpr_withMethodExprArgs() {
// isGeneric() is false, and generics() is empty.
// [Constructing] `new IOException(message, cause())` and `cause()` is a method invocation.
TypeNode type = TypeNode.withReference(ConcreteReference.withClazz(IOException.class));
Variable message = Variable.builder().setName("message").setType(TypeNode.STRING).build();
VariableExpr msgExpr = VariableExpr.builder().setVariable(message).build();
MethodInvocationExpr causeExpr = MethodInvocationExpr.builder().setMethodName("cause").setReturnType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class))).build();
NewObjectExpr newObjectExpr = NewObjectExpr.builder().setType(type).setArguments(Arrays.asList(msgExpr, causeExpr)).build();
newObjectExpr.accept(writerVisitor);
assertEquals("new IOException(message, cause())", writerVisitor.write());
}
Aggregations