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();
}
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();
}
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();
}
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());
}
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();
}
Aggregations