use of com.google.api.generator.engine.ast.AnnotationNode 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.AnnotationNode in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAnnotation_simple.
@Test
public void writeAnnotation_simple() {
AnnotationNode annotation = AnnotationNode.OVERRIDE;
annotation.accept(writerVisitor);
assertEquals("@Override\n", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.AnnotationNode in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createStaticCreatorMethods.
private static List<MethodDefinition> createStaticCreatorMethods(Service service, TypeStore typeStore) {
List<MethodDefinition> methods = new ArrayList<>();
String thisClientName = ClassNames.getServiceClientClassName(service);
String settingsName = ClassNames.getServiceSettingsClassName(service);
TypeNode thisClassType = typeStore.get(thisClientName);
TypeNode exceptionType = typeStore.get("IOException");
TypeNode settingsType = typeStore.get(settingsName);
Preconditions.checkNotNull(settingsType, String.format("Type %s not found", settingsName));
MethodInvocationExpr newBuilderExpr = MethodInvocationExpr.builder().setMethodName("newBuilder").setStaticReferenceType(settingsType).build();
MethodInvocationExpr buildExpr = MethodInvocationExpr.builder().setMethodName("build").setExprReferenceExpr(newBuilderExpr).build();
MethodInvocationExpr createMethodInvocationExpr = MethodInvocationExpr.builder().setMethodName("create").setArguments(Arrays.asList(buildExpr)).setReturnType(typeStore.get(thisClientName)).build();
MethodDefinition createMethodOne = MethodDefinition.builder().setHeaderCommentStatements(ServiceClientCommentComposer.createMethodNoArgComment(ClassNames.getServiceClientClassName(service))).setScope(ScopeNode.PUBLIC).setIsStatic(true).setIsFinal(true).setReturnType(thisClassType).setName("create").setThrowsExceptions(Arrays.asList(exceptionType)).setReturnExpr(createMethodInvocationExpr).build();
methods.add(createMethodOne);
// Second create(ServiceSettings settings) method.
VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setName("settings").setType(typeStore.get(settingsName)).build());
methods.add(MethodDefinition.builder().setHeaderCommentStatements(ServiceClientCommentComposer.createMethodSettingsArgComment(ClassNames.getServiceClientClassName(service))).setScope(ScopeNode.PUBLIC).setIsStatic(true).setIsFinal(true).setReturnType(thisClassType).setName("create").setThrowsExceptions(Arrays.asList(exceptionType)).setArguments(settingsVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(NewObjectExpr.builder().setType(thisClassType).setArguments(settingsVarExpr).build()).build());
// Third create(ServiceStub stub) method.
VariableExpr stubVarExpr = VariableExpr.withVariable(Variable.builder().setType(typeStore.get(ClassNames.getServiceStubClassName(service))).setName("stub").build());
AnnotationNode betaAnnotation = AnnotationNode.builder().setType(typeStore.get("BetaApi")).setDescription("A restructuring of stub classes is planned, so this may break in the future").build();
methods.add(MethodDefinition.builder().setHeaderCommentStatements(ServiceClientCommentComposer.createCreateMethodStubArgComment(ClassNames.getServiceClientClassName(service), settingsVarExpr.type())).setAnnotations(Arrays.asList(betaAnnotation)).setScope(ScopeNode.PUBLIC).setIsStatic(true).setIsFinal(true).setReturnType(thisClassType).setName("create").setArguments(stubVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(NewObjectExpr.builder().setType(thisClassType).setArguments(stubVarExpr).build()).build());
return methods;
}
use of com.google.api.generator.engine.ast.AnnotationNode in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createMethodDefaultMethod.
private static MethodDefinition createMethodDefaultMethod(Method method, String clientName, Map<String, Message> messageTypes, TypeStore typeStore, Map<String, ResourceName> resourceNames, List<Sample> samples) {
String methodName = JavaStyle.toLowerCamelCase(method.name());
TypeNode methodInputType = method.inputType();
TypeNode methodOutputType = method.isPaged() ? typeStore.get(String.format(PAGED_RESPONSE_TYPE_NAME_PATTERN, method.name())) : method.outputType();
List<AnnotationNode> annotations = new ArrayList<>();
if (method.hasLro()) {
LongrunningOperation lro = method.lro();
methodOutputType = TypeNode.withReference(typeStore.get("OperationFuture").reference().copyAndSetGenerics(Arrays.asList(lro.responseType().reference(), lro.metadataType().reference())));
if (method.lro().operationServiceStubType() != null) {
annotations.add(AnnotationNode.withTypeAndDescription(typeStore.get("BetaApi"), "The surface for long-running operations is not stable yet and may change in the" + " future."));
}
}
// Construct the method that accepts a request proto.
VariableExpr requestArgVarExpr = VariableExpr.builder().setVariable(Variable.builder().setName("request").setType(methodInputType).build()).setIsDecl(true).build();
String callableMethodName = method.isPaged() ? String.format(PAGED_CALLABLE_NAME_PATTERN, methodName) : String.format(CALLABLE_NAME_PATTERN, methodName);
if (method.hasLro()) {
callableMethodName = String.format(OPERATION_CALLABLE_NAME_PATTERN, methodName);
}
Optional<Sample> defaultMethodSample = Optional.of(ServiceClientMethodSampleComposer.composeCanonicalSample(method, typeStore.get(clientName), resourceNames, messageTypes));
Optional<String> defaultMethodDocSample = Optional.empty();
if (defaultMethodSample.isPresent()) {
samples.add(defaultMethodSample.get());
defaultMethodDocSample = Optional.of(SampleCodeWriter.writeInlineSample(defaultMethodSample.get().body()));
}
MethodInvocationExpr callableMethodExpr = MethodInvocationExpr.builder().setMethodName(callableMethodName).build();
callableMethodExpr = MethodInvocationExpr.builder().setMethodName(method.hasLro() ? "futureCall" : "call").setArguments(Arrays.asList(requestArgVarExpr.toBuilder().setIsDecl(false).build())).setExprReferenceExpr(callableMethodExpr).setReturnType(methodOutputType).build();
MethodDefinition.Builder methodBuilder = MethodDefinition.builder().setHeaderCommentStatements(ServiceClientCommentComposer.createRpcMethodHeaderComment(method, defaultMethodDocSample)).setScope(ScopeNode.PUBLIC).setIsFinal(true).setName(String.format(method.hasLro() ? "%sAsync" : "%s", methodName)).setArguments(Arrays.asList(requestArgVarExpr));
if (method.isDeprecated()) {
annotations.add(AnnotationNode.withType(TypeNode.DEPRECATED));
}
if (isProtoEmptyType(methodOutputType)) {
methodBuilder = methodBuilder.setBody(Arrays.asList(ExprStatement.withExpr(callableMethodExpr))).setReturnType(TypeNode.VOID);
} else {
methodBuilder = methodBuilder.setReturnExpr(callableMethodExpr).setReturnType(methodOutputType);
}
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
use of com.google.api.generator.engine.ast.AnnotationNode in project gapic-generator-java by googleapis.
the class AbstractServiceClientClassComposer method createConstructorMethods.
private List<MethodDefinition> createConstructorMethods(Service service, TypeStore typeStore, boolean hasLroClient) {
List<MethodDefinition> methods = new ArrayList<>();
String thisClientName = ClassNames.getServiceClientClassName(service);
String settingsName = ClassNames.getServiceSettingsClassName(service);
TypeNode thisClassType = typeStore.get(thisClientName);
TypeNode stubSettingsType = typeStore.get(ClassNames.getServiceStubSettingsClassName(service));
TypeNode exceptionType = typeStore.get("IOException");
VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setName("settings").setType(typeStore.get(settingsName)).build());
VariableExpr stubVarExpr = VariableExpr.withVariable(Variable.builder().setType(typeStore.get(ClassNames.getServiceStubClassName(service))).setName("stub").build());
// Create the ServiceClient(ServiceSettings settings) ctor.
List<Expr> ctorAssignmentExprs = new ArrayList<>();
Expr thisExpr = ValueExpr.withValue(ThisObjectValue.withType(thisClassType));
ctorAssignmentExprs.add(AssignmentExpr.builder().setVariableExpr(settingsVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setValueExpr(settingsVarExpr).build());
ctorAssignmentExprs.add(AssignmentExpr.builder().setVariableExpr(stubVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(CastExpr.builder().setType(stubSettingsType).setExpr(MethodInvocationExpr.builder().setExprReferenceExpr(settingsVarExpr).setMethodName("getStubSettings").setReturnType(stubSettingsType).build()).build()).setMethodName("createStub").setReturnType(stubVarExpr.type()).build()).build());
List<AssignmentExpr> operationsClientAssignExprs = createOperationsClientAssignExprs(thisExpr, stubVarExpr);
if (hasLroClient) {
ctorAssignmentExprs.addAll(operationsClientAssignExprs);
}
methods.add(MethodDefinition.constructorBuilder().setHeaderCommentStatements(ServiceClientCommentComposer.createProtectedCtorSettingsArgComment(ClassNames.getServiceClientClassName(service))).setScope(ScopeNode.PROTECTED).setReturnType(thisClassType).setArguments(settingsVarExpr.toBuilder().setIsDecl(true).build()).setThrowsExceptions(Arrays.asList(exceptionType)).setBody(ctorAssignmentExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build());
// Create the ServiceClient(ServiceStub stub) ctor.
ctorAssignmentExprs.clear();
ctorAssignmentExprs.add(AssignmentExpr.builder().setVariableExpr(settingsVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setValueExpr(ValueExpr.createNullExpr()).build());
ctorAssignmentExprs.add(AssignmentExpr.builder().setVariableExpr(stubVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setValueExpr(stubVarExpr).build());
if (hasLroClient) {
ctorAssignmentExprs.addAll(operationsClientAssignExprs);
}
AnnotationNode betaAnnotation = AnnotationNode.builder().setType(typeStore.get("BetaApi")).setDescription("A restructuring of stub classes is planned, so this may break in the future").build();
methods.add(MethodDefinition.constructorBuilder().setAnnotations(Arrays.asList(betaAnnotation)).setScope(ScopeNode.PROTECTED).setReturnType(thisClassType).setArguments(stubVarExpr.toBuilder().setIsDecl(true).build()).setBody(ctorAssignmentExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build());
return methods;
}
Aggregations