use of com.google.api.generator.engine.ast.NewObjectExpr in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createBuilderHelperMethods.
protected List<MethodDefinition> createBuilderHelperMethods(Service service, TypeStore typeStore) {
List<MethodDefinition> javaMethods = new ArrayList<>();
final TypeNode builderReturnType = typeStore.get(NESTED_BUILDER_CLASS_NAME);
// Create the newBuilder(ClientContext) method.
Function<Expr, NewObjectExpr> newBuilderFn = argExpr -> NewObjectExpr.builder().setType(builderReturnType).setArguments(argExpr).build();
VariableExpr clientContextVarExpr = VariableExpr.withVariable(Variable.builder().setType(FIXED_TYPESTORE.get("ClientContext")).setName("clientContext").build());
javaMethods.add(MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.NEW_BUILDER_METHOD_COMMENT).setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(builderReturnType).setName("newBuilder").setArguments(clientContextVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(newBuilderFn.apply(clientContextVarExpr)).build());
// Create the toBuilder method.
javaMethods.add(MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.TO_BUILDER_METHOD_COMMENT).setScope(ScopeNode.PUBLIC).setReturnType(builderReturnType).setName("toBuilder").setReturnExpr(newBuilderFn.apply(ValueExpr.withValue(ThisObjectValue.withType(typeStore.get(ClassNames.getServiceStubClassName(service)))))).build());
return javaMethods;
}
use of com.google.api.generator.engine.ast.NewObjectExpr 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.NewObjectExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeNewObjectExpr_basic.
@Test
public void writeNewObjectExpr_basic() {
// isGeneric() is true, but generics() is empty.
ConcreteReference ref = ConcreteReference.withClazz(List.class);
TypeNode type = TypeNode.withReference(ref);
NewObjectExpr newObjectExpr = NewObjectExpr.builder().setIsGeneric(true).setType(type).build();
newObjectExpr.accept(writerVisitor);
assertEquals("new List<>()", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.NewObjectExpr in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeNewObjectExprImports_withArgs.
@Test
public void writeNewObjectExprImports_withArgs() {
// [Constructing] `new FileOutputStream(File file)` and the argument needs to be imported.
ConcreteReference fileOutputStreamRef = ConcreteReference.withClazz(FileOutputStream.class);
ConcreteReference fileRef = ConcreteReference.withClazz(File.class);
Variable fileVar = Variable.builder().setName("file").setType(TypeNode.withReference(fileRef)).build();
VariableExpr fileExpr = VariableExpr.builder().setVariable(fileVar).build();
NewObjectExpr newObjectExpr = NewObjectExpr.builder().setType(TypeNode.withReference(fileOutputStreamRef)).setArguments(Arrays.asList(fileExpr)).build();
newObjectExpr.accept(writerVisitor);
assertEquals(LineFormatter.lines("import java.io.File;\n", "import java.io.FileOutputStream;\n\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.NewObjectExpr in project gapic-generator-java by googleapis.
the class HttpJsonServiceStubClassComposer method createLongRunningClient.
@Override
protected List<Statement> createLongRunningClient(Service service, TypeStore typeStore) {
Method pollingMethod = service.operationPollingMethod();
if (pollingMethod != null) {
Expr thisExpr = ValueExpr.withValue(ThisObjectValue.withType(typeStore.get(getTransportContext().classNames().getTransportServiceStubClassName(service))));
VariableExpr callable = VariableExpr.withVariable(Variable.builder().setName(pollingMethod.name().toLowerCase() + "Callable").setType(TypeNode.withReference(ConcreteReference.withClazz(UnaryCallable.class))).build());
VariableExpr methodDescriptor = VariableExpr.withVariable(Variable.builder().setName(pollingMethod.name().toLowerCase() + "MethodDescriptor").setType(TypeNode.withReference(ConcreteReference.withClazz(ApiMethodDescriptor.class))).build());
TypeNode httpJsonLongRunningClientType = TypeNode.withReference(ConcreteReference.builder().setClazz(HttpJsonLongRunningClient.class).setGenerics(Arrays.asList(pollingMethod.inputType().reference(), pollingMethod.outputType().reference())).build());
NewObjectExpr HttpJsonLongRunningClient = NewObjectExpr.builder().setType(httpJsonLongRunningClientType).setArguments(Arrays.asList(callable, MethodInvocationExpr.builder().setExprReferenceExpr(methodDescriptor).setMethodName("getOperationSnapshotFactory").build(), MethodInvocationExpr.builder().setExprReferenceExpr(methodDescriptor).setMethodName("getPollingRequestFactory").build())).build();
AssignmentExpr assignLongRunningClient = AssignmentExpr.builder().setVariableExpr(VariableExpr.builder().setExprReferenceExpr(thisExpr).setVariable(Variable.builder().setName("longRunningClient").setType(TypeNode.withReference(ConcreteReference.withClazz(LongRunningClient.class))).build()).build()).setValueExpr(HttpJsonLongRunningClient).build();
return Arrays.asList(ExprStatement.withExpr(assignLongRunningClient));
} else {
return Collections.emptyList();
}
}
Aggregations