Search in sources :

Example 11 with ClassDefinition

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

the class Writer method writeClazz.

private static String writeClazz(GapicClass gapicClazz, JavaWriterVisitor codeWriter, JarOutputStream jos) {
    ClassDefinition clazz = gapicClazz.classDefinition();
    clazz.accept(codeWriter);
    String code = codeWriter.write();
    codeWriter.clear();
    String path = getPath(clazz.packageString(), clazz.classIdentifier().name());
    String className = clazz.classIdentifier().name();
    JarEntry jarEntry = new JarEntry(String.format("%s/%s.java", path, className));
    try {
        jos.putNextEntry(jarEntry);
        jos.write(code.getBytes(StandardCharsets.UTF_8));
    } catch (IOException e) {
        throw new GapicWriterException(String.format("Could not write code for class %s.%s: %s", clazz.packageString(), clazz.classIdentifier().name(), e.getMessage()), e);
    }
    return path;
}
Also used : ByteString(com.google.protobuf.ByteString) IOException(java.io.IOException) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) JarEntry(java.util.jar.JarEntry)

Example 12 with ClassDefinition

use of com.google.api.generator.engine.ast.ClassDefinition 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)

Example 13 with ClassDefinition

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

the class ComposerTest method gapicClass_addApacheLicense.

@Test
public void gapicClass_addApacheLicense() {
    ClassDefinition classDef = ClassDefinition.builder().setPackageString("com.google.showcase.v1beta1.stub").setName("ComposerPostProcOnFooBar").setScope(ScopeNode.PUBLIC).build();
    List<GapicClass> gapicClassWithHeaderList = Composer.addApacheLicense(Arrays.asList(GapicClass.create(Kind.TEST, classDef)));
    JavaWriterVisitor visitor = new JavaWriterVisitor();
    gapicClassWithHeaderList.get(0).classDefinition().accept(visitor);
    Utils.saveCodegenToFile(this.getClass(), "ComposerPostProcOnFooBar.golden", visitor.write());
    Path goldenFilePath = Paths.get(Utils.getGoldenDir(this.getClass()), "ComposerPostProcOnFooBar.golden");
    Assert.assertCodeEquals(goldenFilePath, visitor.write());
}
Also used : Path(java.nio.file.Path) GapicClass(com.google.api.generator.gapic.model.GapicClass) JavaWriterVisitor(com.google.api.generator.engine.writer.JavaWriterVisitor) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) Test(org.junit.Test)

Example 14 with ClassDefinition

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

the class JavaWriterVisitorTest method writeClassDefinition_commentsStatementsAndMethods.

