-
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 29 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 |
---|---|---|
@@ -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.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 ApacheLicense { | ||
private static final String fileHeadeString = | ||
xiaozhenliu-gg5 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"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_COMMENT_STATEMENT = | ||
Arrays.asList(CommentStatement.withComment(BlockComment.withComment(fileHeadeString))); | ||
} |
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), | ||
|
@@ -118,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( | ||
|
@@ -347,4 +363,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,62 @@ | ||
// 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.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 ComposerTest { | ||
@Test | ||
public void gapicClass_addApacheLicense() { | ||
ClassDefinition classDef = | ||
ClassDefinition.builder() | ||
.setPackageString("com.google.showcase.v1beta1.stub") | ||
.setName("EchoStubSettings") | ||
.setScope(ScopeNode.PUBLIC) | ||
.build(); | ||
List<GapicClass> gapicClassWithHeaderList = | ||
Composer.addApacheLicense(Arrays.asList(GapicClass.create(Kind.TEST, classDef))); | ||
JavaWriterVisitor visitor = new JavaWriterVisitor(); | ||
gapicClassWithHeaderList.get(0).classDefinition().accept(visitor); | ||
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.