Search in sources :

Example 6 with AnonymousClassExpr

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

the class BatchingDescriptorComposer method createBatchingDescriptorFieldDeclExpr.

public static Expr createBatchingDescriptorFieldDeclExpr(Method method, GapicBatchingSettings batchingSettings, Map<String, Message> messageTypes) {
    List<MethodDefinition> javaMethods = new ArrayList<>();
    javaMethods.add(createGetBatchPartitionKeyMethod(method, batchingSettings, messageTypes));
    javaMethods.add(createGetRequestBuilderMethod(method, batchingSettings));
    javaMethods.add(createSplitResponseMethod(method, batchingSettings, messageTypes));
    javaMethods.add(createSplitExceptionMethod(method));
    javaMethods.add(createCountElementsMethod(method, batchingSettings));
    javaMethods.add(createCountByteSMethod(method));
    TypeNode batchingDescriptorType = toType(BATCHING_DESCRIPTOR_REF, method.inputType(), method.outputType());
    AnonymousClassExpr batchingDescriptorClassExpr = AnonymousClassExpr.builder().setType(batchingDescriptorType).setMethods(javaMethods).build();
    String varName = String.format(BATCHING_DESC_PATTERN, JavaStyle.toUpperSnakeCase(method.name()));
    return AssignmentExpr.builder().setVariableExpr(VariableExpr.builder().setVariable(Variable.builder().setType(batchingDescriptorType).setName(varName).build()).setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setIsFinal(true).build()).setValueExpr(batchingDescriptorClassExpr).build();
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) ArrayList(java.util.ArrayList) TypeNode(com.google.api.generator.engine.ast.TypeNode) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr)

Example 7 with AnonymousClassExpr

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

the class DefaultValueComposer method createAnonymousResourceNameClassValue.

@VisibleForTesting
static AnonymousClassExpr createAnonymousResourceNameClassValue(String fieldOrMessageName) {
    TypeNode stringMapType = TypeNode.withReference(ConcreteReference.builder().setClazz(Map.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(String.class))).build());
    // Method code:
    // @Override
    // public Map<String, String> getFieldValuesMap() {
    // Map<String, String> fieldValuesMap = new HashMap<>();
    // fieldValuesMap.put("resource", "resource-12345");
    // return fieldValuesMap;
    // }
    VariableExpr fieldValuesMapVarExpr = VariableExpr.withVariable(Variable.builder().setType(stringMapType).setName("fieldValuesMap").build());
    StringObjectValue fieldOrMessageStringValue = StringObjectValue.withValue(String.format("%s%s", fieldOrMessageName, fieldOrMessageName.hashCode()));
    List<Expr> bodyExprs = Arrays.asList(AssignmentExpr.builder().setVariableExpr(fieldValuesMapVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(NewObjectExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(HashMap.class))).setIsGeneric(true).build()).build(), MethodInvocationExpr.builder().setExprReferenceExpr(fieldValuesMapVarExpr).setMethodName("put").setArguments(ValueExpr.withValue(StringObjectValue.withValue(fieldOrMessageName)), ValueExpr.withValue(fieldOrMessageStringValue)).build());
    MethodDefinition getFieldValuesMapMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(stringMapType).setName("getFieldValuesMap").setBody(bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).setReturnExpr(fieldValuesMapVarExpr).build();
    // Method code:
    // @Override
    // public String getFieldValue(String fieldName) {
    // return getFieldValuesMap().get(fieldName);
    // }
    VariableExpr fieldNameVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.STRING).setName("fieldName").build());
    MethodDefinition getFieldValueMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.STRING).setName("getFieldValue").setArguments(fieldNameVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName("getFieldValuesMap").build()).setMethodName("get").setArguments(fieldNameVarExpr).setReturnType(TypeNode.STRING).build()).build();
    return AnonymousClassExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(com.google.api.resourcenames.ResourceName.class))).setMethods(Arrays.asList(getFieldValuesMapMethod, getFieldValueMethod)).build();
}
Also used : StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) 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) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) HashMap(java.util.HashMap) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) HashMap(java.util.HashMap) Map(java.util.Map) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 8 with AnonymousClassExpr

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

