use of com.google.api.generator.engine.ast.ReturnExpr in project gapic-generator-java by googleapis.
the class ImportWriterVisitorTest method writeReturnExprImports_basic.
@Test
public void writeReturnExprImports_basic() {
ReturnExpr returnExpr = ReturnExpr.withExpr(MethodInvocationExpr.builder().setMethodName("createExpr").setReturnType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class))).build());
returnExpr.accept(writerVisitor);
assertEquals("import com.google.api.generator.engine.ast.Expr;\n\n", writerVisitor.write());
}
use of com.google.api.generator.engine.ast.ReturnExpr in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createPagedListResponseFactoryAssignExpr.
private static Expr createPagedListResponseFactoryAssignExpr(VariableExpr pageStrDescVarExpr, Method method, TypeNode repeatedResponseType, TypeStore typeStore) {
Preconditions.checkState(method.isPaged(), String.format("Method %s is not paged", method.name()));
// Create the PagedListResponseFactory.
TypeNode pagedResponseType = typeStore.get(getPagedResponseTypeName(method.name()));
TypeNode apiFutureType = TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(Arrays.asList(pagedResponseType.reference())).build());
VariableExpr callableVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(ConcreteReference.builder().setClazz(UnaryCallable.class).setGenerics(Arrays.asList(method.inputType().reference(), method.outputType().reference())).build())).setName("callable").build());
VariableExpr requestVarExpr = VariableExpr.withVariable(Variable.builder().setType(method.inputType()).setName("request").build());
VariableExpr contextVarExpr = VariableExpr.withVariable(Variable.builder().setType(FIXED_TYPESTORE.get("ApiCallContext")).setName("context").build());
VariableExpr futureResponseVarExpr = VariableExpr.withVariable(Variable.builder().setType(TypeNode.withReference(ConcreteReference.builder().setClazz(ApiFuture.class).setGenerics(Arrays.asList(method.outputType().reference())).build())).setName("futureResponse").build());
TypeNode pageContextType = TypeNode.withReference(ConcreteReference.builder().setClazz(PageContext.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), repeatedResponseType).stream().map(t -> t.reference()).collect(Collectors.toList())).build());
VariableExpr pageContextVarExpr = VariableExpr.withVariable(Variable.builder().setType(pageContextType).setName("pageContext").build());
AssignmentExpr pageContextAssignExpr = AssignmentExpr.builder().setVariableExpr(pageContextVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("PageContext")).setMethodName("create").setArguments(callableVarExpr, pageStrDescVarExpr, requestVarExpr, contextVarExpr).setReturnType(pageContextVarExpr.type()).build()).build();
Expr returnExpr = MethodInvocationExpr.builder().setStaticReferenceType(typeStore.get(getPagedResponseTypeName(method.name()))).setMethodName("createAsync").setArguments(pageContextVarExpr, futureResponseVarExpr).setReturnType(apiFutureType).build();
MethodDefinition getFuturePagedResponseMethod = MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(apiFutureType).setName("getFuturePagedResponse").setArguments(Arrays.asList(callableVarExpr, requestVarExpr, contextVarExpr, futureResponseVarExpr).stream().map(v -> v.toBuilder().setIsDecl(true).build()).collect(Collectors.toList())).setBody(Arrays.asList(ExprStatement.withExpr(pageContextAssignExpr))).setReturnExpr(returnExpr).build();
// Create the variable.
TypeNode pagedResponseFactoryType = TypeNode.withReference(ConcreteReference.builder().setClazz(PagedListResponseFactory.class).setGenerics(Arrays.asList(method.inputType(), method.outputType(), typeStore.get(getPagedResponseTypeName(method.name()))).stream().map(t -> t.reference()).collect(Collectors.toList())).build());
String varName = String.format(PAGED_RESPONSE_FACTORY_PATTERN, JavaStyle.toUpperSnakeCase(method.name()));
VariableExpr pagedListResponseFactoryVarExpr = VariableExpr.withVariable(Variable.builder().setType(pagedResponseFactoryType).setName(varName).build());
AnonymousClassExpr factoryAnonClassExpr = AnonymousClassExpr.builder().setType(pagedResponseFactoryType).setMethods(Arrays.asList(getFuturePagedResponseMethod)).build();
return AssignmentExpr.builder().setVariableExpr(pagedListResponseFactoryVarExpr.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setIsFinal(true).build()).setValueExpr(factoryAnonClassExpr).build();
}
use of com.google.api.generator.engine.ast.ReturnExpr in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createNestedClassCreateDefaultMethod.
protected MethodDefinition createNestedClassCreateDefaultMethod(TypeStore typeStore, String methodName, String defaultTransportChannelProvider, String defaultTransportChannelProviderBuilder, String defaultApiClientHeaderProviderBuilder) {
List<Statement> bodyStatements = new ArrayList<>();
// Initialize the builder: Builder builder = new Builder((ClientContext) null);
TypeNode builderType = typeStore.get(NESTED_BUILDER_CLASS_NAME);
VariableExpr builderVarExpr = VariableExpr.withVariable(Variable.builder().setType(builderType).setName("builder").build());
bodyStatements.add(ExprStatement.withExpr(AssignmentExpr.builder().setVariableExpr(builderVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(NewObjectExpr.builder().setType(builderType).setArguments(CastExpr.builder().setType(FIXED_TYPESTORE.get("ClientContext")).setExpr(ValueExpr.createNullExpr()).build()).build()).build()));
bodyStatements.add(EMPTY_LINE_STATEMENT);
List<Expr> bodyExprs = new ArrayList<>();
if (defaultTransportChannelProvider != null) {
bodyExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName("setTransportChannelProvider").setArguments(MethodInvocationExpr.builder().setMethodName(defaultTransportChannelProvider).build()).build());
} else {
bodyExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName("setTransportChannelProvider").setArguments(MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName(defaultTransportChannelProviderBuilder).build()).setMethodName("build").build()).build());
}
bodyExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName("setCredentialsProvider").setArguments(MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName("defaultCredentialsProviderBuilder").build()).setMethodName("build").build()).build());
bodyExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName("setInternalHeaderProvider").setArguments(MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName(defaultApiClientHeaderProviderBuilder).build()).setMethodName("build").build()).build());
bodyExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName("setEndpoint").setArguments(MethodInvocationExpr.builder().setMethodName("getDefaultEndpoint").build()).build());
bodyExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName("setMtlsEndpoint").setArguments(MethodInvocationExpr.builder().setMethodName("getDefaultMtlsEndpoint").build()).build());
bodyExprs.add(MethodInvocationExpr.builder().setExprReferenceExpr(builderVarExpr).setMethodName("setSwitchToMtlsEndpointAllowed").setArguments(ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build())).build());
bodyStatements.addAll(bodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList()));
bodyStatements.add(EMPTY_LINE_STATEMENT);
Expr returnExpr = MethodInvocationExpr.builder().setMethodName("initDefaults").setArguments(builderVarExpr).setReturnType(builderType).build();
return MethodDefinition.builder().setScope(ScopeNode.PRIVATE).setIsStatic(true).setReturnType(builderType).setName(methodName).setBody(bodyStatements).setReturnExpr(returnExpr).build();
}
use of com.google.api.generator.engine.ast.ReturnExpr in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createOfOrFormatMethodHelper.
private static List<MethodDefinition> createOfOrFormatMethodHelper(ResourceName resourceName, Map<String, VariableExpr> patternTokenVarExprs, List<List<String>> tokenHierarchies, TypeStore typeStore, boolean isFormatMethod) {
List<MethodDefinition> javaMethods = new ArrayList<>();
String methodNameFormat = isFormatMethod ? "format%s" : "of%s";
String newBuilderMethodNameFormat = "new%s";
String setMethodNameFormat = "set%s";
String buildMethodName = "build";
String toStringMethodName = "toString";
AnnotationNode betaAnnotation = AnnotationNode.builder().setType(FIXED_TYPESTORE.get("BetaApi")).setDescription(String.format("The static %s methods are not stable yet and may be changed in the future.", isFormatMethod ? "format" : "create")).build();
List<AnnotationNode> annotations = Arrays.asList(betaAnnotation);
TypeNode thisClassType = typeStore.get(getThisClassName(resourceName));
TypeNode returnType = isFormatMethod ? TypeNode.STRING : thisClassType;
// Create the newBuilder and variation methods here.
// Variation example: newProjectLocationAutoscalingPolicyBuilder().
boolean hasVariants = tokenHierarchies.size() > 1;
for (int i = 0; i < tokenHierarchies.size(); i++) {
List<String> tokens = tokenHierarchies.get(i);
// PubSub special-case handling.
if (tokens.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)) {
Expr deletedTopicStringValExpr = ValueExpr.withValue(StringObjectValue.withValue(ResourceNameConstants.DELETED_TOPIC_LITERAL));
// Simply return `new TopicName("_deleted-topic_")` or the string value itself.
javaMethods.add(MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setAnnotations(annotations).setReturnType(returnType).setName(String.format(methodNameFormat, concatToUpperCamelCaseName(tokens) + "Name")).setReturnExpr(returnType.equals(TypeNode.STRING) ? deletedTopicStringValExpr : NewObjectExpr.builder().setType(returnType).setArguments(deletedTopicStringValExpr).build()).build());
continue;
}
String builderMethodName = String.format(newBuilderMethodNameFormat, getBuilderTypeName(tokenHierarchies, i));
MethodInvocationExpr returnExpr = MethodInvocationExpr.builder().setMethodName(builderMethodName).build();
for (String token : tokens) {
String javaTokenVarName = JavaStyle.toLowerCamelCase(token);
returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(returnExpr).setMethodName(String.format(setMethodNameFormat, JavaStyle.toUpperCamelCase(token))).setArguments(Arrays.asList(VariableExpr.withVariable(Variable.builder().setName(javaTokenVarName).setType(TypeNode.STRING).build()))).build();
}
returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(returnExpr).setMethodName(buildMethodName).setReturnType(thisClassType).build();
if (isFormatMethod) {
returnExpr = MethodInvocationExpr.builder().setExprReferenceExpr(returnExpr).setMethodName(toStringMethodName).setReturnType(TypeNode.STRING).build();
}
List<VariableExpr> methodArgs = tokens.stream().map(t -> patternTokenVarExprs.get(t).toBuilder().setIsDecl(true).build()).collect(Collectors.toList());
javaMethods.add(MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setAnnotations(i == 0 ? Collections.emptyList() : annotations).setReturnType(returnType).setName(String.format(methodNameFormat, i == 0 ? "" : concatToUpperCamelCaseName(tokens) + "Name")).setArguments(methodArgs).setReturnExpr(returnExpr).build());
if (i == 0 && hasVariants) {
javaMethods.add(MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setAnnotations(annotations).setReturnType(returnType).setName(String.format(methodNameFormat, concatToUpperCamelCaseName(tokenHierarchies.get(i)) + "Name")).setArguments(methodArgs).setReturnExpr(returnExpr).build());
}
}
return javaMethods;
}
use of com.google.api.generator.engine.ast.ReturnExpr in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createParseMethod.
private static MethodDefinition createParseMethod(TypeNode thisClassType, List<VariableExpr> templateFinalVarExprs, List<List<String>> tokenHierarchies, TypeStore typeStore) {
String formattedStringArgName = "formattedString";
VariableExpr formattedStringArgExpr = VariableExpr.withVariable(Variable.builder().setName(formattedStringArgName).setType(TypeNode.STRING).build());
String exceptionMessageString = String.format("%s.parse: %s not in valid format", thisClassType.reference().name(), formattedStringArgName);
ValueExpr exceptionMessageExpr = ValueExpr.withValue(StringObjectValue.withValue(exceptionMessageString));
TypeNode mapStringType = TypeNode.withReference(ConcreteReference.builder().setClazz(Map.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(String.class))).build());
VariableExpr matchMapVarExpr = VariableExpr.withVariable(Variable.builder().setName("matchMap").setType(mapStringType).build());
List<Statement> body = new ArrayList<>();
body.add(IfStatement.builder().setConditionExpr(MethodInvocationExpr.builder().setExprReferenceExpr(formattedStringArgExpr).setMethodName("isEmpty").setReturnType(TypeNode.BOOLEAN).build()).setBody(Arrays.asList(ExprStatement.withExpr(ReturnExpr.withExpr(ValueExpr.createNullExpr())))).build());
List<Expr> formattedStringArgList = Arrays.asList(formattedStringArgExpr);
List<VariableExpr> formattedStringArgDeclList = Arrays.asList(formattedStringArgExpr.toBuilder().setIsDecl(true).build());
boolean hasVariants = tokenHierarchies.size() > 1;
if (!hasVariants) {
List<Expr> methodArgs = Arrays.asList(formattedStringArgExpr, exceptionMessageExpr);
MethodInvocationExpr validatedMatchExpr = MethodInvocationExpr.builder().setExprReferenceExpr(templateFinalVarExprs.get(0)).setMethodName("validatedMatch").setArguments(methodArgs).setReturnType(mapStringType).build();
AssignmentExpr matchMapAssignExpr = AssignmentExpr.builder().setVariableExpr(matchMapVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(validatedMatchExpr).build();
body.add(ExprStatement.withExpr(matchMapAssignExpr));
List<Expr> ofMethodArgExprs = tokenHierarchies.get(0).stream().map(t -> MethodInvocationExpr.builder().setExprReferenceExpr(matchMapVarExpr).setMethodName("get").setArguments(Arrays.asList(ValueExpr.withValue(StringObjectValue.withValue(t)))).build()).collect(Collectors.toList());
MethodInvocationExpr ofMethodExpr = MethodInvocationExpr.builder().setMethodName("of").setArguments(ofMethodArgExprs).setReturnType(thisClassType).build();
return MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(thisClassType).setName("parse").setArguments(formattedStringArgDeclList).setBody(body).setReturnExpr(ofMethodExpr).build();
}
IfStatement.Builder ifStatementBuilder = IfStatement.builder();
String ofMethodNamePattern = "of%sName";
for (int i = 0; i < tokenHierarchies.size(); i++) {
// PubSub special-case handling for the "_deleted-topic_" pattern.
boolean isDeletedTopicPattern = tokenHierarchies.get(i).contains(ResourceNameConstants.DELETED_TOPIC_LITERAL);
VariableExpr templateVarExpr = templateFinalVarExprs.get(i);
MethodInvocationExpr conditionExpr = MethodInvocationExpr.builder().setExprReferenceExpr(templateVarExpr).setMethodName(isDeletedTopicPattern ? "equals" : "matches").setArguments(formattedStringArgList).setReturnType(TypeNode.BOOLEAN).build();
MethodInvocationExpr matchValueExpr = MethodInvocationExpr.builder().setExprReferenceExpr(templateVarExpr).setMethodName("match").setArguments(formattedStringArgList).setReturnType(mapStringType).build();
AssignmentExpr matchMapAssignExpr = AssignmentExpr.builder().setVariableExpr(matchMapVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(matchValueExpr).build();
List<String> tokens = tokenHierarchies.get(i);
MethodInvocationExpr ofMethodExpr = MethodInvocationExpr.builder().setMethodName(String.format(ofMethodNamePattern, concatToUpperCamelCaseName(tokens))).setArguments(tokens.stream().map(t -> MethodInvocationExpr.builder().setExprReferenceExpr(matchMapVarExpr).setMethodName("get").setArguments(Arrays.asList(ValueExpr.withValue(StringObjectValue.withValue(t)))).build()).collect(Collectors.toList())).setReturnType(thisClassType).build();
ReturnExpr subReturnExpr = ReturnExpr.withExpr(ofMethodExpr);
List<Statement> ifStatements = Arrays.asList(matchMapAssignExpr, subReturnExpr).stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList());
if (i == 0) {
ifStatementBuilder = ifStatementBuilder.setConditionExpr(conditionExpr).setBody(ifStatements);
} else {
// _deleted-topic_.
if (isDeletedTopicPattern) {
ifStatements.clear();
ifStatements.add(ExprStatement.withExpr(ReturnExpr.withExpr(NewObjectExpr.builder().setType(thisClassType).setArguments(ValueExpr.withValue(StringObjectValue.withValue(ResourceNameConstants.DELETED_TOPIC_LITERAL))).build())));
}
ifStatementBuilder = ifStatementBuilder.addElseIf(conditionExpr, ifStatements);
}
}
body.add(ifStatementBuilder.build());
body.add(ExprStatement.withExpr(ThrowExpr.builder().setType(FIXED_TYPESTORE.get("ValidationException")).setMessageExpr(exceptionMessageString).build()));
return MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(thisClassType).setName("parse").setArguments(formattedStringArgDeclList).setBody(body).build();
}
Aggregations