use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class AbstractServiceSettingsClassComposer method createCreatorMethod.
private static MethodDefinition createCreatorMethod(Service service, TypeStore typeStore) {
TypeNode stubClassType = typeStore.get(ClassNames.getServiceStubSettingsClassName(service));
VariableExpr stubVarExpr = VariableExpr.withVariable(Variable.builder().setName("stub").setType(stubClassType).build());
TypeNode thisClassType = typeStore.get(ClassNames.getServiceSettingsClassName(service));
MethodInvocationExpr stubBuilderMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(stubVarExpr).setMethodName("toBuilder").build();
TypeNode stubBuilderType = TypeNode.withReference(VaporReference.builder().setName("Builder").setEnclosingClassNames(ClassNames.getServiceSettingsClassName(service)).setPakkage(service.pakkage()).build());
Expr returnMethodExpr = NewObjectExpr.builder().setType(stubBuilderType).setArguments(stubBuilderMethodExpr).build();
returnMethodExpr = MethodInvocationExpr.builder().setExprReferenceExpr(returnMethodExpr).setMethodName("build").setReturnType(thisClassType).build();
return MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setIsFinal(true).setReturnType(thisClassType).setName("create").setArguments(Arrays.asList(stubVarExpr.toBuilder().setIsDecl(true).build())).setThrowsExceptions(Arrays.asList(FIXED_TYPESTORE.get("IOException"))).setReturnExpr(returnMethodExpr).build();
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class DefaultValueComposer method createSimpleMessageBuilderValue.
public static Expr createSimpleMessageBuilderValue(Message message, Map<String, ResourceName> resourceNames, Map<String, Message> messageTypes, Map<String, String> valuePatterns) {
MethodInvocationExpr builderExpr = MethodInvocationExpr.builder().setStaticReferenceType(message.type()).setMethodName("newBuilder").build();
for (Field field : message.fields()) {
if (// Avoid colliding fields.
field.isContainedInOneof() || ((field.isMessage() || (field.isEnum() && // Avoid importing unparsed messages.
message.operationResponse() == null)) && !field.isRepeated() && !messageTypes.containsKey(field.type().reference().fullName()))) {
continue;
}
String setterMethodNamePattern = "set%s";
if (field.isRepeated()) {
setterMethodNamePattern = field.isMap() ? "putAll%s" : "addAll%s";
}
Expr defaultExpr = null;
if (field.hasResourceReference() && resourceNames.get(field.resourceReference().resourceTypeString()) != null) {
defaultExpr = createResourceHelperValue(resourceNames.get(field.resourceReference().resourceTypeString()), field.resourceReference().isChildType(), resourceNames.values().stream().collect(Collectors.toList()), message.name(), /* allowAnonResourceNameClass = */
false);
defaultExpr = MethodInvocationExpr.builder().setExprReferenceExpr(defaultExpr).setMethodName("toString").setReturnType(TypeNode.STRING).build();
} else {
if (message.operationResponse() != null) {
if (field.name().equals(message.operationResponse().statusFieldName())) {
String statusTypeName = message.operationResponse().statusFieldTypeName();
String statusClassName = statusTypeName.substring(statusTypeName.lastIndexOf('.') + 1);
TypeNode statusType = TypeNode.withReference(VaporReference.builder().setName(statusClassName).setPakkage(message.type().reference().fullName()).setIsStaticImport(false).build());
defaultExpr = VariableExpr.builder().setVariable(Variable.builder().setName("DONE").setType(statusType).build()).setStaticReferenceType(statusType).build();
} else if (field.name().equals(message.operationResponse().errorCodeFieldName())) {
defaultExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(field.type()).setValue("0").build());
}
}
if (defaultExpr == null) {
defaultExpr = createValue(field, true, resourceNames, messageTypes, valuePatterns);
}
}
builderExpr = MethodInvocationExpr.builder().setExprReferenceExpr(builderExpr).setMethodName(String.format(setterMethodNamePattern, JavaStyle.toUpperCamelCase(field.name()))).setArguments(defaultExpr).build();
}
return MethodInvocationExpr.builder().setExprReferenceExpr(builderExpr).setMethodName("build").setReturnType(message.type()).build();
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class MockServiceImplClassComposer method createAddResponseMethod.
private static MethodDefinition createAddResponseMethod() {
VariableExpr responseArgExpr = VariableExpr.withVariable(Variable.builder().setName("response").setType(FIXED_TYPESTORE.get("AbstractMessage")).build());
Expr methodInvocationExpr = MethodInvocationExpr.builder().setMethodName("add").setArguments(Arrays.asList(responseArgExpr)).setExprReferenceExpr(responsesVarExpr).build();
return MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("addResponse").setArguments(Arrays.asList(responseArgExpr.toBuilder().setIsDecl(true).build())).setBody(Arrays.asList(ExprStatement.withExpr(methodInvocationExpr))).build();
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class MockServiceImplClassComposer method createAddExceptionMethod.
private static MethodDefinition createAddExceptionMethod() {
VariableExpr exceptionArgExpr = VariableExpr.withVariable(Variable.builder().setName("exception").setType(TypeNode.withReference(ConcreteReference.withClazz(Exception.class))).build());
Expr methodInvocationExpr = MethodInvocationExpr.builder().setMethodName("add").setArguments(Arrays.asList(exceptionArgExpr)).setExprReferenceExpr(responsesVarExpr).build();
return MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setName("addException").setArguments(Arrays.asList(exceptionArgExpr.toBuilder().setIsDecl(true).build())).setBody(Arrays.asList(ExprStatement.withExpr(methodInvocationExpr))).build();
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr 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();
}
Aggregations