Search in sources :

Example 1 with JavaDocComment

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

the class JavaWriterVisitorTest method writeMethodDefinition_withCommentsAnnotationsAndThrows.

@Test
public void writeMethodDefinition_withCommentsAnnotationsAndThrows() {
    LineComment lineComment = LineComment.withComment("AUTO-GENERATED DOCUMENTATION AND METHOD");
    JavaDocComment javaDocComment = JavaDocComment.builder().addComment("This is an override method called `close()`").addParam("valOne", "string type").addParam("valTwo", "boolean type").addComment("The return value is int 3.").build();
    ValueExpr returnExpr = ValueExpr.builder().setValue(PrimitiveValue.builder().setType(TypeNode.INT).setValue("3").build()).build();
    List<VariableExpr> arguments = Arrays.asList(VariableExpr.builder().setVariable(createVariable("valOne", TypeNode.STRING)).setIsDecl(true).build(), VariableExpr.builder().setVariable(createVariable("valTwo", TypeNode.BOOLEAN)).setIsDecl(true).build());
    MethodDefinition methodDefinition = MethodDefinition.builder().setName("close").setIsOverride(true).setIsFinal(true).setIsStatic(true).setScope(ScopeNode.PROTECTED).setReturnType(TypeNode.INT).setThrowsExceptions(Arrays.asList(TypeNode.withExceptionClazz(IOException.class), TypeNode.withExceptionClazz(TimeoutException.class), TypeNode.withExceptionClazz(InterruptedException.class))).setArguments(arguments).setReturnExpr(returnExpr).setHeaderCommentStatements(Arrays.asList(CommentStatement.withComment(lineComment), CommentStatement.withComment(javaDocComment))).setAnnotations(Arrays.asList(AnnotationNode.withSuppressWarnings("all"), AnnotationNode.DEPRECATED)).setBody(Arrays.asList(createForStatement(), ExprStatement.withExpr(createAssignmentExpr("foobar", "false", TypeNode.BOOLEAN)))).build();
    methodDefinition.accept(writerVisitor);
    String expected = LineFormatter.lines("// AUTO-GENERATED DOCUMENTATION AND METHOD\n", "/**\n", "* This is an override method called `close()`\n", "* The return value is int 3.\n", "* @param valOne string type\n", "* @param valTwo boolean type\n", "*/\n", "@SuppressWarnings(\"all\")\n", "@Deprecated\n", "@Override\n", "protected static final int close(String valOne, boolean valTwo) throws" + " IOException, TimeoutException, InterruptedException {\n", "for (String str : getSomeStrings()) {\n", "boolean aBool = false;\n", "}\n", "boolean foobar = false;\n", "return 3;\n", "}\n\n");
    assertEquals(expected, writerVisitor.write());
}
Also used : ValueExpr(com.google.api.generator.engine.ast.ValueExpr) JavaDocComment(com.google.api.generator.engine.ast.JavaDocComment) MethodDefinition(com.google.api.generator.engine.ast.MethodDefinition) LineComment(com.google.api.generator.engine.ast.LineComment) VariableExpr(com.google.api.generator.engine.ast.VariableExpr) Test(org.junit.Test)

Example 2 with JavaDocComment

use of com.google.api.generator.engine.ast.JavaDocComment 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 3 with JavaDocComment

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

the class JavaWriterVisitorTest method writeJavaDocComment_specialChar.

@Test
public void writeJavaDocComment_specialChar() {
    // Only comments and sample codes in JavaDocComment need this escaper.
    // <p> <ol> <li> <ul> are hard-coded in monolith generator, which do not need escaping.
    JavaDocComment javaDocComment = JavaDocComment.builder().addComment("The API has a collection of [Shelf][google.example.library.v1.Shelf] resources").addComment("named `bookShelves/*`").addSampleCode("ApiFuture<Shelf> future =" + " libraryClient.createShelfCallable().futureCall(request);").addOrderedList(Arrays.asList("A \"flattened\" method.", "A \"request object\" method.", "A \"callable\" method.")).addComment("RPC method comment may include special characters: <>&\"`'@.").build();
    String expected = LineFormatter.lines("/**\n", "* The API has a collection of [Shelf][google.example.library.v1.Shelf] resources\n", "* named `bookShelves/&#42;`\n", "* <pre>{@code\n", "* ApiFuture<Shelf> future =" + " libraryClient.createShelfCallable().futureCall(request);\n", "* }</pre>\n", "* <ol>\n", "* <li> A \"flattened\" method.\n", "* <li> A \"request object\" method.\n", "* <li> A \"callable\" method.\n", "* </ol>\n", "* RPC method comment may include special characters: &lt;&gt;&amp;\"`'{@literal @}.\n", "*/\n");
    javaDocComment.accept(writerVisitor);
    assertEquals(expected, writerVisitor.write());
}
Also used : JavaDocComment(com.google.api.generator.engine.ast.JavaDocComment) Test(org.junit.Test)

