Import upstream version 1.13.0+git20210509.1.8851788
Debian Janitor
2 years ago
0 | #!/bin/bash | |
1 | # | |
2 | # Deploy a jar, source jar, and javadoc jar to Sonatype's snapshot repo. | |
3 | # | |
4 | # Adapted from https://coderwall.com/p/9b_lfq and | |
5 | # https://benlimmer.com/2013/12/26/automatically-publish-javadoc-to-gh-pages-with-travis-ci/ | |
6 | ||
7 | SLUG="square/javapoet" | |
8 | JDK="openjdk8" | |
9 | BRANCH="master" | |
10 | ||
11 | set -e | |
12 | ||
13 | if [ "$TRAVIS_REPO_SLUG" != "$SLUG" ]; then | |
14 | echo "Skipping snapshot deployment: wrong repository. Expected '$SLUG' but was '$TRAVIS_REPO_SLUG'." | |
15 | elif [ "$TRAVIS_JDK_VERSION" != "$JDK" ]; then | |
16 | echo "Skipping snapshot deployment: wrong JDK. Expected '$JDK' but was '$TRAVIS_JDK_VERSION'." | |
17 | elif [ "$TRAVIS_PULL_REQUEST" != "false" ]; then | |
18 | echo "Skipping snapshot deployment: was pull request." | |
19 | elif [ "$TRAVIS_BRANCH" != "$BRANCH" ]; then | |
20 | echo "Skipping snapshot deployment: wrong branch. Expected '$BRANCH' but was '$TRAVIS_BRANCH'." | |
21 | else | |
22 | echo "Deploying snapshot..." | |
23 | mvn clean source:jar javadoc:jar deploy --settings=".buildscript/settings.xml" -Dmaven.test.skip=true | |
24 | echo "Snapshot deployed!" | |
25 | fi |
0 | <settings> | |
1 | <servers> | |
2 | <server> | |
3 | <id>sonatype-nexus-snapshots</id> | |
4 | <username>${env.CI_DEPLOY_USERNAME}</username> | |
5 | <password>${env.CI_DEPLOY_PASSWORD}</password> | |
6 | </server> | |
7 | </servers> | |
8 | </settings> |
0 | Contributing | |
1 | ============ | |
2 | ||
3 | If you would like to contribute code you can do so through GitHub by forking | |
4 | the repository and sending a pull request. | |
5 | ||
6 | When submitting code, please make every effort to follow existing conventions | |
7 | and style in order to keep the code as readable as possible. Please also make | |
8 | sure your code compiles by running `mvn clean verify`. Checkstyle failures | |
9 | during compilation indicate errors in your style and can be viewed in the | |
10 | `checkstyle-result.xml` file. | |
11 | ||
12 | Before your code can be accepted into the project you must also sign the | |
13 | [Individual Contributor License Agreement (CLA)][1]. | |
14 | ||
15 | ||
16 | [1]: https://spreadsheets.google.com/spreadsheet/viewform?formkey=dDViT2xzUHAwRkI3X3k5Z0lQM091OGc6MQ&ndplr=1 |
0 | name: build | |
1 | ||
2 | on: [push, pull_request] | |
3 | ||
4 | jobs: | |
5 | jvm: | |
6 | runs-on: ubuntu-latest | |
7 | ||
8 | strategy: | |
9 | fail-fast: false | |
10 | matrix: | |
11 | java-version: | |
12 | - 1.8 | |
13 | - 9 | |
14 | - 10 | |
15 | # TODO: | |
16 | #- 11 | |
17 | #- 12 | |
18 | #- 13 | |
19 | #- 14 | |
20 | #- 15 | |
21 | #- 16 | |
22 | ||
23 | steps: | |
24 | - uses: actions/checkout@v2 | |
25 | ||
26 | - uses: actions/setup-java@v2 | |
27 | with: | |
28 | distribution: 'zulu' | |
29 | java-version: ${{ matrix.java-version }} | |
30 | ||
31 | - run: mvn --no-transfer-progress verify source:jar javadoc:jar | |
32 | ||
33 | - run: mvn --no-transfer-progress deploy --settings=".github/workflows/settings.xml" -Dmaven.test.skip=true | |
34 | if: ${{ github.ref == 'refs/heads/master' && github.repository == 'square/javapoet' && matrix.java-version == '1.8' }} |
0 | <settings> | |
1 | <servers> | |
2 | <server> | |
3 | <id>sonatype-nexus-snapshots</id> | |
4 | <username>${env.SONATYPE_DEPLOY_USERNAME}</username> | |
5 | <password>${env.SONATYPE_DEPLOY_PASSWORD}</password> | |
6 | </server> | |
7 | </servers> | |
8 | </settings> |
0 | language: java | |
1 | ||
2 | matrix: | |
3 | include: | |
4 | - jdk: openjdk8 | |
5 | - jdk: openjdk11 | |
6 | allow_failures: | |
7 | - jdk: openjdk11 | |
8 | ||
9 | before_install: | |
10 | - unset _JAVA_OPTIONS | |
11 | ||
12 | after_success: | |
13 | - .buildscript/deploy_snapshot.sh | |
14 | ||
15 | env: | |
16 | global: | |
17 | - secure: "nkVNCk8H2orIZOmow0t+Qub1lFQCYpJgNZf17zYI5x0JVqQNCqkcTYYDHqzwkvkmixXFCrfYZQuXy7x2qg9zjCX+vmhlmiMWwe8dNa34OLTseuuR2irS0C8nRGRYxKM7EGenRZSqbFVUksKRm2iWnHKxtmCzeDaS7MoMit2wdUo=" | |
18 | - secure: "j8+hPaZnyM+UlOBYOEA96fPbVWbN6bMQ28SGQnFMwxo2axHi9ww9Au1N7002HzHnxX8iyesdWFBigArnEL8zKEoXH9Bmur0sn3Ys4bu72C3ozscP4cjXfYSHj8aVLp1EIMdQPDF7MkCccx9l7ONdsW0ltmdiVUtDxzqkH+63WLU=" | |
19 | ||
20 | branches: | |
21 | except: | |
22 | - gh-pages | |
23 | ||
24 | notifications: | |
25 | email: false | |
26 | ||
27 | cache: | |
28 | directories: | |
29 | - $HOME/.m2 |
0 | Contributing | |
1 | ============ | |
2 | ||
3 | If you would like to contribute code you can do so through GitHub by forking | |
4 | the repository and sending a pull request. | |
5 | ||
6 | When submitting code, please make every effort to follow existing conventions | |
7 | and style in order to keep the code as readable as possible. Please also make | |
8 | sure your code compiles by running `mvn clean verify`. Checkstyle failures | |
9 | during compilation indicate errors in your style and can be viewed in the | |
10 | `checkstyle-result.xml` file. | |
11 | ||
12 | Before your code can be accepted into the project you must also sign the | |
13 | [Individual Contributor License Agreement (CLA)][1]. | |
14 | ||
15 | ||
16 | [1]: https://spreadsheets.google.com/spreadsheet/viewform?formkey=dDViT2xzUHAwRkI3X3k5Z0lQM091OGc6MQ&ndplr=1 |
891 | 891 | <dependency> |
892 | 892 | <groupId>com.squareup</groupId> |
893 | 893 | <artifactId>javapoet</artifactId> |
894 | <version>1.12.1</version> | |
894 | <version>1.13.0</version> | |
895 | 895 | </dependency> |
896 | 896 | ``` |
897 | 897 | or Gradle: |
898 | 898 | ```groovy |
899 | compile 'com.squareup:javapoet:1.12.1' | |
899 | compile 'com.squareup:javapoet:1.13.0' | |
900 | 900 | ``` |
901 | 901 | |
902 | 902 | Snapshots of the development version are available in [Sonatype's `snapshots` repository][snap]. |
0 | Releasing | |
1 | ========= | |
2 | ||
3 | 1. Update the CHANGELOG.md for the impending release. | |
4 | 2. Update the README.md with the new version. | |
5 | 3. `git commit -am "Update changelog for X.Y.Z."` (where X.Y.Z is the new version). | |
6 | 4. `mvn-release`. | |
7 | * `What is the release version for "JavaPoet"? (com.squareup.javapoet) X.Y.Z:` - hit Enter. | |
8 | * `What is SCM release tag or label for "JavaPoet"? (com.squareup.javapoet) javapoet-X.Y.Z:` - hit Enter. | |
9 | * `What is the new development version for "JavaPoet"? (com.squareup.javapoet) X.Y.(Z + 1)-SNAPSHOT:` - enter `X.(Y + 1).0-SNAPSHOT`. | |
10 | * Enter your GPG Passphrase when prompted. | |
11 | 5. Visit Sonatype Nexus and promote the artifact. | |
12 | ||
13 | If step 4 or 5 fails: | |
14 | ||
15 | * Drop the Sonatype repo, | |
16 | * Fix the problem, | |
17 | * Manully revert the version change in `pom.xml` made by `mvn-release`, | |
18 | * Commit, | |
19 | * And start again at step 4. | |
20 | ||
21 | Prerequisites | |
22 | ------------- | |
23 | ||
24 | In `~/.m2/settings.xml`, set the following: | |
25 | ||
26 | ```xml | |
27 | <settings> | |
28 | <servers> | |
29 | <server> | |
30 | <id>sonatype-nexus-staging</id> | |
31 | <username>your-nexus-username</username> | |
32 | <password>your-nexus-password</password> | |
33 | </server> | |
34 | </servers> | |
35 | </settings> | |
36 | ``` | |
37 | ||
38 | In your shell's `.rc` file, set the following: | |
39 | ||
40 | ``` | |
41 | alias mvn-release='mvn clean source:jar javadoc:jar verify && mvn clean release:clean && mvn release:prepare release:perform' | |
42 | ``` | |
43 | ||
44 | Refer to the [GPG Keys][gpg_keys] guide if you need to set up GPG keys for signing. | |
45 | ||
46 | [gpg_keys]: https://square.github.io/okio/releasing/#prerequisite-gpg-keys |
10 | 10 | |
11 | 11 | <groupId>com.squareup</groupId> |
12 | 12 | <artifactId>javapoet</artifactId> |
13 | <version>1.13.0</version> | |
13 | <version>1.14.0-SNAPSHOT</version> | |
14 | 14 | |
15 | 15 | <name>JavaPoet</name> |
16 | 16 | <description>Use beautiful Java code to generate beautiful Java code.</description> |
20 | 20 | <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> |
21 | 21 | |
22 | 22 | <java.version>1.8</java.version> |
23 | <junit.version>4.12</junit.version> | |
23 | <junit.version>4.13.1</junit.version> | |
24 | 24 | <truth.version>0.39</truth.version> |
25 | 25 | <compile-testing.version>0.15</compile-testing.version> |
26 | 26 | </properties> |
26 | 26 | import java.util.Map; |
27 | 27 | import java.util.Objects; |
28 | 28 | import java.util.Set; |
29 | import java.util.regex.Pattern; | |
29 | 30 | import javax.lang.model.SourceVersion; |
30 | 31 | import javax.lang.model.element.Modifier; |
31 | 32 | |
42 | 43 | final class CodeWriter { |
43 | 44 | /** Sentinel value that indicates that no user-provided package has been set. */ |
44 | 45 | private static final String NO_PACKAGE = new String(); |
46 | private static final Pattern LINE_BREAKING_PATTERN = Pattern.compile("\\R"); | |
45 | 47 | |
46 | 48 | private final String indent; |
47 | 49 | private final LineWrapper out; |
468 | 470 | */ |
469 | 471 | CodeWriter emitAndIndent(String s) throws IOException { |
470 | 472 | boolean first = true; |
471 | for (String line : s.split("\\R", -1)) { | |
473 | for (String line : LINE_BREAKING_PATTERN.split(s, -1)) { | |
472 | 474 | // Emit a newline character. Make sure blank lines in Javadoc & comments look good. |
473 | 475 | if (!first) { |
474 | 476 | if ((javadoc || comment) && trailingNewline) { |
262 | 262 | codeWriter.pushType(this); |
263 | 263 | codeWriter.indent(); |
264 | 264 | boolean firstMember = true; |
265 | boolean needsSeparator = kind == Kind.ENUM | |
266 | && (!fieldSpecs.isEmpty() || !methodSpecs.isEmpty() || !typeSpecs.isEmpty()); | |
265 | 267 | for (Iterator<Map.Entry<String, TypeSpec>> i = enumConstants.entrySet().iterator(); |
266 | 268 | i.hasNext(); ) { |
267 | 269 | Map.Entry<String, TypeSpec> enumConstant = i.next(); |
270 | 272 | firstMember = false; |
271 | 273 | if (i.hasNext()) { |
272 | 274 | codeWriter.emit(",\n"); |
273 | } else if (!fieldSpecs.isEmpty() || !methodSpecs.isEmpty() || !typeSpecs.isEmpty()) { | |
274 | codeWriter.emit(";\n"); | |
275 | } else { | |
275 | } else if (!needsSeparator) { | |
276 | 276 | codeWriter.emit("\n"); |
277 | 277 | } |
278 | 278 | } |
279 | ||
280 | if (needsSeparator) codeWriter.emit(";\n"); | |
279 | 281 | |
280 | 282 | // Static fields. |
281 | 283 | for (FieldSpec fieldSpec : fieldSpecs) { |
762 | 764 | } |
763 | 765 | } |
764 | 766 | |
765 | checkArgument(kind != Kind.ENUM || !enumConstants.isEmpty(), | |
766 | "at least one enum constant is required for %s", name); | |
767 | ||
768 | 767 | for (TypeName superinterface : superinterfaces) { |
769 | 768 | checkArgument(superinterface != null, "superinterfaces contains null"); |
770 | 769 | } |
795 | 794 | |
796 | 795 | for (MethodSpec methodSpec : methodSpecs) { |
797 | 796 | if (kind == Kind.INTERFACE) { |
798 | requireExactlyOneOf(methodSpec.modifiers, Modifier.ABSTRACT, Modifier.STATIC, | |
799 | Modifier.DEFAULT); | |
800 | 797 | requireExactlyOneOf(methodSpec.modifiers, Modifier.PUBLIC, Modifier.PRIVATE); |
798 | if (methodSpec.modifiers.contains(Modifier.PRIVATE)) { | |
799 | checkState(!methodSpec.hasModifier(Modifier.DEFAULT), | |
800 | "%s %s.%s cannot be private and default", kind, name, methodSpec.name); | |
801 | checkState(!methodSpec.hasModifier(Modifier.ABSTRACT), | |
802 | "%s %s.%s cannot be private and abstract", kind, name, methodSpec.name); | |
803 | } else { | |
804 | requireExactlyOneOf(methodSpec.modifiers, Modifier.ABSTRACT, Modifier.STATIC, | |
805 | Modifier.DEFAULT); | |
806 | } | |
801 | 807 | } else if (kind == Kind.ANNOTATION) { |
802 | 808 | checkState(methodSpec.modifiers.equals(kind.implicitMethodModifiers), |
803 | 809 | "%s %s.%s requires modifiers %s", |
434 | 434 | + "}\n"); |
435 | 435 | } |
436 | 436 | |
437 | @Test public void enumConstantsRequired() throws Exception { | |
438 | try { | |
439 | TypeSpec.enumBuilder("Roshambo") | |
440 | .build(); | |
441 | fail(); | |
442 | } catch (IllegalArgumentException expected) { | |
443 | } | |
437 | @Test public void noEnumConstants() throws Exception { | |
438 | TypeSpec roshambo = TypeSpec.enumBuilder("Roshambo") | |
439 | .addField(String.class, "NO_ENUM", Modifier.STATIC) | |
440 | .build(); | |
441 | assertThat(toString(roshambo)).isEqualTo("" | |
442 | + "package com.squareup.tacos;\n" | |
443 | + "\n" | |
444 | + "import java.lang.String;\n" | |
445 | + "\n" | |
446 | + "enum Roshambo {\n" | |
447 | + " ;\n" | |
448 | + " static String NO_ENUM;\n" | |
449 | + "}\n"); | |
444 | 450 | } |
445 | 451 | |
446 | 452 | @Test public void onlyEnumsMayHaveEnumConstants() throws Exception { |
897 | 903 | + "\n" |
898 | 904 | + "interface Tacos {\n" |
899 | 905 | + " default int test() {\n" |
906 | + " return 0;\n" | |
907 | + " }\n" | |
908 | + "}\n" | |
909 | ); | |
910 | } | |
911 | ||
912 | @Test | |
913 | public void invalidInterfacePrivateMethods() { | |
914 | try { | |
915 | TypeSpec.interfaceBuilder("Tacos") | |
916 | .addMethod(MethodSpec.methodBuilder("test") | |
917 | .addModifiers(Modifier.PRIVATE, Modifier.DEFAULT) | |
918 | .returns(int.class) | |
919 | .addCode(CodeBlock.builder().addStatement("return 0").build()) | |
920 | .build()) | |
921 | .build(); | |
922 | fail(); | |
923 | } catch (IllegalStateException expected) { | |
924 | } | |
925 | ||
926 | try { | |
927 | TypeSpec.interfaceBuilder("Tacos") | |
928 | .addMethod(MethodSpec.methodBuilder("test") | |
929 | .addModifiers(Modifier.PRIVATE, Modifier.ABSTRACT) | |
930 | .returns(int.class) | |
931 | .build()) | |
932 | .build(); | |
933 | fail(); | |
934 | } catch (IllegalStateException expected) { | |
935 | } | |
936 | ||
937 | try { | |
938 | TypeSpec.interfaceBuilder("Tacos") | |
939 | .addMethod(MethodSpec.methodBuilder("test") | |
940 | .addModifiers(Modifier.PRIVATE, Modifier.PUBLIC) | |
941 | .returns(int.class) | |
942 | .addCode(CodeBlock.builder().addStatement("return 0").build()) | |
943 | .build()) | |
944 | .build(); | |
945 | fail(); | |
946 | } catch (IllegalArgumentException expected) { | |
947 | } | |
948 | } | |
949 | ||
950 | @Test | |
951 | public void interfacePrivateMethods() { | |
952 | TypeSpec bar = TypeSpec.interfaceBuilder("Tacos") | |
953 | .addMethod(MethodSpec.methodBuilder("test") | |
954 | .addModifiers(Modifier.PRIVATE) | |
955 | .returns(int.class) | |
956 | .addCode(CodeBlock.builder().addStatement("return 0").build()) | |
957 | .build()) | |
958 | .build(); | |
959 | ||
960 | assertThat(toString(bar)).isEqualTo("" | |
961 | + "package com.squareup.tacos;\n" | |
962 | + "\n" | |
963 | + "interface Tacos {\n" | |
964 | + " private int test() {\n" | |
965 | + " return 0;\n" | |
966 | + " }\n" | |
967 | + "}\n" | |
968 | ); | |
969 | ||
970 | bar = TypeSpec.interfaceBuilder("Tacos") | |
971 | .addMethod(MethodSpec.methodBuilder("test") | |
972 | .addModifiers(Modifier.PRIVATE, Modifier.STATIC) | |
973 | .returns(int.class) | |
974 | .addCode(CodeBlock.builder().addStatement("return 0").build()) | |
975 | .build()) | |
976 | .build(); | |
977 | ||
978 | assertThat(toString(bar)).isEqualTo("" | |
979 | + "package com.squareup.tacos;\n" | |
980 | + "\n" | |
981 | + "interface Tacos {\n" | |
982 | + " private static int test() {\n" | |
900 | 983 | + " return 0;\n" |
901 | 984 | + " }\n" |
902 | 985 | + "}\n" |