use of com.google.api.generator.engine.ast.AssignmentExpr 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.AssignmentExpr in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createHashCodeMethod.
private static MethodDefinition createHashCodeMethod(List<List<String>> tokenHierarchies) {
List<Statement> assignmentBody = new ArrayList<>();
// code: int h = 1;
Variable hVar = Variable.builder().setType(TypeNode.INT).setName("h").build();
VariableExpr hVarExpr = VariableExpr.builder().setVariable(hVar).build();
ValueExpr hValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("1").build());
AssignmentExpr hAssignmentExpr = AssignmentExpr.builder().setVariableExpr(hVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(hValueExpr).build();
assignmentBody.add(ExprStatement.withExpr(hAssignmentExpr));
// code: h *= 1000003;
// code: h ^= Objects.hashCode(...);
ValueExpr numValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("1000003").build());
AssignmentOperationExpr multiplyAssignmentOpExpr = AssignmentOperationExpr.multiplyAssignmentWithExprs(hVarExpr, numValueExpr);
// PubSub special-case handling - exclude _deleted-topic_.
List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(ts -> !ts.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
// If it has variants, add the multiply and xor assignment operation exprs for fixedValue.
boolean hasVariants = processedTokenHierarchies.size() > 1;
if (hasVariants) {
VariableExpr fixedValueVarExpr = FIXED_CLASS_VARS.get("fixedValue");
assignmentBody.add(ExprStatement.withExpr(multiplyAssignmentOpExpr));
assignmentBody.add(ExprStatement.withExpr(AssignmentOperationExpr.xorAssignmentWithExprs(hVarExpr, createObjectsHashCodeForVarMethod(fixedValueVarExpr))));
}
// Add the multiply and xor assignment operation exprs for tokens.
Set<String> tokenSet = getTokenSet(processedTokenHierarchies);
tokenSet.stream().forEach(token -> {
VariableExpr tokenVarExpr = VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(token)).setType(TypeNode.STRING).build());
assignmentBody.add(ExprStatement.withExpr(multiplyAssignmentOpExpr));
assignmentBody.add(ExprStatement.withExpr(AssignmentOperationExpr.xorAssignmentWithExprs(hVarExpr, createObjectsHashCodeForVarMethod(tokenVarExpr))));
});
return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.INT).setName("hashCode").setBody(assignmentBody).setReturnExpr(hVarExpr).build();
}
use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class SettingsSampleComposer method composeSettingsSample.
public static Optional<Sample> composeSettingsSample(Optional<String> methodNameOpt, String settingsClassName, TypeNode classType) {
if (!methodNameOpt.isPresent()) {
return Optional.empty();
}
// Initialize services settingsBuilder with newBuilder()
// e.g. FoobarSettings.Builder foobarSettingsBuilder = FoobarSettings.newBuilder();
TypeNode builderType = TypeNode.withReference(VaporReference.builder().setEnclosingClassNames(classType.reference().name()).setName("Builder").setPakkage(classType.reference().pakkage()).build());
VariableExpr localSettingsVarExpr = VariableExpr.withVariable(Variable.builder().setName(JavaStyle.toLowerCamelCase(String.format("%sBuilder", settingsClassName))).setType(builderType).build());
MethodInvocationExpr settingsBuilderMethodInvocationExpr = MethodInvocationExpr.builder().setStaticReferenceType(classType).setMethodName("newBuilder").setReturnType(builderType).build();
AssignmentExpr initLocalSettingsExpr = AssignmentExpr.builder().setVariableExpr(localSettingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(settingsBuilderMethodInvocationExpr).build();
// Builder with set value method
// e.g foobarSettingBuilder.fooSetting().setRetrySettings(
// echoSettingsBuilder.echoSettings().getRetrySettings().toBuilder().setTotalTimeout(Duration.ofSeconds(30)).build());
MethodInvocationExpr settingBuilderMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(localSettingsVarExpr).setMethodName(JavaStyle.toLowerCamelCase(String.format("%sSettings", methodNameOpt.get()))).build();
String disambiguation = "Settings";
MethodInvocationExpr retrySettingsArgExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingBuilderMethodInvocationExpr).setMethodName("getRetrySettings").build();
retrySettingsArgExpr = MethodInvocationExpr.builder().setExprReferenceExpr(retrySettingsArgExpr).setMethodName("toBuilder").build();
MethodInvocationExpr ofSecondMethodInvocationExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Duration.class))).setMethodName("ofSeconds").setArguments(ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("30").build())).build();
retrySettingsArgExpr = MethodInvocationExpr.builder().setExprReferenceExpr(retrySettingsArgExpr).setMethodName("setTotalTimeout").setArguments(ofSecondMethodInvocationExpr).build();
retrySettingsArgExpr = MethodInvocationExpr.builder().setExprReferenceExpr(retrySettingsArgExpr).setMethodName("build").build();
settingBuilderMethodInvocationExpr = MethodInvocationExpr.builder().setExprReferenceExpr(settingBuilderMethodInvocationExpr).setMethodName("setRetrySettings").setArguments(retrySettingsArgExpr).build();
// Initialize clientSetting with builder() method.
// e.g: Foobar<Stub>Settings foobarSettings = foobarSettingsBuilder.build();
VariableExpr settingsVarExpr = VariableExpr.withVariable(Variable.builder().setType(classType).setName(JavaStyle.toLowerCamelCase(settingsClassName)).build());
AssignmentExpr settingBuildAssignmentExpr = AssignmentExpr.builder().setVariableExpr(settingsVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(MethodInvocationExpr.builder().setExprReferenceExpr(localSettingsVarExpr).setMethodName("build").setReturnType(classType).build()).build();
List<Statement> statements = Arrays.asList(initLocalSettingsExpr, settingBuilderMethodInvocationExpr, settingBuildAssignmentExpr).stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList());
RegionTag regionTag = RegionTag.builder().setServiceName(classType.reference().name()).setRpcName(methodNameOpt.get()).build();
return Optional.of(Sample.builder().setBody(statements).setRegionTag(regionTag).build());
}
use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeIfStatement_withElse.
@Test
public void writeIfStatement_withElse() {
AssignmentExpr assignExpr = createAssignmentExpr("x", "3", TypeNode.INT);
Statement assignExprStatement = ExprStatement.withExpr(assignExpr);
List<Statement> ifBody = Arrays.asList(assignExprStatement, assignExprStatement);
VariableExpr condExpr = createVariableExpr("condition", TypeNode.BOOLEAN);
IfStatement ifStatement = IfStatement.builder().setConditionExpr(condExpr).setBody(ifBody).setElseBody(ifBody).build();
ifStatement.accept(writerVisitor);
assertEquals(String.format("%s%s%s" + "%s%s%s%s", "if (condition) {\n", "int x = 3;\n", "int x = 3;\n", "} else {\n", "int x = 3;\n", "int x = 3;\n", "}\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.AssignmentExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method createAssignmentExpr.
/**
* =============================== HELPERS ===============================
*/
private static AssignmentExpr createAssignmentExpr(String variableName, String value, TypeNode type) {
VariableExpr variableExpr = createVariableDeclExpr(variableName, type);
Value val = PrimitiveValue.builder().setType(type).setValue(value).build();
Expr valueExpr = ValueExpr.builder().setValue(val).build();
return AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(valueExpr).build();
}
Aggregations