Search in sources :

Example 11 with NewObjectExpr

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();
}
Also used : ValueExpr(com.google.api.generator.engine.ast.ValueExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) VaporReference(com.google.api.generator.engine.ast.VaporReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Example 12 with NewObjectExpr

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());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)12 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)9 TypeNode (com.google.api.generator.engine.ast.TypeNode)8 Variable (com.google.api.generator.engine.ast.Variable)8 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)7 Test (org.junit.Test)7 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)5 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)5 VaporReference (com.google.api.generator.engine.ast.VaporReference)3 ArrayList (java.util.ArrayList)3 UnaryCallable (com.google.api.gax.rpc.UnaryCallable)2 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)2 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)2 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)2 Statement (com.google.api.generator.engine.ast.Statement)2 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)2 IOException (java.io.IOException)2 MonitoredResourceDescriptor (com.google.api.MonitoredResourceDescriptor)1 ApiFunction (com.google.api.core.ApiFunction)1 ApiFuture (com.google.api.core.ApiFuture)1