Search in sources :

Example 1 with TernaryExpr

use of com.google.api.generator.engine.ast.TernaryExpr in project gapic-generator-java by googleapis.

the class ImportWriterVisitorTest method writeTernaryExprImports.

@Test
public void writeTernaryExprImports() {
    MethodInvocationExpr conditionExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class))).setMethodName("isExpr").setReturnType(TypeNode.BOOLEAN).build();
    MethodInvocationExpr thenExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Strings.class))).setMethodName("isNullOrEmpty").setReturnType(TypeNode.BOOLEAN).build();
    MethodInvocationExpr elseExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(TypeNode.class))).setMethodName("isPrimitiveType").setReturnType(TypeNode.BOOLEAN).build();
    TernaryExpr ternaryExpr = TernaryExpr.builder().setConditionExpr(conditionExpr).setThenExpr(thenExpr).setElseExpr(elseExpr).build();
    ternaryExpr.accept(writerVisitor);
    assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.Expr;\n", "import com.google.api.generator.engine.ast.TypeNode;\n", "import com.google.common.base.Strings;\n\n"), writerVisitor.write());
}
Also used : ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) 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) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) ArithmeticOperationExpr(com.google.api.generator.engine.ast.ArithmeticOperationExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Strings(com.google.common.base.Strings) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) Test(org.junit.Test)

Example 2 with TernaryExpr

use of com.google.api.generator.engine.ast.TernaryExpr in project gapic-generator-java by googleapis.

the class JavaCodeGeneratorTest method createAddBooksContainsNovel.

private MethodDefinition createAddBooksContainsNovel() {
    ConcreteReference bookKindStackRef = ConcreteReference.builder().setClazz(Stack.class).setGenerics(Arrays.asList(bookKindRef)).build();
    Variable bookKindStackVar = createVarFromConcreteRef(bookKindStackRef, "stack");
    Variable containsNovelVar = createVarFromType(TypeNode.BOOLEAN, "containsNovel");
    Variable bookVar = createVarFromVaporRef(bookClassRef, "addedBook");
    TernaryExpr ternaryExpr = createTernaryExpr(containsNovelVar);
    AssignmentExpr setContainsNovelToFalse = createAssignmentExpr(createVarDeclExpr(containsNovelVar), ValueExpr.withValue(createBooleanValue("false")));
    MethodInvocationExpr stackIsEmpty = MethodInvocationExpr.builder().setMethodName("isEmpty").setExprReferenceExpr(VariableExpr.withVariable(bookKindStackVar)).setReturnType(TypeNode.BOOLEAN).build();
    MethodInvocationExpr stackPop = methodExprWithRef(bookKindStackVar, "pop");
    MethodInvocationExpr addBookToShelfMethod = MethodInvocationExpr.builder().setMethodName("addBookToShelf").setArguments(stackPop, VariableExpr.withVariable(shelfVar)).setReturnType(TypeNode.withReference(bookClassRef)).build();
    AssignmentExpr createNewAddedBook = createAssignmentExpr(createVarDeclExpr(bookVar), addBookToShelfMethod);
    InstanceofExpr addedBookIsNovelInstance = InstanceofExpr.builder().setExpr(VariableExpr.withVariable(bookVar)).setCheckType(TypeNode.withReference(novelClassRef)).build();
    AssignmentExpr setContainsNovelToTrue = createAssignmentExpr(VariableExpr.withVariable(containsNovelVar), ValueExpr.withValue(createBooleanValue("true")));
    IfStatement ifStatement = IfStatement.builder().setConditionExpr(addedBookIsNovelInstance).setBody(Arrays.asList(ExprStatement.withExpr(setContainsNovelToTrue))).build();
    // TODO: update the conditionExpr from `stack.isEmpty()` to `!stack.isEmpty()`
    WhileStatement whileStatement = WhileStatement.builder().setConditionExpr(stackIsEmpty).setBody(Arrays.asList(ExprStatement.withExpr(createNewAddedBook), ifStatement)).build();
    return MethodDefinition.builder().setHeaderCommentStatements(createPreMethodJavaDocComment()).setArguments(Arrays.asList(createVarDeclExpr(shelfVar), createVarDeclExpr(bookKindStackVar))).setName("addBooksContainsNovel").setReturnType(TypeNode.STRING).setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(setContainsNovelToFalse), whileStatement)).setReturnExpr(ternaryExpr).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) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr)

