Search in sources :

Example 1 with ClassDefinition

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

the class AbstractServiceStubSettingsClassComposer method generate.

@Override
public GapicClass generate(GapicContext context, Service service) {
    // TODO(miraleung): Robustify this against a null serviceConfig.
    GapicServiceConfig serviceConfig = context.serviceConfig();
    Map<String, Message> messageTypes = context.messages();
    String pakkage = String.format("%s.stub", service.pakkage());
    TypeStore typeStore = createDynamicTypes(service, pakkage);
    List<Sample> samples = new ArrayList<>();
    Set<String> deprecatedSettingVarNames = new HashSet<>();
    Map<String, VariableExpr> methodSettingsMemberVarExprs = createMethodSettingsClassMemberVarExprs(service, serviceConfig, typeStore, /* isNestedClass= */
    false, deprecatedSettingVarNames);
    String className = ClassNames.getServiceStubSettingsClassName(service);
    List<CommentStatement> classHeaderComments = createClassHeaderComments(service, typeStore.get(className), samples);
    ClassDefinition classDef = ClassDefinition.builder().setPackageString(pakkage).setHeaderCommentStatements(classHeaderComments).setAnnotations(createClassAnnotations(service)).setScope(ScopeNode.PUBLIC).setName(className).setExtendsType(createExtendsType(service, typeStore)).setStatements(createClassStatements(service, serviceConfig, methodSettingsMemberVarExprs, messageTypes, typeStore)).setMethods(createClassMethods(service, methodSettingsMemberVarExprs, deprecatedSettingVarNames, typeStore)).setNestedClasses(Arrays.asList(createNestedBuilderClass(service, serviceConfig, typeStore))).build();
    return GapicClass.create(GapicClass.Kind.STUB, classDef, SampleComposerUtil.handleDuplicateSamples(samples));
}
Also used : Message(com.google.api.generator.gapic.model.Message) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) Sample(com.google.api.generator.gapic.model.Sample) ArrayList(java.util.ArrayList) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) GapicServiceConfig(com.google.api.generator.gapic.model.GapicServiceConfig) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) HashSet(java.util.HashSet)

Example 2 with ClassDefinition

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

the class AbstractTransportServiceStubClassComposer method generate.