Example 4 with JavaDocComment

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

the class JavaWriterVisitorTest method writeJavaDocCommentStatement_allComponents.

@Test
public void writeJavaDocCommentStatement_allComponents() {
    String content = "this is a test comment";
    String deprecatedText = "Use the {@link ArchivedBookName} class instead.";
    String paramName = "shelfName";
    String paramDescription = "The name of the shelf where books are published to.";
    String paragraph1 = "This class provides the ability to make remote calls to the backing service through" + " method calls that map to API methods. Sample code to get started:";
    String paragraph2 = "The surface of this class includes several types of Java methods for each of the API's" + " methods:";
    String sampleCode = createSampleCode();
    List<String> orderedlList = Arrays.asList("A flattened method.", " A request object method.", "A callable method.");
    String throwsType = "com.google.api.gax.rpc.ApiException";
    String throwsDescription = "if the remote call fails.";
    JavaDocComment javaDocComment = JavaDocComment.builder().addComment(content).addParagraph(paragraph1).addSampleCode(sampleCode).addParagraph(paragraph2).addOrderedList(orderedlList).addSampleCode(sampleCode).addParam(paramName, paramDescription).setThrows(throwsType, throwsDescription).setDeprecated(deprecatedText).build();
    CommentStatement commentStatement = CommentStatement.withComment(javaDocComment);
    String expected = LineFormatter.lines("/**\n", "* this is a test comment\n", "* <p> This class provides the ability to make remote calls to the backing service" + " through method calls that map to API methods. Sample code to get started:\n", "* <pre>{@code\n", "* try (boolean condition = false) {\n", "* int x = 3;\n", "* }\n", "* }</pre>\n", "* <p> The surface of this class includes several types of Java methods for each of" + " the API's methods:\n", "* <ol>\n", "* <li> A flattened method.\n", "* <li>  A request object method.\n", "* <li> A callable method.\n", "* </ol>\n", "* <pre>{@code\n", "* try (boolean condition = false) {\n", "* int x = 3;\n", "* }\n", "* }</pre>\n", "* @param shelfName The name of the shelf where books are published to.\n", "* @throws com.google.api.gax.rpc.ApiException if the remote call fails.\n", "* @deprecated Use the {@link ArchivedBookName} class instead.\n", "*/\n");
    commentStatement.accept(writerVisitor);
    assertEquals(expected, writerVisitor.write());
}
Also used : CommentStatement(com.google.api.generator.engine.ast.CommentStatement) JavaDocComment(com.google.api.generator.engine.ast.JavaDocComment) Test(org.junit.Test)

Aggregations

JavaDocComment (com.google.api.generator.engine.ast.JavaDocComment)4 Test (org.junit.Test)4 CommentStatement (com.google.api.generator.engine.ast.CommentStatement)2 LineComment (com.google.api.generator.engine.ast.LineComment)2 MethodDefinition (com.google.api.generator.engine.ast.MethodDefinition)2 BlockStatement (com.google.api.generator.engine.ast.BlockStatement)1 BreakStatement (com.google.api.generator.engine.ast.BreakStatement)1 ClassDefinition (com.google.api.generator.engine.ast.ClassDefinition)1 ConcreteReference (com.google.api.generator.engine.ast.ConcreteReference)1 EmptyLineStatement (com.google.api.generator.engine.ast.EmptyLineStatement)1 ExprStatement (com.google.api.generator.engine.ast.ExprStatement)1 ForStatement (com.google.api.generator.engine.ast.ForStatement)1 GeneralForStatement (com.google.api.generator.engine.ast.GeneralForStatement)1 IfStatement (com.google.api.generator.engine.ast.IfStatement)1 Reference (com.google.api.generator.engine.ast.Reference)1 Statement (com.google.api.generator.engine.ast.Statement)1 SynchronizedStatement (com.google.api.generator.engine.ast.SynchronizedStatement)1 TryCatchStatement (com.google.api.generator.engine.ast.TryCatchStatement)1 ValueExpr (com.google.api.generator.engine.ast.ValueExpr)1 VaporReference (com.google.api.generator.engine.ast.VaporReference)1