use of com.google.api.generator.engine.ast.MethodInvocationExpr 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();
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class HttpJsonServiceStubClassComposer method appendField.
// Generates: [nameVar].append(":").append([requestVar].get[FieldName]());
private ExprStatement appendField(VariableExpr nameVar, VariableExpr requestVar, String fieldName) {
BiFunction<String, List<Expr>, Function<MethodInvocationExpr, MethodInvocationExpr>> methodMaker = getMethodMaker();
ValueExpr colonValueExpr = ValueExpr.builder().setValue(StringObjectValue.builder().setValue(":").build()).build();
MethodInvocationExpr opNameAppendColonExpr = MethodInvocationExpr.builder().setMethodName("append").setArguments(colonValueExpr).setExprReferenceExpr(nameVar).build();
MethodInvocationExpr getField = MethodInvocationExpr.builder().setExprReferenceExpr(requestVar).setMethodName(getMethodFormat(fieldName)).build();
opNameAppendColonExpr = methodMaker.apply("append", Collections.singletonList(getField)).apply(opNameAppendColonExpr);
return ExprStatement.withExpr(opNameAppendColonExpr);
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class HttpJsonServiceStubClassComposer method setPollingRequestFactoryExpr.
private List<Expr> setPollingRequestFactoryExpr(Method protoMethod, Map<String, Message> messageTypes) {
BiFunction<String, List<Expr>, Function<MethodInvocationExpr, MethodInvocationExpr>> methodMaker = getMethodMaker();
Message inputOperationMessage = messageTypes.get(protoMethod.inputType().reference().fullName());
List<Statement> createBody = new ArrayList<>(1);
// Generate input variables for create
VariableExpr compoundOperationIdVarExpr = VariableExpr.builder().setVariable(Variable.builder().setType(TypeNode.STRING).setName("compoundOperationId").build()).build();
// Generate idComponenets
TypeNode listStringType = TypeNode.withReference(ConcreteReference.builder().setClazz(List.class).setGenerics(ConcreteReference.withClazz(String.class)).build());
TypeNode arrayListStringType = TypeNode.withReference(ConcreteReference.builder().setClazz(ArrayList.class).setGenerics(ConcreteReference.withClazz(String.class)).build());
TypeNode arraysType = TypeNode.withReference(ConcreteReference.withClazz(Arrays.class));
VariableExpr idComponentsVarExpr = VariableExpr.withVariable(Variable.builder().setName("idComponents").setType(listStringType).build());
MethodInvocationExpr compoundOperationIdSplitExpr = MethodInvocationExpr.builder().setExprReferenceExpr(compoundOperationIdVarExpr).setMethodName("split").setArguments(ValueExpr.withValue(StringObjectValue.withValue(":"))).setReturnType(arrayListStringType).build();
MethodInvocationExpr asListExpr = MethodInvocationExpr.builder().setStaticReferenceType(arraysType).setMethodName("asList").setArguments(compoundOperationIdSplitExpr).setReturnType(arrayListStringType).build();
AssignmentExpr idComponentsAssignExpr = AssignmentExpr.builder().setVariableExpr(idComponentsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(asListExpr).build();
createBody.add(ExprStatement.withExpr(idComponentsAssignExpr));
// Generate return statement
TypeNode getOperationRequestType = TypeNode.withReference(protoMethod.inputType().reference());
MethodInvocationExpr newBuilderExpr = MethodInvocationExpr.builder().setStaticReferenceType(getOperationRequestType).setMethodName("newBuilder").build();
BiMap<String, String> responseFieldsMap = inputOperationMessage.operationResponseFields();
List<String> responseFieldAnnotationNames = new ArrayList<>(responseFieldsMap.keySet());
Collections.sort(responseFieldAnnotationNames);
Set<String> responseFieldsNames = responseFieldsMap.inverse().keySet();
Set<String> allFieldsNames = inputOperationMessage.fieldMap().keySet();
ArrayList<String> nonResponseFieldsNames = new ArrayList<>();
for (String fieldName : allFieldsNames) {
if (!responseFieldsNames.contains(fieldName)) {
nonResponseFieldsNames.add(fieldName);
}
}
Collections.sort(nonResponseFieldsNames);
int index = 0;
for (String fieldAnnotationName : responseFieldAnnotationNames) {
newBuilderExpr = methodMaker.apply(setMethodFormat(responseFieldsMap.get(fieldAnnotationName)), Collections.singletonList(getExpr(idComponentsVarExpr, Integer.toString(index)))).apply(newBuilderExpr);
index++;
}
for (String fieldName : nonResponseFieldsNames) {
newBuilderExpr = methodMaker.apply(setMethodFormat(fieldName), Collections.singletonList(getExpr(idComponentsVarExpr, Integer.toString(index)))).apply(newBuilderExpr);
index++;
}
MethodInvocationExpr buildExpr = MethodInvocationExpr.builder().setExprReferenceExpr(newBuilderExpr).setMethodName("build").setReturnType(getOperationRequestType).build();
// Return lambda anonymous class
return Collections.singletonList(LambdaExpr.builder().setArguments(compoundOperationIdVarExpr.toBuilder().setIsDecl(true).build()).setBody(createBody).setReturnExpr(buildExpr).build());
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr in project gapic-generator-java by googleapis.
the class RetrySettingsComposer method createRetryCodeDefinitionExpr.
private static Expr createRetryCodeDefinitionExpr(String codeName, List<Code> retryCodes, VariableExpr definitionsVarExpr) {
// Construct something like `definitions.put("code_name",
// ImmutableSet.copYOf(Lists.<StatusCode.Code>newArrayList()));`
MethodInvocationExpr codeListExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Lists")).setGenerics(Arrays.asList(STATUS_CODE_CODE_TYPE.reference())).setMethodName("newArrayList").setArguments(retryCodes.stream().map(c -> toStatusCodeEnumRefExpr(c)).collect(Collectors.toList())).build();
MethodInvocationExpr codeSetExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("ImmutableSet")).setMethodName("copyOf").setArguments(codeListExpr).build();
return MethodInvocationExpr.builder().setExprReferenceExpr(definitionsVarExpr).setMethodName("put").setArguments(ValueExpr.withValue(StringObjectValue.withValue(codeName)), codeSetExpr).build();
}
use of com.google.api.generator.engine.ast.MethodInvocationExpr 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("defaultHttpJsonTransportProviderBuilder").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();
}
Aggregations