use of com.google.api.generator.engine.ast.VaporReference in project gapic-generator-java by googleapis.
the class HttpJsonServiceCallableFactoryClassComposer method createOperationCallableMethod.
@Override
protected MethodDefinition createOperationCallableMethod(Service service, TypeStore typeStore) {
String methodVariantName = "Operation";
String requestTemplateName = "RequestT";
String responseTemplateName = "ResponseT";
List<String> methodTemplateNames = Arrays.asList(requestTemplateName, responseTemplateName, "MetadataT");
// Always add @BetaApi annotation to the generated createOperationCallable() method for now,
// until LRO is fully implemented.
//
// Remove the @BetaApi annotation once the LRO feature is fully implemented and stabilized.
AnnotationNode betaAnnotation = AnnotationNode.withTypeAndDescription(typeStore.get("BetaApi"), "The surface for long-running operations is not stable yet and may change in the" + " future.");
// Generate generic method without the body
TypeNode operationType = service.operationType();
if (operationType == null) {
operationType = DEFAULT_OPERATION_TYPE;
}
MethodDefinition method = createGenericCallableMethod(service, typeStore, /*methodTemplateNames=*/
methodTemplateNames, /*returnCallableKindName=*/
methodVariantName, /*returnCallableTemplateNames=*/
methodTemplateNames, /*methodVariantName=*/
methodVariantName, /*httpJsonCallSettingsTemplateObjects=*/
Arrays.asList(requestTemplateName, operationType), /*callSettingsVariantName=*/
methodVariantName, /*callSettingsTemplateObjects=*/
methodTemplateNames.stream().map(n -> (Object) n).collect(Collectors.toList()), Arrays.asList(betaAnnotation));
List<Statement> createOperationCallableBody = new ArrayList<>();
List<VariableExpr> arguments = new ArrayList<>(method.arguments());
Variable httpJsonCallSettingsVar = arguments.get(0).variable();
Variable operationCallSettingsVar = arguments.get(1).variable();
Variable clientContextVar = arguments.get(2).variable();
Variable operationsStubVar = arguments.get(3).variable();
// Generate innerCallable
VariableExpr innerCallableVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("innerCallable").setType(TypeNode.withReference(ConcreteReference.withClazz(UnaryCallable.class))).build()).setTemplateObjects(Arrays.asList(requestTemplateName, methodVariantName)).build();
MethodInvocationExpr getInitialCallSettingsExpr = MethodInvocationExpr.builder().setExprReferenceExpr(VariableExpr.withVariable(operationCallSettingsVar)).setMethodName("getInitialCallSettings").build();
MethodInvocationExpr createBaseUnaryCallableExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(HttpJsonCallableFactory.class))).setMethodName("createBaseUnaryCallable").setArguments(VariableExpr.withVariable(httpJsonCallSettingsVar), getInitialCallSettingsExpr, VariableExpr.withVariable(clientContextVar)).setReturnType(TypeNode.withReference(ConcreteReference.withClazz(UnaryCallable.class))).build();
AssignmentExpr innerCallableAssignExpr = AssignmentExpr.builder().setVariableExpr(innerCallableVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(createBaseUnaryCallableExpr).build();
createOperationCallableBody.add(ExprStatement.withExpr(innerCallableAssignExpr));
// This is a temporary solution
VaporReference requestT = VaporReference.builder().setName("RequestT").setPakkage(service.pakkage() + ".stub").build();
TypeNode initialCallableType = TypeNode.withReference(ConcreteReference.builder().setClazz(HttpJsonOperationSnapshotCallable.class).setGenerics(requestT, operationType.reference()).build());
// Generate initialCallable
VariableExpr initialCallableVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("initialCallable").setType(initialCallableType).build()).build();
MethodInvocationExpr getMethodDescriptorExpr = MethodInvocationExpr.builder().setExprReferenceExpr(VariableExpr.withVariable(httpJsonCallSettingsVar)).setMethodName("getMethodDescriptor").build();
MethodInvocationExpr getOperationSnapshotFactoryExpr = MethodInvocationExpr.builder().setExprReferenceExpr(getMethodDescriptorExpr).setMethodName("getOperationSnapshotFactory").build();
TypeNode operationSnapshotCallableType = TypeNode.withReference(ConcreteReference.builder().setClazz(HttpJsonOperationSnapshotCallable.class).setGenerics(requestT, operationType.reference()).build());
NewObjectExpr initialCallableObject = NewObjectExpr.builder().setType(operationSnapshotCallableType).setIsGeneric(true).setArguments(innerCallableVarExpr, getOperationSnapshotFactoryExpr).build();
AssignmentExpr initialCallableAssignExpr = AssignmentExpr.builder().setVariableExpr(initialCallableVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(initialCallableObject).build();
createOperationCallableBody.add(ExprStatement.withExpr(initialCallableAssignExpr));
// Generate return statement
MethodInvocationExpr longRunningClient = MethodInvocationExpr.builder().setExprReferenceExpr(VariableExpr.withVariable(operationsStubVar)).setMethodName("longRunningClient").build();
MethodInvocationExpr createOperationCallable = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(HttpJsonCallableFactory.class))).setMethodName("createOperationCallable").setArguments(VariableExpr.withVariable(operationCallSettingsVar), VariableExpr.withVariable(clientContextVar), longRunningClient, initialCallableVarExpr).setReturnType(TypeNode.withReference(ConcreteReference.withClazz(OperationCallable.class))).build();
// Add body and return statement to method
return method.toBuilder().setBody(createOperationCallableBody).setReturnExpr(createOperationCallable).build();
}
use of com.google.api.generator.engine.ast.VaporReference in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeSuperObjectValue_accessFieldAndInvokeMethod.
@Test
public void writeSuperObjectValue_accessFieldAndInvokeMethod() {
VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
TypeNode classType = TypeNode.withReference(ref);
SuperObjectValue superObjectValue = SuperObjectValue.withType(classType);
ValueExpr superValueExpr = ValueExpr.withValue(superObjectValue);
Variable subVariable = Variable.builder().setName("name").setType(TypeNode.STRING).build();
VariableExpr superVariableExpr = VariableExpr.builder().setVariable(subVariable).setExprReferenceExpr(superValueExpr).build();
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("getName").setExprReferenceExpr(ValueExpr.withValue(superObjectValue)).setReturnType(TypeNode.STRING).build();
AssignmentExpr assignmentExpr = AssignmentExpr.builder().setVariableExpr(superVariableExpr).setValueExpr(methodExpr).build();
assignmentExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("super.name = super.getName()");
}
use of com.google.api.generator.engine.ast.VaporReference in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeReferenceConstructorExprImports_withArgs.
@Test
public void writeReferenceConstructorExprImports_withArgs() {
VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
TypeNode classType = TypeNode.withReference(ref);
VariableExpr streamVarExpr = VariableExpr.builder().setVariable(createVariable("stream", TypeNode.withReference(ConcreteReference.withClazz(LongStream.class)))).build();
ReferenceConstructorExpr referenceConstructorExpr = ReferenceConstructorExpr.thisBuilder().setArguments(Arrays.asList(streamVarExpr)).setType(classType).build();
referenceConstructorExpr.accept(writerVisitor);
}
use of com.google.api.generator.engine.ast.VaporReference in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeSuperObjectValueImports.
@Test
public void writeSuperObjectValueImports() {
VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.examples.v1").build();
TypeNode typeNode = TypeNode.withReference(ref);
SuperObjectValue superObjectValue = SuperObjectValue.withType(typeNode);
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("getName").setExprReferenceExpr(ValueExpr.withValue(superObjectValue)).setReturnType(TypeNode.STRING).build();
methodExpr.accept(writerVisitor);
assertEquals("import com.google.example.examples.v1.Student;\n\n", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.VaporReference in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeReferenceConstructorExprImports_basic.
@Test
public void writeReferenceConstructorExprImports_basic() {
VaporReference ref = VaporReference.builder().setName("Parent").setPakkage("com.google.example.v1").build();
TypeNode classType = TypeNode.withReference(ref);
ReferenceConstructorExpr referenceConstructorExpr = ReferenceConstructorExpr.superBuilder().setType(classType).build();
referenceConstructorExpr.accept(writerVisitor);
assertEquals("import com.google.example.v1.Parent;\n\n", writerVisitor.write());
}
Aggregations