use of com.google.api.generator.engine.ast.AnnotationNode in project gapic-generator-java by googleapis.
the class AbstractServiceStubSettingsClassComposer method createNestedClassSettingsBuilderGetterMethods.
private static List<MethodDefinition> createNestedClassSettingsBuilderGetterMethods(Map<String, VariableExpr> nestedMethodSettingsMemberVarExprs, Set<String> nestedDeprecatedSettingVarNames) {
Reference operationCallSettingsBuilderRef = ConcreteReference.withClazz(OperationCallSettings.Builder.class);
Function<TypeNode, Boolean> isOperationCallSettingsBuilderFn = t -> t.reference().copyAndSetGenerics(ImmutableList.of()).equals(operationCallSettingsBuilderRef);
AnnotationNode lroBetaAnnotation = AnnotationNode.builder().setType(FIXED_TYPESTORE.get("BetaApi")).setDescription("The surface for use by generated code is not stable yet and may change in the" + " future.").build();
AnnotationNode deprecatedAnnotation = AnnotationNode.withType(TypeNode.DEPRECATED);
List<MethodDefinition> javaMethods = new ArrayList<>();
for (Map.Entry<String, VariableExpr> settingsVarEntry : nestedMethodSettingsMemberVarExprs.entrySet()) {
String varName = settingsVarEntry.getKey();
VariableExpr settingsVarExpr = settingsVarEntry.getValue();
boolean isOperationCallSettings = isOperationCallSettingsBuilderFn.apply(settingsVarExpr.type());
List<AnnotationNode> annotations = new ArrayList<>();
if (isOperationCallSettings) {
annotations.add(lroBetaAnnotation);
}
boolean isDeprecated = nestedDeprecatedSettingVarNames.contains(varName);
if (isDeprecated) {
annotations.add(deprecatedAnnotation);
}
javaMethods.add(MethodDefinition.builder().setHeaderCommentStatements(SettingsCommentComposer.createCallSettingsBuilderGetterComment(getMethodNameFromSettingsVarName(varName), isDeprecated)).setAnnotations(annotations).setScope(ScopeNode.PUBLIC).setReturnType(settingsVarExpr.type()).setName(settingsVarExpr.variable().identifier().name()).setReturnExpr(settingsVarExpr).build());
}
return javaMethods;
}
use of com.google.api.generator.engine.ast.AnnotationNode 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.AnnotationNode in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createNestedBuilderClass.
private static ClassDefinition createNestedBuilderClass(TypeNode outerClassType, List<String> tokens, VariableExpr templateFinalVarExpr, String resourceNamePattern, TypeStore typeStore, boolean hasVariants, boolean isDefaultClass) {
String className = isDefaultClass ? "Builder" : getBuilderTypeName(tokens);
// Class member declarations.
List<VariableExpr> classMemberVarExprs = tokens.stream().map(t -> VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(t)).setType(TypeNode.STRING).build())).collect(Collectors.toList());
List<Statement> classMemberDecls = classMemberVarExprs.stream().map(v -> ExprStatement.withExpr(v.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).build())).collect(Collectors.toList());
// Constructor.
List<MethodDefinition> nestedClassMethods = new ArrayList<>();
TypeNode thisClassType = typeStore.get(className);
MethodDefinition ctor = MethodDefinition.constructorBuilder().setScope(ScopeNode.PROTECTED).setReturnType(thisClassType).build();
nestedClassMethods.add(ctor);
// Getters and setters.
List<MethodDefinition> getterMethods = new ArrayList<>();
List<MethodDefinition> setterMethods = new ArrayList<>();
ValueExpr thisExpr = ValueExpr.withValue(ThisObjectValue.withType(thisClassType));
for (int i = 0; i < tokens.size(); i++) {
String token = tokens.get(i);
String upperCamelTokenName = JavaStyle.toUpperCamelCase(token);
VariableExpr currClassTokenVarExpr = classMemberVarExprs.get(i);
// Getter.
MethodDefinition getterMethod = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.STRING).setName(String.format("get%s", upperCamelTokenName)).setReturnExpr(currClassTokenVarExpr).build();
getterMethods.add(getterMethod);
// Setter.
VariableExpr tokenArgVarExpr = currClassTokenVarExpr;
AssignmentExpr fieldAssignExpr = AssignmentExpr.builder().setVariableExpr(currClassTokenVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build()).setValueExpr(tokenArgVarExpr).build();
MethodDefinition setterMethod = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(thisClassType).setName(String.format("set%s", upperCamelTokenName)).setArguments(classMemberVarExprs.get(i).toBuilder().setIsDecl(true).build()).setBody(Arrays.asList(ExprStatement.withExpr(fieldAssignExpr))).setReturnExpr(thisExpr).build();
setterMethods.add(setterMethod);
}
nestedClassMethods.addAll(getterMethods);
nestedClassMethods.addAll(setterMethods);
// Private builder constructor method.
if (isDefaultClass) {
VariableExpr outerClassVarExpr = VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(outerClassType.reference().name())).setType(outerClassType).build());
List<Expr> builderCtorBodyExprs = new ArrayList<>();
if (hasVariants) {
// TODO(miraleung): Use eq operator instead.
MethodInvocationExpr equalsCheckExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Objects")).setMethodName("equals").setArguments(FIXED_CLASS_VARS.get("pathTemplate").toBuilder().setExprReferenceExpr(outerClassVarExpr).build(), templateFinalVarExpr).build();
builderCtorBodyExprs.add(MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("Preconditions")).setMethodName("checkArgument").setArguments(equalsCheckExpr, ValueExpr.withValue(StringObjectValue.withValue(String.format("toBuilder is only supported when %s has the pattern of %s", outerClassType.reference().name(), resourceNamePattern)))).build());
}
for (VariableExpr memberVarExpr : classMemberVarExprs) {
VariableExpr currClassTokenVarExpr = memberVarExpr.toBuilder().setExprReferenceExpr(thisExpr).build();
builderCtorBodyExprs.add(AssignmentExpr.builder().setVariableExpr(currClassTokenVarExpr).setValueExpr(currClassTokenVarExpr.toBuilder().setExprReferenceExpr(outerClassVarExpr).build()).build());
}
MethodDefinition fromOuterTypeCtor = MethodDefinition.constructorBuilder().setScope(ScopeNode.PRIVATE).setReturnType(thisClassType).setArguments(outerClassVarExpr.toBuilder().setIsDecl(true).build()).setBody(builderCtorBodyExprs.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList())).build();
nestedClassMethods.add(fromOuterTypeCtor);
}
// Last build() method.
MethodDefinition buildMethod = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(outerClassType).setName("build").setReturnExpr(NewObjectExpr.builder().setType(outerClassType).setArguments(thisExpr).build()).build();
nestedClassMethods.add(buildMethod);
// Return the class.
AnnotationNode betaAnnotation = AnnotationNode.builder().setType(FIXED_TYPESTORE.get("BetaApi")).setDescription("The per-pattern Builders are not stable yet and may be changed in the future.").build();
List<AnnotationNode> classAnnotations = isDefaultClass ? Collections.emptyList() : Arrays.asList(betaAnnotation);
return ClassDefinition.builder().setHeaderCommentStatements(CommentStatement.withComment(JavaDocComment.withComment(String.format(BUILDER_CLASS_HEADER_PATTERN, resourceNamePattern)))).setAnnotations(classAnnotations).setIsNested(true).setScope(ScopeNode.PUBLIC).setIsStatic(true).setName(className).setStatements(classMemberDecls).setMethods(nestedClassMethods).build();
}
use of com.google.api.generator.engine.ast.AnnotationNode in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAnnotation_withDescription.
@Test
public void writeAnnotation_withDescription() {
AnnotationNode annotation = AnnotationNode.withSuppressWarnings("all");
annotation.accept(writerVisitor);
assertEquals("@SuppressWarnings(\"all\")\n", writerVisitor.write());
}
Aggregations