Search in sources :

Example 6 with AnnotationNode

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();
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) UnaryCallable(com.google.api.gax.rpc.UnaryCallable) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) OperationCallable(com.google.api.gax.rpc.OperationCallable) ArrayList(java.util.ArrayList) VaporReference(com.google.api.generator.engine.ast.VaporReference) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) HttpJsonOperationSnapshotCallable(com.google.api.gax.httpjson.HttpJsonOperationSnapshotCallable) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 7 with AnnotationNode

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());
}
Also used : AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) Test(org.junit.Test)

Example 8 with AnnotationNode

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;
}
Also used : MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 9 with AnnotationNode

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();
}
Also used : LongrunningOperation(com.google.api.generator.gapic.model.LongrunningOperation) Sample(com.google.api.generator.gapic.model.Sample) ArrayList(java.util.ArrayList) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 10 with AnnotationNode

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;
}
Also used : ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) AnnotationNode(com.google.api.generator.engine.ast.AnnotationNode) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Aggregations

AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)14 TypeNode (com.google.api.generator.engine.ast.TypeNode)12 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)11 ArrayList (java.util.ArrayList)11 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)10 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)9 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)8 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)8 CastExpr (com.google.api.generator.engine.ast.CastExpr)7 Expr (com.google.api.generator.engine.ast.Expr)7 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)7 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)7 Statement (com.google.api.generator.engine.ast.Statement)7 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)7 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)7 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)6 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)6 Reference (com.google.api.generator.engine.ast.Reference)6 Variable (com.google.api.generator.engine.ast.Variable)6 BetaApi (com.google.api.core.BetaApi)5