Search in sources :

Example 91 with MethodInvocationExpr

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

the class ImportWriterVisitorTest method writeArithmeticOperationExprImports.

@Test
public void writeArithmeticOperationExprImports() {
    MethodInvocationExpr lhsExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class))).setMethodName("getSomething").setReturnType(TypeNode.STRING).build();
    ValueExpr rhsExpr = ValueExpr.createNullExpr();
    ArithmeticOperationExpr arithmeticOperationExpr = ArithmeticOperationExpr.concatWithExprs(lhsExpr, rhsExpr);
    arithmeticOperationExpr.accept(writerVisitor);
    assertEquals("import com.google.api.generator.engine.ast.Expr;\n\n", writerVisitor.write());
}
Also used : ValueExpr(com.google.api.generator.engine.ast.ValueExpr) ArithmeticOperationExpr(com.google.api.generator.engine.ast.ArithmeticOperationExpr) 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) Test(org.junit.Test)

Example 92 with MethodInvocationExpr

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

the class ImportWriterVisitorTest method writeNewObjectExprImports_methodExprArg.

@Test
public void writeNewObjectExprImports_methodExprArg() {
    // [Constructing] `new IOException(message, cause(mapArg))` and `cause(mapArg)` is a method
    // invocation with a `HashMap` argument.
    TypeNode exceptionType = TypeNode.withReference(ConcreteReference.withClazz(IOException.class));
    Variable message = Variable.builder().setName("message").setType(TypeNode.STRING).build();
    TypeNode mapType = TypeNode.withReference(ConcreteReference.withClazz(HashMap.class));
    VariableExpr mapExpr = VariableExpr.builder().setVariable(Variable.builder().setName("mapArg").setType(mapType).build()).build();
    VariableExpr msgExpr = VariableExpr.builder().setVariable(message).build();
    MethodInvocationExpr causeExpr = MethodInvocationExpr.builder().setMethodName("cause").setArguments(Arrays.asList(mapExpr)).setReturnType(TypeNode.withReference(ConcreteReference.withClazz(Throwable.class))).build();
    NewObjectExpr newObjectExpr = NewObjectExpr.builder().setType(exceptionType).setArguments(Arrays.asList(msgExpr, causeExpr)).build();
    newObjectExpr.accept(writerVisitor);
    assertEquals(LineFormatter.lines("import java.io.IOException;\n", "import java.util.HashMap;\n\n"), writerVisitor.write());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) HashMap(java.util.HashMap) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) IOException(java.io.IOException) Test(org.junit.Test)

Example 93 with MethodInvocationExpr

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

the class JavaWriterVisitorTest method writeNewObjectExpr_withGenericsAndArgs.

@Test
public void writeNewObjectExpr_withGenericsAndArgs() {
    // isGeneric() is true and generics() is not empty.
    ConcreteReference listRef = ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(Integer.class))).build();
    ConcreteReference mapRef = ConcreteReference.builder().setClazz(HashMap.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), listRef)).build();
    TypeNode type = TypeNode.withReference(mapRef);
    TypeNode someType = TypeNode.withReference(VaporReference.builder().setName("SomeClass").setPakkage("com.google.api.generator.engine").build());
    MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("foobar").setReturnType(TypeNode.INT).setStaticReferenceType(someType).build();
    Variable num = Variable.builder().setName("num").setType(TypeNode.FLOAT).build();
    VariableExpr numExpr = VariableExpr.builder().setVariable(num).build();
    NewObjectExpr newObjectExpr = NewObjectExpr.builder().setIsGeneric(true).setType(type).setArguments(Arrays.asList(methodExpr, numExpr)).build();
    newObjectExpr.accept(writerVisitor);
    assertEquals("new HashMap<String, List<Integer>>(SomeClass.foobar(), num)", writerVisitor.write());
}
Also used : Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) NewObjectExpr(com.google.api.generator.engine.ast.NewObjectExpr) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) Test(org.junit.Test)

Example 94 with MethodInvocationExpr

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

