Search in sources :

Example 21 with Variable

use of com.google.api.generator.engine.ast.Variable 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 22 with Variable

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

the class SampleCodeWriterTest method createAssignmentExpr.

private static AssignmentExpr createAssignmentExpr(String varName, String varValue, TypeNode type) {
    Variable variable = Variable.builder().setName(varName).setType(type).build();
    VariableExpr variableExpr = VariableExpr.builder().setVariable(variable).setIsDecl(true).build();
    return AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(ValueExpr.withValue(PrimitiveValue.builder().setType(type).setValue(varValue).build())).build();
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) VariableExpr(com.google.api.generator.engine.ast.VariableExpr)

Example 23 with Variable

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

the class SampleCodeWriterTest method setUp.

@BeforeClass
public static void setUp() {
    TypeNode settingType = TypeNode.withReference(ConcreteReference.withClazz(ClientSettings.class));
    Variable aVar = Variable.builder().setName("clientSettings").setType(settingType).build();
    VariableExpr aVarExpr = VariableExpr.withVariable(aVar);
    MethodInvocationExpr aValueExpr = MethodInvocationExpr.builder().setExprReferenceExpr(MethodInvocationExpr.builder().setMethodName("newBuilder").setStaticReferenceType(settingType).build()).setReturnType(settingType).setMethodName("build").build();
    AssignmentExpr assignmentExpr = AssignmentExpr.builder().setVariableExpr(aVarExpr.toBuilder().setIsDecl(true).build()).setValueExpr(aValueExpr).build();
    TryCatchStatement sampleStatement = TryCatchStatement.builder().setTryResourceExpr(createAssignmentExpr("aBool", "false", TypeNode.BOOLEAN)).setTryBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))).setIsSampleCode(true).build();
    testingSampleStatements = Arrays.asList(ExprStatement.withExpr(assignmentExpr), sampleStatement);
    regionTag = RegionTag.builder().setApiShortName("testing").setApiVersion("v1").setServiceName("samples").setRpcName("write").build();
    testingSample = Sample.builder().setFileHeader(Arrays.asList(CommentStatement.withComment(BlockComment.withComment("Apache License")))).setBody(testingSampleStatements).setRegionTag(regionTag).build();
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) ClientSettings(com.google.api.gax.rpc.ClientSettings) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) BeforeClass(org.junit.BeforeClass)

Example 24 with Variable

use of com.google.api.generator.engine.ast.Variable 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 25 with Variable

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

the class JavaWriterVisitorTest method writeAssignmentExpr_varToVar.

@Test
public void writeAssignmentExpr_varToVar() {
    Variable variable = Variable.builder().setName("foobar").setType(TypeNode.INT).build();
    VariableExpr variableExpr = VariableExpr.builder().setVariable(variable).setIsStatic(true).setIsFinal(true).setScope(ScopeNode.PRIVATE).setIsDecl(true).build();
    Variable anotherVariable = Variable.builder().setName("y").setType(TypeNode.INT).build();
    Expr valueExpr = VariableExpr.builder().setVariable(anotherVariable).build();
    AssignmentExpr assignExpr = AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(valueExpr).build();
    assignExpr.accept(writerVisitor);
    assertEquals("private static final int foobar = y", 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) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) Test(org.junit.Test)

Aggregations

Variable (com.google.api.generator.engine.ast.Variable)58 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)54 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)37 Test (org.junit.Test)35 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)34 TypeNode (com.google.api.generator.engine.ast.TypeNode)29 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)26 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)25 Expr (com.google.api.generator.engine.ast.Expr)24 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)20 AnonymousClassExpr (com.google.api.generator.engine.ast.AnonymousClassExpr)19 CastExpr (com.google.api.generator.engine.ast.CastExpr)17 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)17 ReturnExpr (com.google.api.generator.engine.ast.ReturnExpr)17 TernaryExpr (com.google.api.generator.engine.ast.TernaryExpr)17 ThrowExpr (com.google.api.generator.engine.ast.ThrowExpr)17 List (java.util.List)17 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)16 PrimitiveValue (com.google.api.generator.engine.ast.PrimitiveValue)16 ArrayList (java.util.ArrayList)16