use of com.google.api.generator.engine.ast.ValueExpr in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createClassStatements.
private static List<Statement> createClassStatements(List<VariableExpr> templateFinalVarExprs, Map<String, VariableExpr> patternTokenVarExprs, List<String> patterns, List<List<String>> tokenHierarchies) {
List<Expr> memberVars = new ArrayList<>();
// "projects/{project}/locations/{location}/autoscalingPolicies/{autoscaling_policy}");
for (int i = 0; i < patterns.size(); i++) {
VariableExpr varExpr = templateFinalVarExprs.get(i).toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setIsFinal(true).build();
String pattern = patterns.get(i);
Expr valueExpr = MethodInvocationExpr.builder().setStaticReferenceType(FIXED_TYPESTORE.get("PathTemplate")).setMethodName("createWithoutUrlEncoding").setArguments(Arrays.asList(ValueExpr.withValue(StringObjectValue.withValue(pattern)))).setReturnType(FIXED_TYPESTORE.get("PathTemplate")).build();
// PubSub special-case handling for _deleted-topic_.
if (pattern.equals(ResourceNameConstants.DELETED_TOPIC_LITERAL)) {
valueExpr = ValueExpr.withValue(StringObjectValue.withValue(pattern));
}
memberVars.add(AssignmentExpr.builder().setVariableExpr(varExpr).setValueExpr(valueExpr).build());
}
memberVars.add(FIXED_CLASS_VARS.get("fieldValuesMap").toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsVolatile(true).build());
boolean hasVariants = tokenHierarchies.size() > 1;
if (hasVariants) {
Function<VariableExpr, VariableExpr> toDeclFn = v -> v.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).build();
memberVars.add(toDeclFn.apply(FIXED_CLASS_VARS.get("pathTemplate")));
memberVars.add(toDeclFn.apply(FIXED_CLASS_VARS.get("fixedValue")));
}
// Private per-token string variables.
// Use the token set as a key to maintain ordering (for consistency).
Function<VariableExpr, VariableExpr> toFinalDeclFn = v -> v.toBuilder().setIsDecl(true).setScope(ScopeNode.PRIVATE).setIsFinal(true).build();
// Special-cased PubSub handling.
List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(tokens -> !tokens.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
memberVars.addAll(getTokenSet(processedTokenHierarchies).stream().map(t -> toFinalDeclFn.apply(patternTokenVarExprs.get(t))).collect(Collectors.toList()));
return memberVars.stream().map(e -> ExprStatement.withExpr(e)).collect(Collectors.toList());
}
use of com.google.api.generator.engine.ast.ValueExpr in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createEqualsMethod.
private static MethodDefinition createEqualsMethod(ResourceName resourceName, List<List<String>> tokenHierarchies, TypeStore typeStore) {
// Create method definition variables.
Variable oVariable = Variable.builder().setType(TypeNode.withReference(javaObjectReference)).setName("o").build();
VariableExpr argVarExpr = VariableExpr.builder().setIsDecl(false).setVariable(oVariable).build();
TypeNode thisClassType = typeStore.get(getThisClassName(resourceName));
ValueExpr thisValueExpr = ValueExpr.withValue(ThisObjectValue.withType(thisClassType));
ValueExpr trueValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build());
// Create first if statement's return expression
ReturnExpr returnTrueExpr = ReturnExpr.withExpr(trueValueExpr);
// Create second if statement's condition expression
RelationalOperationExpr oEqualsThisExpr = RelationalOperationExpr.equalToWithExprs(argVarExpr, thisValueExpr);
RelationalOperationExpr oNotEqualsNullExpr = RelationalOperationExpr.notEqualToWithExprs(argVarExpr, ValueExpr.createNullExpr());
MethodInvocationExpr getClassMethodInvocationExpr = MethodInvocationExpr.builder().setMethodName("getClass").build();
RelationalOperationExpr getClassEqualsExpr = RelationalOperationExpr.equalToWithExprs(getClassMethodInvocationExpr, getClassMethodInvocationExpr.toBuilder().setExprReferenceExpr(argVarExpr).build());
LogicalOperationExpr orLogicalExpr = LogicalOperationExpr.logicalOrWithExprs(oNotEqualsNullExpr, getClassEqualsExpr);
// Create second if statement's body assignment expression.
Variable thatVariable = Variable.builder().setName("that").setType(thisClassType).build();
VariableExpr thatVariableExpr = VariableExpr.builder().setIsDecl(false).setVariable(thatVariable).build();
CastExpr oCastExpr = CastExpr.builder().setExpr(argVarExpr).setType(thisClassType).build();
AssignmentExpr thatAssignmentExpr = AssignmentExpr.builder().setVariableExpr(thatVariableExpr.toBuilder().setIsDecl(true).build()).setValueExpr(oCastExpr).build();
// PubSub special-case handling - exclude _deleted-topic_.
List<List<String>> processedTokenHierarchies = tokenHierarchies.stream().filter(ts -> !ts.contains(ResourceNameConstants.DELETED_TOPIC_LITERAL)).collect(Collectors.toList());
// Create return expression in the second if statement's body.
Set<String> tokenSet = getTokenSet(processedTokenHierarchies);
Iterator<String> itToken = tokenSet.iterator();
Expr curTokenExpr = createObjectsEqualsForTokenMethodExpr(thisValueExpr, thatVariableExpr, Variable.builder().setType(TypeNode.STRING).setName(JavaStyle.toLowerCamelCase(itToken.next())).build());
while (itToken.hasNext()) {
Expr nextTokenExpr = createObjectsEqualsForTokenMethodExpr(thisValueExpr, thatVariableExpr, Variable.builder().setType(TypeNode.STRING).setName(JavaStyle.toLowerCamelCase(itToken.next())).build());
curTokenExpr = LogicalOperationExpr.logicalAndWithExprs(curTokenExpr, nextTokenExpr);
}
ReturnExpr secondIfReturnExpr = ReturnExpr.withExpr(curTokenExpr);
// Code: if (o == this) { return true;}
IfStatement firstIfStatement = IfStatement.builder().setConditionExpr(oEqualsThisExpr).setBody(Arrays.asList(ExprStatement.withExpr(returnTrueExpr))).build();
// Code: if (o != null || getClass() == o.getClass()) { FoobarName that = ((FoobarName) o);
// return ..}
IfStatement secondIfStatement = IfStatement.builder().setConditionExpr(orLogicalExpr).setBody(Arrays.asList(ExprStatement.withExpr(thatAssignmentExpr), ExprStatement.withExpr(secondIfReturnExpr))).build();
// Create method's return expression.
ValueExpr falseValueExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("false").build());
return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setArguments(argVarExpr.toBuilder().setIsDecl(true).build()).setReturnType(TypeNode.BOOLEAN).setName("equals").setReturnExpr(falseValueExpr).setBody(Arrays.asList(firstIfStatement, secondIfStatement)).build();
}
use of com.google.api.generator.engine.ast.ValueExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeSuperObjectValue_accessFieldAndInvokeMethod.
@Test
public void writeSuperObjectValue_accessFieldAndInvokeMethod() {
VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
TypeNode classType = TypeNode.withReference(ref);
SuperObjectValue superObjectValue = SuperObjectValue.withType(classType);
ValueExpr superValueExpr = ValueExpr.withValue(superObjectValue);
Variable subVariable = Variable.builder().setName("name").setType(TypeNode.STRING).build();
VariableExpr superVariableExpr = VariableExpr.builder().setVariable(subVariable).setExprReferenceExpr(superValueExpr).build();
MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("getName").setExprReferenceExpr(ValueExpr.withValue(superObjectValue)).setReturnType(TypeNode.STRING).build();
AssignmentExpr assignmentExpr = AssignmentExpr.builder().setVariableExpr(superVariableExpr).setValueExpr(methodExpr).build();
assignmentExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("super.name = super.getName()");
}
use of com.google.api.generator.engine.ast.ValueExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeRelationOperationExpr_notEqualTo.
@Test
public void writeRelationOperationExpr_notEqualTo() {
TypeNode someType = TypeNode.withReference(VaporReference.builder().setName("SomeClass").setPakkage("com.google.api.generator.engine").build());
MethodInvocationExpr lhsExpr = MethodInvocationExpr.builder().setMethodName("getName").setStaticReferenceType(someType).setReturnType(TypeNode.STRING).build();
ValueExpr rhsExpr = ValueExpr.createNullExpr();
RelationalOperationExpr notEqualToOperationExpr = RelationalOperationExpr.notEqualToWithExprs(lhsExpr, rhsExpr);
notEqualToOperationExpr.accept(writerVisitor);
assertThat(writerVisitor.write()).isEqualTo("SomeClass.getName() != null");
}
use of com.google.api.generator.engine.ast.ValueExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeMethodDefinition_withCommentsAnnotationsAndThrows.
@Test
public void writeMethodDefinition_withCommentsAnnotationsAndThrows() {
LineComment lineComment = LineComment.withComment("AUTO-GENERATED DOCUMENTATION AND METHOD");
JavaDocComment javaDocComment = JavaDocComment.builder().addComment("This is an override method called `close()`").addParam("valOne", "string type").addParam("valTwo", "boolean type").addComment("The return value is int 3.").build();
ValueExpr returnExpr = ValueExpr.builder().setValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("3").build()).build();
List<VariableExpr> arguments = Arrays.asList(VariableExpr.builder().setVariable(createVariable("valOne", TypeNode.STRING)).setIsDecl(true).build(), VariableExpr.builder().setVariable(createVariable("valTwo", TypeNode.BOOLEAN)).setIsDecl(true).build());
MethodDefinition methodDefinition = MethodDefinition.builder().setName("close").setIsOverride(true).setIsFinal(true).setIsStatic(true).setScope(ScopeNode.PROTECTED).setReturnType(TypeNode.INT).setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(IOException.class), TypeNode.withExceptionClazz(TimeoutException.class), TypeNode.withExceptionClazz(InterruptedException.class))).setArguments(arguments).setReturnExpr(returnExpr).setHeaderCommentStatements(Arrays.asList(CommentStatement.withComment(lineComment), CommentStatement.withComment(javaDocComment))).setAnnotations(Arrays.asList(AnnotationNode.withSuppressWarnings("all"), AnnotationNode.DEPRECATED)).setBody(Arrays.asList(createForStatement(), ExprStatement.withExpr(createAssignmentExpr("foobar", "false", TypeNode.BOOLEAN)))).build();
methodDefinition.accept(writerVisitor);
String expected = LineFormatter.lines("// AUTO-GENERATED DOCUMENTATION AND METHOD\n", "/**\n", "* This is an override method called `close()`\n", "* The return value is int 3.\n", "* @param valOne string type\n", "* @param valTwo boolean type\n", "*/\n", "@SuppressWarnings(\"all\")\n", "@Deprecated\n", "@Override\n", "protected static final int close(String valOne, boolean valTwo) throws" + " IOException, TimeoutException, InterruptedException {\n", "for (String str : getSomeStrings()) {\n", "boolean aBool = false;\n", "}\n", "boolean foobar = false;\n", "return 3;\n", "}\n\n");
assertEquals(expected, writerVisitor.write());
}
Aggregations