-
Notifications
You must be signed in to change notification settings - Fork 53
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[ggj] feat: support file header (apache license) for outer class definition #157
Changes from 17 commits
4d48254
712a2b9
aecb241
35cf94c
d8d9cb1
4044ee4
85e87a4
44c0cf5
23813f8
18f6267
7b0eea2
c0d3be7
88d71cf
45dbadb
0f59ef8
2707ea1
0f34aec
6364167
590c7c6
a356da9
50c1fb1
31c0468
7ef4b4a
ede98cb
d59e1a3
7a19396
7879471
5eccd75
43f24c0
e051381
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -62,14 +62,15 @@ public class JavaWriterVisitor implements AstNodeVisitor { | |
|
||
private static final String COLON = ":"; | ||
private static final String COMMA = ","; | ||
private static final String BLOCK_COMMENT_START = "/**"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please separate the changes in #168 from this PR. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hold this PR until #168 is merged. |
||
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 = "\""; | ||
private static final String EQUALS = "="; | ||
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 -> { | ||
|
@@ -624,6 +629,8 @@ public void visit(MethodDefinition methodDefinition) { | |
@Override | ||
public void visit(ClassDefinition classDefinition) { | ||
if (!classDefinition.isNested()) { | ||
statements(classDefinition.fileHeader().stream().collect(Collectors.toList())); | ||
newline(); | ||
importWriterVisitor.initialize( | ||
classDefinition.packageString(), classDefinition.classIdentifier().name()); | ||
buffer.append(String.format("package %s;", classDefinition.packageString())); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looking at this again, maybe call this class "ApacheLicense" instead, since it doesn't do any composing? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since it's not a composer, how about moving it to |
||
private static final String fileHeadeString = | ||
"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."; | ||
|
||
public static final List<CommentStatement> APACHE_LICENSE = | ||
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
Arrays.asList(CommentStatement.withComment(BlockComment.withComment(fileHeadeString)));; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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.common.annotations.VisibleForTesting; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
@@ -85,7 +86,8 @@ public static List<GapicClass> 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); | ||
} | ||
|
@@ -100,6 +102,7 @@ private static GapicClass generateGenericClass(Kind kind, String name, Service s | |
|
||
ClassDefinition classDef = | ||
ClassDefinition.builder() | ||
.setFileHeader(ApacheLicenseComposer.APACHE_LICENSE) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We currently generate many classes, and this method is a placeholder that will be going away. Can you think of ways to restructure this such that we can add a license to a class and test that in isolation? How would we incorporate that into the existing classes? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sure, good point. Since we are creating different GapicClasses in the composers, I think a central place to add a file header is
Alternatives:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. GapicClass has no extra logic beyond holding data, so let's keep it that way. Let's add a helper method in Composer.java that adds license headers to each GapicClass instead. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. updated the |
||
.setPackageString(pakkage) | ||
.setName(name) | ||
.setScope(ScopeNode.PUBLIC) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
|
@@ -32,6 +32,7 @@ public void validClassDefinition_basicWithComments() { | |
"This class is for advanced usage and reflects the underlying API directly.") | ||
.build(); | ||
ClassDefinition.builder() | ||
.setFileHeader(createFileHeader()) | ||
.setHeaderCommentStatements( | ||
Arrays.asList( | ||
CommentStatement.withComment(lineComment), | ||
|
@@ -347,4 +348,8 @@ private static ForStatement createForStatement() { | |
.setBody(Arrays.asList(ExprStatement.withExpr(createAssignmentExpr()))) | ||
.build(); | ||
} | ||
// Create a simple block comment to stand for the Apache License header. | ||
private static List<CommentStatement> createFileHeader() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is just used once, do we need a helper method for it? Would prefer to just add the logic to the method where it's used. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated with adding a unit test (nestClassWithFileHeader) which also uses this. So I guess we can keep it. But this similar helper is only called once in JavaWriterVisitorTest, so removed it there. Thanks |
||
return Arrays.asList(CommentStatement.withComment(BlockComment.withComment("Apache License"))); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// 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.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; | ||
|
||
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<String, Message> messageTypes = Parser.parseMessages(echoFileDescriptor); | ||
Map<String, ResourceName> resourceNames = Parser.parseResourceNames(echoFileDescriptor); | ||
Set<ResourceName> outputResourceNames = new HashSet<>(); | ||
|
||
List<Service> services = | ||
Parser.parseService(echoFileDescriptor, messageTypes, resourceNames, outputResourceNames); | ||
Service echoProtoService = services.get(0); | ||
JavaWriterVisitor visitor = new JavaWriterVisitor(); | ||
Composer.generateStubServiceSettings(echoProtoService).classDefinition().accept(visitor); | ||
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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"; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to error-out in
build()
if this is a nested class?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, added one condition check in
build
to confirm nested classes do not have file headers.