use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class AbstractServiceSettingsClassComposer method createBuilderHelperMethods.
private static List<MethodDefinition> createBuilderHelperMethods(Service service, TypeStore typeStore) {
TypeNode builderType = typeStore.get(BUILDER_CLASS_NAME);
VariableExpr clientContextVarExpr = VariableExpr.withVariable(Variable.builder().setName("clientContext").setType(FIXED_TYPESTORE.get("ClientContext")).build());
MethodDefinition newBuilderMethodTwo = MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.NEW_BUILDER_METHOD_COMMENT).setScope(ScopeNode.PUBLIC).setIsStatic(true).setReturnType(builderType).setName("newBuilder").setArguments(clientContextVarExpr.toBuilder().setIsDecl(true).build()).setReturnExpr(NewObjectExpr.builder().setType(builderType).setArguments(Arrays.asList(clientContextVarExpr)).build()).build();
MethodDefinition toBuilderMethod = MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.TO_BUILDER_METHOD_COMMENT).setScope(ScopeNode.PUBLIC).setReturnType(builderType).setName("toBuilder").setReturnExpr(NewObjectExpr.builder().setType(builderType).setArguments(ValueExpr.withValue(ThisObjectValue.withType(typeStore.get(ClassNames.getServiceSettingsClassName(service))))).build()).build();
return Arrays.asList(newBuilderMethodTwo, toBuilderMethod);
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class AbstractServiceSettingsClassComposer method createNestedBuilderSettingsGetterMethods.
private static List<MethodDefinition> createNestedBuilderSettingsGetterMethods(Service service, TypeStore typeStore) {
BiFunction<TypeNode, String, MethodDefinition.Builder> methodMakerFn = (retType, methodName) -> MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(retType).setName(methodName).setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName("getStubSettingsBuilder").build()).setMethodName(methodName).setReturnType(retType).build());
List<MethodDefinition> javaMethods = new ArrayList<>();
for (Method protoMethod : service.methods()) {
String javaStyleName = JavaStyle.toLowerCamelCase(protoMethod.name());
String javaMethodName = String.format("%sSettings", javaStyleName);
MethodDefinition.Builder methodBuilder = methodMakerFn.apply(getCallSettingsBuilderType(protoMethod, typeStore), javaMethodName);
javaMethods.add(methodBuilder.setHeaderCommentStatements(SettingsCommentComposer.createCallSettingsBuilderGetterComment(getMethodNameFromSettingsVarName(javaMethodName), protoMethod.isDeprecated())).setAnnotations(protoMethod.isDeprecated() ? Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)) : Collections.emptyList()).build());
if (protoMethod.hasLro()) {
javaMethodName = String.format("%sOperationSettings", javaStyleName);
methodBuilder = methodMakerFn.apply(getOperationCallSettingsBuilderType(protoMethod), javaMethodName);
javaMethods.add(methodBuilder.setHeaderCommentStatements(SettingsCommentComposer.createCallSettingsBuilderGetterComment(getMethodNameFromSettingsVarName(javaMethodName), protoMethod.isDeprecated())).setAnnotations(protoMethod.isDeprecated() ? Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)) : Collections.emptyList()).build());
}
}
return javaMethods;
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class AbstractServiceSettingsClassComposer method createSettingsGetterMethods.
// TODO(miraleung): Consider merging this with createNestedBuilderSettingsGetterMethods.
private static List<MethodDefinition> createSettingsGetterMethods(Service service, TypeStore typeStore) {
TypeNode stubSettingsType = typeStore.get(ClassNames.getServiceStubSettingsClassName(service));
BiFunction<TypeNode, String, MethodDefinition.Builder> methodMakerFn = (retType, javaMethodName) -> MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(retType).setName(javaMethodName).setReturnExpr(MethodInvocationExpr.builder().setExprReferenceExpr(CastExpr.builder().setType(stubSettingsType).setExpr(MethodInvocationExpr.builder().setMethodName("getStubSettings").setReturnType(FIXED_TYPESTORE.get("StubSettings")).build()).build()).setMethodName(javaMethodName).setReturnType(retType).build());
List<MethodDefinition> javaMethods = new ArrayList<>();
for (Method protoMethod : service.methods()) {
String javaStyleName = JavaStyle.toLowerCamelCase(protoMethod.name());
String javaMethodName = String.format("%sSettings", JavaStyle.toLowerCamelCase(protoMethod.name()));
MethodDefinition.Builder methodBuilder = methodMakerFn.apply(getCallSettingsType(protoMethod, typeStore), javaMethodName);
javaMethods.add(methodBuilder.setHeaderCommentStatements(SettingsCommentComposer.createCallSettingsGetterComment(getMethodNameFromSettingsVarName(javaMethodName), protoMethod.isDeprecated())).setAnnotations(protoMethod.isDeprecated() ? Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)) : Collections.emptyList()).build());
if (protoMethod.hasLro()) {
javaMethodName = String.format("%sOperationSettings", javaStyleName);
methodBuilder = methodMakerFn.apply(getOperationCallSettingsType(protoMethod), javaMethodName);
javaMethods.add(methodBuilder.setHeaderCommentStatements(SettingsCommentComposer.createCallSettingsGetterComment(getMethodNameFromSettingsVarName(javaMethodName), protoMethod.isDeprecated())).setAnnotations(protoMethod.isDeprecated() ? Arrays.asList(AnnotationNode.withType(TypeNode.DEPRECATED)) : Collections.emptyList()).build());
}
}
return javaMethods;
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class AbstractServiceStubClassComposer method createOperationsStubGetters.
private List<MethodDefinition> createOperationsStubGetters(TypeStore typeStore, TypeNode operationsStubType) {
List<MethodDefinition> getters = new ArrayList<>();
Iterator<String> operationStubNameIt = getTransportContext().transportOperationsStubNames().iterator();
Iterator<TypeNode> operationStubTypeIt = getTransportContext().operationsStubTypes().iterator();
while (operationStubNameIt.hasNext() && operationStubTypeIt.hasNext()) {
String methodName = String.format("get%s", JavaStyle.toUpperCamelCase(operationStubNameIt.next()));
// TODO: refactor this
TypeNode actualOperationsStubType = operationStubTypeIt.next();
if (operationsStubType != null) {
actualOperationsStubType = operationsStubType;
}
getters.add(createOperationsStubGetterMethodDefinition(actualOperationsStubType, methodName, typeStore));
}
return getters;
}
use of com.google.api.generator.engine.ast.MethodDefinition 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;
}
Aggregations