@Override
public GapicClass generate(GapicContext context, Service service) {
    String pakkage = service.pakkage() + ".stub";
    TypeStore typeStore = createDynamicTypes(service, pakkage);
    String className = getTransportContext().classNames().getTransportServiceStubClassName(service);
    GapicClass.Kind kind = Kind.STUB;
    Map<String, VariableExpr> protoMethodNameToDescriptorVarExprs = createProtoMethodNameToDescriptorClassMembers(service, getTransportContext().methodDescriptorClass());
    Map<String, VariableExpr> callableClassMemberVarExprs = createCallableClassMembers(service, typeStore);
    Map<String, VariableExpr> classMemberVarExprs = new LinkedHashMap<>();
    classMemberVarExprs.put(BACKGROUND_RESOURCES_MEMBER_NAME, VariableExpr.withVariable(Variable.builder().setName(BACKGROUND_RESOURCES_MEMBER_NAME).setType(FIXED_TYPESTORE.get("BackgroundResource")).build()));
    if (generateOperationsStubLogic(service)) {
        // Transport-specific service stub may have only one element of the following, thus get(0).
        TypeNode operationsStubType = getTransportOperationsStubType(service);
        classMemberVarExprs.put(getTransportContext().transportOperationsStubNames().get(0), VariableExpr.withVariable(Variable.builder().setName(getTransportContext().transportOperationsStubNames().get(0)).setType(operationsStubType).build()));
    }
    boolean operationPollingMethod = checkOperationPollingMethod(service);
    if (operationPollingMethod) {
        VariableExpr longRunningVarExpr = declareLongRunningClient();
        if (longRunningVarExpr != null) {
            classMemberVarExprs.put("longRunningClient", longRunningVarExpr);
        }
    }
    classMemberVarExprs.put(CALLABLE_FACTORY_MEMBER_NAME, VariableExpr.withVariable(Variable.builder().setName(CALLABLE_FACTORY_MEMBER_NAME).setType(getTransportContext().stubCallableFactoryType()).build()));
    Map<String, Message> messageTypes = context.messages();
    List<Statement> classStatements = createClassStatements(service, protoMethodNameToDescriptorVarExprs, callableClassMemberVarExprs, classMemberVarExprs, messageTypes);
    StubCommentComposer commentComposer = new StubCommentComposer(getTransportContext().transportName());
    ClassDefinition classDef = ClassDefinition.builder().setPackageString(pakkage).setHeaderCommentStatements(commentComposer.createTransportServiceStubClassHeaderComments(service.name(), service.isDeprecated())).setAnnotations(createClassAnnotations(service)).setScope(ScopeNode.PUBLIC).setName(className).setExtendsType(typeStore.get(getTransportContext().classNames().getServiceStubClassName(service))).setMethods(createClassMethods(context, service, typeStore, classMemberVarExprs, callableClassMemberVarExprs, protoMethodNameToDescriptorVarExprs, classStatements)).setStatements(classStatements).build();
    return GapicClass.create(kind, classDef);
}
Also used : Message(com.google.api.generator.gapic.model.Message) EmptyLineStatement(com.google.api.generator.engine.ast.EmptyLineStatement) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) LinkedHashMap(java.util.LinkedHashMap) StubCommentComposer(com.google.api.generator.gapic.composer.comment.StubCommentComposer) GapicClass(com.google.api.generator.gapic.model.GapicClass) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) Kind(com.google.api.generator.gapic.model.GapicClass.Kind) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode)

Example 3 with ClassDefinition

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

the class AbstractServiceClientClassComposer method generate.

@Override
public GapicClass generate(GapicContext context, Service service) {
    Map<String, ResourceName> resourceNames = context.helperResourceNames();
    Map<String, Message> messageTypes = context.messages();
    TypeStore typeStore = createTypes(service, messageTypes);
    String className = ClassNames.getServiceClientClassName(service);
    GapicClass.Kind kind = Kind.MAIN;
    String pakkage = service.pakkage();
    boolean hasLroClient = service.hasStandardLroMethods();
    List<Sample> samples = new ArrayList<>();
    Map<String, List<String>> grpcRpcsToJavaMethodNames = new HashMap<>();
    ClassDefinition classDef = ClassDefinition.builder().setHeaderCommentStatements(createClassHeaderComments(service, typeStore, resourceNames, messageTypes, samples)).setPackageString(pakkage).setAnnotations(createClassAnnotations(service, typeStore)).setScope(ScopeNode.PUBLIC).setName(className).setImplementsTypes(createClassImplements(typeStore)).setStatements(createFieldDeclarations(service, typeStore, hasLroClient)).setMethods(createClassMethods(service, messageTypes, typeStore, resourceNames, hasLroClient, grpcRpcsToJavaMethodNames, samples)).setNestedClasses(createNestedPagingClasses(service, messageTypes, typeStore)).build();
    updateGapicMetadata(context, service, className, grpcRpcsToJavaMethodNames);
    return GapicClass.create(kind, classDef, SampleComposerUtil.handleDuplicateSamples(samples));
}
Also used : Message(com.google.api.generator.gapic.model.Message) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ResourceName(com.google.api.generator.gapic.model.ResourceName) Sample(com.google.api.generator.gapic.model.Sample) ArrayList(java.util.ArrayList) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) GapicClass(com.google.api.generator.gapic.model.GapicClass) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) Kind(com.google.api.generator.gapic.model.GapicClass.Kind) List(java.util.List) ArrayList(java.util.ArrayList)

Example 4 with ClassDefinition

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

the class AbstractServiceSettingsClassComposer method generate.

