use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeMethodDefinition_withArgumentsAndReturnExpr.
@Test
public void writeMethodDefinition_withArgumentsAndReturnExpr() {
ValueExpr returnExpr = ValueExpr.builder().setValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("3").build()).build();
List<VariableExpr> arguments = Arrays.asList(VariableExpr.builder().setVariable(createVariable("x", TypeNode.INT)).setIsDecl(true).build(), VariableExpr.builder().setVariable(createVariable("y", TypeNode.INT)).setIsDecl(true).build());
MethodDefinition methodDefinition = MethodDefinition.builder().setName("close").setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.INT).setArguments(arguments).setReturnExpr(returnExpr).setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("foobar", "false", TypeNode.BOOLEAN)))).build();
methodDefinition.accept(writerVisitor);
assertEquals(LineFormatter.lines("public int close(int x, int y) {\n", "boolean foobar = false;\n", "return 3;\n", "}\n\n"), writerVisitor.write());
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAnonymousClassExpr_generics.
@Test
public void writeAnonymousClassExpr_generics() {
// [Constructing] Function<List<IOException>, MethodDefinition>
ConcreteReference exceptionListRef = ConcreteReference.builder().setClazz(List.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(IOException.class))).build();
ConcreteReference methodDefinitionRef = ConcreteReference.withClazz(MethodDefinition.class);
ConcreteReference ref = ConcreteReference.builder().setClazz(Function.class).setGenerics(Arrays.asList(exceptionListRef, methodDefinitionRef)).build();
TypeNode type = TypeNode.withReference(ref);
// [Constructing] an input argument whose type is `List<IOException>`
VariableExpr arg = VariableExpr.builder().setVariable(Variable.builder().setName("arg").setType(TypeNode.withReference(exceptionListRef)).build()).setIsDecl(true).build();
// [Constructing] a return variable expression whose type is `MethodDefinition`
VariableExpr returnArg = VariableExpr.builder().setVariable(Variable.builder().setName("returnArg").setType(TypeNode.withReference(methodDefinitionRef)).build()).build();
MethodDefinition method = MethodDefinition.builder().setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.withReference(methodDefinitionRef)).setArguments(Arrays.asList(arg)).setIsOverride(true).setReturnExpr(returnArg).setName("apply").build();
AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setMethods(Arrays.asList(method)).build();
anonymousClassExpr.accept(writerVisitor);
String expected = LineFormatter.lines("new Function<List<IOException>, MethodDefinition>() {\n", "@Override\n", "public MethodDefinition apply(List<IOException> arg) {\n", "return returnArg;\n", "}\n\n}");
assertEquals(expected, writerVisitor.write());
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class JavaCodeGeneratorTest method createNestedClassBook.
private ClassDefinition createNestedClassBook() {
VariableExpr bookKindDel = createVarPublicDeclExpr(bookKindVar);
MethodDefinition createBookMethod = createAbstractCreateBookMethod();
VariableExpr seriesNumDel = createVarPublicDeclExpr(seriesNumVar);
return ClassDefinition.builder().setHeaderCommentStatements(Arrays.asList(createPreMethodLineComment("Test nested abstract class and abstract method."))).setMethods(Arrays.asList(createBookMethod)).setStatements(Arrays.asList(ExprStatement.withExpr(bookKindDel), ExprStatement.withExpr(seriesNumDel))).setName("Book").setScope(ScopeNode.PUBLIC).setIsNested(true).setIsAbstract(true).build();
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeAnonymousClassExpr_withStatementsMethods.
@Test
public void writeAnonymousClassExpr_withStatementsMethods() {
ConcreteReference ref = ConcreteReference.withClazz(Runnable.class);
TypeNode type = TypeNode.withReference(ref);
// [Constructing] private static final String s = "foo";
Variable variable = createVariable("s", TypeNode.STRING);
VariableExpr variableExpr = VariableExpr.builder().setScope(ScopeNode.PRIVATE).setIsDecl(true).setIsFinal(true).setIsStatic(true).setVariable(variable).build();
ValueExpr valueExpr = ValueExpr.builder().setValue(StringObjectValue.withValue("foo")).build();
AssignmentExpr assignmentExpr = AssignmentExpr.builder().setVariableExpr(variableExpr).setValueExpr(valueExpr).build();
ExprStatement exprStatement = ExprStatement.withExpr(assignmentExpr);
MethodDefinition methodDefinition = MethodDefinition.builder().setName("run").setIsOverride(true).setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("x", "3", TypeNode.INT)))).build();
AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setStatements(Arrays.asList(exprStatement)).setMethods(Arrays.asList(methodDefinition)).build();
anonymousClassExpr.accept(writerVisitor);
String expected = LineFormatter.lines("new Runnable() {\n", "private static final String s = \"foo\";\n", "@Override\n", "public void run() {\n", "int x = 3;\n}\n\n}");
assertEquals(expected, writerVisitor.write());
}
use of com.google.api.generator.engine.ast.MethodDefinition in project gapic-generator-java by googleapis.
the class JavaWriterVisitorTest method writeMethodDefinition_templatedReturnTypeAndArguments.
@Test
public void writeMethodDefinition_templatedReturnTypeAndArguments() {
Reference mapRef = ConcreteReference.withClazz(Map.class);
List<VariableExpr> arguments = Arrays.asList(VariableExpr.builder().setVariable(createVariable("x", TypeNode.withReference(mapRef))).setIsDecl(true).setTemplateObjects(Arrays.asList("K", TypeNode.STRING)).build(), VariableExpr.builder().setVariable(createVariable("y", TypeNode.withReference(mapRef))).setIsDecl(true).setTemplateObjects(Arrays.asList("T", "V")).build());
TypeNode returnType = TypeNode.withReference(mapRef);
MethodDefinition methodDefinition = MethodDefinition.builder().setName("close").setScope(ScopeNode.PUBLIC).setReturnType(returnType).setTemplateNames(Arrays.asList("T", "K", "V")).setReturnTemplateNames(Arrays.asList("K", "V")).setArguments(arguments).setReturnExpr(MethodInvocationExpr.builder().setMethodName("foobar").setReturnType(returnType).build()).build();
methodDefinition.accept(writerVisitor);
assertEquals(LineFormatter.lines("public <T, K, V> Map<K, V> close(Map<K, String> x, Map<T, V> y) {\n", "return foobar();\n", "}\n\n"), writerVisitor.write());
}
Aggregations