Search in sources :

Example 86 with VariableExpr

use of com.google.api.generator.engine.ast.VariableExpr 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());
}
Also used : ValueExpr(com.google.api.generator.engine.ast.ValueExpr) JavaDocComment(com.google.api.generator.engine.ast.JavaDocComment) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) LineComment(com.google.api.generator.engine.ast.LineComment) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Test(org.junit.Test)

Example 87 with VariableExpr

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

the class JavaWriterVisitorTest method writeAssignmentOperationExpr_multiplyAssignment.

@Test
public void writeAssignmentOperationExpr_multiplyAssignment() {
    VariableExpr lhsExpr = createVariableExpr("h", TypeNode.INT);
    ValueExpr rhsExpr = ValueExpr.withValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("1000003").build());
    AssignmentOperationExpr assignmentOperationExpr = AssignmentOperationExpr.multiplyAssignmentWithExprs(lhsExpr, rhsExpr);
    assignmentOperationExpr.accept(writerVisitor);
    assertThat(writerVisitor.write()).isEqualTo("h *= 1000003");
}
Also used : ValueExpr(com.google.api.generator.engine.ast.ValueExpr) AssignmentOperationExpr(com.google.api.generator.engine.ast.AssignmentOperationExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Test(org.junit.Test)

Example 88 with VariableExpr

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

the class JavaWriterVisitorTest method writeLogicalOperationExpr_logicalAnd.

@Test
public void writeLogicalOperationExpr_logicalAnd() {
    VariableExpr lhsExpr = VariableExpr.withVariable(createVariable("isEmpty", TypeNode.BOOLEAN));
    VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
    TypeNode classType = TypeNode.withReference(ref);
    MethodInvocationExpr rhsExpr = MethodInvocationExpr.builder().setMethodName("isValid").setExprReferenceExpr(ValueExpr.withValue(ThisObjectValue.withType(classType))).setReturnType(TypeNode.BOOLEAN).build();
    LogicalOperationExpr logicalOperationExpr = LogicalOperationExpr.logicalAndWithExprs(lhsExpr, rhsExpr);
    logicalOperationExpr.accept(writerVisitor);
    assertThat(writerVisitor.write()).isEqualTo("isEmpty && this.isValid()");
}
Also used : MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) LogicalOperationExpr(com.google.api.generator.engine.ast.LogicalOperationExpr) VaporReference(com.google.api.generator.engine.ast.VaporReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 89 with VariableExpr

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

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

VariableExpr (com.google.api.generator.engine.ast.VariableExpr)217 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)137 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)132 TypeNode (com.google.api.generator.engine.ast.TypeNode)118 Expr (com.google.api.generator.engine.ast.Expr)114 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)107 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)91 Variable (com.google.api.generator.engine.ast.Variable)88 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)87 ArrayList (java.util.ArrayList)87 Test (org.junit.Test)82 Statement (com.google.api.generator.engine.ast.Statement)80 List (java.util.List)64 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)62 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)58 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)56 CastExpr (com.google.api.generator.engine.ast.CastExpr)53 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)53 Arrays (java.util.Arrays)48 IfStatement (com.google.api.generator.engine.ast.IfStatement)47