From 4d48254f324edb5599c89099c42b4e962029cccb Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Wed, 5 Aug 2020 20:14:23 -0700 Subject: [PATCH 01/23] add file header comment --- .../google/api/generator/engine/ast/ClassDefinition.java | 9 ++++++++- .../api/generator/engine/writer/JavaWriterVisitor.java | 1 + 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java b/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java index c36dd0f65e..8fc7155ba1 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java +++ b/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java @@ -23,6 +23,9 @@ @AutoValue public abstract class ClassDefinition implements AstNode { + // Required for outer classes + @Nullable + public abstract CommentStatement fileHeader(); // Required. public abstract ScopeNode scope(); // Required. @@ -78,6 +81,8 @@ public static Builder builder() { @AutoValue.Builder public abstract static class Builder { + public abstract Builder setFileHeader(CommentStatement fileHeader); + public abstract Builder setScope(ScopeNode scope); public abstract Builder setPackageString(String pkg); @@ -117,8 +122,10 @@ public ClassDefinition build() { ClassDefinition classDef = autoBuild(); - // Only nested classes can forego having a package. + // Only nested classes can forego having a package or file header. if (!classDef.isNested()) { + // Preconditions.checkNotNull(classDef.fileHeader(), "Outer classes must have a file + // header."); Preconditions.checkNotNull( classDef.packageString(), "Outer classes must have a package name defined"); Preconditions.checkState(!classDef.isStatic(), "Outer classes cannot be static"); diff --git a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java index 9f6984c1b3..43ff113d11 100644 --- a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java +++ b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java @@ -623,6 +623,7 @@ public void visit(MethodDefinition methodDefinition) { @Override public void visit(ClassDefinition classDefinition) { if (!classDefinition.isNested()) { + // classDefinition.fileHeader().accept(this); importWriterVisitor.initialize( classDefinition.packageString(), classDefinition.classIdentifier().name()); buffer.append(String.format("package %s;", classDefinition.packageString())); From 712a2b977e15df3dd8199b881fea3a2ab1f566e8 Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Thu, 6 Aug 2020 11:58:50 -0700 Subject: [PATCH 02/23] gapic unit tests pass --- .../generator/engine/ast/ClassDefinition.java | 7 ++- .../engine/writer/JavaWriterVisitor.java | 4 +- .../google/api/generator/gapic/BUILD.bazel | 1 + .../generator/gapic/composer/FileHeader.java | 46 +++++++++++++++++++ ...pcServiceCallableFactoryClassComposer.java | 1 + .../GrpcServiceStubClassComposer.java | 4 +- .../composer/MockServiceClassComposer.java | 1 + .../MockServiceImplClassComposer.java | 1 + .../composer/ServiceClientClassComposer.java | 1 + .../ServiceClientTestClassComposer.java | 1 + .../ServiceSettingsClassComposer.java | 1 + .../composer/ServiceStubClassComposer.java | 1 + .../engine/ast/ClassDefinitionTest.java | 34 +++++++++++++- .../engine/writer/JavaWriterVisitorTest.java | 17 +++++-- .../google/api/generator/gapic/BUILD.bazel | 1 + .../api/generator/gapic/composer/BUILD.bazel | 1 + ...rviceCallableFactoryClassComposerTest.java | 16 ++++++- .../GrpcServiceStubClassComposerTest.java | 16 ++++++- .../MockServiceClassComposerTest.java | 16 ++++++- .../MockServiceImplClassComposerTest.java | 16 ++++++- .../ServiceClientClassComposerTest.java | 16 ++++++- .../ServiceClientTestClassComposerTest.java | 16 ++++++- .../ServiceSettingsClassComposerTest.java | 16 ++++++- .../ServiceStubClassComposerTest.java | 16 ++++++- 24 files changed, 232 insertions(+), 18 deletions(-) create mode 100644 src/main/java/com/google/api/generator/gapic/composer/FileHeader.java diff --git a/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java b/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java index 8fc7155ba1..61a6e99138 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java +++ b/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java @@ -25,7 +25,7 @@ public abstract class ClassDefinition implements AstNode { // Required for outer classes @Nullable - public abstract CommentStatement fileHeader(); + public abstract ImmutableList fileHeader(); // Required. public abstract ScopeNode scope(); // Required. @@ -81,7 +81,7 @@ public static Builder builder() { @AutoValue.Builder public abstract static class Builder { - public abstract Builder setFileHeader(CommentStatement fileHeader); + public abstract Builder setFileHeader(List fileHeader); public abstract Builder setScope(ScopeNode scope); @@ -124,8 +124,7 @@ public ClassDefinition build() { // Only nested classes can forego having a package or file header. if (!classDef.isNested()) { - // Preconditions.checkNotNull(classDef.fileHeader(), "Outer classes must have a file - // header."); + Preconditions.checkNotNull(classDef.fileHeader(), "Outer classes must have a file header."); Preconditions.checkNotNull( classDef.packageString(), "Outer classes must have a package name defined"); Preconditions.checkState(!classDef.isStatic(), "Outer classes cannot be static"); diff --git a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java index 43ff113d11..c5eefdaa0f 100644 --- a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java +++ b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java @@ -51,6 +51,7 @@ import java.util.Iterator; import java.util.List; import java.util.Map; +import java.util.stream.Collectors; import java.util.stream.IntStream; public class JavaWriterVisitor implements AstNodeVisitor { @@ -623,7 +624,8 @@ public void visit(MethodDefinition methodDefinition) { @Override public void visit(ClassDefinition classDefinition) { if (!classDefinition.isNested()) { - // classDefinition.fileHeader().accept(this); + statements(classDefinition.fileHeader().stream().collect(Collectors.toList())); + newline(); importWriterVisitor.initialize( classDefinition.packageString(), classDefinition.classIdentifier().name()); buffer.append(String.format("package %s;", classDefinition.packageString())); diff --git a/src/main/java/com/google/api/generator/gapic/BUILD.bazel b/src/main/java/com/google/api/generator/gapic/BUILD.bazel index 1af953bac7..4021c8018a 100644 --- a/src/main/java/com/google/api/generator/gapic/BUILD.bazel +++ b/src/main/java/com/google/api/generator/gapic/BUILD.bazel @@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"]) filegroup( name = "gapic_files", srcs = glob(["*.java"]) + [ + "//src/main/java/com/google/api/generator/gapic/composer:composer_files", "//src/main/java/com/google/api/generator/gapic/model:model_files", "//src/main/java/com/google/api/generator/gapic/protoparser:protoparser_files", ], diff --git a/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java b/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java new file mode 100644 index 0000000000..a4366f9457 --- /dev/null +++ b/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java @@ -0,0 +1,46 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.api.generator.gapic.composer; + +import com.google.api.generator.engine.ast.CommentStatement; +import com.google.api.generator.engine.ast.LineComment; +import java.util.ArrayList; +import java.util.List; + +public class FileHeader { + public static List create() { + String[] fileHeadeStrings = { + "Copyright 2020 Google LLC", + "", + "Licensed under the Apache License, Version 2.0 (the \"License\");", + "you may not use this file except in compliance with the License.", + "You may obtain a copy of the License at", + "", + " http://www.apache.org/licenses/LICENSE-2.0", + "", + "Unless required by applicable law or agreed to in writing, software", + "distributed under the License is distributed on an \"AS IS\" BASIS,", + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.", + "See the License for the specific language governing permissions and", + "limitations under the License." + }; + + List fileHeader = new ArrayList<>(); + for (String s : fileHeadeStrings) { + fileHeader.add(CommentStatement.withComment(LineComment.withComment(s))); + } + return fileHeader; + } +} diff --git a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java index 8263b17312..cb81695a9b 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java @@ -72,6 +72,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() + .setFileHeader(FileHeader.create()) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setImplementsTypes(createClassImplements(types)) diff --git a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java index 1cd16fa2b5..3f7faaba2d 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java @@ -135,6 +135,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() + .setFileHeader(FileHeader.create()) .setPackageString(pakkage) .setAnnotations(createClassAnnotations()) .setScope(ScopeNode.PUBLIC) @@ -232,7 +233,8 @@ private static Statement createMethodDescriptorVariableDecl( return ExprStatement.withExpr( AssignmentExpr.builder() .setVariableExpr( - methodDescriptorVarExpr.toBuilder() + methodDescriptorVarExpr + .toBuilder() .setIsDecl(true) .setScope(ScopeNode.PRIVATE) .setIsStatic(true) diff --git a/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java index 089f995562..4ac95880ec 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java @@ -61,6 +61,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() + .setFileHeader(FileHeader.create()) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java index e1de1f04aa..07354d7db7 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java @@ -98,6 +98,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() + .setFileHeader(FileHeader.create()) .setPackageString(pakkage) .setAnnotations(createClassAnnotations()) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java index 901d6f0207..7ad31de7a1 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java @@ -85,6 +85,7 @@ public GapicClass generate(Service service, Map messageTypes) { ClassDefinition classDef = ClassDefinition.builder() + .setFileHeader(FileHeader.create()) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setImplementsTypes(createClassImplements(types)) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java index 16531e389b..cc5e62a64b 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java @@ -112,6 +112,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() + .setFileHeader(FileHeader.create()) .setPackageString(pakkage) .setAnnotations(createClassAnnotations()) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java index 838e468107..5ca832fa4a 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java @@ -91,6 +91,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() + .setFileHeader(FileHeader.create()) .setPackageString(pakkage) .setAnnotations(createClassAnnotations()) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java index a8d76e5750..35d42508cb 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java @@ -64,6 +64,7 @@ public GapicClass generate(Service service, Map messageTypes) { ClassDefinition classDef = ClassDefinition.builder() + .setFileHeader(FileHeader.create()) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setIsAbstract(true) diff --git a/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java b/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java index 155618708e..407a39aeb6 100644 --- a/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java @@ -21,10 +21,10 @@ import org.junit.Test; public class ClassDefinitionTest { - @Test public void validClassDefinition_basic() { ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -53,6 +53,7 @@ public void validClassDefinition_nestedBasic() { @Test public void validClassDefinition_withAnnotationsExtendsAndImplements() { ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -97,6 +98,7 @@ public void validClassDefinition_statementsAndMethods() { List methods = Arrays.asList(method, method); ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setIsFinal(true) @@ -113,6 +115,7 @@ public void invalidClassDefinition_implementsNullType() { IllegalStateException.class, () -> { ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -121,12 +124,26 @@ public void invalidClassDefinition_implementsNullType() { }); } + @Test + public void invalidClassDefinition_outerClassMissingFileHeader() { + assertThrows( + NullPointerException.class, + () -> { + ClassDefinition.builder() + .setPackageString("com.google.example.library.v1.stub") + .setName("LibraryServiceStub") + .setScope(ScopeNode.PUBLIC) + .build(); + }); + } + @Test public void invalidClassDefinition_outerClassMissingPackage() { assertThrows( NullPointerException.class, () -> { ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) .build(); @@ -139,6 +156,7 @@ public void invalidClassDefinition_outerClassStatic() { IllegalStateException.class, () -> { ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setIsStatic(true) @@ -153,6 +171,7 @@ public void invalidClassDefinition_outerClassPrivate() { IllegalStateException.class, () -> { ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PRIVATE) @@ -166,6 +185,7 @@ public void invalidClassDefinition_extendsPrimitiveType() { IllegalStateException.class, () -> { ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -180,6 +200,7 @@ public void invalidClassDefinition_extendsNullType() { IllegalStateException.class, () -> { ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -194,6 +215,7 @@ public void invalidClassDefinition_abstractFinal() { IllegalStateException.class, () -> { ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setIsAbstract(true) @@ -210,6 +232,7 @@ public void invalidClassDefinition_implementsPrimtiveType() { IllegalStateException.class, () -> { ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -225,6 +248,7 @@ public void invalidClassDefinition_extendsImplementsSameType() { IllegalStateException.class, () -> { ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -254,6 +278,7 @@ public void invalidClassDefinition_assignmentWithUnscopedVariableExprStatement() IllegalStateException.class, () -> { ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -276,6 +301,7 @@ public void invalidClassDefinition_unscopedVariableExprStatement() { IllegalStateException.class, () -> { ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -291,6 +317,7 @@ public void invalidClassDefinition_badStatementType() { IllegalStateException.class, () -> { ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -336,4 +363,9 @@ private static ForStatement createForStatement() { .setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr()))) .build(); } + + private static List createFileHeader() { + LineComment lineComment = LineComment.withComment("Apache License"); + return Arrays.asList(CommentStatement.withComment(lineComment)); + } } diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index 3d379a8a80..9dd14ca974 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -1502,6 +1502,7 @@ public void writeMethodDefinition_templatedReturnTypeAndArguments() { public void writeClassDefinition_basic() { ClassDefinition classDef = ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -1511,7 +1512,8 @@ public void writeClassDefinition_basic() { assertEquals( writerVisitor.write(), String.format( - createLines(3), + createLines(4), + "// Apache License\n\n", "package com.google.example.library.v1.stub;\n", "\n", "public class LibraryServiceStub {}\n")); @@ -1521,6 +1523,7 @@ public void writeClassDefinition_basic() { public void writeClassDefinition_withAnnotationsExtendsAndImplements() { ClassDefinition classDef = ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -1540,7 +1543,8 @@ public void writeClassDefinition_withAnnotationsExtendsAndImplements() { assertEquals( writerVisitor.write(), String.format( - createLines(5), + createLines(6), + "// Apache License\n\n", "package com.google.example.library.v1.stub;\n", "\n", "@Deprecated\n", @@ -1617,6 +1621,7 @@ public void writeClassDefinition_statementsAndMethods() { ClassDefinition classDef = ClassDefinition.builder() + .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -1629,7 +1634,8 @@ public void writeClassDefinition_statementsAndMethods() { assertEquals( writerVisitor.write(), String.format( - createLines(23), + createLines(24), + "// Apache License\n\n", "package com.google.example.library.v1.stub;\n", "\n", "import com.google.api.generator.engine.ast.AssignmentExpr;\n", @@ -1760,4 +1766,9 @@ private static String createSampleCode() { tryCatch.accept(writerVisitor); return writerVisitor.write(); } + + private static List createFileHeader() { + LineComment apacheLicense = LineComment.withComment("Apache License"); + return Arrays.asList(CommentStatement.withComment(apacheLicense)); + } } diff --git a/src/test/java/com/google/api/generator/gapic/BUILD.bazel b/src/test/java/com/google/api/generator/gapic/BUILD.bazel index 82549247f9..ef1035d943 100644 --- a/src/test/java/com/google/api/generator/gapic/BUILD.bazel +++ b/src/test/java/com/google/api/generator/gapic/BUILD.bazel @@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"]) filegroup( name = "gapic_files", srcs = [ + "//src/test/java/com/google/api/generator/gapic/composer:composer_files", "//src/test/java/com/google/api/generator/gapic/model:model_files", "//src/test/java/com/google/api/generator/gapic/protoparser:protoparser_files", ], diff --git a/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel b/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel index 1e83bcc3b6..4cb438e4ba 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel +++ b/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel @@ -7,6 +7,7 @@ TESTS = [ "MockServiceImplClassComposerTest", "ServiceClientClassComposerTest", "ServiceClientTestClassComposerTest", + "ServiceSettingsClassComposerTest", "ServiceStubClassComposerTest", ] diff --git a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java index e75c4bb8c5..63e5443600 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java @@ -50,12 +50,26 @@ public void generateServiceClasses() { JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); + System.out.println(visitor.write()); assertEquals(EXPECTED_CLASS_STRING, visitor.write()); } // TODO(miraleung): Update this when a file-diffing test mechanism is in place. private static final String EXPECTED_CLASS_STRING = - "package com.google.showcase.v1beta1.stub;\n" + "// Copyright 2020 Google LLC\n" + + "//\n" + + "// Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + "// you may not use this file except in compliance with the License.\n" + + "// You may obtain a copy of the License at\n" + + "//\n" + + "// http://www.apache.org/licenses/LICENSE-2.0\n" + + "//\n" + + "// Unless required by applicable law or agreed to in writing, software\n" + + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + "// See the License for the specific language governing permissions and\n" + + "// limitations under the License.\n\n" + + "package com.google.showcase.v1beta1.stub;\n" + "\n" + "import com.google.api.gax.grpc.GrpcCallSettings;\n" + "import com.google.api.gax.grpc.GrpcCallableFactory;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java index 13f520abf6..05e5851fe9 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java @@ -50,12 +50,26 @@ public void generateServiceClasses() { JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); + System.out.println(visitor.write()); assertEquals(EXPECTED_CLASS_STRING, visitor.write()); } // TODO(miraleung): Update this when a file-diffing test mechanism is in place. private static final String EXPECTED_CLASS_STRING = - "package com.google.showcase.v1beta1.stub;\n" + "// Copyright 2020 Google LLC\n" + + "//\n" + + "// Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + "// you may not use this file except in compliance with the License.\n" + + "// You may obtain a copy of the License at\n" + + "//\n" + + "// http://www.apache.org/licenses/LICENSE-2.0\n" + + "//\n" + + "// Unless required by applicable law or agreed to in writing, software\n" + + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + "// See the License for the specific language governing permissions and\n" + + "// limitations under the License.\n\n" + + "package com.google.showcase.v1beta1.stub;\n" + "\n" + "import static com.google.showcase.v1beta1.EchoClient.PagedExpandPagedResponse;\n" + "\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java index 1248dae21b..a472209dc7 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java @@ -49,12 +49,26 @@ public void generateServiceClasses() { JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); + System.out.println(visitor.write()); assertEquals(EXPECTED_CLASS_STRING, visitor.write()); } // TODO(miraleung): Update this when a file-diffing test mechanism is in place. private static final String EXPECTED_CLASS_STRING = - "package com.google.showcase.v1beta1;\n" + "// Copyright 2020 Google LLC\n" + + "//\n" + + "// Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + "// you may not use this file except in compliance with the License.\n" + + "// You may obtain a copy of the License at\n" + + "//\n" + + "// http://www.apache.org/licenses/LICENSE-2.0\n" + + "//\n" + + "// Unless required by applicable law or agreed to in writing, software\n" + + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + "// See the License for the specific language governing permissions and\n" + + "// limitations under the License.\n\n" + + "package com.google.showcase.v1beta1;\n" + "\n" + "import com.google.api.core.BetaApi;\n" + "import com.google.api.gax.grpc.testing.MockGrpcService;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java index 25b6a01833..ca4e423244 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java @@ -50,12 +50,26 @@ public void generateServiceClasses() { JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); + System.out.println(visitor.write()); assertEquals(EXPECTED_CLASS_STRING, visitor.write()); } // TODO(miraleung): Update this when a file-diffing test mechanism is in place. private static final String EXPECTED_CLASS_STRING = - "package com.google.showcase.v1beta1;\n" + "// Copyright 2020 Google LLC\n" + + "//\n" + + "// Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + "// you may not use this file except in compliance with the License.\n" + + "// You may obtain a copy of the License at\n" + + "//\n" + + "// http://www.apache.org/licenses/LICENSE-2.0\n" + + "//\n" + + "// Unless required by applicable law or agreed to in writing, software\n" + + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + "// See the License for the specific language governing permissions and\n" + + "// limitations under the License.\n\n" + + "package com.google.showcase.v1beta1;\n" + "\n" + "import com.google.api.core.BetaApi;\n" + "import com.google.longrunning.Operation;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java index ce766355b9..45dc800006 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java @@ -50,12 +50,26 @@ public void generateServiceClasses() { JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); + System.out.println(visitor.write()); assertEquals(EXPECTED_CLASS_STRING, visitor.write()); } // TODO(miraleung): Update this when a file-diffing test mechanism is in place. private static final String EXPECTED_CLASS_STRING = - "package com.google.showcase.v1beta1;\n" + "// Copyright 2020 Google LLC\n" + + "//\n" + + "// Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + "// you may not use this file except in compliance with the License.\n" + + "// You may obtain a copy of the License at\n" + + "//\n" + + "// http://www.apache.org/licenses/LICENSE-2.0\n" + + "//\n" + + "// Unless required by applicable law or agreed to in writing, software\n" + + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + "// See the License for the specific language governing permissions and\n" + + "// limitations under the License.\n\n" + + "package com.google.showcase.v1beta1;\n" + "\n" + "import com.google.api.gax.core.BackgroundResource;\n" + "import com.google.api.gax.longrunning.OperationFuture;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java index ab1925fa41..d9250021fd 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java @@ -50,12 +50,26 @@ public void generateServiceClasses() { JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); + System.out.println(visitor.write()); assertEquals(EXPECTED_CLASS_STRING, visitor.write()); } // TODO(miraleung): Update this when a file-diffing test mechanism is in place. private static final String EXPECTED_CLASS_STRING = - "package com.google.showcase.v1beta1;\n" + "// Copyright 2020 Google LLC\n" + + "//\n" + + "// Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + "// you may not use this file except in compliance with the License.\n" + + "// You may obtain a copy of the License at\n" + + "//\n" + + "// http://www.apache.org/licenses/LICENSE-2.0\n" + + "//\n" + + "// Unless required by applicable law or agreed to in writing, software\n" + + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + "// See the License for the specific language governing permissions and\n" + + "// limitations under the License.\n\n" + + "package com.google.showcase.v1beta1;\n" + "\n" + "import com.google.api.gax.core.NoCredentialsProvider;\n" + "import com.google.api.gax.grpc.testing.LocalChannelProvider;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java index 268d0805b6..37652060bd 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java @@ -50,12 +50,26 @@ public void generateServiceClasses() { JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); + System.out.println(visitor.write()); assertEquals(EXPECTED_CLASS_STRING, visitor.write()); } // TODO(miraleung): Update this when a file-diffing test mechanism is in place. private static final String EXPECTED_CLASS_STRING = - "package com.google.showcase.v1beta1;\n" + "// Copyright 2020 Google LLC\n" + + "//\n" + + "// Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + "// you may not use this file except in compliance with the License.\n" + + "// You may obtain a copy of the License at\n" + + "//\n" + + "// http://www.apache.org/licenses/LICENSE-2.0\n" + + "//\n" + + "// Unless required by applicable law or agreed to in writing, software\n" + + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + "// See the License for the specific language governing permissions and\n" + + "// limitations under the License.\n\n" + + "package com.google.showcase.v1beta1;\n" + "\n" + "import static com.google.showcase.v1beta1.EchoClient.PagedExpandPagedResponse;\n" + "\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java index 0422eda88a..9dbde4bad1 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java @@ -49,12 +49,26 @@ public void generateServiceClasses() { JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); + System.out.println(visitor.write()); assertEquals(EXPECTED_CLASS_STRING, visitor.write()); } // TODO(miraleung): Update this when a file-diffing test mechanism is in place. private static final String EXPECTED_CLASS_STRING = - "package com.google.showcase.v1beta1.stub;\n" + "// Copyright 2020 Google LLC\n" + + "//\n" + + "// Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + "// you may not use this file except in compliance with the License.\n" + + "// You may obtain a copy of the License at\n" + + "//\n" + + "// http://www.apache.org/licenses/LICENSE-2.0\n" + + "//\n" + + "// Unless required by applicable law or agreed to in writing, software\n" + + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + "// See the License for the specific language governing permissions and\n" + + "// limitations under the License.\n\n" + + "package com.google.showcase.v1beta1.stub;\n" + "\n" + "import com.google.api.gax.core.BackgroundResource;\n" + "import com.google.api.gax.rpc.BidiStreamingCallable;\n" From aecb2418f2da959970b24d8e0181eb921cbb4764 Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Thu, 6 Aug 2020 12:05:40 -0700 Subject: [PATCH 03/23] fix bazel rules --- src/main/java/com/google/api/generator/gapic/BUILD.bazel | 1 + src/test/java/com/google/api/generator/gapic/BUILD.bazel | 1 + .../java/com/google/api/generator/gapic/composer/BUILD.bazel | 1 + 3 files changed, 3 insertions(+) diff --git a/src/main/java/com/google/api/generator/gapic/BUILD.bazel b/src/main/java/com/google/api/generator/gapic/BUILD.bazel index 1af953bac7..c1978825f4 100644 --- a/src/main/java/com/google/api/generator/gapic/BUILD.bazel +++ b/src/main/java/com/google/api/generator/gapic/BUILD.bazel @@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"]) filegroup( name = "gapic_files", srcs = glob(["*.java"]) + [ + "//src/test/java/com/google/api/generator/gapic/composer:composer_files", "//src/main/java/com/google/api/generator/gapic/model:model_files", "//src/main/java/com/google/api/generator/gapic/protoparser:protoparser_files", ], diff --git a/src/test/java/com/google/api/generator/gapic/BUILD.bazel b/src/test/java/com/google/api/generator/gapic/BUILD.bazel index 82549247f9..ef1035d943 100644 --- a/src/test/java/com/google/api/generator/gapic/BUILD.bazel +++ b/src/test/java/com/google/api/generator/gapic/BUILD.bazel @@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"]) filegroup( name = "gapic_files", srcs = [ + "//src/test/java/com/google/api/generator/gapic/composer:composer_files", "//src/test/java/com/google/api/generator/gapic/model:model_files", "//src/test/java/com/google/api/generator/gapic/protoparser:protoparser_files", ], diff --git a/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel b/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel index 1e83bcc3b6..4cb438e4ba 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel +++ b/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel @@ -7,6 +7,7 @@ TESTS = [ "MockServiceImplClassComposerTest", "ServiceClientClassComposerTest", "ServiceClientTestClassComposerTest", + "ServiceSettingsClassComposerTest", "ServiceStubClassComposerTest", ] From d8d9cb179bbc506451c0e9345c48b13141a42854 Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Thu, 6 Aug 2020 12:49:00 -0700 Subject: [PATCH 04/23] clean up --- .../google/api/generator/gapic/composer/FileHeader.java | 8 +++++++- .../composer/GrpcServiceCallableFactoryClassComposer.java | 2 +- .../gapic/composer/GrpcServiceStubClassComposer.java | 2 +- .../gapic/composer/MockServiceClassComposer.java | 2 +- .../gapic/composer/MockServiceImplClassComposer.java | 2 +- .../gapic/composer/ServiceClientClassComposer.java | 2 +- .../gapic/composer/ServiceClientTestClassComposer.java | 2 +- .../gapic/composer/ServiceSettingsClassComposer.java | 2 +- .../gapic/composer/ServiceStubClassComposer.java | 2 +- .../api/generator/engine/ast/ClassDefinitionTest.java | 1 + .../generator/engine/writer/ImportWriterVisitorTest.java | 1 - .../GrpcServiceCallableFactoryClassComposerTest.java | 1 - .../gapic/composer/GrpcServiceStubClassComposerTest.java | 1 - .../gapic/composer/MockServiceClassComposerTest.java | 1 - .../gapic/composer/MockServiceImplClassComposerTest.java | 1 - .../gapic/composer/ServiceClientClassComposerTest.java | 1 - .../composer/ServiceClientTestClassComposerTest.java | 1 - .../gapic/composer/ServiceSettingsClassComposerTest.java | 1 - .../gapic/composer/ServiceStubClassComposerTest.java | 1 - 19 files changed, 16 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java b/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java index a4366f9457..f406f293fb 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java +++ b/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java @@ -20,7 +20,13 @@ import java.util.List; public class FileHeader { - public static List create() { + private static final List APACHE_LICENSE = create(); + + public static List createApacheLicense() { + return APACHE_LICENSE; + } + + private static List create() { String[] fileHeadeStrings = { "Copyright 2020 Google LLC", "", diff --git a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java index cb81695a9b..17a48566f1 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java @@ -72,7 +72,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.create()) + .setFileHeader(FileHeader.createApacheLicense()) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setImplementsTypes(createClassImplements(types)) diff --git a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java index 3f7faaba2d..3fa9f14935 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java @@ -135,7 +135,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.create()) + .setFileHeader(FileHeader.createApacheLicense()) .setPackageString(pakkage) .setAnnotations(createClassAnnotations()) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java index 4ac95880ec..5bf1566e5d 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java @@ -61,7 +61,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.create()) + .setFileHeader(FileHeader.createApacheLicense()) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java index 07354d7db7..4204539b91 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java @@ -98,7 +98,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.create()) + .setFileHeader(FileHeader.createApacheLicense()) .setPackageString(pakkage) .setAnnotations(createClassAnnotations()) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java index 7ad31de7a1..e3caac0800 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java @@ -85,7 +85,7 @@ public GapicClass generate(Service service, Map messageTypes) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.create()) + .setFileHeader(FileHeader.createApacheLicense()) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setImplementsTypes(createClassImplements(types)) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java index cc5e62a64b..69c55f5e5c 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java @@ -112,7 +112,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.create()) + .setFileHeader(FileHeader.createApacheLicense()) .setPackageString(pakkage) .setAnnotations(createClassAnnotations()) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java index 5ca832fa4a..773a8fc2ed 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java @@ -91,7 +91,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.create()) + .setFileHeader(FileHeader.createApacheLicense()) .setPackageString(pakkage) .setAnnotations(createClassAnnotations()) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java index 35d42508cb..d2472dfa6e 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java @@ -64,7 +64,7 @@ public GapicClass generate(Service service, Map messageTypes) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.create()) + .setFileHeader(FileHeader.createApacheLicense()) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setIsAbstract(true) diff --git a/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java b/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java index 914cf5d50f..c91cd7ad8c 100644 --- a/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java @@ -21,6 +21,7 @@ import org.junit.Test; public class ClassDefinitionTest { + @Test public void validClassDefinition_basicWithComments() { LineComment lineComment = LineComment.withComment("AUTO-GENERATED DOCUMENTATION AND CLASS"); diff --git a/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java index 52bfefb6d1..43b69100dd 100644 --- a/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java @@ -87,7 +87,6 @@ public void writeNewObjectExprImports_withArgs() { .setArguments(Arrays.asList(fileExpr)) .build(); newObjectExpr.accept(writerVisitor); - System.out.println(writerVisitor.write()); assertEquals( writerVisitor.write(), String.format( diff --git a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java index 63e5443600..04b34d9d15 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java @@ -50,7 +50,6 @@ public void generateServiceClasses() { JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); - System.out.println(visitor.write()); assertEquals(EXPECTED_CLASS_STRING, visitor.write()); } diff --git a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java index 05e5851fe9..672b56c0c5 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java @@ -50,7 +50,6 @@ public void generateServiceClasses() { JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); - System.out.println(visitor.write()); assertEquals(EXPECTED_CLASS_STRING, visitor.write()); } diff --git a/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java index a472209dc7..14731e3da9 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java @@ -49,7 +49,6 @@ public void generateServiceClasses() { JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); - System.out.println(visitor.write()); assertEquals(EXPECTED_CLASS_STRING, visitor.write()); } diff --git a/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java index ca4e423244..4dd5f0c113 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java @@ -50,7 +50,6 @@ public void generateServiceClasses() { JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); - System.out.println(visitor.write()); assertEquals(EXPECTED_CLASS_STRING, visitor.write()); } diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java index 45dc800006..1b043140b0 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java @@ -50,7 +50,6 @@ public void generateServiceClasses() { JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); - System.out.println(visitor.write()); assertEquals(EXPECTED_CLASS_STRING, visitor.write()); } diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java index d9250021fd..c567484b43 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java @@ -50,7 +50,6 @@ public void generateServiceClasses() { JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); - System.out.println(visitor.write()); assertEquals(EXPECTED_CLASS_STRING, visitor.write()); } diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java index 37652060bd..f647a73b57 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java @@ -50,7 +50,6 @@ public void generateServiceClasses() { JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); - System.out.println(visitor.write()); assertEquals(EXPECTED_CLASS_STRING, visitor.write()); } diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java index 9dbde4bad1..dcca008058 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java @@ -49,7 +49,6 @@ public void generateServiceClasses() { JavaWriterVisitor visitor = new JavaWriterVisitor(); clazz.classDefinition().accept(visitor); - System.out.println(visitor.write()); assertEquals(EXPECTED_CLASS_STRING, visitor.write()); } From 4044ee4a868f4827f330b4031b2fec576cc44f69 Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Thu, 6 Aug 2020 12:49:12 -0700 Subject: [PATCH 05/23] format code --- .../com/google/api/generator/gapic/composer/FileHeader.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java b/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java index f406f293fb..e11ee95f0c 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java +++ b/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java @@ -23,7 +23,7 @@ public class FileHeader { private static final List APACHE_LICENSE = create(); public static List createApacheLicense() { - return APACHE_LICENSE; + return APACHE_LICENSE; } private static List create() { From 85e87a4939117db91bb7263112d24d02ff97fb0f Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Thu, 6 Aug 2020 13:52:52 -0700 Subject: [PATCH 06/23] file header class --- .../generator/engine/ast/ClassDefinition.java | 4 ++-- .../generator/gapic/composer/FileHeader.java | 21 +++++++++---------- ...pcServiceCallableFactoryClassComposer.java | 2 +- .../GrpcServiceStubClassComposer.java | 2 +- .../composer/MockServiceClassComposer.java | 2 +- .../MockServiceImplClassComposer.java | 2 +- .../composer/ServiceClientClassComposer.java | 2 +- .../ServiceClientTestClassComposer.java | 2 +- .../ServiceSettingsClassComposer.java | 2 +- .../composer/ServiceStubClassComposer.java | 2 +- 10 files changed, 20 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java b/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java index de38e1c46b..46e01449b0 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java +++ b/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java @@ -23,7 +23,7 @@ @AutoValue public abstract class ClassDefinition implements AstNode { - // Required for outer classes + // Required for outer classes. @Nullable public abstract ImmutableList fileHeader(); // Required. @@ -127,7 +127,7 @@ public ClassDefinition build() { ClassDefinition classDef = autoBuild(); - // Only nested classes can forego having a package or file header. + // Only nested classes can forego having a package and file header. if (!classDef.isNested()) { Preconditions.checkNotNull(classDef.fileHeader(), "Outer classes must have a file header."); Preconditions.checkNotNull( diff --git a/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java b/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java index e11ee95f0c..b178377356 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java +++ b/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java @@ -17,16 +17,13 @@ import com.google.api.generator.engine.ast.CommentStatement; import com.google.api.generator.engine.ast.LineComment; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; public class FileHeader { - private static final List APACHE_LICENSE = create(); + public static final List APACHE_LICENSE = createApacheLicense(); - public static List createApacheLicense() { - return APACHE_LICENSE; - } - - private static List create() { + private static List createApacheLicense() { String[] fileHeadeStrings = { "Copyright 2020 Google LLC", "", @@ -43,10 +40,12 @@ private static List create() { "limitations under the License." }; - List fileHeader = new ArrayList<>(); - for (String s : fileHeadeStrings) { - fileHeader.add(CommentStatement.withComment(LineComment.withComment(s))); - } - return fileHeader; + List apacheFileHeader = new ArrayList<>(); + Arrays.stream(fileHeadeStrings) + .forEach( + s -> { + apacheFileHeader.add(CommentStatement.withComment(LineComment.withComment(s))); + }); + return apacheFileHeader; } } diff --git a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java index 17a48566f1..daf85512e9 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java @@ -72,7 +72,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.createApacheLicense()) + .setFileHeader(FileHeader.APACHE_LICENSE) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setImplementsTypes(createClassImplements(types)) diff --git a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java index 3fa9f14935..9c629a10a7 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java @@ -135,7 +135,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.createApacheLicense()) + .setFileHeader(FileHeader.APACHE_LICENSE) .setPackageString(pakkage) .setAnnotations(createClassAnnotations()) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java index 5bf1566e5d..e7865bc878 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java @@ -61,7 +61,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.createApacheLicense()) + .setFileHeader(FileHeader.APACHE_LICENSE) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java index 4204539b91..edca09a8a3 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java @@ -98,7 +98,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.createApacheLicense()) + .setFileHeader(FileHeader.APACHE_LICENSE) .setPackageString(pakkage) .setAnnotations(createClassAnnotations()) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java index e3caac0800..8e79f54460 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java @@ -85,7 +85,7 @@ public GapicClass generate(Service service, Map messageTypes) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.createApacheLicense()) + .setFileHeader(FileHeader.APACHE_LICENSE) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setImplementsTypes(createClassImplements(types)) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java index 69c55f5e5c..dfae4aae80 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java @@ -112,7 +112,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.createApacheLicense()) + .setFileHeader(FileHeader.APACHE_LICENSE) .setPackageString(pakkage) .setAnnotations(createClassAnnotations()) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java index 773a8fc2ed..e290be11b7 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java @@ -91,7 +91,7 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.createApacheLicense()) + .setFileHeader(FileHeader.APACHE_LICENSE) .setPackageString(pakkage) .setAnnotations(createClassAnnotations()) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java index d2472dfa6e..7a8ea6ad5a 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java @@ -64,7 +64,7 @@ public GapicClass generate(Service service, Map messageTypes) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.createApacheLicense()) + .setFileHeader(FileHeader.APACHE_LICENSE) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setIsAbstract(true) From 44c0cf5223440a40c704e31434e1eafb01a5bd32 Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Thu, 6 Aug 2020 14:09:24 -0700 Subject: [PATCH 07/23] simplify create header --- .../api/generator/engine/ast/ClassDefinitionTest.java | 5 ++--- .../api/generator/engine/writer/JavaWriterVisitorTest.java | 6 +++--- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java b/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java index c91cd7ad8c..917c165716 100644 --- a/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java @@ -375,9 +375,8 @@ private static ForStatement createForStatement() { .setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr()))) .build(); } - + // Create a simple line comment to stand for the Apache License header. private static List createFileHeader() { - LineComment lineComment = LineComment.withComment("Apache License"); - return Arrays.asList(CommentStatement.withComment(lineComment)); + return Arrays.asList(CommentStatement.withComment(LineComment.withComment("Apache License"))); } } diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index 72f78ccb60..b9394166fb 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -1794,9 +1794,9 @@ private static String createSampleCode() { tryCatch.accept(writerVisitor); return writerVisitor.write(); } - + + // Create a simple line comment to stand for the Apache License header. private static List createFileHeader() { - LineComment apacheLicense = LineComment.withComment("Apache License"); - return Arrays.asList(CommentStatement.withComment(apacheLicense)); + return Arrays.asList(CommentStatement.withComment(LineComment.withComment("Apache License"))); } } From 23813f80f43ac2abe0f8a8564699b905b6d92f4a Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Thu, 6 Aug 2020 14:09:38 -0700 Subject: [PATCH 08/23] format --- .../api/generator/engine/writer/JavaWriterVisitorTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index b9394166fb..1dd045a209 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -1794,7 +1794,7 @@ private static String createSampleCode() { tryCatch.accept(writerVisitor); return writerVisitor.write(); } - + // Create a simple line comment to stand for the Apache License header. private static List createFileHeader() { return Arrays.asList(CommentStatement.withComment(LineComment.withComment("Apache License"))); From 18f62679dbc3749b1be280f6a4c8a6f9c63bfc9d Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Thu, 6 Aug 2020 14:13:48 -0700 Subject: [PATCH 09/23] new line after file header --- .../composer/GrpcServiceCallableFactoryClassComposerTest.java | 3 ++- .../gapic/composer/GrpcServiceStubClassComposerTest.java | 3 ++- .../generator/gapic/composer/MockServiceClassComposerTest.java | 3 ++- .../gapic/composer/MockServiceImplClassComposerTest.java | 3 ++- .../gapic/composer/ServiceClientClassComposerTest.java | 3 ++- .../gapic/composer/ServiceClientTestClassComposerTest.java | 3 ++- .../gapic/composer/ServiceSettingsClassComposerTest.java | 3 ++- .../generator/gapic/composer/ServiceStubClassComposerTest.java | 3 ++- 8 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java index 04b34d9d15..56b911bb32 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java @@ -67,7 +67,8 @@ public void generateServiceClasses() { + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + "// See the License for the specific language governing permissions and\n" - + "// limitations under the License.\n\n" + + "// limitations under the License.\n" + + "\n" + "package com.google.showcase.v1beta1.stub;\n" + "\n" + "import com.google.api.gax.grpc.GrpcCallSettings;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java index 672b56c0c5..7cb295e4ad 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java @@ -67,7 +67,8 @@ public void generateServiceClasses() { + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + "// See the License for the specific language governing permissions and\n" - + "// limitations under the License.\n\n" + + "// limitations under the License.\n" + + "\n" + "package com.google.showcase.v1beta1.stub;\n" + "\n" + "import static com.google.showcase.v1beta1.EchoClient.PagedExpandPagedResponse;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java index 14731e3da9..d65c21a8e8 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java @@ -66,7 +66,8 @@ public void generateServiceClasses() { + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + "// See the License for the specific language governing permissions and\n" - + "// limitations under the License.\n\n" + + "// limitations under the License.\n" + + "\n" + "package com.google.showcase.v1beta1;\n" + "\n" + "import com.google.api.core.BetaApi;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java index 4dd5f0c113..e2c7530f62 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java @@ -67,7 +67,8 @@ public void generateServiceClasses() { + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + "// See the License for the specific language governing permissions and\n" - + "// limitations under the License.\n\n" + + "// limitations under the License.\n" + + "\n" + "package com.google.showcase.v1beta1;\n" + "\n" + "import com.google.api.core.BetaApi;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java index 1b043140b0..75bd81248b 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java @@ -67,7 +67,8 @@ public void generateServiceClasses() { + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + "// See the License for the specific language governing permissions and\n" - + "// limitations under the License.\n\n" + + "// limitations under the License.\n" + + "\n" + "package com.google.showcase.v1beta1;\n" + "\n" + "import com.google.api.gax.core.BackgroundResource;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java index c567484b43..4a97fdaac4 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java @@ -67,7 +67,8 @@ public void generateServiceClasses() { + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + "// See the License for the specific language governing permissions and\n" - + "// limitations under the License.\n\n" + + "// limitations under the License.\n" + + "\n" + "package com.google.showcase.v1beta1;\n" + "\n" + "import com.google.api.gax.core.NoCredentialsProvider;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java index f647a73b57..a7510f0c3a 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java @@ -67,7 +67,8 @@ public void generateServiceClasses() { + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + "// See the License for the specific language governing permissions and\n" - + "// limitations under the License.\n\n" + + "// limitations under the License.\n" + + "\n" + "package com.google.showcase.v1beta1;\n" + "\n" + "import static com.google.showcase.v1beta1.EchoClient.PagedExpandPagedResponse;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java index dcca008058..97465d15d3 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java @@ -66,7 +66,8 @@ public void generateServiceClasses() { + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + "// See the License for the specific language governing permissions and\n" - + "// limitations under the License.\n\n" + + "// limitations under the License.\n" + + "\n" + "package com.google.showcase.v1beta1.stub;\n" + "\n" + "import com.google.api.gax.core.BackgroundResource;\n" From c0d3be775a41e0ba775eb670ed19c26bb3fd8e3c Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Fri, 7 Aug 2020 15:36:24 -0700 Subject: [PATCH 10/23] file header is optional for class --- .../generator/engine/ast/ClassDefinition.java | 7 ++- .../gapic/composer/ApacheLicenseComposer.java | 37 ++++++++++++++ .../generator/gapic/composer/FileHeader.java | 51 ------------------- ...pcServiceCallableFactoryClassComposer.java | 1 - .../GrpcServiceStubClassComposer.java | 1 - .../composer/MockServiceClassComposer.java | 1 - .../MockServiceImplClassComposer.java | 1 - .../composer/ServiceClientClassComposer.java | 1 - .../ServiceClientTestClassComposer.java | 1 - .../ServiceSettingsClassComposer.java | 1 - .../composer/ServiceStubClassComposer.java | 1 - .../engine/ast/ClassDefinitionTest.java | 29 +---------- .../engine/writer/JavaWriterVisitorTest.java | 10 ++-- ...rviceCallableFactoryClassComposerTest.java | 16 +----- .../GrpcServiceStubClassComposerTest.java | 16 +----- .../MockServiceClassComposerTest.java | 16 +----- .../MockServiceImplClassComposerTest.java | 16 +----- .../ServiceClientClassComposerTest.java | 16 +----- .../ServiceClientTestClassComposerTest.java | 16 +----- .../ServiceSettingsClassComposerTest.java | 16 +----- .../ServiceStubClassComposerTest.java | 16 +----- 21 files changed, 52 insertions(+), 218 deletions(-) create mode 100644 src/main/java/com/google/api/generator/gapic/composer/ApacheLicenseComposer.java delete mode 100644 src/main/java/com/google/api/generator/gapic/composer/FileHeader.java diff --git a/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java b/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java index 46e01449b0..1c179125a9 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java +++ b/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java @@ -23,8 +23,7 @@ @AutoValue public abstract class ClassDefinition implements AstNode { - // Required for outer classes. - @Nullable + // Optional. public abstract ImmutableList fileHeader(); // Required. public abstract ScopeNode scope(); @@ -69,6 +68,7 @@ public void accept(AstNodeVisitor visitor) { public static Builder builder() { return new AutoValue_ClassDefinition.Builder() + .setFileHeader(Collections.emptyList()) .setHeaderCommentStatements(Collections.emptyList()) .setIsNested(false) .setIsFinal(false) @@ -127,9 +127,8 @@ public ClassDefinition build() { ClassDefinition classDef = autoBuild(); - // Only nested classes can forego having a package and file header. + // Only nested classes can forego having a package. if (!classDef.isNested()) { - Preconditions.checkNotNull(classDef.fileHeader(), "Outer classes must have a file header."); Preconditions.checkNotNull( classDef.packageString(), "Outer classes must have a package name defined"); Preconditions.checkState(!classDef.isStatic(), "Outer classes cannot be static"); diff --git a/src/main/java/com/google/api/generator/gapic/composer/ApacheLicenseComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ApacheLicenseComposer.java new file mode 100644 index 0000000000..a3016de081 --- /dev/null +++ b/src/main/java/com/google/api/generator/gapic/composer/ApacheLicenseComposer.java @@ -0,0 +1,37 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.api.generator.gapic.composer; + +import com.google.api.generator.engine.ast.BlockComment; +import com.google.api.generator.engine.ast.CommentStatement; +import java.util.Arrays; +import java.util.List; + +public class ApacheLicenseComposer { + private static final String fileHeadeString = + "Copyright 2020 Google LLC\n" + + "Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + "you may not use this file except in compliance with the License.\n" + + "You may obtain a copy of the License at\n\n" + + " http://www.apache.org/licenses/LICENSE-2.0\n\n" + + "Unless required by applicable law or agreed to in writing, software\n" + + "distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + "See the License for the specific language governing permissions and\n" + + "limitations under the License."; + + public static final List APACHE_LICENSE = + Arrays.asList(CommentStatement.withComment(BlockComment.withComment(fileHeadeString)));; +} diff --git a/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java b/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java deleted file mode 100644 index b178377356..0000000000 --- a/src/main/java/com/google/api/generator/gapic/composer/FileHeader.java +++ /dev/null @@ -1,51 +0,0 @@ -// Copyright 2020 Google LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package com.google.api.generator.gapic.composer; - -import com.google.api.generator.engine.ast.CommentStatement; -import com.google.api.generator.engine.ast.LineComment; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - -public class FileHeader { - public static final List APACHE_LICENSE = createApacheLicense(); - - private static List createApacheLicense() { - String[] fileHeadeStrings = { - "Copyright 2020 Google LLC", - "", - "Licensed under the Apache License, Version 2.0 (the \"License\");", - "you may not use this file except in compliance with the License.", - "You may obtain a copy of the License at", - "", - " http://www.apache.org/licenses/LICENSE-2.0", - "", - "Unless required by applicable law or agreed to in writing, software", - "distributed under the License is distributed on an \"AS IS\" BASIS,", - "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.", - "See the License for the specific language governing permissions and", - "limitations under the License." - }; - - List apacheFileHeader = new ArrayList<>(); - Arrays.stream(fileHeadeStrings) - .forEach( - s -> { - apacheFileHeader.add(CommentStatement.withComment(LineComment.withComment(s))); - }); - return apacheFileHeader; - } -} diff --git a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java index daf85512e9..8263b17312 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposer.java @@ -72,7 +72,6 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.APACHE_LICENSE) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setImplementsTypes(createClassImplements(types)) diff --git a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java index 9c629a10a7..0d1520719d 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposer.java @@ -135,7 +135,6 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.APACHE_LICENSE) .setPackageString(pakkage) .setAnnotations(createClassAnnotations()) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java index e7865bc878..089f995562 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/MockServiceClassComposer.java @@ -61,7 +61,6 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.APACHE_LICENSE) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java index edca09a8a3..e1de1f04aa 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposer.java @@ -98,7 +98,6 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.APACHE_LICENSE) .setPackageString(pakkage) .setAnnotations(createClassAnnotations()) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java index 8e79f54460..901d6f0207 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientClassComposer.java @@ -85,7 +85,6 @@ public GapicClass generate(Service service, Map messageTypes) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.APACHE_LICENSE) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setImplementsTypes(createClassImplements(types)) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java index dfae4aae80..16531e389b 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposer.java @@ -112,7 +112,6 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.APACHE_LICENSE) .setPackageString(pakkage) .setAnnotations(createClassAnnotations()) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java index e290be11b7..838e468107 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposer.java @@ -91,7 +91,6 @@ public GapicClass generate(Service service, Map ignore) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.APACHE_LICENSE) .setPackageString(pakkage) .setAnnotations(createClassAnnotations()) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java index 7a8ea6ad5a..a8d76e5750 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ServiceStubClassComposer.java @@ -64,7 +64,6 @@ public GapicClass generate(Service service, Map messageTypes) { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(FileHeader.APACHE_LICENSE) .setPackageString(pakkage) .setAnnotations(createClassAnnotations(types)) .setIsAbstract(true) diff --git a/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java b/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java index 917c165716..dde18940a7 100644 --- a/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java @@ -23,7 +23,7 @@ public class ClassDefinitionTest { @Test - public void validClassDefinition_basicWithComments() { + public void validClassDefinition_basicWithCommentsAndHeader() { LineComment lineComment = LineComment.withComment("AUTO-GENERATED DOCUMENTATION AND CLASS"); JavaDocComment javaDocComment = JavaDocComment.builder() @@ -65,7 +65,6 @@ public void validClassDefinition_nestedBasic() { @Test public void validClassDefinition_withAnnotationsExtendsAndImplements() { ClassDefinition.builder() - .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -110,7 +109,6 @@ public void validClassDefinition_statementsAndMethods() { List methods = Arrays.asList(method, method); ClassDefinition.builder() - .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setIsFinal(true) @@ -127,7 +125,6 @@ public void invalidClassDefinition_implementsNullType() { IllegalStateException.class, () -> { ClassDefinition.builder() - .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -136,26 +133,12 @@ public void invalidClassDefinition_implementsNullType() { }); } - @Test - public void invalidClassDefinition_outerClassMissingFileHeader() { - assertThrows( - NullPointerException.class, - () -> { - ClassDefinition.builder() - .setPackageString("com.google.example.library.v1.stub") - .setName("LibraryServiceStub") - .setScope(ScopeNode.PUBLIC) - .build(); - }); - } - @Test public void invalidClassDefinition_outerClassMissingPackage() { assertThrows( NullPointerException.class, () -> { ClassDefinition.builder() - .setFileHeader(createFileHeader()) .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) .build(); @@ -168,7 +151,6 @@ public void invalidClassDefinition_outerClassStatic() { IllegalStateException.class, () -> { ClassDefinition.builder() - .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setIsStatic(true) @@ -183,7 +165,6 @@ public void invalidClassDefinition_outerClassPrivate() { IllegalStateException.class, () -> { ClassDefinition.builder() - .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PRIVATE) @@ -197,7 +178,6 @@ public void invalidClassDefinition_extendsPrimitiveType() { IllegalStateException.class, () -> { ClassDefinition.builder() - .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -212,7 +192,6 @@ public void invalidClassDefinition_extendsNullType() { IllegalStateException.class, () -> { ClassDefinition.builder() - .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -227,7 +206,6 @@ public void invalidClassDefinition_abstractFinal() { IllegalStateException.class, () -> { ClassDefinition.builder() - .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setIsAbstract(true) @@ -244,7 +222,6 @@ public void invalidClassDefinition_implementsPrimtiveType() { IllegalStateException.class, () -> { ClassDefinition.builder() - .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -260,7 +237,6 @@ public void invalidClassDefinition_extendsImplementsSameType() { IllegalStateException.class, () -> { ClassDefinition.builder() - .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -290,7 +266,6 @@ public void invalidClassDefinition_assignmentWithUnscopedVariableExprStatement() IllegalStateException.class, () -> { ClassDefinition.builder() - .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -313,7 +288,6 @@ public void invalidClassDefinition_unscopedVariableExprStatement() { IllegalStateException.class, () -> { ClassDefinition.builder() - .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -329,7 +303,6 @@ public void invalidClassDefinition_badStatementType() { IllegalStateException.class, () -> { ClassDefinition.builder() - .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index 1dd045a209..d081480241 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -1499,7 +1499,7 @@ public void writeMethodDefinition_templatedReturnTypeAndArguments() { } @Test - public void writeClassDefinition_basic() { + public void writeClassDefinition_basicWithFileHeader() { ClassDefinition classDef = ClassDefinition.builder() .setFileHeader(createFileHeader()) @@ -1523,7 +1523,6 @@ public void writeClassDefinition_basic() { public void writeClassDefinition_withAnnotationsExtendsAndImplements() { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -1543,8 +1542,7 @@ public void writeClassDefinition_withAnnotationsExtendsAndImplements() { assertEquals( writerVisitor.write(), String.format( - createLines(6), - "// Apache License\n\n", + createLines(5), "package com.google.example.library.v1.stub;\n", "\n", "@Deprecated\n", @@ -1632,7 +1630,6 @@ public void writeClassDefinition_commentsStatementsAndMethods() { ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(createFileHeader()) .setPackageString("com.google.example.library.v1.stub") .setHeaderCommentStatements( Arrays.asList( @@ -1648,8 +1645,7 @@ public void writeClassDefinition_commentsStatementsAndMethods() { classDef.accept(writerVisitor); String expected = String.format( - createLines(37), - "// Apache License\n\n", + createLines(36), "package com.google.example.library.v1.stub;\n", "\n", "import com.google.api.generator.engine.ast.AssignmentExpr;\n", diff --git a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java index 56b911bb32..e75c4bb8c5 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceCallableFactoryClassComposerTest.java @@ -55,21 +55,7 @@ public void generateServiceClasses() { // TODO(miraleung): Update this when a file-diffing test mechanism is in place. private static final String EXPECTED_CLASS_STRING = - "// Copyright 2020 Google LLC\n" - + "//\n" - + "// Licensed under the Apache License, Version 2.0 (the \"License\");\n" - + "// you may not use this file except in compliance with the License.\n" - + "// You may obtain a copy of the License at\n" - + "//\n" - + "// http://www.apache.org/licenses/LICENSE-2.0\n" - + "//\n" - + "// Unless required by applicable law or agreed to in writing, software\n" - + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" - + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - + "// See the License for the specific language governing permissions and\n" - + "// limitations under the License.\n" - + "\n" - + "package com.google.showcase.v1beta1.stub;\n" + "package com.google.showcase.v1beta1.stub;\n" + "\n" + "import com.google.api.gax.grpc.GrpcCallSettings;\n" + "import com.google.api.gax.grpc.GrpcCallableFactory;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java index 7cb295e4ad..13f520abf6 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/GrpcServiceStubClassComposerTest.java @@ -55,21 +55,7 @@ public void generateServiceClasses() { // TODO(miraleung): Update this when a file-diffing test mechanism is in place. private static final String EXPECTED_CLASS_STRING = - "// Copyright 2020 Google LLC\n" - + "//\n" - + "// Licensed under the Apache License, Version 2.0 (the \"License\");\n" - + "// you may not use this file except in compliance with the License.\n" - + "// You may obtain a copy of the License at\n" - + "//\n" - + "// http://www.apache.org/licenses/LICENSE-2.0\n" - + "//\n" - + "// Unless required by applicable law or agreed to in writing, software\n" - + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" - + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - + "// See the License for the specific language governing permissions and\n" - + "// limitations under the License.\n" - + "\n" - + "package com.google.showcase.v1beta1.stub;\n" + "package com.google.showcase.v1beta1.stub;\n" + "\n" + "import static com.google.showcase.v1beta1.EchoClient.PagedExpandPagedResponse;\n" + "\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java index d65c21a8e8..1248dae21b 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/MockServiceClassComposerTest.java @@ -54,21 +54,7 @@ public void generateServiceClasses() { // TODO(miraleung): Update this when a file-diffing test mechanism is in place. private static final String EXPECTED_CLASS_STRING = - "// Copyright 2020 Google LLC\n" - + "//\n" - + "// Licensed under the Apache License, Version 2.0 (the \"License\");\n" - + "// you may not use this file except in compliance with the License.\n" - + "// You may obtain a copy of the License at\n" - + "//\n" - + "// http://www.apache.org/licenses/LICENSE-2.0\n" - + "//\n" - + "// Unless required by applicable law or agreed to in writing, software\n" - + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" - + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - + "// See the License for the specific language governing permissions and\n" - + "// limitations under the License.\n" - + "\n" - + "package com.google.showcase.v1beta1;\n" + "package com.google.showcase.v1beta1;\n" + "\n" + "import com.google.api.core.BetaApi;\n" + "import com.google.api.gax.grpc.testing.MockGrpcService;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java index e2c7530f62..25b6a01833 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/MockServiceImplClassComposerTest.java @@ -55,21 +55,7 @@ public void generateServiceClasses() { // TODO(miraleung): Update this when a file-diffing test mechanism is in place. private static final String EXPECTED_CLASS_STRING = - "// Copyright 2020 Google LLC\n" - + "//\n" - + "// Licensed under the Apache License, Version 2.0 (the \"License\");\n" - + "// you may not use this file except in compliance with the License.\n" - + "// You may obtain a copy of the License at\n" - + "//\n" - + "// http://www.apache.org/licenses/LICENSE-2.0\n" - + "//\n" - + "// Unless required by applicable law or agreed to in writing, software\n" - + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" - + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - + "// See the License for the specific language governing permissions and\n" - + "// limitations under the License.\n" - + "\n" - + "package com.google.showcase.v1beta1;\n" + "package com.google.showcase.v1beta1;\n" + "\n" + "import com.google.api.core.BetaApi;\n" + "import com.google.longrunning.Operation;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java index 75bd81248b..ce766355b9 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientClassComposerTest.java @@ -55,21 +55,7 @@ public void generateServiceClasses() { // TODO(miraleung): Update this when a file-diffing test mechanism is in place. private static final String EXPECTED_CLASS_STRING = - "// Copyright 2020 Google LLC\n" - + "//\n" - + "// Licensed under the Apache License, Version 2.0 (the \"License\");\n" - + "// you may not use this file except in compliance with the License.\n" - + "// You may obtain a copy of the License at\n" - + "//\n" - + "// http://www.apache.org/licenses/LICENSE-2.0\n" - + "//\n" - + "// Unless required by applicable law or agreed to in writing, software\n" - + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" - + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - + "// See the License for the specific language governing permissions and\n" - + "// limitations under the License.\n" - + "\n" - + "package com.google.showcase.v1beta1;\n" + "package com.google.showcase.v1beta1;\n" + "\n" + "import com.google.api.gax.core.BackgroundResource;\n" + "import com.google.api.gax.longrunning.OperationFuture;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java index 4a97fdaac4..ab1925fa41 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceClientTestClassComposerTest.java @@ -55,21 +55,7 @@ public void generateServiceClasses() { // TODO(miraleung): Update this when a file-diffing test mechanism is in place. private static final String EXPECTED_CLASS_STRING = - "// Copyright 2020 Google LLC\n" - + "//\n" - + "// Licensed under the Apache License, Version 2.0 (the \"License\");\n" - + "// you may not use this file except in compliance with the License.\n" - + "// You may obtain a copy of the License at\n" - + "//\n" - + "// http://www.apache.org/licenses/LICENSE-2.0\n" - + "//\n" - + "// Unless required by applicable law or agreed to in writing, software\n" - + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" - + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - + "// See the License for the specific language governing permissions and\n" - + "// limitations under the License.\n" - + "\n" - + "package com.google.showcase.v1beta1;\n" + "package com.google.showcase.v1beta1;\n" + "\n" + "import com.google.api.gax.core.NoCredentialsProvider;\n" + "import com.google.api.gax.grpc.testing.LocalChannelProvider;\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java index a7510f0c3a..268d0805b6 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceSettingsClassComposerTest.java @@ -55,21 +55,7 @@ public void generateServiceClasses() { // TODO(miraleung): Update this when a file-diffing test mechanism is in place. private static final String EXPECTED_CLASS_STRING = - "// Copyright 2020 Google LLC\n" - + "//\n" - + "// Licensed under the Apache License, Version 2.0 (the \"License\");\n" - + "// you may not use this file except in compliance with the License.\n" - + "// You may obtain a copy of the License at\n" - + "//\n" - + "// http://www.apache.org/licenses/LICENSE-2.0\n" - + "//\n" - + "// Unless required by applicable law or agreed to in writing, software\n" - + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" - + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - + "// See the License for the specific language governing permissions and\n" - + "// limitations under the License.\n" - + "\n" - + "package com.google.showcase.v1beta1;\n" + "package com.google.showcase.v1beta1;\n" + "\n" + "import static com.google.showcase.v1beta1.EchoClient.PagedExpandPagedResponse;\n" + "\n" diff --git a/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java index 97465d15d3..0422eda88a 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ServiceStubClassComposerTest.java @@ -54,21 +54,7 @@ public void generateServiceClasses() { // TODO(miraleung): Update this when a file-diffing test mechanism is in place. private static final String EXPECTED_CLASS_STRING = - "// Copyright 2020 Google LLC\n" - + "//\n" - + "// Licensed under the Apache License, Version 2.0 (the \"License\");\n" - + "// you may not use this file except in compliance with the License.\n" - + "// You may obtain a copy of the License at\n" - + "//\n" - + "// http://www.apache.org/licenses/LICENSE-2.0\n" - + "//\n" - + "// Unless required by applicable law or agreed to in writing, software\n" - + "// distributed under the License is distributed on an \"AS IS\" BASIS,\n" - + "// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" - + "// See the License for the specific language governing permissions and\n" - + "// limitations under the License.\n" - + "\n" - + "package com.google.showcase.v1beta1.stub;\n" + "package com.google.showcase.v1beta1.stub;\n" + "\n" + "import com.google.api.gax.core.BackgroundResource;\n" + "import com.google.api.gax.rpc.BidiStreamingCallable;\n" From 88d71cf672006f213ddd66f2f9160a2cd7d4144a Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Fri, 7 Aug 2020 16:35:55 -0700 Subject: [PATCH 11/23] add composer test --- .../generator/gapic/composer/Composer.java | 4 +- .../engine/ast/ClassDefinitionTest.java | 4 +- .../engine/writer/JavaWriterVisitorTest.java | 6 +-- .../api/generator/gapic/composer/BUILD.bazel | 1 + .../gapic/composer/ComposerTest.java | 53 +++++++++++++++++++ 5 files changed, 62 insertions(+), 6 deletions(-) create mode 100644 src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java diff --git a/src/main/java/com/google/api/generator/gapic/composer/Composer.java b/src/main/java/com/google/api/generator/gapic/composer/Composer.java index 381fe6c7b5..60039d8194 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/Composer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/Composer.java @@ -20,6 +20,7 @@ import com.google.api.generator.gapic.model.GapicClass.Kind; import com.google.api.generator.gapic.model.Message; import com.google.api.generator.gapic.model.Service; +import com.google.common.annotations.VisibleForTesting; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -73,7 +74,8 @@ public static List generateMocksAndTestClasses( } /** ====================== STUB CLASSES ==================== */ - private static GapicClass generateStubServiceSettings(Service service) { + @VisibleForTesting + protected static GapicClass generateStubServiceSettings(Service service) { return generateGenericClass( Kind.STUB, String.format("%sStubSettings", service.name()), service); } diff --git a/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java b/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java index dde18940a7..bafe033006 100644 --- a/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java @@ -348,8 +348,8 @@ private static ForStatement createForStatement() { .setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr()))) .build(); } - // Create a simple line comment to stand for the Apache License header. + // Create a simple block comment to stand for the Apache License header. private static List createFileHeader() { - return Arrays.asList(CommentStatement.withComment(LineComment.withComment("Apache License"))); + return Arrays.asList(CommentStatement.withComment(BlockComment.withComment("Apache License"))); } } diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index d081480241..6aab3a2d4b 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -1513,7 +1513,7 @@ public void writeClassDefinition_basicWithFileHeader() { writerVisitor.write(), String.format( createLines(4), - "// Apache License\n\n", + "/** Apache License */\n", "package com.google.example.library.v1.stub;\n", "\n", "public class LibraryServiceStub {}\n")); @@ -1791,8 +1791,8 @@ private static String createSampleCode() { return writerVisitor.write(); } - // Create a simple line comment to stand for the Apache License header. + // Create a simple block comment to stand for the Apache License header. private static List createFileHeader() { - return Arrays.asList(CommentStatement.withComment(LineComment.withComment("Apache License"))); + return Arrays.asList(CommentStatement.withComment(BlockComment.withComment("Apache License"))); } } diff --git a/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel b/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel index 4cb438e4ba..9fd6d70cf6 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel +++ b/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel @@ -1,6 +1,7 @@ package(default_visibility = ["//visibility:public"]) TESTS = [ + "ComposerTest", "GrpcServiceCallableFactoryClassComposerTest", "GrpcServiceStubClassComposerTest", "MockServiceClassComposerTest", diff --git a/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java new file mode 100644 index 0000000000..2be04cb7a4 --- /dev/null +++ b/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java @@ -0,0 +1,53 @@ +// Copyright 2020 Google LLC +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package com.google.api.generator.gapic.composer; + +import static junit.framework.Assert.assertEquals; + +import com.google.api.generator.engine.writer.JavaWriterVisitor; +import com.google.api.generator.gapic.model.Message; +import com.google.api.generator.gapic.model.Service; +import com.google.api.generator.gapic.protoparser.Parser; +import com.google.protobuf.Descriptors.FileDescriptor; +import com.google.protobuf.Descriptors.ServiceDescriptor; +import com.google.showcase.v1beta1.EchoOuterClass; +import java.util.List; +import java.util.Map; +import org.junit.Before; +import org.junit.Test; + +public class ComposerTest { + private ServiceDescriptor echoService; + private FileDescriptor echoFileDescriptor; + + @Before + public void setUp() { + echoFileDescriptor = EchoOuterClass.getDescriptor(); + echoService = echoFileDescriptor.getServices().get(0); + assertEquals(echoService.getName(), "Echo"); + } + + @Test + public void generateStubServiceSettings() { + Map messageTypes = Parser.parseMessages(echoFileDescriptor); + List services = Parser.parseService(echoFileDescriptor, messageTypes); + Service echoProtoService = services.get(0); + JavaWriterVisitor visitor = new JavaWriterVisitor(); + Composer.generateStubServiceSettings(echoProtoService).classDefinition().accept(visitor); + assertEquals( + visitor.write(), + "package com.google.showcase.v1beta1.stub;\n\npublic class EchoStubSettings {}\n"); + } +} From 0f59ef8703d2dd9a6a7bf9f2cf195192fb3ee0ea Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Sat, 8 Aug 2020 16:11:31 -0700 Subject: [PATCH 12/23] multiple line comment --- .../generator/engine/ast/BlockComment.java | 11 +--------- .../engine/writer/JavaWriterVisitor.java | 21 ++++++++++++------- .../writer/ImportWriterVisitorTest.java | 1 - .../engine/writer/JavaWriterVisitorTest.java | 9 ++++---- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/BlockComment.java b/src/main/java/com/google/api/generator/engine/ast/BlockComment.java index d71347b489..00f98aa2ec 100644 --- a/src/main/java/com/google/api/generator/engine/ast/BlockComment.java +++ b/src/main/java/com/google/api/generator/engine/ast/BlockComment.java @@ -14,7 +14,6 @@ package com.google.api.generator.engine.ast; -import com.google.api.generator.engine.escaper.MetacharEscaper; import com.google.auto.value.AutoValue; @AutoValue @@ -38,14 +37,6 @@ public static BlockComment withComment(String comment) { public abstract static class Builder { public abstract Builder setComment(String comment); - // Private accessor. - abstract String comment(); - - public abstract BlockComment autoBuild(); - - public BlockComment build() { - setComment(MetacharEscaper.escaper(comment())); - return autoBuild(); - } + public abstract BlockComment build(); } } diff --git a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java index e7bb13d62b..97f0f01ef3 100644 --- a/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java +++ b/src/main/java/com/google/api/generator/engine/writer/JavaWriterVisitor.java @@ -62,7 +62,7 @@ public class JavaWriterVisitor implements AstNodeVisitor { private static final String COLON = ":"; private static final String COMMA = ","; - private static final String BLOCK_COMMENT_START = "/**"; + private static final String BLOCK_COMMENT_START = "/*"; private static final String BLOCK_COMMENT_END = "*/"; private static final String DOT = "."; private static final String ESCAPED_QUOTE = "\""; @@ -70,6 +70,7 @@ public class JavaWriterVisitor implements AstNodeVisitor { private static final String LEFT_ANGLE = "<"; private static final String LEFT_BRACE = "{"; private static final String LEFT_PAREN = "("; + private static final String JAVADOC_COMMENT_START = "/**"; private static final String QUESTION_MARK = "?"; private static final String RIGHT_ANGLE = ">"; private static final String RIGHT_BRACE = "}"; @@ -487,17 +488,21 @@ public void visit(LineComment lineComment) { } public void visit(BlockComment blockComment) { - // Split comments by new line and embrace the comment block with `/** */`. - String sourceComment = blockComment.comment(); - String formattedSource = - JavaFormatter.format( - String.format("%s %s %s", BLOCK_COMMENT_START, sourceComment, BLOCK_COMMENT_END)); - buffer.append(formattedSource); + // Split comments by new line and embrace the comment block with `/* */`. + StringBuilder sourceComment = new StringBuilder(); + sourceComment.append(BLOCK_COMMENT_START).append(NEWLINE); + Arrays.stream(blockComment.comment().split("\\r?\\n")) + .forEach( + comment -> { + sourceComment.append(String.format("%s %s%s", ASTERISK, comment, NEWLINE)); + }); + sourceComment.append(BLOCK_COMMENT_END); + buffer.append(JavaFormatter.format(sourceComment.toString())); } public void visit(JavaDocComment javaDocComment) { StringBuilder sourceComment = new StringBuilder(); - sourceComment.append(BLOCK_COMMENT_START).append(NEWLINE); + sourceComment.append(JAVADOC_COMMENT_START).append(NEWLINE); Arrays.stream(javaDocComment.comment().split("\\r?\\n")) .forEach( comment -> { diff --git a/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java index 8456363e71..0a780a5e9c 100644 --- a/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/ImportWriterVisitorTest.java @@ -89,7 +89,6 @@ public void writeNewObjectExprImports_withArgs() { .setArguments(Arrays.asList(fileExpr)) .build(); newObjectExpr.accept(writerVisitor); - System.out.println(writerVisitor.write()); assertEquals( writerVisitor.write(), String.format( diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index d3a3bc6d08..064fde6f83 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -322,7 +322,7 @@ public void writeBlockCommentStatement_basic() { String content = "this is a test comment"; BlockComment blockComment = BlockComment.builder().setComment(content).build(); CommentStatement commentStatement = CommentStatement.withComment(blockComment); - String expected = "/** this is a test comment */\n"; + String expected = "/*\n" + "* this is a test comment\n" + "*/\n"; commentStatement.accept(writerVisitor); assertEquals(writerVisitor.write(), expected); } @@ -400,11 +400,10 @@ public void writeJavaDocCommentStatement_allComponents() { } @Test - public void writeBlockComment_specialChar() { - String content = "Testing special characters: \b\t\n\r\"`'?/\\,.[]{}|-_!@#$%^()"; + public void writeBlockComment_multipleLine() { + String content = "Apache License \nThis is a test file header"; BlockComment blockComment = BlockComment.builder().setComment(content).build(); - String expected = - "/** Testing special characters: \\b\\t\\n\\r\"`'?/\\\\,.[]{}|-_!@#$%^() */\n"; + String expected = "/*\n" + "* Apache License\n" + "* This is a test file header\n" + "*/\n"; blockComment.accept(writerVisitor); assertEquals(writerVisitor.write(), expected); } From 0f34aecd31c9c8d4d2d777bf0293956176dbcdaf Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Sat, 8 Aug 2020 16:42:08 -0700 Subject: [PATCH 13/23] multiple line comment for header --- .../gapic/composer/ApacheLicenseComposer.java | 4 +-- .../generator/gapic/composer/Composer.java | 1 + .../engine/writer/JavaWriterVisitorTest.java | 6 ++-- .../gapic/composer/ComposerTest.java | 35 ++++++++++++++++--- 4 files changed, 38 insertions(+), 8 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ApacheLicenseComposer.java b/src/main/java/com/google/api/generator/gapic/composer/ApacheLicenseComposer.java index a3016de081..7d6ad98d94 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ApacheLicenseComposer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/ApacheLicenseComposer.java @@ -21,11 +21,11 @@ public class ApacheLicenseComposer { private static final String fileHeadeString = - "Copyright 2020 Google LLC\n" + "Copyright 2020 Google LLC\n\n" + "Licensed under the Apache License, Version 2.0 (the \"License\");\n" + "you may not use this file except in compliance with the License.\n" + "You may obtain a copy of the License at\n\n" - + " http://www.apache.org/licenses/LICENSE-2.0\n\n" + + " https://www.apache.org/licenses/LICENSE-2.0\n\n" + "Unless required by applicable law or agreed to in writing, software\n" + "distributed under the License is distributed on an \"AS IS\" BASIS,\n" + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" diff --git a/src/main/java/com/google/api/generator/gapic/composer/Composer.java b/src/main/java/com/google/api/generator/gapic/composer/Composer.java index 8f595b8f36..d4122d3b8c 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/Composer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/Composer.java @@ -102,6 +102,7 @@ private static GapicClass generateGenericClass(Kind kind, String name, Service s ClassDefinition classDef = ClassDefinition.builder() + .setFileHeader(ApacheLicenseComposer.APACHE_LICENSE) .setPackageString(pakkage) .setName(name) .setScope(ScopeNode.PUBLIC) diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index 7c6b1df34f..2a718823d8 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -1511,8 +1511,10 @@ public void writeClassDefinition_basicWithFileHeader() { assertEquals( writerVisitor.write(), String.format( - createLines(4), - "/** Apache License */\n", + createLines(6), + "/*\n", + " * Apache License\n", + " */\n\n", "package com.google.example.library.v1.stub;\n", "\n", "public class LibraryServiceStub {}\n")); diff --git a/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java index 2be04cb7a4..0844fe2c97 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java @@ -18,13 +18,16 @@ import com.google.api.generator.engine.writer.JavaWriterVisitor; import com.google.api.generator.gapic.model.Message; +import com.google.api.generator.gapic.model.ResourceName; import com.google.api.generator.gapic.model.Service; import com.google.api.generator.gapic.protoparser.Parser; import com.google.protobuf.Descriptors.FileDescriptor; import com.google.protobuf.Descriptors.ServiceDescriptor; import com.google.showcase.v1beta1.EchoOuterClass; +import java.util.HashSet; import java.util.List; import java.util.Map; +import java.util.Set; import org.junit.Before; import org.junit.Test; @@ -42,12 +45,36 @@ public void setUp() { @Test public void generateStubServiceSettings() { Map messageTypes = Parser.parseMessages(echoFileDescriptor); - List services = Parser.parseService(echoFileDescriptor, messageTypes); + Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); + Set outputResourceNames = new HashSet<>(); + + List services = + Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames); Service echoProtoService = services.get(0); JavaWriterVisitor visitor = new JavaWriterVisitor(); Composer.generateStubServiceSettings(echoProtoService).classDefinition().accept(visitor); - assertEquals( - visitor.write(), - "package com.google.showcase.v1beta1.stub;\n\npublic class EchoStubSettings {}\n"); + System.out.println(visitor.write()); + System.out.println(EXPECTED_CLASS_STRING); + + assertEquals(visitor.write(), EXPECTED_CLASS_STRING); } + + private static final String EXPECTED_CLASS_STRING = + "/*\n" + + " * Copyright 2020 Google LLC\n" + + " *\n" + + " * Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + " * you may not use this file except in compliance with the License.\n" + + " * You may obtain a copy of the License at\n" + + " *\n" + + " * https://www.apache.org/licenses/LICENSE-2.0\n" + + " *\n" + + " * Unless required by applicable law or agreed to in writing, software\n" + + " * distributed under the License is distributed on an \"AS IS\" BASIS,\n" + + " * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" + + " * See the License for the specific language governing permissions and\n" + + " * limitations under the License.\n" + + " */\n\n" + + "package com.google.showcase.v1beta1.stub;\n\n" + + "public class EchoStubSettings {}\n"; } From 6364167587bf0322a4efe726eba04725e0470312 Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Mon, 10 Aug 2020 16:55:54 -0700 Subject: [PATCH 14/23] rename apache license --- .../com/google/api/generator/gapic/composer/Composer.java | 3 ++- .../ApacheLicenseComposer.java => utils/ApacheLicense.java} | 6 +++--- .../java/com/google/api/generator/gapic/utils/BUILD.bazel | 1 + 3 files changed, 6 insertions(+), 4 deletions(-) rename src/main/java/com/google/api/generator/gapic/{composer/ApacheLicenseComposer.java => utils/ApacheLicense.java} (91%) diff --git a/src/main/java/com/google/api/generator/gapic/composer/Composer.java b/src/main/java/com/google/api/generator/gapic/composer/Composer.java index d4122d3b8c..3f83e8babf 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/Composer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/Composer.java @@ -22,6 +22,7 @@ import com.google.api.generator.gapic.model.Message; import com.google.api.generator.gapic.model.ResourceName; import com.google.api.generator.gapic.model.Service; +import com.google.api.generator.gapic.utils.ApacheLicense; import com.google.common.annotations.VisibleForTesting; import java.util.ArrayList; import java.util.List; @@ -102,7 +103,7 @@ private static GapicClass generateGenericClass(Kind kind, String name, Service s ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(ApacheLicenseComposer.APACHE_LICENSE) + .setFileHeader(ApacheLicense.APACHE_LICENSE_COMMENT_STATEMENT) .setPackageString(pakkage) .setName(name) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/composer/ApacheLicenseComposer.java b/src/main/java/com/google/api/generator/gapic/utils/ApacheLicense.java similarity index 91% rename from src/main/java/com/google/api/generator/gapic/composer/ApacheLicenseComposer.java rename to src/main/java/com/google/api/generator/gapic/utils/ApacheLicense.java index 7d6ad98d94..3623ff8dc7 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/ApacheLicenseComposer.java +++ b/src/main/java/com/google/api/generator/gapic/utils/ApacheLicense.java @@ -12,14 +12,14 @@ // See the License for the specific language governing permissions and // limitations under the License. -package com.google.api.generator.gapic.composer; +package com.google.api.generator.gapic.utils; import com.google.api.generator.engine.ast.BlockComment; import com.google.api.generator.engine.ast.CommentStatement; import java.util.Arrays; import java.util.List; -public class ApacheLicenseComposer { +public class ApacheLicense { private static final String fileHeadeString = "Copyright 2020 Google LLC\n\n" + "Licensed under the Apache License, Version 2.0 (the \"License\");\n" @@ -32,6 +32,6 @@ public class ApacheLicenseComposer { + "See the License for the specific language governing permissions and\n" + "limitations under the License."; - public static final List APACHE_LICENSE = + public static final List APACHE_LICENSE_COMMENT_STATEMENT = Arrays.asList(CommentStatement.withComment(BlockComment.withComment(fileHeadeString)));; } diff --git a/src/main/java/com/google/api/generator/gapic/utils/BUILD.bazel b/src/main/java/com/google/api/generator/gapic/utils/BUILD.bazel index e4a82fdb64..f93ddda40d 100644 --- a/src/main/java/com/google/api/generator/gapic/utils/BUILD.bazel +++ b/src/main/java/com/google/api/generator/gapic/utils/BUILD.bazel @@ -12,5 +12,6 @@ java_library( ], deps = [ "@com_google_guava_guava//jar", + "//src/main/java/com/google/api/generator/engine/ast", ], ) From 590c7c618679a274264c3313f0c19406446deac4 Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Mon, 10 Aug 2020 18:18:14 -0700 Subject: [PATCH 15/23] move file header addition to gapic class --- .../generator/engine/ast/ClassDefinition.java | 2 + .../generator/gapic/composer/Composer.java | 4 -- .../api/generator/gapic/model/GapicClass.java | 10 ++++ .../generator/gapic/utils/ApacheLicense.java | 2 +- .../api/generator/gapic/utils/BUILD.bazel | 2 +- .../api/generator/gapic/composer/BUILD.bazel | 1 - .../api/generator/gapic/model/BUILD.bazel | 3 ++ .../GapicClassTest.java} | 50 ++++++------------- 8 files changed, 31 insertions(+), 43 deletions(-) rename src/test/java/com/google/api/generator/gapic/{composer/ComposerTest.java => model/GapicClassTest.java} (55%) diff --git a/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java b/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java index 1c179125a9..8d59ba5ac5 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java +++ b/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java @@ -81,6 +81,8 @@ public static Builder builder() { .setNestedClasses(Collections.emptyList()); } + public abstract Builder toBuilder(); + @AutoValue.Builder public abstract static class Builder { public abstract Builder setFileHeader(List fileHeader); diff --git a/src/main/java/com/google/api/generator/gapic/composer/Composer.java b/src/main/java/com/google/api/generator/gapic/composer/Composer.java index 3f83e8babf..2c9d85b3ad 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/Composer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/Composer.java @@ -22,8 +22,6 @@ import com.google.api.generator.gapic.model.Message; import com.google.api.generator.gapic.model.ResourceName; import com.google.api.generator.gapic.model.Service; -import com.google.api.generator.gapic.utils.ApacheLicense; -import com.google.common.annotations.VisibleForTesting; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -87,7 +85,6 @@ public static List generateMocksAndTestClasses( } /** ====================== STUB CLASSES ==================== */ - @VisibleForTesting protected static GapicClass generateStubServiceSettings(Service service) { return generateGenericClass( Kind.STUB, String.format("%sStubSettings", service.name()), service); @@ -103,7 +100,6 @@ private static GapicClass generateGenericClass(Kind kind, String name, Service s ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(ApacheLicense.APACHE_LICENSE_COMMENT_STATEMENT) .setPackageString(pakkage) .setName(name) .setScope(ScopeNode.PUBLIC) diff --git a/src/main/java/com/google/api/generator/gapic/model/GapicClass.java b/src/main/java/com/google/api/generator/gapic/model/GapicClass.java index 8ae0376efc..0f6958a210 100644 --- a/src/main/java/com/google/api/generator/gapic/model/GapicClass.java +++ b/src/main/java/com/google/api/generator/gapic/model/GapicClass.java @@ -15,6 +15,7 @@ package com.google.api.generator.gapic.model; import com.google.api.generator.engine.ast.ClassDefinition; +import com.google.api.generator.gapic.utils.ApacheLicense; import com.google.auto.value.AutoValue; @AutoValue @@ -35,6 +36,15 @@ public static GapicClass create(Kind kind, ClassDefinition classDefinition) { return builder().setKind(kind).setClassDefinition(classDefinition).build(); } + public GapicClass addApacheLicense() { + ClassDefinition classWithHeader = + classDefinition() + .toBuilder() + .setFileHeader(ApacheLicense.APACHE_LICENSE_COMMENT_STATEMENT) + .build(); + return create(kind(), classWithHeader); + } + static Builder builder() { return new AutoValue_GapicClass.Builder(); } diff --git a/src/main/java/com/google/api/generator/gapic/utils/ApacheLicense.java b/src/main/java/com/google/api/generator/gapic/utils/ApacheLicense.java index 3623ff8dc7..1b6b5f0a73 100644 --- a/src/main/java/com/google/api/generator/gapic/utils/ApacheLicense.java +++ b/src/main/java/com/google/api/generator/gapic/utils/ApacheLicense.java @@ -33,5 +33,5 @@ public class ApacheLicense { + "limitations under the License."; public static final List APACHE_LICENSE_COMMENT_STATEMENT = - Arrays.asList(CommentStatement.withComment(BlockComment.withComment(fileHeadeString)));; + Arrays.asList(CommentStatement.withComment(BlockComment.withComment(fileHeadeString))); } diff --git a/src/main/java/com/google/api/generator/gapic/utils/BUILD.bazel b/src/main/java/com/google/api/generator/gapic/utils/BUILD.bazel index f93ddda40d..934634f689 100644 --- a/src/main/java/com/google/api/generator/gapic/utils/BUILD.bazel +++ b/src/main/java/com/google/api/generator/gapic/utils/BUILD.bazel @@ -11,7 +11,7 @@ java_library( ":utils_files", ], deps = [ - "@com_google_guava_guava//jar", "//src/main/java/com/google/api/generator/engine/ast", + "@com_google_guava_guava//jar", ], ) diff --git a/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel b/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel index 9fd6d70cf6..4cb438e4ba 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel +++ b/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel @@ -1,7 +1,6 @@ package(default_visibility = ["//visibility:public"]) TESTS = [ - "ComposerTest", "GrpcServiceCallableFactoryClassComposerTest", "GrpcServiceStubClassComposerTest", "MockServiceClassComposerTest", diff --git a/src/test/java/com/google/api/generator/gapic/model/BUILD.bazel b/src/test/java/com/google/api/generator/gapic/model/BUILD.bazel index 4373b2111f..16f0e30101 100644 --- a/src/test/java/com/google/api/generator/gapic/model/BUILD.bazel +++ b/src/test/java/com/google/api/generator/gapic/model/BUILD.bazel @@ -1,6 +1,7 @@ package(default_visibility = ["//visibility:public"]) TESTS = [ + "GapicClassTest", "MethodTest", ] @@ -16,6 +17,8 @@ filegroup( deps = [ "//src/main/java/com/google/api/generator:autovalue", "//src/main/java/com/google/api/generator/gapic/model", + "//src/main/java/com/google/api/generator/engine/writer", + "//src/main/java/com/google/api/generator/engine/ast", "@com_google_truth_truth//jar", "@junit_junit//jar", ], diff --git a/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java b/src/test/java/com/google/api/generator/gapic/model/GapicClassTest.java similarity index 55% rename from src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java rename to src/test/java/com/google/api/generator/gapic/model/GapicClassTest.java index 0844fe2c97..8fe57daf69 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/model/GapicClassTest.java @@ -12,50 +12,28 @@ // See the License for the specific language governing permissions and // limitations under the License. -package com.google.api.generator.gapic.composer; +package com.google.api.generator.gapic.model; import static junit.framework.Assert.assertEquals; +import com.google.api.generator.engine.ast.ClassDefinition; +import com.google.api.generator.engine.ast.ScopeNode; import com.google.api.generator.engine.writer.JavaWriterVisitor; -import com.google.api.generator.gapic.model.Message; -import com.google.api.generator.gapic.model.ResourceName; -import com.google.api.generator.gapic.model.Service; -import com.google.api.generator.gapic.protoparser.Parser; -import com.google.protobuf.Descriptors.FileDescriptor; -import com.google.protobuf.Descriptors.ServiceDescriptor; -import com.google.showcase.v1beta1.EchoOuterClass; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import org.junit.Before; +import com.google.api.generator.gapic.model.GapicClass.Kind; import org.junit.Test; -public class ComposerTest { - private ServiceDescriptor echoService; - private FileDescriptor echoFileDescriptor; - - @Before - public void setUp() { - echoFileDescriptor = EchoOuterClass.getDescriptor(); - echoService = echoFileDescriptor.getServices().get(0); - assertEquals(echoService.getName(), "Echo"); - } - +public class GapicClassTest { @Test - public void generateStubServiceSettings() { - Map messageTypes = Parser.parseMessages(echoFileDescriptor); - Map resourceNames = Parser.parseResourceNames(echoFileDescriptor); - Set outputResourceNames = new HashSet<>(); - - List services = - Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames); - Service echoProtoService = services.get(0); + public void gapicClass_addApacheLicense() { + ClassDefinition classDef = + ClassDefinition.builder() + .setPackageString("com.google.showcase.v1beta1.stub") + .setName("EchoStubSettings") + .setScope(ScopeNode.PUBLIC) + .build(); + GapicClass gapicClassWithHeader = GapicClass.create(Kind.TEST, classDef).addApacheLicense(); JavaWriterVisitor visitor = new JavaWriterVisitor(); - Composer.generateStubServiceSettings(echoProtoService).classDefinition().accept(visitor); - System.out.println(visitor.write()); - System.out.println(EXPECTED_CLASS_STRING); - + gapicClassWithHeader.classDefinition().accept(visitor); assertEquals(visitor.write(), EXPECTED_CLASS_STRING); } From a356da97a1d02d0218bd71030d49305ea839931c Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Mon, 10 Aug 2020 18:37:58 -0700 Subject: [PATCH 16/23] format --- .../java/com/google/api/generator/gapic/model/BUILD.bazel | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/google/api/generator/gapic/model/BUILD.bazel b/src/test/java/com/google/api/generator/gapic/model/BUILD.bazel index 16f0e30101..06c900adab 100644 --- a/src/test/java/com/google/api/generator/gapic/model/BUILD.bazel +++ b/src/test/java/com/google/api/generator/gapic/model/BUILD.bazel @@ -16,9 +16,9 @@ filegroup( test_class = "com.google.api.generator.gapic.model.{0}".format(test_name), deps = [ "//src/main/java/com/google/api/generator:autovalue", - "//src/main/java/com/google/api/generator/gapic/model", - "//src/main/java/com/google/api/generator/engine/writer", "//src/main/java/com/google/api/generator/engine/ast", + "//src/main/java/com/google/api/generator/engine/writer", + "//src/main/java/com/google/api/generator/gapic/model", "@com_google_truth_truth//jar", "@junit_junit//jar", ], From 50c1fb1f25c0052e9cd18fc1d95d0e8c85c4ca57 Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Mon, 10 Aug 2020 18:50:42 -0700 Subject: [PATCH 17/23] convert change in Composer --- .../java/com/google/api/generator/gapic/composer/Composer.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/Composer.java b/src/main/java/com/google/api/generator/gapic/composer/Composer.java index 2c9d85b3ad..ec7c6889d7 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/Composer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/Composer.java @@ -85,7 +85,7 @@ public static List generateMocksAndTestClasses( } /** ====================== STUB CLASSES ==================== */ - protected static GapicClass generateStubServiceSettings(Service service) { + private static GapicClass generateStubServiceSettings(Service service) { return generateGenericClass( Kind.STUB, String.format("%sStubSettings", service.name()), service); } From 31c046852e09faac6328c6b960c93328eb3ddde9 Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Tue, 11 Aug 2020 22:49:06 -0700 Subject: [PATCH 18/23] add edge unit test --- .../engine/writer/JavaWriterVisitorTest.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index 064fde6f83..8920341bf8 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -400,7 +400,7 @@ public void writeJavaDocCommentStatement_allComponents() { } @Test - public void writeBlockComment_multipleLine() { + public void writeBlockComment_shortLines() { String content = "Apache License \nThis is a test file header"; BlockComment blockComment = BlockComment.builder().setComment(content).build(); String expected = "/*\n" + "* Apache License\n" + "* This is a test file header\n" + "*/\n"; @@ -408,6 +408,22 @@ public void writeBlockComment_multipleLine() { assertEquals(writerVisitor.write(), expected); } + @Test + public void writeBlockComment_newLineInBetween() { + String content = + "Apache License \nLicensed under the Apache License, Version 2.0 (the \"License\");\n\nyou may not use this file except in compliance with the License."; + BlockComment blockComment = BlockComment.builder().setComment(content).build(); + String expected = + "/*\n" + + "* Apache License\n" + + "* Licensed under the Apache License, Version 2.0 (the \"License\");\n" + + "*\n" + + "* you may not use this file except in compliance with the License.\n" + + "*/\n"; + blockComment.accept(writerVisitor); + assertEquals(writerVisitor.write(), expected); + } + @Test public void writeLineComment_longLine() { String content = From 7ef4b4a51d150d343154871ef891844cb21fe472 Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Wed, 12 Aug 2020 09:34:40 -0700 Subject: [PATCH 19/23] move header addition to composer class --- .../generator/gapic/composer/Composer.java | 23 ++++++++++++++++++- .../api/generator/gapic/model/GapicClass.java | 10 -------- .../api/generator/gapic/composer/BUILD.bazel | 1 + .../ComposerTest.java} | 13 +++++++---- .../api/generator/gapic/model/BUILD.bazel | 3 --- 5 files changed, 32 insertions(+), 18 deletions(-) rename src/test/java/com/google/api/generator/gapic/{model/GapicClassTest.java => composer/ComposerTest.java} (83%) diff --git a/src/main/java/com/google/api/generator/gapic/composer/Composer.java b/src/main/java/com/google/api/generator/gapic/composer/Composer.java index ec7c6889d7..02f06359a9 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/Composer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/Composer.java @@ -22,6 +22,8 @@ import com.google.api.generator.gapic.model.Message; import com.google.api.generator.gapic.model.ResourceName; import com.google.api.generator.gapic.model.Service; +import com.google.api.generator.gapic.utils.ApacheLicense; +import com.google.common.annotations.VisibleForTesting; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -36,7 +38,7 @@ public static List composeServiceClasses(GapicContext context) { clazzes.addAll(generateServiceClasses(service, context.messages())); } clazzes.addAll(generateResourceNameHelperClasses(context.helperResourceNames())); - return clazzes; + return addApacheLicenseToGapicClassList(clazzes); } public static List generateServiceClasses( @@ -106,4 +108,23 @@ private static GapicClass generateGenericClass(Kind kind, String name, Service s .build(); return GapicClass.create(kind, classDef); } + + @VisibleForTesting + protected static List addApacheLicenseToGapicClassList( + List gapicClassList) { + gapicClassList = + gapicClassList.stream() + .map( + gapicClass -> { + ClassDefinition classWithHeader = + gapicClass + .classDefinition() + .toBuilder() + .setFileHeader(ApacheLicense.APACHE_LICENSE_COMMENT_STATEMENT) + .build(); + return GapicClass.create(gapicClass.kind(), classWithHeader); + }) + .collect(Collectors.toList()); + return gapicClassList; + } } diff --git a/src/main/java/com/google/api/generator/gapic/model/GapicClass.java b/src/main/java/com/google/api/generator/gapic/model/GapicClass.java index 0f6958a210..8ae0376efc 100644 --- a/src/main/java/com/google/api/generator/gapic/model/GapicClass.java +++ b/src/main/java/com/google/api/generator/gapic/model/GapicClass.java @@ -15,7 +15,6 @@ package com.google.api.generator.gapic.model; import com.google.api.generator.engine.ast.ClassDefinition; -import com.google.api.generator.gapic.utils.ApacheLicense; import com.google.auto.value.AutoValue; @AutoValue @@ -36,15 +35,6 @@ public static GapicClass create(Kind kind, ClassDefinition classDefinition) { return builder().setKind(kind).setClassDefinition(classDefinition).build(); } - public GapicClass addApacheLicense() { - ClassDefinition classWithHeader = - classDefinition() - .toBuilder() - .setFileHeader(ApacheLicense.APACHE_LICENSE_COMMENT_STATEMENT) - .build(); - return create(kind(), classWithHeader); - } - static Builder builder() { return new AutoValue_GapicClass.Builder(); } diff --git a/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel b/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel index 4cb438e4ba..9fd6d70cf6 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel +++ b/src/test/java/com/google/api/generator/gapic/composer/BUILD.bazel @@ -1,6 +1,7 @@ package(default_visibility = ["//visibility:public"]) TESTS = [ + "ComposerTest", "GrpcServiceCallableFactoryClassComposerTest", "GrpcServiceStubClassComposerTest", "MockServiceClassComposerTest", diff --git a/src/test/java/com/google/api/generator/gapic/model/GapicClassTest.java b/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java similarity index 83% rename from src/test/java/com/google/api/generator/gapic/model/GapicClassTest.java rename to src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java index 8fe57daf69..af1e39c201 100644 --- a/src/test/java/com/google/api/generator/gapic/model/GapicClassTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java @@ -12,17 +12,20 @@ // See the License for the specific language governing permissions and // limitations under the License. -package com.google.api.generator.gapic.model; +package com.google.api.generator.gapic.composer; import static junit.framework.Assert.assertEquals; import com.google.api.generator.engine.ast.ClassDefinition; import com.google.api.generator.engine.ast.ScopeNode; import com.google.api.generator.engine.writer.JavaWriterVisitor; +import com.google.api.generator.gapic.model.GapicClass; import com.google.api.generator.gapic.model.GapicClass.Kind; +import java.util.Arrays; +import java.util.List; import org.junit.Test; -public class GapicClassTest { +public class ComposerTest { @Test public void gapicClass_addApacheLicense() { ClassDefinition classDef = @@ -31,9 +34,11 @@ public void gapicClass_addApacheLicense() { .setName("EchoStubSettings") .setScope(ScopeNode.PUBLIC) .build(); - GapicClass gapicClassWithHeader = GapicClass.create(Kind.TEST, classDef).addApacheLicense(); + GapicClass gapicClass = GapicClass.create(Kind.TEST, classDef); + List gapicClassWithHeaderList = + Composer.addApacheLicenseToGapicClassList(Arrays.asList(gapicClass)); JavaWriterVisitor visitor = new JavaWriterVisitor(); - gapicClassWithHeader.classDefinition().accept(visitor); + gapicClassWithHeaderList.get(0).classDefinition().accept(visitor); assertEquals(visitor.write(), EXPECTED_CLASS_STRING); } diff --git a/src/test/java/com/google/api/generator/gapic/model/BUILD.bazel b/src/test/java/com/google/api/generator/gapic/model/BUILD.bazel index 06c900adab..4373b2111f 100644 --- a/src/test/java/com/google/api/generator/gapic/model/BUILD.bazel +++ b/src/test/java/com/google/api/generator/gapic/model/BUILD.bazel @@ -1,7 +1,6 @@ package(default_visibility = ["//visibility:public"]) TESTS = [ - "GapicClassTest", "MethodTest", ] @@ -16,8 +15,6 @@ filegroup( test_class = "com.google.api.generator.gapic.model.{0}".format(test_name), deps = [ "//src/main/java/com/google/api/generator:autovalue", - "//src/main/java/com/google/api/generator/engine/ast", - "//src/main/java/com/google/api/generator/engine/writer", "//src/main/java/com/google/api/generator/gapic/model", "@com_google_truth_truth//jar", "@junit_junit//jar", From d59e1a3bb03d3a2655862ce859fc999966deea9a Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Wed, 12 Aug 2020 09:47:26 -0700 Subject: [PATCH 20/23] format --- .../com/google/api/generator/gapic/composer/ComposerTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java index af1e39c201..6824676a9a 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java @@ -34,9 +34,9 @@ public void gapicClass_addApacheLicense() { .setName("EchoStubSettings") .setScope(ScopeNode.PUBLIC) .build(); - GapicClass gapicClass = GapicClass.create(Kind.TEST, classDef); List gapicClassWithHeaderList = - Composer.addApacheLicenseToGapicClassList(Arrays.asList(gapicClass)); + Composer.addApacheLicenseToGapicClassList( + Arrays.asList(GapicClass.create(Kind.TEST, classDef))); JavaWriterVisitor visitor = new JavaWriterVisitor(); gapicClassWithHeaderList.get(0).classDefinition().accept(visitor); assertEquals(visitor.write(), EXPECTED_CLASS_STRING); From 7a19396fddb54f71d50124f359b072fd03c654de Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Fri, 14 Aug 2020 11:03:01 -0700 Subject: [PATCH 21/23] feedback --- .../generator/gapic/composer/Composer.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/main/java/com/google/api/generator/gapic/composer/Composer.java b/src/main/java/com/google/api/generator/gapic/composer/Composer.java index 02f06359a9..bea6b8d1b8 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/Composer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/Composer.java @@ -112,19 +112,17 @@ private static GapicClass generateGenericClass(Kind kind, String name, Service s @VisibleForTesting protected static List addApacheLicenseToGapicClassList( List gapicClassList) { - gapicClassList = - gapicClassList.stream() - .map( - gapicClass -> { - ClassDefinition classWithHeader = - gapicClass - .classDefinition() - .toBuilder() - .setFileHeader(ApacheLicense.APACHE_LICENSE_COMMENT_STATEMENT) - .build(); - return GapicClass.create(gapicClass.kind(), classWithHeader); - }) - .collect(Collectors.toList()); - return gapicClassList; + return gapicClassList.stream() + .map( + gapicClass -> { + ClassDefinition classWithHeader = + gapicClass + .classDefinition() + .toBuilder() + .setFileHeader(ApacheLicense.APACHE_LICENSE_COMMENT_STATEMENT) + .build(); + return GapicClass.create(gapicClass.kind(), classWithHeader); + }) + .collect(Collectors.toList()); } } From 43f24c0ecb9240484cd369459c983f7ce86008cc Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Mon, 17 Aug 2020 17:14:35 -0700 Subject: [PATCH 22/23] feedback --- .../api/generator/engine/ast/ClassDefinition.java | 3 +++ .../api/generator/gapic/composer/Composer.java | 5 ++--- .../generator/engine/ast/ClassDefinitionTest.java | 15 +++++++++++++++ .../engine/writer/JavaWriterVisitorTest.java | 9 +++------ .../generator/gapic/composer/ComposerTest.java | 3 +-- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java b/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java index 8d59ba5ac5..8e9f58fbd4 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java +++ b/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java @@ -136,6 +136,9 @@ public ClassDefinition build() { Preconditions.checkState(!classDef.isStatic(), "Outer classes cannot be static"); Preconditions.checkState( !classDef.scope().equals(ScopeNode.PRIVATE), "Outer classes cannot be private"); + } else { + Preconditions.checkState( + classDef.fileHeader().isEmpty(), "Nested classes cannot have file header"); } // Abstract classes cannot be marked final. diff --git a/src/main/java/com/google/api/generator/gapic/composer/Composer.java b/src/main/java/com/google/api/generator/gapic/composer/Composer.java index bea6b8d1b8..15aed08d4d 100644 --- a/src/main/java/com/google/api/generator/gapic/composer/Composer.java +++ b/src/main/java/com/google/api/generator/gapic/composer/Composer.java @@ -38,7 +38,7 @@ public static List composeServiceClasses(GapicContext context) { clazzes.addAll(generateServiceClasses(service, context.messages())); } clazzes.addAll(generateResourceNameHelperClasses(context.helperResourceNames())); - return addApacheLicenseToGapicClassList(clazzes); + return addApacheLicense(clazzes); } public static List generateServiceClasses( @@ -110,8 +110,7 @@ private static GapicClass generateGenericClass(Kind kind, String name, Service s } @VisibleForTesting - protected static List addApacheLicenseToGapicClassList( - List gapicClassList) { + protected static List addApacheLicense(List gapicClassList) { return gapicClassList.stream() .map( gapicClass -> { diff --git a/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java b/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java index bafe033006..d2a0db7633 100644 --- a/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java +++ b/src/test/java/com/google/api/generator/engine/ast/ClassDefinitionTest.java @@ -119,6 +119,21 @@ public void validClassDefinition_statementsAndMethods() { // No exception thrown, we're good. } + @Test + public void invalidClassDefinition_nestedWithFileHeader() { + assertThrows( + IllegalStateException.class, + () -> { + ClassDefinition.builder() + .setPackageString("com.google.example.library.v1.stub") + .setName("LibraryServiceStub") + .setIsNested(true) + .setScope(ScopeNode.PUBLIC) + .setFileHeader(createFileHeader()) + .build(); + }); + } + @Test public void invalidClassDefinition_implementsNullType() { assertThrows( diff --git a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java index 64e40bb27a..3cc2beac3e 100644 --- a/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java +++ b/src/test/java/com/google/api/generator/engine/writer/JavaWriterVisitorTest.java @@ -1611,9 +1611,11 @@ public void writeMethodDefinition_templatedReturnTypeAndArguments() { @Test public void writeClassDefinition_basicWithFileHeader() { + List fileHeader = + Arrays.asList(CommentStatement.withComment(BlockComment.withComment("Apache License"))); ClassDefinition classDef = ClassDefinition.builder() - .setFileHeader(createFileHeader()) + .setFileHeader(fileHeader) .setPackageString("com.google.example.library.v1.stub") .setName("LibraryServiceStub") .setScope(ScopeNode.PUBLIC) @@ -1972,9 +1974,4 @@ private static String createSampleCode() { tryCatch.accept(writerVisitor); return writerVisitor.write(); } - - // Create a simple block comment to stand for the Apache License header. - private static List createFileHeader() { - return Arrays.asList(CommentStatement.withComment(BlockComment.withComment("Apache License"))); - } } diff --git a/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java b/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java index 6824676a9a..e5102b3211 100644 --- a/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java +++ b/src/test/java/com/google/api/generator/gapic/composer/ComposerTest.java @@ -35,8 +35,7 @@ public void gapicClass_addApacheLicense() { .setScope(ScopeNode.PUBLIC) .build(); List gapicClassWithHeaderList = - Composer.addApacheLicenseToGapicClassList( - Arrays.asList(GapicClass.create(Kind.TEST, classDef))); + Composer.addApacheLicense(Arrays.asList(GapicClass.create(Kind.TEST, classDef))); JavaWriterVisitor visitor = new JavaWriterVisitor(); gapicClassWithHeaderList.get(0).classDefinition().accept(visitor); assertEquals(visitor.write(), EXPECTED_CLASS_STRING); From e051381e715352d9189cb19508601df1e8178dac Mon Sep 17 00:00:00 2001 From: xiaozhenliugg Date: Tue, 18 Aug 2020 18:33:41 -0700 Subject: [PATCH 23/23] clean up --- .../com/google/api/generator/engine/ast/ClassDefinition.java | 2 +- .../com/google/api/generator/gapic/utils/ApacheLicense.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java b/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java index 8e9f58fbd4..65475b8d15 100644 --- a/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java +++ b/src/main/java/com/google/api/generator/engine/ast/ClassDefinition.java @@ -138,7 +138,7 @@ public ClassDefinition build() { !classDef.scope().equals(ScopeNode.PRIVATE), "Outer classes cannot be private"); } else { Preconditions.checkState( - classDef.fileHeader().isEmpty(), "Nested classes cannot have file header"); + classDef.fileHeader().isEmpty(), "Nested classes cannot have a file header"); } // Abstract classes cannot be marked final. diff --git a/src/main/java/com/google/api/generator/gapic/utils/ApacheLicense.java b/src/main/java/com/google/api/generator/gapic/utils/ApacheLicense.java index 1b6b5f0a73..00de28f7e5 100644 --- a/src/main/java/com/google/api/generator/gapic/utils/ApacheLicense.java +++ b/src/main/java/com/google/api/generator/gapic/utils/ApacheLicense.java @@ -20,7 +20,7 @@ import java.util.List; public class ApacheLicense { - private static final String fileHeadeString = + private static final String fileHeaderString = "Copyright 2020 Google LLC\n\n" + "Licensed under the Apache License, Version 2.0 (the \"License\");\n" + "you may not use this file except in compliance with the License.\n" @@ -33,5 +33,5 @@ public class ApacheLicense { + "limitations under the License."; public static final List APACHE_LICENSE_COMMENT_STATEMENT = - Arrays.asList(CommentStatement.withComment(BlockComment.withComment(fileHeadeString))); + Arrays.asList(CommentStatement.withComment(BlockComment.withComment(fileHeaderString))); }