use of com.google.api.generator.engine.ast.MethodDefinition 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.MethodDefinition in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createFieldValueGetterMethods.
private static List<MethodDefinition> createFieldValueGetterMethods(ResourceName resourceName, Map<String, VariableExpr> patternTokenVarExprs, List<List<String>> tokenHierarchies, TypeStore typeStore) {
List<MethodDefinition> javaMethods = new ArrayList<>();
TypeNode thisClassType = typeStore.get(getThisClassName(resourceName));
javaMethods.add(createGetFieldValuesMapMethod(resourceName, thisClassType, patternTokenVarExprs, tokenHierarchies));
javaMethods.add(createGetFieldValueMethod());
return javaMethods;
}
use of com.google.api.generator.engine.ast.MethodDefinition 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.MethodDefinition 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.MethodDefinition 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();
}
Aggregations