the class JavaWriterVisitorTest method writeThisObjectValue_accessFieldAndInvokeMethod.

@Test
public void writeThisObjectValue_accessFieldAndInvokeMethod() {
    VaporReference ref = VaporReference.builder().setName("Student").setPakkage("com.google.example.v1").build();
    TypeNode classType = TypeNode.withReference(ref);
    ThisObjectValue thisObjectValue = ThisObjectValue.withType(classType);
    ValueExpr thisValueExpr = ValueExpr.withValue(thisObjectValue);
    VariableExpr varExpr = VariableExpr.builder().setVariable(Variable.builder().setName("id").setType(TypeNode.STRING).build()).build();
    Variable subVariable = Variable.builder().setName("name").setType(TypeNode.STRING).build();
    VariableExpr thisVariableExpr = VariableExpr.builder().setVariable(subVariable).setExprReferenceExpr(thisValueExpr).build();
    MethodInvocationExpr methodExpr = MethodInvocationExpr.builder().setMethodName("getName").setExprReferenceExpr(ValueExpr.withValue(thisObjectValue)).setArguments(Arrays.asList(varExpr)).setReturnType(TypeNode.STRING).build();
    AssignmentExpr assignmentExpr = AssignmentExpr.builder().setVariableExpr(thisVariableExpr).setValueExpr(methodExpr).build();
    assignmentExpr.accept(writerVisitor);
    assertThat(writerVisitor.write()).isEqualTo("this.name = this.getName(id)");
}
Also used : ThisObjectValue(com.google.api.generator.engine.ast.ThisObjectValue) ValueExpr(com.google.api.generator.engine.ast.ValueExpr) Variable(com.google.api.generator.engine.ast.Variable) MethodInvocationExpr(com.google.api.generator.engine.ast.MethodInvocationExpr) VaporReference(com.google.api.generator.engine.ast.VaporReference) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) Test(org.junit.Test)

Example 95 with MethodInvocationExpr

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

the class ImportWriterVisitorTest method writeRelationalOperationExprImports.

@Test
public void writeRelationalOperationExprImports() {
    MethodInvocationExpr lhsExpr = MethodInvocationExpr.builder().setStaticReferenceType(TypeNode.withReference(ConcreteReference.withClazz(Expr.class))).setMethodName("getSomething").setReturnType(TypeNode.STRING).build();
    TypeNode someType = TypeNode.withReference(VaporReference.builder().setName("SomeClass").setPakkage("com.google.api.generator.engine").build());
    MethodInvocationExpr rhsExpr = MethodInvocationExpr.builder().setMethodName("getName").setStaticReferenceType(someType).setReturnType(TypeNode.STRING).build();
    RelationalOperationExpr relationalOperationExpr = RelationalOperationExpr.equalToWithExprs(lhsExpr, rhsExpr);
    relationalOperationExpr.accept(writerVisitor);
    assertEquals(LineFormatter.lines("import com.google.api.generator.engine.SomeClass;\n", "import com.google.api.generator.engine.ast.Expr;\n\n"), writerVisitor.write());
}
Also used : RelationalOperationExpr(com.google.api.generator.engine.ast.RelationalOperationExpr) 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) Test(org.junit.Test)

Aggregations

MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)103 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)76 TypeNode (com.google.api.generator.engine.ast.TypeNode)72 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)59 Expr (com.google.api.generator.engine.ast.Expr)50 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)48 ArrayList (java.util.ArrayList)41 Variable (com.google.api.generator.engine.ast.Variable)39 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)38 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)36 Statement (com.google.api.generator.engine.ast.Statement)33 List (java.util.List)31 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)29 Test (org.junit.Test)29 RelationalOperationExpr (com.google.api.generator.engine.ast.RelationalOperationExpr)27 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)26 Arrays (java.util.Arrays)26 JavaStyle (com.google.api.generator.gapic.utils.JavaStyle)25 Map (java.util.Map)25 Function (java.util.function.Function)24