use of com.google.api.generator.engine.ast.ReturnExpr in project gapic-generator-java by googleapis.
the class ResourceNameHelperClassComposer method createToStringMethod.
private static MethodDefinition createToStringMethod(List<VariableExpr> templateFinalVarExprs, Map<String, VariableExpr> patternTokenVarExprs, List<List<String>> tokenHierarchies) {
boolean hasVariants = tokenHierarchies.size() > 1;
if (!hasVariants) {
List<Expr> instantiateArgExprs = new ArrayList<>();
List<String> tokens = getTokenSet(tokenHierarchies).stream().collect(Collectors.toList());
for (String token : tokens) {
Preconditions.checkNotNull(patternTokenVarExprs.get(token), String.format("No expression found for token %s amongst values %s", token, patternTokenVarExprs.toString()));
instantiateArgExprs.add(ValueExpr.withValue(StringObjectValue.withValue(token)));
instantiateArgExprs.add(patternTokenVarExprs.get(token));
}
MethodInvocationExpr returnInstantiateExpr = MethodInvocationExpr.builder().setExprReferenceExpr(templateFinalVarExprs.get(0)).setMethodName("instantiate").setArguments(instantiateArgExprs).setReturnType(TypeNode.STRING).build();
return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.STRING).setName("toString").setReturnExpr(returnInstantiateExpr).build();
}
VariableExpr fixedValueVarExpr = FIXED_CLASS_VARS.get("fixedValue");
// Code: return fixedValue != null ? fixedValue : pathTemplate.instantiate(getFieldValuesMap())
Expr fixedValueNullCheck = RelationalOperationExpr.notEqualToWithExprs(fixedValueVarExpr, ValueExpr.createNullExpr());
MethodInvocationExpr instantiateExpr = MethodInvocationExpr.builder().setExprReferenceExpr(FIXED_CLASS_VARS.get("pathTemplate")).setMethodName("instantiate").setArguments(MethodInvocationExpr.builder().setMethodName("getFieldValuesMap").build()).setReturnType(TypeNode.STRING).build();
TernaryExpr returnExpr = TernaryExpr.builder().setConditionExpr(fixedValueNullCheck).setElseExpr(instantiateExpr).setThenExpr(fixedValueVarExpr).build();
return MethodDefinition.builder().setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.STRING).setName("toString").setReturnExpr(returnExpr).build();
}
use of com.google.api.generator.engine.ast.ReturnExpr in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method createAddShelfMethod.
private MethodDefinition createAddShelfMethod() {
Variable nameVar = createVarFromType(TypeNode.STRING, "name");
Variable seriesDoubleNumVar = createVarFromType(TypeNode.DOUBLE, "seriesDoubleNum");
CastExpr seriesNumDoubleToIntExpr = CastExpr.builder().setExpr(VariableExpr.withVariable(seriesDoubleNumVar)).setType(TypeNode.INT).build();
AssignmentExpr castSeriesNumExpr = createAssignmentExpr(createVarDeclExpr(seriesNumVar), seriesNumDoubleToIntExpr);
ReturnExpr maxValueReturnExpr = createReturnExpr("Series number equals to max int value.");
ReturnExpr duplicateKeyReturnExpr = createReturnExpr("Shelf is already existing in the map.");
// TODO: update the condition from `condition` to `seriesNum == MAX_VALUE`
IfStatement maxValueCheck = IfStatement.builder().setConditionExpr(VariableExpr.withVariable(createVarFromType(TypeNode.BOOLEAN, "condition"))).setBody(Arrays.asList(ExprStatement.withExpr(maxValueReturnExpr))).build();
NewObjectExpr newShelfExpr = createNewObjectExprWithArg(nameVar);
MethodInvocationExpr addShelfToListExpr = methodExprWithRefAndArg(shelfListVar, "add", Arrays.asList(newShelfExpr));
MethodInvocationExpr putShelfToMapExpr = methodExprWithRefAndArg(shelfMapVar, "put", Arrays.asList(VariableExpr.withVariable(nameVar), newShelfExpr));
MethodInvocationExpr mapContainsKey = methodExprWithRefArgAndReturn(shelfMapVar, Arrays.asList(VariableExpr.withVariable(nameVar)));
IfStatement mapContainsKeyCheck = IfStatement.builder().setConditionExpr(mapContainsKey).setBody(Arrays.asList(ExprStatement.withExpr(duplicateKeyReturnExpr))).build();
return MethodDefinition.builder().setAnnotations(Arrays.asList(AnnotationNode.OVERRIDE)).setName("addShelf").setReturnType(TypeNode.STRING).setReturnExpr(ValueExpr.withValue(StringObjectValue.withValue("Shelf added."))).setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(castSeriesNumExpr), maxValueCheck, ExprStatement.withExpr(addShelfToListExpr), mapContainsKeyCheck, ExprStatement.withExpr(putShelfToMapExpr))).setArguments(Arrays.asList(createVarDeclExpr(nameVar), createVarDeclExpr(seriesDoubleNumVar))).build();
}
use of com.google.api.generator.engine.ast.ReturnExpr in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeReturnExpr_basic.
@Test
public void writeReturnExpr_basic() {
ReturnExpr returnExpr = ReturnExpr.withExpr(ValueExpr.withValue(StringObjectValue.withValue("asdf")));
returnExpr.accept(writerVisitor);
assertEquals("return \"asdf\"", writerVisitor.write());
}
Aggregations