Search in sources :

Example 41 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.

the class MockServiceClassComposer method createGetRequestsMethod.

private static MethodDefinition createGetRequestsMethod(VariableExpr serviceImplVarExpr, TypeStore typeStore) {
    TypeNode returnType = TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(typeStore.get("AbstractMessage").reference())).build());
    String methodName = "getRequests";
    Expr returnExpr = MethodInvocationExpr.builder().setMethodName(methodName).setReturnType(returnType).setExprReferenceExpr(serviceImplVarExpr).build();
    return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(returnType).setName(methodName).setReturnExpr(returnExpr).build();
}
Also used : NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) List(java.util.List) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 42 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.

the class MockServiceClassComposer method createConstructor.

private static MethodDefinition createConstructor(Service service, VariableExpr serviceImplVarExpr, TypeStore typeStore) {
    String mockImplName = ClassNames.getMockServiceImplClassName(service);
    Expr serviceImplAssignExpr = AssignmentExpr.builder().setVariableExpr(serviceImplVarExpr).setValueExpr(NewObjectExpr.builder().setType(typeStore.get(mockImplName)).build()).build();
    return MethodDefinition.constructorBuilder().setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(serviceImplAssignExpr))).setReturnType(typeStore.get(ClassNames.getMockServiceClassName(service))).build();
}
Also used : NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr)

Example 43 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr 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 44 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.

the class HttpJsonServiceStubClassComposer method createTypeRegistry.

@Override
protected List<Statement> createTypeRegistry(Service service) {
    TypeNode typeRegistryType = FIXED_REST_TYPESTORE.get(TypeRegistry.class.getSimpleName());
    VariableExpr typeRegistryVarExpr = TYPE_REGISTRY_VAR_EXPR.toBuilder().setIsDecl(true).setIsStatic(true).setScope(ScopeNode.PRIVATE).setIsFinal(true).build();
    Map<String, TypeNode> anyTypes = new HashMap<>();
    for (Method method : service.methods()) {
        if (method.hasLro()) {
            TypeNode anyType = method.lro().responseType();
            anyTypes.put(anyType.reference().fullName(), anyType);
            anyType = method.lro().metadataType();
            anyTypes.put(anyType.reference().fullName(), anyType);
        }
    }
    Expr typeRegistryBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(typeRegistryType).setMethodName("newBuilder").build();
    for (TypeNode anyType : anyTypes.values()) {
        typeRegistryBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(typeRegistryBuilderExpr).setMethodName("add").setArguments(MethodInvocationExpr.builder().setStaticReferenceType(anyType).setMethodName("getDescriptor").build()).build();
    }
    typeRegistryBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(typeRegistryBuilderExpr).setMethodName("build").setReturnType(typeRegistryType).build();
    return Collections.singletonList(ExprStatement.withExpr(AssignmentExpr.builder().setVariableExpr(typeRegistryVarExpr).setValueExpr(typeRegistryBuilderExpr).build()));
}
Also used : ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) HashMap(java.util.HashMap) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Method(com.google.api.generator.gapic.model.Method) TypeRegistry(com.google.protobuf.TypeRegistry)

Example 45 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr in project gapic-generator-java by googleapis.

the class SampleComposer method createExecutableSample.

private static ClassDefinition createExecutableSample(List<CommentStatement> fileHeader, String packageName, String sampleClassName, List<AssignmentExpr> sampleVariableAssignments, List<Statement> sampleBody, RegionTag regionTag) {
    String sampleMethodName = JavaStyle.toLowerCamelCase(sampleClassName);
    List<VariableExpr> sampleMethodArgs = composeSampleMethodArgs(sampleVariableAssignments);
    MethodDefinition mainMethod = composeMainMethod(composeMainBody(sampleVariableAssignments, composeInvokeMethodStatement(sampleMethodName, sampleMethodArgs)));
    MethodDefinition sampleMethod = composeSampleMethod(sampleMethodName, sampleMethodArgs, sampleBody);
    return composeSampleClass(fileHeader, packageName, sampleClassName, mainMethod, sampleMethod, regionTag);
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr)

Aggregations

VariableExpr (com.google.api.generator.engine.ast.VariableExpr)217 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)137 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)132 TypeNode (com.google.api.generator.engine.ast.TypeNode)118 Expr (com.google.api.generator.engine.ast.Expr)114 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)107 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)91 Variable (com.google.api.generator.engine.ast.Variable)88 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)87 ArrayList (java.util.ArrayList)87 Test (org.junit.Test)82 Statement (com.google.api.generator.engine.ast.Statement)80 List (java.util.List)64 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)62 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)58 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)56 CastExpr (com.google.api.generator.engine.ast.CastExpr)53 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)53 Arrays (java.util.Arrays)48 IfStatement (com.google.api.generator.engine.ast.IfStatement)47