the class ServiceClientCallableMethodSampleComposer method composeStreamClientSample.

private static Sample composeStreamClientSample(Method method, VariableExpr clientVarExpr, AssignmentExpr requestAssignmentExpr) {
    List<Expr> bodyExprs = new ArrayList<>();
    // Create responseObserver variable expression.
    // e.g. ApiStream<EchoResponse> responseObserver
    TypeNode responseObserverType = TypeNode.withReference(ConcreteReference.builder().setClazz(ApiStreamObserver.class).setGenerics(method.inputType().reference()).build());
    VariableExpr responseObserverVarExpr = VariableExpr.withVariable(Variable.builder().setName("responseObserver").setType(responseObserverType).build());
    // Create an anonymous class for ApiStreamObserver that contains the methods onNext, onError,
    // and onCompleted.
    MethodDefinition onNextMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setName("onNext").setArguments(VariableExpr.builder().setVariable(Variable.builder().setName("response").setType(method.outputType()).build()).setIsDecl(true).build()).setBody(Arrays.asList(CommentStatement.withComment(LineComment.withComment("Do something when a response is received.")))).setReturnType(TypeNode.VOID).build();
    MethodDefinition onErrorMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setName("onError").setArguments(VariableExpr.builder().setVariable(Variable.builder().setName("t").setType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class))).build()).setIsDecl(true).build()).setBody(Arrays.asList(CommentStatement.withComment(LineComment.withComment("Add error-handling")))).setReturnType(TypeNode.VOID).build();
    MethodDefinition onCompletedMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setName("onCompleted").setBody(Arrays.asList(CommentStatement.withComment(LineComment.withComment("Do something when complete.")))).setReturnType(TypeNode.VOID).build();
    AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(responseObserverType).setMethods(onNextMethod, onErrorMethod, onCompletedMethod).build();
    AssignmentExpr responseObserverAssignmentExpr = AssignmentExpr.builder().setVariableExpr(responseObserverVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(anonymousClassExpr).build();
    bodyExprs.add(responseObserverAssignmentExpr);
    // Create an assignment expression for request observer variable expression when invoking the
    // client's streaming call.
    // e.g. ApiStreamObserver<EchoRequest> requestObserver =
    // echoClient.collectCallable().clientStreamingCall(responseObserver);
    TypeNode requestObserverType = TypeNode.withReference(ConcreteReference.builder().setClazz(ApiStreamObserver.class).setGenerics(method.inputType().reference()).build());
    VariableExpr requestObserverVarExpr = VariableExpr.withVariable(Variable.builder().setName("requestObserver").setType(responseObserverType).build());
    MethodInvocationExpr clientStreamCallMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientVarExpr).setMethodName(JavaStyle.toLowerCamelCase(method.name())).build();
    clientStreamCallMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(clientStreamCallMethodInvocationExpr).setArguments(responseObserverVarExpr).setMethodName("clientStreamingCall").setReturnType(requestObserverType).build();
    AssignmentExpr requestObserverAssignmentExpr = AssignmentExpr.builder().setVariableExpr(requestObserverVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(clientStreamCallMethodInvocationExpr).build();
    bodyExprs.add(requestObserverAssignmentExpr);
    // Add assignment expression of request with its default value.
    bodyExprs.add(requestAssignmentExpr);
    // Invoke onNext method with argument of request variable.
    // e.g. requestObserver.onNext(request)
    MethodInvocationExpr onNextMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(requestObserverVarExpr).setMethodName("onNext").setArguments(requestAssignmentExpr.variableExpr().toBuilder().setIsDecl(false).build()).build();
    bodyExprs.add(onNextMethodExpr);
    RegionTag regionTag = RegionTag.builder().setServiceName(clientVarExpr.variable().identifier().name()).setRpcName(method.name()).setIsAsynchronous(true).build();
    return Sample.builder().setBody(bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).setRegionTag(regionTag).build();
}
Also used : Expr(com.google.api.generator.engine.ast.Expr) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) RegionTag(com.google.api.generator.gapic.model.RegionTag) ApiStreamObserver(com.google.api.gax.rpc.ApiStreamObserver) 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) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr)

