Search in sources :

Example 26 with MethodDefinition

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

the class ImportWriterVisitorTest method writeMethodDefinitionImports_templatedMixedNamesAndTypes.

@Test
public void writeMethodDefinitionImports_templatedMixedNamesAndTypes() {
    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.withReference(ConcreteReference.withClazz(AssignmentExpr.class)))).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("import com.google.api.generator.engine.ast.AssignmentExpr;\n", "import java.util.Map;\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) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) TypeNode(com.google.api.generator.engine.ast.TypeNode) Test(org.junit.Test)

Example 27 with MethodDefinition

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

the class ImportWriterVisitorTest method writeAnonymousClassExprImports.

@Test
public void writeAnonymousClassExprImports() {
    // [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] HashMap<String, Integer> map;
    ConcreteReference mapRef = ConcreteReference.builder().setClazz(HashMap.class).setGenerics(Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(Integer.class))).build();
    VariableExpr mapExpr = VariableExpr.builder().setVariable(Variable.builder().setName("map").setType(TypeNode.withReference(mapRef)).build()).setIsDecl(true).build();
    ExprStatement exprStatement = ExprStatement.withExpr(mapExpr);
    // [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)).setReturnExpr(returnArg).setName("apply").build();
    AnonymousClassExpr anonymousClassExpr = AnonymousClassExpr.builder().setType(type).setMethods(Arrays.asList(method)).setStatements(Arrays.asList(exprStatement)).build();
    anonymousClassExpr.accept(writerVisitor);
    assertEquals(LineFormatter.lines("import com.google.api.generator.engine.ast.MethodDefinition;\n", "import com.google.common.base.Function;\n", "import java.io.IOException;\n", "import java.util.HashMap;\n", "import java.util.List;\n\n"), writerVisitor.write());
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) 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 28 with MethodDefinition

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

the class JavaCodeGeneratorTest method createOverrideCreateBookMethod.

private MethodDefinition createOverrideCreateBookMethod(VaporReference classRef, Expr bookKindDefaultExpr) {
    VariableExpr thisBookKindVariableExpr = createVarExprFromRefVarExpr(bookKindVar, ValueExpr.withValue(ThisObjectValue.withType(TypeNode.withReference(classRef))));
    VariableExpr thisSeriesNumVariableExpr = createVarExprFromRefVarExpr(seriesNumVar, ValueExpr.withValue(ThisObjectValue.withType(TypeNode.withReference(classRef))));
    AssignmentExpr thisSeriesNumAssignExpr = createAssignmentExpr(thisSeriesNumVariableExpr, VariableExpr.withVariable(seriesNumVar));
    AssignmentExpr thisBookKindAssignmentExpr = createAssignmentExpr(thisBookKindVariableExpr, bookKindDefaultExpr);
    MethodDefinition overrideCreateBook = MethodDefinition.builder().setAnnotations(Arrays.asList(AnnotationNode.OVERRIDE)).setArguments(Arrays.asList(createVarDeclExpr(seriesNumVar), createVarDeclExpr(bookKindVar))).setReturnType(TypeNode.VOID).setName("createBook").setScope(ScopeNode.PUBLIC).setBody(Arrays.asList(ExprStatement.withExpr(thisSeriesNumAssignExpr), ExprStatement.withExpr(thisBookKindAssignmentExpr))).build();
    return overrideCreateBook;
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Example 29 with MethodDefinition

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

the class JavaCodeGeneratorTest method createNestedClassShelf.

private ClassDefinition createNestedClassShelf() {
    VariableExpr shelfNameDel = createVarPublicDeclExpr(shelfNameVar);
    VariableExpr seriesNumDel = createVarPublicDeclExpr(seriesNumVar);
    VariableExpr shelfServiceNameDel = createVarPublicDeclExpr(shelfServiceNameVar);
    AssignmentExpr shelfServiceNameAssignmentExpr = createAssignmentExpr(shelfServiceNameDel, VariableExpr.withVariable(createVarFromType(TypeNode.STRING, "serviceName")));
    MethodDefinition nestedShelfClassCtor = createNestedShelfClassCtor();
    return ClassDefinition.builder().setIsNested(true).setMethods(Arrays.asList(nestedShelfClassCtor)).setStatements(Arrays.asList(ExprStatement.withExpr(shelfNameDel), ExprStatement.withExpr(seriesNumDel), ExprStatement.withExpr(shelfServiceNameAssignmentExpr))).setName("Shelf").setScope(ScopeNode.PUBLIC).build();
}
Also used : MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr)

