use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class MockServiceImplClassComposer method createGenericProtoMethodOverride.
private static MethodDefinition createGenericProtoMethodOverride(Method protoMethod) {
ConcreteReference streamObserverRef = ConcreteReference.withClazz(StreamObserver.class);
TypeNode objectType = TypeNode.withReference(javaObjectReference);
VariableExpr localResponseVarExpr = VariableExpr.withVariable(Variable.builder().setName("response").setType(objectType).build());
VariableExpr responseObserverVarExpr = VariableExpr.withVariable(Variable.builder().setName("responseObserver").setType(TypeNode.withReference(streamObserverRef.copyAndSetGenerics(Arrays.asList(protoMethod.outputType().reference())))).build());
if (protoMethod.stream().equals(Stream.CLIENT) || protoMethod.stream().equals(Stream.BIDI)) {
return createGenericClientStreamingProtoMethodOverride(protoMethod, responseObserverVarExpr, localResponseVarExpr);
}
VariableExpr requestArgVarExpr = VariableExpr.withVariable(Variable.builder().setName("request").setType(protoMethod.inputType()).build());
return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName(JavaStyle.toLowerCamelCase(protoMethod.name())).setArguments(Arrays.asList(requestArgVarExpr.toBuilder().setIsDecl(true).build(), responseObserverVarExpr.toBuilder().setIsDecl(true).build())).setBody(Arrays.asList(ExprStatement.withExpr(AssignmentExpr.builder().setVariableExpr(localResponseVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setMethodName("poll").setExprReferenceExpr(responsesVarExpr).setReturnType(objectType).build()).build()), createHandleObjectStatement(protoMethod, requestArgVarExpr, responseObserverVarExpr, localResponseVarExpr))).build();
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class ServiceClientTestClassComposer method createSetUpMethod.
@Override
protected MethodDefinition createSetUpMethod(Service service, Map<String, VariableExpr> classMemberVarExprs, TypeStore typeStore) {
VariableExpr clientVarExpr = classMemberVarExprs.get(CLIENT_VAR_NAME);
VariableExpr serviceHelperVarExpr = classMemberVarExprs.get(SERVICE_HELPER_VAR_NAME);
VariableExpr channelProviderVarExpr = classMemberVarExprs.get(CHANNEL_PROVIDER_VAR_NAME);
Expr resetServiceHelperExpr = MethodInvocationExpr.builder().setExprReferenceExpr(serviceHelperVarExpr).setMethodName("reset").build();
Expr channelProviderInitExpr = AssignmentExpr.builder().setVariableExpr(channelProviderVarExpr).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(serviceHelperVarExpr).setMethodName("createChannelProvider").setReturnType(channelProviderVarExpr.type()).build()).build();
TypeNode settingsType = typeStore.get(ClassNames.getServiceSettingsClassName(service));
VariableExpr localSettingsVarExpr = VariableExpr.withVariable(Variable.builder().setName("settings").setType(settingsType).build());
Expr settingsBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(settingsType).setMethodName("newBuilder").build();
Function<Expr, BiFunction<String, Expr, MethodInvocationExpr>> methodBuilderFn = methodExpr -> (mName, argExpr) -> MethodInvocationExpr.builder().setExprReferenceExpr(methodExpr).setMethodName(mName).setArguments(Arrays.asList(argExpr)).build();
settingsBuilderExpr = methodBuilderFn.apply(settingsBuilderExpr).apply("setTransportChannelProvider", classMemberVarExprs.get(CHANNEL_PROVIDER_VAR_NAME));
settingsBuilderExpr = methodBuilderFn.apply(settingsBuilderExpr).apply("setCredentialsProvider", MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("NoCredentialsProvider")).setMethodName("create").build());
settingsBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingsBuilderExpr).setMethodName("build").setReturnType(localSettingsVarExpr.type()).build();
Expr initLocalSettingsExpr = AssignmentExpr.builder().setVariableExpr(localSettingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(settingsBuilderExpr).build();
Expr initClientExpr = AssignmentExpr.builder().setVariableExpr(clientVarExpr).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get(ClassNames.getServiceClientClassName(service))).setMethodName("create").setArguments(Arrays.asList(localSettingsVarExpr)).setReturnType(clientVarExpr.type()).build()).build();
return MethodDefinition.builder().setAnnotations(Arrays.asList(AnnotationNode.withType(FIXED_TYPESTORE.get("Before")))).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("setUp").setThrowsExceptions(Arrays.asList(FIXED_TYPESTORE.get("IOException"))).setBody(Arrays.asList(resetServiceHelperExpr, channelProviderInitExpr, initLocalSettingsExpr, initClientExpr).stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build();
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class ServiceStubSettingsClassComposer method createDefaultTransportChannelProviderMethod.
@Override
public MethodDefinition createDefaultTransportChannelProviderMethod() {
TypeNode returnType = FIXED_TYPESTORE.get("TransportChannelProvider");
MethodInvocationExpr transportProviderBuilderExpr = MethodInvocationExpr.builder().setMethodName("defaultGrpcTransportProviderBuilder").build();
transportProviderBuilderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(transportProviderBuilderExpr).setMethodName("build").setReturnType(returnType).build();
return MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(returnType).setName("defaultTransportChannelProvider").setReturnExpr(transportProviderBuilderExpr).build();
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createToStringListMethod.
private static MethodDefinition createToStringListMethod(TypeNode thisClassType) {
TypeNode listClassType = TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(thisClassType.reference())).build());
VariableExpr valuesVarExpr = VariableExpr.withVariable(Variable.builder().setName("values").setType(listClassType).build());
TypeNode listStringType = TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class))).build());
VariableExpr listVarExpr = VariableExpr.withVariable(Variable.builder().setName("list").setType(listStringType).build());
AssignmentExpr listAssignExpr = AssignmentExpr.builder().setVariableExpr(listVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(NewObjectExpr.builder().setType(TypeNode.withReference(ConcreteReference.withClazz(ArrayList.class))).setIsGeneric(true).setArguments(Arrays.asList(MethodInvocationExpr.builder().setExprReferenceExpr(valuesVarExpr).setMethodName("size").build())).build()).build();
VariableExpr valueVarExpr = VariableExpr.withVariable(Variable.builder().setName("value").setType(thisClassType).build());
// We use an equality check instead of Objects.isNull() for Java 7 compatibility.
Expr isNullCheck = RelationalOperationExpr.equalToWithExprs(valueVarExpr, ValueExpr.createNullExpr());
Statement listAddEmptyStringStatement = ExprStatement.withExpr(MethodInvocationExpr.builder().setExprReferenceExpr(listVarExpr).setMethodName("add").setArguments(Arrays.asList(ValueExpr.withValue(StringObjectValue.withValue("")))).build());
Statement listAddValueStatement = ExprStatement.withExpr(MethodInvocationExpr.builder().setExprReferenceExpr(listVarExpr).setMethodName("add").setArguments(Arrays.asList(MethodInvocationExpr.builder().setExprReferenceExpr(valueVarExpr).setMethodName("toString").build())).build());
IfStatement ifStatement = IfStatement.builder().setConditionExpr(isNullCheck).setBody(Arrays.asList(listAddEmptyStringStatement)).setElseBody(Arrays.asList(listAddValueStatement)).build();
ForStatement forStatement = ForStatement.builder().setLocalVariableExpr(valueVarExpr.toBuilder().setIsDecl(true).build()).setCollectionExpr(valuesVarExpr).setBody(Arrays.asList(ifStatement)).build();
return MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(listStringType).setName("toStringList").setArguments(Arrays.asList(valuesVarExpr.toBuilder().setIsDecl(true).build())).setBody(Arrays.asList(ExprStatement.withExpr(listAssignExpr), forStatement)).setReturnExpr(listVarExpr).build();
}
use of com.google.api.generator.engine.ast.TypeNode in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createFixedClassMemberVariables.
private static Map<String, VariableExpr> createFixedClassMemberVariables() {
Map<String, TypeNode> memberVars = new HashMap<>();
Reference stringRef = ConcreteReference.withClazz(String.class);
memberVars.put("fieldValuesMap", TypeNode.withReference(ConcreteReference.builder().setClazz(Map.class).setGenerics(Arrays.asList(stringRef, stringRef)).build()));
memberVars.put("pathTemplate", TypeNode.withReference(ConcreteReference.withClazz(PathTemplate.class)));
memberVars.put("fixedValue", TypeNode.STRING);
return memberVars.entrySet().stream().map(e -> Variable.builder().setName(e.getKey()).setType(e.getValue()).build()).collect(Collectors.toMap(v -> v.identifier().name(), v -> VariableExpr.withVariable(v)));
}
Aggregations