Search in sources :

Example 11 with ReturnExpr

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();
}
Also used : LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) Expr(com.google.api.generator.engine.ast.Expr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ArrayList(java.util.ArrayList) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr)

Example 12 with ReturnExpr

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();
}
Also used : IfStatement(com.google.api.generator.engine.ast.IfStatement) Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Example 13 with ReturnExpr

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());
}
Also used : ReturnExpr(com.google.api.generator.engine.ast.ReturnExpr) Test(org.junit.Test)

Aggregations

ReturnExpr (com.google.api.generator.engine.ast.ReturnExpr)13 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)11 CastExpr (com.google.api.generator.engine.ast.CastExpr)11 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)11 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)11 Expr (com.google.api.generator.engine.ast.Expr)10 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)10 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)10 ThrowExpr (com.google.api.generator.engine.ast.ThrowExpr)10 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)10 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)10 ArrayList (java.util.ArrayList)10 TypeNode (com.google.api.generator.engine.ast.TypeNode)9 IfStatement (com.google.api.generator.engine.ast.IfStatement)8 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)7 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)7 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)7 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)7 Reference (com.google.api.generator.engine.ast.Reference)7 Statement (com.google.api.generator.engine.ast.Statement)7