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());
}
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());
}
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());
}
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)");
}
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());
}
Aggregations