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