Example 30 with MethodDefinition

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

the class JavaCodeGeneratorTest method validJavaClass.

@Test
public void validJavaClass() {
    // Create outer class variableDecls.
    // [code] private static final String serviceName = "LibraryServiceStub";
    VariableExpr serviceName = createServiceNameVarExpr();
    AssignmentExpr serviceNameDel = createAssignmentExpr(serviceName, ValueExpr.withValue(StringObjectValue.withValue("LibraryServiceStub")));
    // [code] protected List<Shelf> shelfList;
    VariableExpr shelfListExpr = createShelfListVarExpr();
    // [code] public static HashMap<String, Shelf> shelfMap;
    VariableExpr shelfMapExpr = createShelfMapVarExpr();
    // Create the LibraryServiceStub constructor
    MethodDefinition libraryServiceCtor = createLibServiceCtor();
    // Create nested class Shelf.
    ClassDefinition nestedClassShelf = createNestedClassShelf();
    // Create nested abstract class Book.
    ClassDefinition nestedClassBook = createNestedClassBook();
    // Create nested class Novel.
    ClassDefinition nestedClassNovel = createNestedClassNovel();
    // Create method `addShelf`
    MethodDefinition addShelfMethod = createAddShelfMethod();
    // Create method `updateShelfMap`
    MethodDefinition updateShelfMap = createUpdateShelfMap();
    // Create method `printShelfListToFile`
    MethodDefinition printShelfListToFile = createPrintShelfListToFile();
    // Create method `addBooksContainsNovel`
    MethodDefinition addBooksContainsNovel = createAddBooksContainsNovel();
    // Create private method `addBookToShelf`
    MethodDefinition addBookToShelf = createAddBookToShelf();
    // Create outer class LibraryServiceStub
    ClassDefinition libraryServiceStubClass = ClassDefinition.builder().setFileHeader(Arrays.asList(createFileHeader())).setHeaderCommentStatements(Arrays.asList(createOuterClassJavaDocComment())).setPackageString("com.google.example.library.core").setAnnotations(Arrays.asList(AnnotationNode.withSuppressWarnings("all"), AnnotationNode.DEPRECATED, AnnotationNode.OVERRIDE)).setImplementsTypes(Arrays.asList(TypeNode.withReference(libraryServiceRef))).setExtendsType(TypeNode.withReference(stubRef)).setScope(ScopeNode.PUBLIC).setStatements(Arrays.asList(ExprStatement.withExpr(serviceNameDel), ExprStatement.withExpr(shelfListExpr), ExprStatement.withExpr(shelfMapExpr))).setMethods(Arrays.asList(libraryServiceCtor, addShelfMethod, updateShelfMap, printShelfListToFile, addBooksContainsNovel, addBookToShelf)).setNestedClasses(Arrays.asList(nestedClassShelf, nestedClassBook, nestedClassNovel)).setName("LibraryServiceStub").build();
    JavaWriterVisitor javaWriterVisitor = new JavaWriterVisitor();
    libraryServiceStubClass.accept(javaWriterVisitor);
    Utils.saveCodegenToFile(this.getClass(), "JavaCodeGeneratorTest.golden", javaWriterVisitor.write());
    Path goldenFilePath = Paths.get(GOLDENFILES_DIRECTORY, "JavaCodeGeneratorTest.golden");
    Assert.assertCodeEquals(goldenFilePath, javaWriterVisitor.write());
}
Also used : Path(java.nio.file.Path) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) JavaWriterVisitor(com.google.api.generator.engine.writer.JavaWriterVisitor) AssignmentExpr(com.google.api.generator.engine.ast.AssignmentExpr) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) 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