Example 3 with TernaryExpr

use of com.google.api.generator.engine.ast.TernaryExpr in project gapic-generator-java by googleapis.

the class JavaWriterVisitorTest method writeTernaryExpr_basic.

@Test
public void writeTernaryExpr_basic() {
    Variable conditionVariable = Variable.builder().setName("condition").setType(TypeNode.BOOLEAN).build();
    VariableExpr conditionExpr = VariableExpr.builder().setVariable(conditionVariable).build();
    Value value1 = PrimitiveValue.builder().setType(TypeNode.INT).setValue("3").build();
    Expr thenExpr = ValueExpr.builder().setValue(value1).build();
    Value value2 = PrimitiveValue.builder().setType(TypeNode.INT).setValue("4").build();
    Expr elseExpr = ValueExpr.builder().setValue(value2).build();
    TernaryExpr ternaryExpr = TernaryExpr.builder().setConditionExpr(conditionExpr).setThenExpr(thenExpr).setElseExpr(elseExpr).build();
    ternaryExpr.accept(writerVisitor);
    assertEquals("condition ? 3 : 4", writerVisitor.write());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) ReferenceConstructorExpr(com.google.api.generator.engine.ast.ReferenceConstructorExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) EnumRefExpr(com.google.api.generator.engine.ast.EnumRefExpr) 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) UnaryOperationExpr(com.google.api.generator.engine.ast.UnaryOperationExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) InstanceofExpr(com.google.api.generator.engine.ast.InstanceofExpr) CastExpr(com.google.api.generator.engine.ast.CastExpr) ArithmeticOperationExpr(com.google.api.generator.engine.ast.ArithmeticOperationExpr) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) LambdaExpr(com.google.api.generator.engine.ast.LambdaExpr) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) ThrowExpr(com.google.api.generator.engine.ast.ThrowExpr) ThisObjectValue(com.google.api.generator.engine.ast.ThisObjectValue) StringObjectValue(com.google.api.generator.engine.ast.StringObjectValue) PrimitiveValue(com.google.api.generator.engine.ast.PrimitiveValue) NullObjectValue(com.google.api.generator.engine.ast.NullObjectValue) Value(com.google.api.generator.engine.ast.Value) SuperObjectValue(com.google.api.generator.engine.ast.SuperObjectValue) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TernaryExpr(com.google.api.generator.engine.ast.TernaryExpr) Test(org.junit.Test)

Example 4 with TernaryExpr

use of com.google.api.generator.engine.ast.TernaryExpr 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)

Aggregations

AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)4 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)4 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)4 Expr (com.google.api.generator.engine.ast.Expr)3 InstanceofExpr (com.google.api.generator.engine.ast.InstanceofExpr)3 LogicalOperationExpr (com.google.api.generator.engine.ast.LogicalOperationExpr)3 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)3 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)3 ReturnExpr (com.google.api.generator.engine.ast.ReturnExpr)3 ThrowExpr (com.google.api.generator.engine.ast.ThrowExpr)3 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)3 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)3 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)2 ArithmeticOperationExpr (com.google.api.generator.engine.ast.ArithmeticOperationExpr)2 AssignmentOperationExpr (com.google.api.generator.engine.ast.AssignmentOperationExpr)2 CastExpr (com.google.api.generator.engine.ast.CastExpr)2 EnumRefExpr (com.google.api.generator.engine.ast.EnumRefExpr)2 LambdaExpr (com.google.api.generator.engine.ast.LambdaExpr)2 ReferenceConstructorExpr (com.google.api.generator.engine.ast.ReferenceConstructorExpr)2 UnaryOperationExpr (com.google.api.generator.engine.ast.UnaryOperationExpr)2