@Test
public void writeClassDefinition_commentsStatementsAndMethods() {
    LineComment lineComment = LineComment.withComment("AUTO-GENERATED DOCUMENTATION AND CLASS");
    JavaDocComment javaDocComment = JavaDocComment.builder().addComment("Class to configure an instance of {@link LibraryServiceStub}.").addParagraph("The default instance has everything set to sensible defaults:").addUnorderedList(Arrays.asList("The default service address (library-example.googleapis.com) and default port" + " (1234) are used.", "Credentials are acquired automatically through Application Default" + " Credentials.", "Retries are configured for idempotent methods but not for non-idempotent" + " methods.")).build();
    List<Reference> subGenerics = Arrays.asList(ConcreteReference.withClazz(String.class), ConcreteReference.withClazz(MethodDefinition.class));
    Reference mapEntryReference = ConcreteReference.builder().setClazz(Map.Entry.class).setGenerics(subGenerics).build();
    List<Reference> generics = Arrays.asList(ConcreteReference.withClazz(ClassDefinition.class), mapEntryReference);
    Reference mapReference = ConcreteReference.builder().setClazz(Map.class).setGenerics(generics).build();
    List<Statement> statements = Arrays.asList(ExprStatement.withExpr(VariableExpr.builder().setVariable(createVariable("x", TypeNode.withReference(ConcreteReference.withClazz(AssignmentExpr.class)))).setIsDecl(true).setScope(ScopeNode.PRIVATE).build()), ExprStatement.withExpr(VariableExpr.builder().setVariable(createVariable("y", TypeNode.withReference(mapReference))).setIsDecl(true).setScope(ScopeNode.PROTECTED).build()));
    MethodDefinition methodOne = MethodDefinition.builder().setName("open").setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.BOOLEAN).setReturnExpr(ValueExpr.builder().setValue(PrimitiveValue.builder().setType(TypeNode.BOOLEAN).setValue("true").build()).build()).build();
    MethodDefinition methodTwo = MethodDefinition.builder().setName("close").setScope(ScopeNode.PUBLIC).setReturnType(TypeNode.VOID).setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr("foobar", "false", TypeNode.BOOLEAN)))).build();
    List<MethodDefinition> methods = Arrays.asList(methodOne, methodTwo);
    ClassDefinition nestedClassDef = ClassDefinition.builder().setName("IAmANestedClass").setIsNested(true).setScope(ScopeNode.PRIVATE).setIsStatic(true).setMethods(Arrays.asList(methodOne)).build();
    ClassDefinition classDef = ClassDefinition.builder().setPackageString("com.google.example.library.v1.stub").setHeaderCommentStatements(Arrays.asList(CommentStatement.withComment(lineComment), CommentStatement.withComment(javaDocComment))).setName("LibraryServiceStub").setScope(ScopeNode.PUBLIC).setStatements(statements).setMethods(methods).setNestedClasses(Arrays.asList(nestedClassDef)).build();
    classDef.accept(writerVisitor);
    String expected = LineFormatter.lines("package com.google.example.library.v1.stub;\n", "\n", "import com.google.api.generator.engine.ast.AssignmentExpr;\n", "import com.google.api.generator.engine.ast.ClassDefinition;\n", "import com.google.api.generator.engine.ast.MethodDefinition;\n", "import java.util.Map;\n", "\n", "// AUTO-GENERATED DOCUMENTATION AND CLASS\n", "/**\n", " * Class to configure an instance of {{@literal @}link LibraryServiceStub}.\n", " *\n", " * <p>The default instance has everything set to sensible defaults:\n", " *\n", " * <ul>\n", " *   <li>The default service address (library-example.googleapis.com) and default" + " port (1234) are\n", " *       used.\n", " *   <li>Credentials are acquired automatically through Application Default" + " Credentials.\n", " *   <li>Retries are configured for idempotent methods but not for non-idempotent" + " methods.\n", " * </ul>\n", " */\n", "public class LibraryServiceStub {\n", "  private AssignmentExpr x;\n", "  protected Map<ClassDefinition, Map.Entry<String, MethodDefinition>> y;\n\n", "  public boolean open() {\n", "    return true;\n", "  }\n\n", "  public void close() {\n", "    boolean foobar = false;\n", "  }\n", "\n", "  private static class IAmANestedClass {\n\n", "    public boolean open() {\n", "      return true;\n", "    }\n", "  }\n", "}\n");
    assertEquals(expected, writerVisitor.write());
}
Also used : JavaDocComment(com.google.api.generator.engine.ast.JavaDocComment) 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) BlockStatement(com.google.api.generator.engine.ast.BlockStatement) BreakStatement(com.google.api.generator.engine.ast.BreakStatement) WhileStatement(com.google.api.generator.engine.ast.WhileStatement) ForStatement(com.google.api.generator.engine.ast.ForStatement) EmptyLineStatement(com.google.api.generator.engine.ast.EmptyLineStatement) IfStatement(com.google.api.generator.engine.ast.IfStatement) GeneralForStatement(com.google.api.generator.engine.ast.GeneralForStatement) TryCatchStatement(com.google.api.generator.engine.ast.TryCatchStatement) SynchronizedStatement(com.google.api.generator.engine.ast.SynchronizedStatement) CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ExprStatement(com.google.api.generator.engine.ast.ExprStatement) Statement(com.google.api.generator.engine.ast.Statement) LineComment(com.google.api.generator.engine.ast.LineComment) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Example 15 with ClassDefinition

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

the class JavaWriterVisitorTest method writeClassDefinition_basicWithFileHeader.

@Test
public void writeClassDefinition_basicWithFileHeader() {
    List<CommentStatement> fileHeader = Arrays.asList(CommentStatement.withComment(BlockComment.withComment("Apache License")));
    ClassDefinition classDef = ClassDefinition.builder().setFileHeader(fileHeader).setPackageString("com.google.example.library.v1.stub").setName("LibraryServiceStub").setScope(ScopeNode.PUBLIC).build();
    classDef.accept(writerVisitor);
    assertEquals(LineFormatter.lines("/*\n", " * Apache License\n", " */\n\n", "package com.google.example.library.v1.stub;\n", "\n", "public class LibraryServiceStub {}\n"), writerVisitor.write());
}
Also used : CommentStatement(com.google.api.generator.engine.ast.CommentStatement) ClassDefinition(com.google.api.generator.engine.ast.ClassDefinition) Test(org.junit.Test)

Aggregations

ClassDefinition (com.google.api.generator.engine.ast.ClassDefinition)23 TypeStore (com.google.api.generator.gapic.composer.store.TypeStore)13 GapicClass (com.google.api.generator.gapic.model.GapicClass)12 Kind (com.google.api.generator.gapic.model.GapicClass.Kind)10 ArrayList (java.util.ArrayList)9 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)8 VariableExpr (com.google.api.generator.engine.ast.VariableExpr)8 Message (com.google.api.generator.gapic.model.Message)7 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)6 TypeNode (com.google.api.generator.engine.ast.TypeNode)6 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)5 Statement (com.google.api.generator.engine.ast.Statement)5 ResourceName (com.google.api.generator.gapic.model.ResourceName)5 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 AssignmentExpr (com.google.api.generator.engine.ast.AssignmentExpr)4 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)4 Reference (com.google.api.generator.engine.ast.Reference)4 List (java.util.List)4 BetaApi (com.google.api.core.BetaApi)3