Search in sources :

Example 61 with MethodDefinition

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

Example 62 with MethodDefinition

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());
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) IOException(java.io.IOException) TypeNode(com.google.api.generator.engine.ast.TypeNode) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) Test(org.junit.Test)

Example 63 with MethodDefinition

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();
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr)

Example 64 with MethodDefinition

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());
}
Also used : ValueExpr(com.google.api.generator.engine.ast.ValueExpr) Variable(com.google.api.generator.engine.ast.Variable) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) AnonymousClassExpr(com.google.api.generator.engine.ast.AnonymousClassExpr) Test(org.junit.Test)

Example 65 with MethodDefinition

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());
}
Also used : Reference(com.google.api.generator.engine.ast.Reference) ConcreteReference(com.google.api.generator.engine.ast.ConcreteReference) VaporReference(com.google.api.generator.engine.ast.VaporReference) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Aggregations

MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)68 TypeNode (com.google.api.generator.engine.ast.TypeNode)55 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)52 ArrayList (java.util.ArrayList)43 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)39 MethodInvocationExpr (com.google.api.generator.engine.ast.MethodInvocationExpr)39 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)38 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)37 Expr (com.google.api.generator.engine.ast.Expr)35 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)35 AnnotationNode (com.google.api.generator.engine.ast.AnnotationNode)34 NewObjectExpr (com.google.api.generator.engine.ast.NewObjectExpr)34 Variable (com.google.api.generator.engine.ast.Variable)32 ClassDefinition (com.google.api.generator.engine.ast.ClassDefinition)30 ScopeNode (com.google.api.generator.engine.ast.ScopeNode)30 TypeStore (com.google.api.generator.gapic.composer.store.TypeStore)30 GapicContext (com.google.api.generator.gapic.model.GapicContext)30 JavaStyle (com.google.api.generator.gapic.utils.JavaStyle)30 Arrays (java.util.Arrays)30 List (java.util.List)30