@Override
public GapicClass generate(GapicContext context, Service service) {
    String pakkage = service.pakkage();
    TypeStore typeStore = createDynamicTypes(service);
    String className = ClassNames.getServiceSettingsClassName(service);
    GapicClass.Kind kind = Kind.MAIN;
    List<Sample> samples = new ArrayList<>();
    List<CommentStatement> classHeaderComments = createClassHeaderComments(service, typeStore.get(className), samples);
    ClassDefinition classDef = ClassDefinition.builder().setPackageString(pakkage).setHeaderCommentStatements(classHeaderComments).setAnnotations(createClassAnnotations(service)).setScope(ScopeNode.PUBLIC).setName(className).setExtendsType(TypeNode.withReference(FIXED_TYPESTORE.get("ClientSettings").reference().copyAndSetGenerics(Arrays.asList(typeStore.get(ClassNames.getServiceSettingsClassName(service)).reference())))).setMethods(createClassMethods(service, typeStore)).setNestedClasses(Arrays.asList(createNestedBuilderClass(service, typeStore))).build();
    return GapicClass.create(kind, classDef, SampleComposerUtil.handleDuplicateSamples(samples));
}
Also used : CommentStatement(com.google.api.generator.engine.ast.CommentStatement) Sample(com.google.api.generator.gapic.model.Sample) GapicClass(com.google.api.generator.gapic.model.GapicClass) ArrayList(java.util.ArrayList) TypeStore(com.google.api.generator.gapic.composer.store.TypeStore) Kind(com.google.api.generator.gapic.model.GapicClass.Kind) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition)

Example 5 with ClassDefinition

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

the class AbstractServiceClientClassComposer method createNestedPagingClasses.

private static List<ClassDefinition> createNestedPagingClasses(Service service, Map<String, Message> messageTypes, TypeStore typeStore) {
    List<ClassDefinition> nestedClasses = new ArrayList<>();
    for (Method method : service.methods()) {
        if (!method.isPaged()) {
            continue;
        }
        // Find the repeated field.
        Message methodOutputMessage = messageTypes.get(method.outputType().reference().fullName());
        Field repeatedPagedResultsField = methodOutputMessage.findAndUnwrapPaginatedRepeatedField();
        Preconditions.checkNotNull(repeatedPagedResultsField, String.format("No repeated field found on message %s for method %s", methodOutputMessage.name(), method.name()));
        TypeNode repeatedResponseType = repeatedPagedResultsField.type();
        nestedClasses.add(createNestedRpcPagedResponseClass(method, repeatedResponseType, messageTypes, typeStore));
        nestedClasses.add(createNestedRpcPageClass(method, repeatedResponseType, messageTypes, typeStore));
        nestedClasses.add(createNestedRpcFixedSizeCollectionClass(method, repeatedResponseType, messageTypes, typeStore));
    }
    return nestedClasses;
}
Also used : Field(com.google.api.generator.gapic.model.Field) Message(com.google.api.generator.gapic.model.Message) ArrayList(java.util.ArrayList) Method(com.google.api.generator.gapic.model.Method) TypeNode(com.google.api.generator.engine.ast.TypeNode) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition)

Aggregations

ClassDefinition (com.google.api.generator.engine.ast.ClassDefinition)23 TypeStore (com.google.api.generator.gapic.composer.store.TypeStore)13 GapicClass (com.google.api.generator.gapic.model.GapicClass)12 Kind (com.google.api.generator.gapic.model.GapicClass.Kind)10 ArrayList (java.util.ArrayList)9 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)8 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)8 Message (com.google.api.generator.gapic.model.Message)7 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)6 TypeNode (com.google.api.generator.engine.ast.TypeNode)6 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)5 Statement (com.google.api.generator.engine.ast.Statement)5 ResourceName (com.google.api.generator.gapic.model.ResourceName)5 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)4 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)4 Reference (com.google.api.generator.engine.ast.Reference)4 List (java.util.List)4 BetaApi (com.google.api.core.BetaApi)3