Example 9 with AnonymousClassExpr

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

the class JavaWriterVisitorTest method writeAnonymousClassExpr_generics.

@Test
public void writeAnonymousClassExpr_generics() {
    // [Constructing] Function<List<IOException>, MethodDefinition>
    ConcreteReference exceptionListRef = ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(IOException.class))).build();
    ConcreteReference methodDefinitionRef = ConcreteReference.withClazz(MethodDefinition.class);
    ConcreteReference ref = ConcreteReference.builder().setClazz(Function.class).setGenerics(Arrays.asList(exceptionListRef, methodDefinitionRef)).build();
    TypeNode type = TypeNode.withReference(ref);
    // [Constructing] an input argument whose type is `List<IOException>`
    VariableExpr arg = VariableExpr.builder().setVariable(Variable.builder().setName("arg").setType(TypeNode.withReference(exceptionListRef)).build()).setIsDecl(true).build();
    // [Constructing] a return variable expression whose type is `MethodDefinition`
    VariableExpr returnArg = VariableExpr.builder().setVariable(Variable.builder().setName("returnArg").setType(TypeNode.withReference(methodDefinitionRef)).build()).build();
    MethodDefinition method = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.withReference(methodDefinitionRef)).setArguments(Arrays.asList(arg)).setIsOverride(true).setReturnExpr(returnArg).setName("apply").build();
    AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setMethods(Arrays.asList(method)).build();
    anonymousClassExpr.accept(writerVisitor);
    String expected = LineFormatter.lines("new Function<List<IOException>, MethodDefinition>() {\n", "@Override\n", "public MethodDefinition apply(List<IOException> arg) {\n", "return returnArg;\n", "}\n\n}");
    assertEquals(expected, writerVisitor.write());
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) IOException(java.io.IOException) TypeNode(com.google.api.generator.engine.ast.TypeNode) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) Test(org.junit.Test)

Example 10 with AnonymousClassExpr

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

the class JavaCodeGeneratorTest method createAddBookToShelf.

private MethodDefinition createAddBookToShelf() {
    Variable bookVar = createVarFromVaporRef(bookClassRef, "book");
    AnonymousClassExpr anonymousBookClassExpr = createAnonymousClass();
    AssignmentExpr createNewBook = createAssignmentExpr(createVarDeclExpr(bookVar), anonymousBookClassExpr);
    return MethodDefinition.builder().setHeaderCommentStatements(Arrays.asList(createPreMethodLineComment("Private helper."))).setName("addBookToShelf").setReturnType(TypeNode.withReference(bookClassRef)).setArguments(Arrays.asList(createVarDeclExpr(bookKindVar), createVarDeclExpr(shelfVar))).setScope(ScopeNode.PRIVATE).setBody(Arrays.asList(ExprStatement.withExpr(createNewBook))).setReturnExpr(VariableExpr.withVariable(bookVar)).build();
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr)

Aggregations

AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)11 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)10 TypeNode (com.google.api.generator.engine.ast.TypeNode)10 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)8 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)8 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)6 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)6 Expr (com.google.api.generator.engine.ast.Expr)5 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)5 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)5 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)4 IOException (java.io.IOException)4 Test (org.junit.Test)4 Variable (com.google.api.generator.engine.ast.Variable)3 ArrayList (java.util.ArrayList)3 MonitoredResourceDescriptor (com.google.api.MonitoredResourceDescriptor)2 ApiFunction (com.google.api.core.ApiFunction)2 ApiFuture (com.google.api.core.ApiFuture)2 BetaApi (com.google.api.core.BetaApi)2 BatchingSettings (com.google.api.gax.batching.BatchingSettings)2