Compare commits

...

153 Commits

Author SHA1 Message Date
Ivan Babiankou
db3cf7d0b8 very last try 2022-08-05 17:39:42 +02:00
Ivan Babiankou
50db75df52 Hmm, what is the difference? 2022-08-05 17:04:13 +02:00
Ivan Babiankou
8ca6982154 Revert performance tweaks 2022-08-05 16:57:47 +02:00
Ivan Babiankou
b8962fdba6 GIVE ME MORE 2022-08-05 16:41:53 +02:00
Ivan Babiankou
3fd83852fb Drop -Xmx 2022-08-05 16:29:53 +02:00
Ivan Babiankou
0ed60fe726 more builds 2022-08-05 16:20:45 +02:00
Ivan Babiankou
6fc4c8c43c trigger more builds 2022-08-05 16:20:08 +02:00
Ivan Babiankou
3921884481 build this branch 2022-08-05 16:01:24 +02:00
Stephan Schroevers
10369e2624 Speed up the build
The build is sped up in two ways:
1. By tweaking the JVM flags passed to the main process.
2. By tweaking the JVM flags passed to the forked Surefire processes.

(cherry picked from commit cf34951eff29d73d6d1aa99679e205e50a28586a)
2022-08-05 16:00:42 +02:00
Rick Ossendrijver
708c8affcf Apply suggestions 2022-08-05 16:00:42 +02:00
Pieter Dirk Soels
0fa9541db6 Remove argument to print versions as we already did so in another step 2022-08-05 16:00:42 +02:00
Stephan Schroevers
5b261045d1 More cleanup 2022-08-05 16:00:42 +02:00
Stephan Schroevers
8be8730fd4 Build all PR branches 2022-08-05 16:00:42 +02:00
Stephan Schroevers
b12b2e8ebc Rename file, run parallel build 2022-08-05 16:00:42 +02:00
Stephan Schroevers
13567d23e4 Suggestions 2022-08-05 16:00:42 +02:00
Rick Ossendrijver
78cabb58c1 PSM-1324 Apply suggestion 2022-08-05 16:00:42 +02:00
Rick Ossendrijver
812787544a PSM-1324 Apply feedback 2022-08-05 16:00:42 +02:00
Rick Ossendrijver
f35a1fba53 Migrate from Travis CI to GitHub Actions 2022-08-05 16:00:42 +02:00
Picnic-Bot
78035644dc Upgrade NullAway 0.9.8 -> 0.9.9 (#175)
See:
- https://github.com/uber/NullAway/blob/master/CHANGELOG.md
- https://github.com/uber/NullAway/compare/v0.9.8...v0.9.9
2022-08-05 09:10:49 +02:00
Picnic-Bot
ef67d41512 Upgrade pitest-maven-plugin 1.9.2 -> 1.9.3 (#159)
See:
- https://github.com/hcoles/pitest/releases/tag/1.9.3
- https://github.com/hcoles/pitest/compare/1.9.2...1.9.3
2022-08-04 16:53:54 +02:00
Picnic-Bot
4cecff923a Upgrade Checker Framework Annotations 3.23.0 -> 3.24.0 (#174)
See:
- https://github.com/typetools/checker-framework/releases/tag/checker-framework-3.24.0
- https://github.com/typetools/checker-framework/compare/checker-framework-3.23.0...checker-framework-3.24.0
2022-08-04 08:10:10 +02:00
Stephan Schroevers
38a57db994 Introduce ErrorProneTestHelperSourceFormat check (#147)
This new checker inspects inline code passed to `CompilationTestHelper` and
`BugCheckerRefactoringTestHelper` instances. It requires that this code is
properly formatted and that its imports are organized. Only code that
represents the expected output of a refactoring operation is allowed to have
unused imports, as most `BugChecker`s do not (and are not able to) remove
imports that become obsolete as a result of applying their suggested fix(es).
2022-08-03 21:47:31 +02:00
Stephan Schroevers
336557cf8e Introduce apply-error-prone-suggestions.sh (#171)
This script compiles the code using Error Prone and applies its suggestions. 
The set of checks applied can optionally be restricted by name.
2022-08-03 17:05:38 +02:00
Picnic-Bot
9055dfff19 Upgrade Checkstyle 10.3.1 -> 10.3.2 (#173)
See:
- https://checkstyle.sourceforge.io/releasenotes.html
- https://github.com/checkstyle/checkstyle/releases/tag/checkstyle-10.3.2
- https://github.com/checkstyle/checkstyle/compare/checkstyle-10.3.1...checkstyle-10.3.2
2022-08-02 12:26:40 +02:00
Rick Ossendrijver
3712a15195 Implement workaround for MCOMPILER-503 (#149)
By moving around some annotation processor classpath entries we ensure that the
`maven-compiler-plugin` uses the appropriate version of Error Prone.

See https://issues.apache.org/jira/browse/MCOMPILER-503
2022-08-02 09:36:46 +02:00
Picnic-Bot
9d487e4a88 Upgrade New Relic Java Agent 7.8.0 -> 7.9.0 (#168)
See:
- https://github.com/newrelic/newrelic-java-agent/releases/tag/v7.9.0
- https://github.com/newrelic/newrelic-java-agent/compare/v7.8.0...v7.9.0
2022-08-01 17:35:58 +02:00
Picnic-Bot
b2b086761c Upgrade JUnit Jupiter 5.8.2 -> 5.9.0 (#167)
See:
- https://junit.org/junit5/docs/current/release-notes/index.html
- https://github.com/junit-team/junit5/releases/tag/r5.9.0
- https://github.com/junit-team/junit5/compare/r5.8.2...r5.9.0
2022-08-01 17:27:08 +02:00
Picnic-Bot
ff64247b6d Upgrade maven-resources-plugin 3.2.0 -> 3.3.0 (#166)
See:
- https://issues.apache.org/jira/issues/?jql=project%20%3D%20MRESOURCES%20AND%20fixVersion%20%3E%203.2.0%20AND%20fixVersion%20%3C%3D%203.3.0
- https://github.com/apache/maven-resources-plugin/compare/maven-resources-plugin-3.2.0...maven-resources-plugin-3.3.0
2022-08-01 17:15:19 +02:00
Picnic-Bot
bc7443c72d Upgrade maven-install-plugin 3.0.0 -> 3.0.1 (#164)
See:
- https://issues.apache.org/jira/issues/?jql=project%20%3D%20MINSTALL%20AND%20fixVersion%20%3E%20%203.0.0%20AND%20fixVersion%20%3C%3D%203.0.1
- https://github.com/apache/maven-install-plugin/compare/maven-install-plugin-3.0.0...maven-install-plugin-3.0.1
2022-08-01 16:21:54 +02:00
Picnic-Bot
abf4d68fba Upgrade extra-enforcer-rules 1.6.0 -> 1.6.1 (#165)
See:
- https://github.com/mojohaus/extra-enforcer-rules/releases/tag/extra-enforcer-rules-1.6.1
- https://github.com/mojohaus/extra-enforcer-rules/compare/extra-enforcer-rules-1.6.0...extra-enforcer-rules-1.6.1
2022-08-01 16:20:24 +02:00
Picnic-Bot
5c5f7d849e Upgrade Guava 31.0.1-jre -> 31.1-jre (#77)
See:
- https://guava.dev/releases/31.1-jre/api/diffs/
- https://github.com/google/guava/releases/tag/v31.1
- https://github.com/google/guava/compare/v31.0.1...v31.1
2022-08-01 14:48:56 +02:00
Picnic-Bot
65c4694936 Upgrade Error Prone 2.10.0 -> 2.14.0 (#76)
This also requires the upgrade of errorprone-slf4j 0.1.4 -> 0.1.12.

See:
- https://github.com/google/error-prone/releases/tag/v2.11.0
- https://github.com/google/error-prone/releases/tag/v2.12.0
- https://github.com/google/error-prone/releases/tag/v2.12.1
- https://github.com/google/error-prone/releases/tag/v2.13.0
- https://github.com/google/error-prone/releases/tag/v2.13.1
- https://github.com/google/error-prone/releases/tag/v2.14.0
- https://github.com/google/error-prone/compare/v2.10.0...v2.14.0
- https://github.com/PicnicSupermarket/error-prone/compare/v2.10.0-picnic-3...v2.14.0-picnic-2
- https://github.com/KengoTODA/errorprone-slf4j/releases/tag/v0.1.5
- https://github.com/KengoTODA/errorprone-slf4j/releases/tag/v0.1.6
- https://github.com/KengoTODA/errorprone-slf4j/releases/tag/v0.1.7
- https://github.com/KengoTODA/errorprone-slf4j/releases/tag/v0.1.8
- https://github.com/KengoTODA/errorprone-slf4j/releases/tag/v0.1.9
- https://github.com/KengoTODA/errorprone-slf4j/releases/tag/v0.1.10
- https://github.com/KengoTODA/errorprone-slf4j/releases/tag/v0.1.11
- https://github.com/KengoTODA/errorprone-slf4j/releases/tag/v0.1.12
- https://github.com/KengoTODA/errorprone-slf4j/compare/v0.1.4...v0.1.12
2022-07-31 15:50:16 +02:00
Picnic-Bot
a45291c7d8 Upgrade Spring Boot 2.7.1 -> 2.7.2 (#163)
See:
- https://github.com/spring-projects/spring-boot/releases/tag/v2.7.2
- https://github.com/spring-projects/spring-boot/compare/v2.7.1...v2.7.2
2022-07-30 15:11:37 +02:00
Picnic-Bot
71012f31ab Upgrade swagger-annotations 2.2.1 -> 2.2.2 (#162)
See:
- https://github.com/swagger-api/swagger-core/releases/tag/v2.2.2
- https://github.com/swagger-api/swagger-core/compare/v2.2.1...v2.2.2
2022-07-30 14:43:54 +02:00
Picnic-Bot
6e0905c033 Upgrade maven-install-plugin 2.5.2 -> 3.0.0 (#161)
See: 
- https://issues.apache.org/jira/issues/?jql=project%20%3D%20MINSTALL%20AND%20fixVersion%20%3E%20%202.5.2%20AND%20fixVersion%20%3C%3D%203.0.0
- https://github.com/apache/maven-install-plugin/compare/maven-install-plugin-2.5.2...maven-install-plugin-3.0.0
2022-07-21 09:01:43 +02:00
Picnic-Bot
af5ac85428 Upgrade maven-deploy-plugin 2.8.2 -> 3.0.0 (#160)
See:
- https://issues.apache.org/jira/issues/?jql=project%20%3D%20MDEPLOY%20AND%20fixVersion%20%3E%202.8.2%20AND%20fixVersion%20%3C%3D%203.0.0
- https://github.com/apache/maven-deploy-plugin/compare/maven-deploy-plugin-2.8.2...maven-deploy-plugin-3.0.0
2022-07-21 08:49:42 +02:00
Picnic-Bot
0329c25f78 Upgrade sortpom-maven-plugin 3.1.3 -> 3.2.0 (#157)
See:
- https://github.com/Ekryd/sortpom/wiki/Versions
- https://github.com/Ekryd/sortpom/releases/tag/sortpom-parent-3.2.0
- https://github.com/Ekryd/sortpom/compare/sortpom-parent-3.1.3...sortpom-parent-3.2.0
2022-07-21 08:04:22 +02:00
Picnic-Bot
9e67e2b795 Upgrade Spring 5.3.21 -> 5.3.22 (#155)
See:
- https://github.com/spring-projects/spring-framework/releases/tag/v5.3.22
- https://github.com/spring-projects/spring-framework/compare/v5.3.21...v5.3.22
2022-07-15 14:52:27 +02:00
Picnic-Bot
4bafea05f4 Upgrade Project Reactor 2020.0.20 -> 2020.0.21 (#154)
See:
- https://github.com/reactor/reactor/releases/tag/2020.0.21
- https://github.com/reactor/reactor/compare/2020.0.20...2020.0.21
2022-07-15 08:46:21 +02:00
Picnic-Bot
8ce9cab2dd Upgrade Checker Framework Annotations 3.22.2 -> 3.23.0 (#153)
See:
- https://github.com/typetools/checker-framework/releases/tag/checker-framework-3.23.0
- https://github.com/typetools/checker-framework/compare/checker-framework-3.22.2...checker-framework-3.23.0
2022-07-13 07:34:25 +02:00
Pieter Dirk Soels
ae30625524 Clarify MapToNullable Refaster template (#150) 2022-07-12 12:54:22 +02:00
Picnic-Bot
dc0046ebfc Upgrade extra-enforcer-rules 1.5.1 -> 1.6.0 (#152)
See:
- https://github.com/mojohaus/extra-enforcer-rules/releases/tag/extra-enforcer-rules-1.6.0
- https://github.com/mojohaus/extra-enforcer-rules/compare/extra-enforcer-rules-1.5.1...extra-enforcer-rules-1.6.0
2022-07-12 11:41:23 +02:00
Picnic-Bot
ad6d774818 Upgrade pitest-maven-plugin 1.9.0 -> 1.9.2 (#151)
See:
- https://github.com/hcoles/pitest/releases/tag/1.9.1
- https://github.com/hcoles/pitest/releases/tag/1.9.2
- https://github.com/hcoles/pitest/compare/1.9.0...1.9.2
2022-07-12 11:11:30 +02:00
Stephan Schroevers
405f5874ac Apply Checkstyle to Refaster test resources (#146)
Similar to formatting the `*TemplatesTest{In,Out}put.java` files to enforce
certain style aspects, this is expected to reduce future code review effort.

The test code changes were applied to appease the
`SimplifyBooleanExpression` check.
2022-07-06 13:39:59 +02:00
Stephan Schroevers
bf5199ea3d Fix build-time Refaster template loading (#121)
When using the system classloader, `RefasterCheckTest` is able to
successfully load the Refaster templates from the classpath, causing the
tests to pass. However, when during a build the Java compiler loads
`RefasterCheck`, the templates on the annotation processor classpath are
_not_ exposed through the system classloader.
2022-06-30 13:30:03 +02:00
Gijs de Jong
c500516bb4 Prefer AssertJ's isNull() assertion over isEqualTo(null) (#133) 2022-06-29 18:55:22 +02:00
Picnic-Bot
85d68a4f34 Upgrade Spring Boot 2.5.14 -> 2.7.1 (#81)
See:
- https://github.com/spring-projects/spring-boot/releases/tag/v2.6.0-M1
- https://github.com/spring-projects/spring-boot/releases/tag/v2.6.0-M2
- https://github.com/spring-projects/spring-boot/releases/tag/v2.6.0-M3
- https://github.com/spring-projects/spring-boot/releases/tag/v2.6.0-RC1
- https://github.com/spring-projects/spring-boot/releases/tag/v2.6.0
- https://github.com/spring-projects/spring-boot/releases/tag/v2.6.1
- https://github.com/spring-projects/spring-boot/releases/tag/v2.6.2
- https://github.com/spring-projects/spring-boot/releases/tag/v2.6.3
- https://github.com/spring-projects/spring-boot/releases/tag/v2.6.4
- https://github.com/spring-projects/spring-boot/releases/tag/v2.6.5
- https://github.com/spring-projects/spring-boot/releases/tag/v2.6.6
- https://github.com/spring-projects/spring-boot/releases/tag/v2.6.7
- https://github.com/spring-projects/spring-boot/releases/tag/v2.6.8
- https://github.com/spring-projects/spring-boot/releases/tag/v2.6.9
- https://github.com/spring-projects/spring-boot/releases/tag/v2.7.0-M1
- https://github.com/spring-projects/spring-boot/releases/tag/v2.7.0-M2
- https://github.com/spring-projects/spring-boot/releases/tag/v2.7.0-M3
- https://github.com/spring-projects/spring-boot/releases/tag/v2.7.0-RC1
- https://github.com/spring-projects/spring-boot/releases/tag/v2.7.0
- https://github.com/spring-projects/spring-boot/releases/tag/v2.7.1
- https://github.com/spring-projects/spring-boot/compare/v2.5.14...v2.7.1
2022-06-29 10:33:15 +02:00
Rick Ossendrijver
46467951dd Have Checkstyle disallow blank lines at the start of blocks (#141) 2022-06-28 14:11:55 +02:00
Picnic-Bot
6f7ce2067f Upgrade Checkstyle 10.3 -> 10.3.1 (#148)
See:
- https://checkstyle.sourceforge.io/releasenotes.html
- https://github.com/checkstyle/checkstyle/releases/tag/checkstyle-10.3.1
- https://github.com/checkstyle/checkstyle/compare/checkstyle-10.3...checkstyle-10.3.1
2022-06-28 12:58:32 +02:00
Gijs de Jong
f30ba36d18 Prefer {Mono,Flux}#cast over {Mono,Flux}#map performing a cast (#127) 2022-06-28 08:51:26 +02:00
Picnic-Bot
65736ce83f Upgrade NullAway 0.9.7 -> 0.9.8 (#145)
See:
- https://github.com/uber/NullAway/blob/master/CHANGELOG.md
- https://github.com/uber/NullAway/compare/v0.9.7...v0.9.8
2022-06-27 08:19:39 +02:00
Rick Ossendrijver
17d5805d5a Drop public modifier from BugPattern test classes (#136) 2022-06-26 11:20:07 +02:00
Picnic-Bot
ce01b62832 Upgrade pitest-maven-plugin 1.8.1 -> 1.9.0 and pitest-junit5-plugin 0.16 -> 1.0.0 (#143)
See:
- https://github.com/hcoles/pitest/releases/tag/1.9.0
- https://github.com/hcoles/pitest/compare/1.8.1...1.9.0
- https://github.com/pitest/pitest-junit5-plugin/compare/0.16...1.0.0
2022-06-24 14:51:13 +02:00
Picnic-Bot
4ee555c62b Upgrade maven-enforcer-plugin 3.0.0 -> 3.1.0 (#126)
See:
- https://issues.apache.org/jira/issues/?jql=project%20%3D%20MENFORCER%20AND%20fixVersion%20%3E%203.0.0%20AND%20fixVersion%20%3C%3D%203.1.0%20
- https://github.com/apache/maven-enforcer/compare/enforcer-3.0.0...enforcer-3.1.0
2022-06-21 13:23:32 +02:00
Gijs de Jong
a24bbbe99d Rewrite Web{,Test}Client HTTP method specification expressions (#129) 2022-06-21 07:37:04 +02:00
Gijs de Jong
679e83bc48 Replace unnecessary UriBuilder arguments passed to Web(Test)Client#uri (#128) 2022-06-20 22:37:49 +02:00
Gijs de Jong
0bdc171613 Introduce CollectorMutability check (#135)
This check flags `Collectors.to{List,Map,Set}` usages, suggesting that they be
replaced with expressions that clearly indicate the (im)mutability of the 
resultant collection.

Refaster templates that violated the new check are simplified by removing the
relevant expressions; they were too contrived to be updated some other way.
2022-06-20 11:08:06 +02:00
Picnic-Bot
9b05ec62c6 Upgrade Spring Boot 2.5.6 -> 2.5.14 (#134)
See:
- https://github.com/spring-projects/spring-boot/releases/tag/v2.5.7
- https://github.com/spring-projects/spring-boot/releases/tag/v2.5.8
- https://github.com/spring-projects/spring-boot/releases/tag/v2.5.9
- https://github.com/spring-projects/spring-boot/releases/tag/v2.5.10
- https://github.com/spring-projects/spring-boot/releases/tag/v2.5.11
- https://github.com/spring-projects/spring-boot/releases/tag/v2.5.12
- https://github.com/spring-projects/spring-boot/releases/tag/v2.5.13
- https://github.com/spring-projects/spring-boot/releases/tag/v2.5.14
- https://github.com/spring-projects/spring-boot/compare/v2.5.6...v2.5.14
2022-06-20 09:52:31 +02:00
Picnic-Bot
72866183f5 Upgrade Spring 5.3.20 -> 5.3.21 (#139)
See:
- https://github.com/spring-projects/spring-framework/releases/tag/v5.3.21
- https://github.com/spring-projects/spring-framework/compare/v5.3.20...v5.3.21
2022-06-20 09:08:43 +02:00
Picnic-Bot
9d7f569be5 Upgrade swagger-annotations 2.2.0 -> 2.2.1 (#137)
See:
- https://github.com/swagger-api/swagger-core/releases/tag/v2.2.1
- https://github.com/swagger-api/swagger-core/compare/v2.2.0...v2.2.1
2022-06-19 17:59:34 +02:00
Picnic-Bot
f72adca292 Upgrade New Relic Java Agent 7.7.0 -> 7.8.0 (#140)
See:
- https://github.com/newrelic/newrelic-java-agent/releases/tag/v7.8.0
- https://github.com/newrelic/newrelic-java-agent/compare/v7.7.0...v7.8.0
2022-06-19 15:35:04 +02:00
Picnic-Bot
5148143ae5 Upgrade Project Reactor 2020.0.19 -> 2020.0.20 (#130)
See:
- https://github.com/reactor/reactor/releases/tag/2020.0.20
- https://github.com/reactor/reactor/compare/2020.0.19...2020.0.20
2022-06-19 15:07:38 +02:00
Picnic-Bot
3d7fbbf7a2 Upgrade pitest-maven-plugin 1.8.0 -> 1.8.1 (#138)
See:
- https://github.com/hcoles/pitest/releases/tag/1.8.1
- https://github.com/hcoles/pitest/compare/1.8.0...1.8.1
2022-06-19 14:57:43 +02:00
Picnic-Bot
8b6864d8a0 Upgrade baseline-error-prone 4.42.0 -> 4.145.0 (#58)
See:
- https://github.com/palantir/gradle-baseline/releases/tag/4.47.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.48.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.49.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.50.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.51.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.52.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.53.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.54.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.55.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.56.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.57.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.58.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.59.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.60.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.61.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.62.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.63.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.64.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.65.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.66.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.67.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.68.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.69.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.70.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.71.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.72.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.73.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.74.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.75.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.76.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.77.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.78.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.79.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.80.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.81.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.82.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.83.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.84.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.85.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.86.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.87.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.88.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.89.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.90.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.91.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.92.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.93.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.94.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.95.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.96.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.97.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.98.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.99.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.100.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.101.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.102.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.103.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.104.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.105.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.106.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.107.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.108.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.109.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.110.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.111.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.112.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.113.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.114.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.115.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.116.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.117.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.118.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.119.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.120.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.121.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.122.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.123.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.124.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.125.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.126.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.127.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.128.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.129.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.130.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.131.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.132.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.133.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.134.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.135.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.136.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.137.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.138.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.139.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.140.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.141.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.142.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.143.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.144.0
- https://github.com/palantir/gradle-baseline/releases/tag/4.145.0
- https://github.com/palantir/gradle-baseline/compare/4.46.0...4.145.0
2022-06-19 14:40:46 +02:00
Picnic-Bot
a03017b5e6 Upgrade Checker Framework Annotations 3.22.1 -> 3.22.2 (#131)
See https://github.com/typetools/checker-framework/compare/checker-framework-3.22.1...checker-framework-3.22.2
2022-06-16 22:23:05 +02:00
Rick Ossendrijver
a227436f19 Introduce .renovaterc.json (#125) 2022-06-15 16:46:12 +02:00
Picnic-Bot
3f6558b7c0 Upgrade fmt-maven-plugin 2.18 -> 2.19 (#132)
See:
- https://github.com/spotify/fmt-maven-plugin/releases/tag/2.19.0
- https://github.com/spotify/fmt-maven-plugin/compare/2.18.0...2.19.0
2022-06-15 08:43:35 +02:00
Picnic-Bot
ce06396521 Upgrade pitest-junit5-plugin 0.15 -> 0.16 (#124)
See https://github.com/pitest/pitest-junit5-plugin/compare/0.15...0.16
2022-06-11 14:39:01 +02:00
Rick Ossendrijver
268766a32a Fix copy-paste error in ReactorTemplates Javadoc (#123) 2022-06-09 09:08:36 +02:00
Rick Ossendrijver
5366effd74 Upgrade Error Prone fork v2.10.0-picnic-1 -> v2.10.0-picnic-3 (#120)
See:
- https://github.com/PicnicSupermarket/error-prone/releases/tag/v2.10.0-picnic-2
- https://github.com/PicnicSupermarket/error-prone/releases/tag/v2.10.0-picnic-3
- https://github.com/PicnicSupermarket/error-prone/compare/v2.10.0-picnic-1...v2.10.0-picnic-3
2022-06-08 12:55:40 +02:00
Picnic-Bot
0a63361500 Upgrade Truth 1.0.1 -> 1.1.3 (#78)
See:
- https://github.com/google/truth/releases/tag/release_1_1
- https://github.com/google/truth/releases/tag/release_1_1_1
- https://github.com/google/truth/releases/tag/release_1_1_2
- https://github.com/google/truth/releases/tag/release_1_1_3
- https://github.com/google/truth/compare/release_1_0_1...release_1_1_3
2022-06-05 20:52:45 +02:00
Picnic-Bot
a074e2fdd6 Upgrade Checker Framework Annotations 3.22.0 -> 3.22.1 (#117)
See:
- https://github.com/typetools/checker-framework/releases/tag/checker-framework-3.22.1
- https://github.com/typetools/checker-framework/compare/checker-framework-3.22.0...checker-framework-3.22.1
2022-06-03 14:59:14 +02:00
Picnic-Bot
f06d4e41cf Upgrade Mockito 4.6.0 -> 4.6.1 (#118)
See:
- https://github.com/mockito/mockito/releases/tag/v4.6.1
- https://github.com/mockito/mockito/compare/v4.6.0...v4.6.1
2022-06-03 09:20:27 +02:00
Rick Ossendrijver
cf7bb657fa Enable Checkstyle's LocalVariableName check (#111) 2022-06-01 16:53:07 +02:00
Rick Ossendrijver
8277b43955 Move Refaster template test resources to proper package (#113) 2022-06-01 14:55:29 +02:00
Picnic-Bot
691f2c8311 Upgrade AssertJ Core 3.22.0 -> 3.23.1 (#116)
See:
- https://assertj.github.io/doc/#assertj-core-release-notes
- https://github.com/joel-costigliola/assertj-core/compare/assertj-core-3.22.0...assertj-core-3.23.1
2022-06-01 13:09:17 +02:00
Rick Ossendrijver
362518b0f4 Resolve Refaster template naming and test inconsistencies (#109) 2022-05-31 13:54:17 +02:00
Picnic-Bot
b9a3840e25 Upgrade Checkstyle 10.2 -> 10.3 (#115)
See:
- https://checkstyle.sourceforge.io/releasenotes.html
- https://github.com/checkstyle/checkstyle/releases/tag/checkstyle-10.3
- https://github.com/checkstyle/checkstyle/compare/checkstyle-10.2...checkstyle-10.3
2022-05-31 10:40:39 +02:00
Picnic-Bot
14d8bddbec Upgrade sortpom-maven-plugin 3.0.1 -> 3.1.3 (#112)
See:
- https://github.com/Ekryd/sortpom/wiki/Versions
- https://github.com/Ekryd/sortpom/releases/tag/sortpom-parent-3.1.0
- https://github.com/Ekryd/sortpom/releases/tag/sortpom-parent-3.1.1
- https://github.com/Ekryd/sortpom/releases/tag/sortpom-parent-3.1.2
- https://github.com/Ekryd/sortpom/releases/tag/sortpom-parent-3.1.3
- https://github.com/Ekryd/sortpom/compare/sortpom-parent-3.0.1...sortpom-parent-3.1.3
2022-05-31 09:51:13 +02:00
Picnic-Bot
6c7a0b81ea Upgrade Mockito 4.5.1 -> 4.6.0 (#114)
See:
- https://github.com/mockito/mockito/releases/tag/v4.6.0
- https://github.com/mockito/mockito/compare/v4.5.1...v4.6.0
2022-05-31 09:35:59 +02:00
David Mischke
3a3825f7ba Simplify unnecessary String.format(...) expressions passed to AssertJ (#101) 2022-05-26 14:24:23 +02:00
Stephan Schroevers
31e54cc990 Enforce test resources code formatting (#105)
This change should have been part of #75.
2022-05-26 13:56:19 +02:00
Pim Tegelaar
775a2688ca Introduce AssertJThrowingCallableTemplates (#92) 2022-05-26 13:29:35 +02:00
Rick Ossendrijver
3d854a0cc5 Rename RefasterRuleResourceCompiler to RefasterRuleCompiler (#110)
The associated Maven module is renamed from `refaster-resource-compiler` to
just `refaster-compiler`.
2022-05-26 13:21:28 +02:00
Stephan Schroevers
6ee013da58 Drop unnecessary dependency declarations (#106) 2022-05-26 10:51:47 +02:00
David Mischke
962d18dcb5 Drop unnecessary AssertJ usage in StepVerifier expression (#103) 2022-05-23 22:55:09 +02:00
Picnic-Bot
aab308a190 Upgrade pitest-maven-plugin 1.7.6 -> 1.8.0 (#108)
See:
- https://github.com/hcoles/pitest/releases/tag/1.8.0
- https://github.com/hcoles/pitest/compare/1.7.6...1.8.0
2022-05-23 09:21:53 +02:00
Picnic-Bot
009bd5d0d7 Upgrade versions-maven-plugin 2.10.0 -> 2.11.0 (#107)
See:
- https://github.com/mojohaus/versions-maven-plugin/releases/tag/versions-maven-plugin-2.11.0
- https://github.com/mojohaus/versions-maven-plugin/compare/versions-maven-plugin-2.10.0...versions-maven-plugin-2.11.0
2022-05-23 08:45:15 +02:00
Pim Tegelaar
8e57f64e31 Prevent JUnitMethodDeclarationCheck from renaming likely-overridden methods (#93) 2022-05-17 18:15:31 +02:00
Picnic-Bot
53e81f3611 Upgrade Jackson 2.13.2.20220328 -> 2.13.3 (#100)
See:
- https://github.com/FasterXML/jackson/wiki/Jackson-Release-2.13.3
- https://github.com/FasterXML/jackson-bom/compare/jackson-bom-2.13.2.20220328...jackson-bom-2.13.3
2022-05-17 11:41:56 +02:00
Picnic-Bot
8745105251 Upgrade Spring 5.3.19 -> 5.3.20 (#99)
See:
- https://github.com/spring-projects/spring-framework/releases/tag/v5.3.20
- https://github.com/spring-projects/spring-framework/compare/v5.3.19...v5.3.20
2022-05-17 11:24:18 +02:00
Picnic-Bot
3eb3c20b1d Upgrade Project Reactor 2020.0.18 -> 2020.0.19 (#98)
See:
- https://github.com/reactor/reactor/releases/tag/2020.0.19
- https://github.com/reactor/reactor/compare/2020.0.18...2020.0.19
2022-05-17 10:26:33 +02:00
Picnic-Bot
121618f277 Upgrade NullAway 0.9.6 -> 0.9.7 (#97)
See:
- https://github.com/uber/NullAway/blob/master/CHANGELOG.md
- https://github.com/uber/NullAway/compare/v0.9.6...v0.9.7
2022-05-17 10:09:59 +02:00
Picnic-Bot
5cf4194168 Upgrade pitest-maven-plugin 1.7.5 -> 1.7.6 (#94)
See:
- https://github.com/hcoles/pitest/releases/tag/1.7.6
- https://github.com/hcoles/pitest/compare/1.7.5...1.7.6
2022-05-10 09:28:27 +02:00
Picnic-Bot
c434cd318c Upgrade New Relic Java Agent 7.6.0 -> 7.7.0
See:
- https://github.com/newrelic/newrelic-java-agent/releases/tag/v7.7.0
- https://github.com/newrelic/newrelic-java-agent/compare/v7.6.0...v7.7.0
2022-05-10 09:02:30 +02:00
Picnic-Bot
22aa9cb213 Upgrade Checker Framework Annotations 3.21.4 -> 3.22.0
See:
- https://github.com/typetools/checker-framework/releases/tag/checker-framework-3.22.0
- https://github.com/typetools/checker-framework/compare/checker-framework-3.21.4...checker-framework-3.22.0
2022-05-10 07:36:16 +02:00
Rick Ossendrijver
3edc483e7c Extend set of parameter types recognized by RequestMappingAnnotationCheck (#91)
While there, restrict the supported set of `WebRequest` subtypes to just
`WebRequest` and `NativeWebRequest`.
2022-04-29 14:15:51 +02:00
Stephan Schroevers
fff5d7b329 Apply assorted Maven parent improvements (#85) 2022-04-29 13:35:14 +02:00
Picnic-Bot
4ca80952ab Upgrade modernizer-maven-plugin 2.3.0 -> 2.4.0 (#90)
See:
- https://github.com/gaul/modernizer-maven-plugin/releases/tag/modernizer-maven-plugin-2.4.0
- https://github.com/gaul/modernizer-maven-plugin/compare/modernizer-maven-plugin-2.3.0...modernizer-maven-plugin-2.4.0
2022-04-26 12:52:11 +02:00
Picnic-Bot
8cecb8bf30 Upgrade Spring 5.3.13 -> 5.3.19 (#74)
See:
- https://github.com/spring-projects/spring-framework/releases/tag/v5.3.14
- https://github.com/spring-projects/spring-framework/releases/tag/v5.3.15
- https://github.com/spring-projects/spring-framework/releases/tag/v5.3.16
- https://github.com/spring-projects/spring-framework/releases/tag/v5.3.17
- https://github.com/spring-projects/spring-framework/releases/tag/v5.3.18
- https://github.com/spring-projects/spring-framework/releases/tag/v5.3.19
- https://github.com/spring-projects/spring-framework/compare/v5.3.13...v5.3.19
2022-04-26 12:38:23 +02:00
Picnic-Bot
a937bf0ddf Upgrade Checker Framework Annotations 3.19.0 -> 3.21.4 (#87)
See:
- https://github.com/typetools/checker-framework/releases/tag/checker-framework-3.20.0
- https://github.com/typetools/checker-framework/releases/tag/checker-framework-3.21.0
- https://github.com/typetools/checker-framework/releases/tag/checker-framework-3.21.1
- https://github.com/typetools/checker-framework/releases/tag/checker-framework-3.21.2
- https://github.com/typetools/checker-framework/releases/tag/checker-framework-3.21.3
- https://github.com/typetools/checker-framework/releases/tag/checker-framework-3.21.4
- https://github.com/typetools/checker-framework/compare/checker-framework-3.19.0...checker-framework-3.21.4
2022-04-26 12:31:02 +02:00
Picnic-Bot
0b71c2b576 Upgrade versions-maven-plugin 2.8.1 -> 2.10.0 (#89)
See:
- https://github.com/mojohaus/versions-maven-plugin/releases/tag/versions-maven-plugin-2.9.0
- https://github.com/mojohaus/versions-maven-plugin/releases/tag/versions-maven-plugin-2.10.0
- https://github.com/mojohaus/versions-maven-plugin/compare/versions-maven-plugin-2.8.1...versions-maven-plugin-2.10.0
2022-04-26 11:35:51 +02:00
Picnic-Bot
239d38d69f Upgrade extra-enforcer-rules 1.4 -> 1.5.1 (#88)
See:
- https://github.com/mojohaus/extra-enforcer-rules/releases/tag/extra-enforcer-rules-1.5.0
- https://github.com/mojohaus/extra-enforcer-rules/releases/tag/extra-enforcer-rules-1.5.1
- https://github.com/mojohaus/extra-enforcer-rules/compare/extra-enforcer-rules-1.4...extra-enforcer-rules-1.5.1
2022-04-26 11:09:17 +02:00
Picnic-Bot
ab57aa5eb6 Upgrade pitest-maven-plugin 1.7.3 -> 1.7.5 (#72)
See:
- https://github.com/hcoles/pitest/releases/tag/1.7.4
- https://github.com/hcoles/pitest/releases/tag/1.7.5
- https://github.com/hcoles/pitest/compare/1.7.3...1.7.5
2022-04-25 22:46:44 +02:00
Picnic-Bot
49267337cb Upgrade JUnit Jupiter 5.8.1 -> 5.8.2 (#71)
See:
- https://junit.org/junit5/docs/current/release-notes/index.html
- https://github.com/junit-team/junit5/releases/tag/r5.8.2
- https://github.com/junit-team/junit5/compare/r5.8.1...r5.8.2
2022-04-25 13:56:02 +02:00
Picnic-Bot
c201fe1fd2 Upgrade SLF4J API 1.7.32 -> 1.7.36 (#73)
See:
- https://www.slf4j.org/news.html
- https://github.com/qos-ch/slf4j/compare/v_1.7.32...v_1.7.36
2022-04-25 13:48:50 +02:00
Picnic-Bot
fb20b6f93d Upgrade AutoValue 1.8.2 -> 1.9 (#83)
See:
- https://github.com/google/auto/releases/tag/auto-value-1.9
- https://github.com/google/auto/compare/auto-value-1.8.2...auto-value-1.9
2022-04-25 13:36:41 +02:00
Picnic-Bot
9264c25b57 Upgrade fmt-maven-plugin 2.12 -> 2.18 (#75)
This also upgrades Google Java Format 1.11.0 -> 1.15.0, improving support for 
Java 17 syntax, among other things.

See:
- https://github.com/google/google-java-format/releases/tag/v1.12.0
- https://github.com/google/google-java-format/releases/tag/v1.13.0
- https://github.com/google/google-java-format/releases/tag/v1.14.0
- https://github.com/google/google-java-format/releases/tag/v1.15.0
- https://github.com/google/google-java-format/compare/v1.11.0...v1.15.0
- https://github.com/spotify/fmt-maven-plugin/releases/tag/2.13.0
- https://github.com/spotify/fmt-maven-plugin/releases/tag/2.14.0
- https://github.com/spotify/fmt-maven-plugin/releases/tag/2.15.0
- https://github.com/spotify/fmt-maven-plugin/releases/tag/2.16.0
- https://github.com/spotify/fmt-maven-plugin/releases/tag/2.17.0
- https://github.com/spotify/fmt-maven-plugin/releases/tag/2.18.0
- https://github.com/spotify/fmt-maven-plugin/compare/2.12.0...2.18.0
2022-04-25 13:29:19 +02:00
Picnic-Bot
ca628eef6c Upgrade New Relic Java Agent 7.4.3 -> 7.6.0 (#79)
See:
- https://github.com/newrelic/newrelic-java-agent/releases/tag/v7.5.0
- https://github.com/newrelic/newrelic-java-agent/releases/tag/v7.6.0
- https://github.com/newrelic/newrelic-java-agent/compare/v7.4.3...v7.6.0
2022-04-25 09:27:25 +02:00
Picnic-Bot
62168dcd4b Upgrade Checkstyle 10.1 -> 10.2 (#80)
See:
- https://checkstyle.sourceforge.io/releasenotes.html
- https://github.com/checkstyle/checkstyle/releases/tag/checkstyle-10.2
- https://github.com/checkstyle/checkstyle/compare/checkstyle-10.1...checkstyle-10.2
2022-04-25 09:17:24 +02:00
Picnic-Bot
96a82eaf85 Upgrade Mockito 4.0.0 -> 4.5.1 (#84)
See:
- https://github.com/mockito/mockito/releases/tag/v4.1.0
- https://github.com/mockito/mockito/releases/tag/v4.2.0
- https://github.com/mockito/mockito/releases/tag/v4.3.0
- https://github.com/mockito/mockito/releases/tag/v4.3.1
- https://github.com/mockito/mockito/releases/tag/v4.4.0
- https://github.com/mockito/mockito/releases/tag/v4.5.0
- https://github.com/mockito/mockito/releases/tag/v4.5.1
- https://github.com/mockito/mockito/compare/v4.0.0...v4.5.1
2022-04-25 09:06:44 +02:00
Picnic-Bot
0c615b4c15 Upgrade Project Reactor 2020.0.13 -> 2020.0.18 (#51)
See:
- https://github.com/reactor/reactor/releases/tag/2020.0.14
- https://github.com/reactor/reactor/releases/tag/2020.0.15
- https://github.com/reactor/reactor/releases/tag/2020.0.16
- https://github.com/reactor/reactor/releases/tag/2020.0.17
- https://github.com/reactor/reactor/releases/tag/2020.0.18
- https://github.com/reactor/reactor/compare/2020.0.13...2020.0.18
2022-04-24 17:05:53 +02:00
Picnic-Bot
6304130ae3 Upgrade swagger-annotations 1.6.3 -> 1.6.6 (#52)
See:
- https://github.com/swagger-api/swagger-core/releases/tag/v1.6.4
- https://github.com/swagger-api/swagger-core/releases/tag/v1.6.5
- https://github.com/swagger-api/swagger-core/releases/tag/v1.6.6
- https://github.com/swagger-api/swagger-core/compare/v1.6.3...v1.6.6
2022-04-24 16:57:18 +02:00
Picnic-Bot
1674e99ba4 Upgrade maven-jar-plugin 3.2.0 -> 3.2.2 (#54)
See:
- https://issues.apache.org/jira/issues/?jql=project%20%3D%20MJAR%20AND%20fixVersion%20%3E%203.2.0%20AND%20fixVersion%20%3C%3D%203.2.2
- https://github.com/apache/maven-jar-plugin/compare/maven-jar-plugin-3.2.0...maven-jar-plugin-3.2.2
2022-04-24 16:49:47 +02:00
Picnic-Bot
75bbc8e5b5 Upgrade AspectJ 1.9.7 -> 1.9.9.1 (#55)
See:
- https://github.com/eclipse/org.aspectj/releases/tag/V1_9_8
- https://github.com/eclipse/org.aspectj/releases/tag/V1_9_9
- https://github.com/eclipse/org.aspectj/releases/tag/V1_9_9_1
- https://github.com/eclipse/org.aspectj/compare/V1_9_7...V1_9_9_1
2022-04-24 16:27:16 +02:00
Picnic-Bot
fe3ed2be63 Upgrade jacoco-maven-plugin 0.8.7 -> 0.8.8 (#56)
See:
- https://github.com/jacoco/jacoco/releases/tag/v0.8.8
- https://github.com/jacoco/jacoco/compare/v0.8.7...v0.8.8
2022-04-24 16:20:18 +02:00
Picnic-Bot
f36350f3ba Upgrade Forbidden APIs plugin 3.2 -> 3.3 (#59)
See https://github.com/policeman-tools/forbidden-apis/compare/3.2...3.3
2022-04-24 16:12:47 +02:00
Picnic-Bot
4888ad3aaf Upgrade swagger-annotations 2.1.11 -> 2.2.0 (#60)
See:
- https://github.com/swagger-api/swagger-core/releases/tag/v2.1.12
- https://github.com/swagger-api/swagger-core/releases/tag/v2.1.13
- https://github.com/swagger-api/swagger-core/releases/tag/v2.2.0
- https://github.com/swagger-api/swagger-core/compare/v2.1.11...v2.2.0
2022-04-24 16:04:48 +02:00
Picnic-Bot
9daf73f148 Upgrade maven-clean-plugin 3.1.0 -> 3.2.0 (#61)
See:
- https://issues.apache.org/jira/issues/?jql=project%20%3D%20MCLEAN%20AND%20fixVersion%20%3E%203.1.0%20AND%20fixVersion%20%3C%3D%203.2.0
- https://github.com/apache/maven-clean-plugin/compare/maven-clean-plugin-3.1.0...maven-clean-plugin-3.2.0
2022-04-24 15:55:59 +02:00
Picnic-Bot
b08ca514c1 Upgrade maven-compiler-plugin 3.8.1 -> 3.10.1 (#62)
And drop the redundant `--source` and `--target` flags.

See:
- https://github.com/apache/maven-compiler-plugin/releases/tag/maven-compiler-plugin-3.9.0
- https://github.com/apache/maven-compiler-plugin/releases/tag/maven-compiler-plugin-3.10.0
- https://github.com/apache/maven-compiler-plugin/releases/tag/maven-compiler-plugin-3.10.1
- https://github.com/apache/maven-compiler-plugin/compare/maven-compiler-plugin-3.8.1...maven-compiler-plugin-3.10.1
2022-04-24 15:48:25 +02:00
Picnic-Bot
4eed7dd0e8 Upgrade maven-dependency-plugin 3.1.2 -> 3.3.0 (#63)
See:
- https://issues.apache.org/jira/issues/?jql=project%20%3D%20MDEP%20AND%20fixVersion%20%3E%203.1.2%20AND%20fixVersion%20%3C%3D%203.3.0
- https://github.com/apache/maven-dependency-plugin/compare/maven-dependency-plugin-3.1.2...maven-dependency-plugin-3.3.0
2022-04-24 15:37:56 +02:00
Picnic-Bot
618c62e7a5 Upgrade maven-javadoc-plugin 3.3.1 -> 3.4.0 (#64)
See:
- https://issues.apache.org/jira/issues/?jql=project%20%3D%20MJAVADOC%20AND%20fixVersion%20%3E%203.3.1%20AND%20fixVersion%20%3C%3D%203.4.0
- https://github.com/apache/maven-javadoc-plugin/compare/maven-javadoc-plugin-3.3.1...maven-javadoc-plugin-3.4.0
2022-04-24 15:30:33 +02:00
Picnic-Bot
b46f075ddf Upgrade maven-site-plugin 3.9.1 -> 3.12.0 (#65)
See:
- https://issues.apache.org/jira/issues/?jql=project%20%3D%20MSITE%20AND%20fixVersion%20%3E%203.9.1%20AND%20fixVersion%20%3C%3D%203.12.0
- https://github.com/apache/maven-site-plugin/compare/maven-site-plugin-3.9.1...maven-site-plugin-3.12.0
2022-04-24 15:12:26 +02:00
Picnic-Bot
64436ff9b6 Upgrade AssertJ Core 3.21.0 -> 3.22.0 (#66)
See:
- https://assertj.github.io/doc/#assertj-core-release-notes
- https://github.com/joel-costigliola/assertj-core/compare/assertj-core-3.21.0...assertj-core-3.22.0
2022-04-24 15:03:13 +02:00
Picnic-Bot
e028077f1e Upgrade Checkstyle 9.1 -> 10.1 (#67)
See:
- https://checkstyle.sourceforge.io/releasenotes.html
- https://github.com/checkstyle/checkstyle/releases/tag/checkstyle-9.2
- https://github.com/checkstyle/checkstyle/releases/tag/checkstyle-9.2.1
- https://github.com/checkstyle/checkstyle/releases/tag/checkstyle-9.3
- https://github.com/checkstyle/checkstyle/releases/tag/checkstyle-10.0
- https://github.com/checkstyle/checkstyle/releases/tag/checkstyle-10.1
- https://github.com/checkstyle/checkstyle/compare/checkstyle-9.1...checkstyle-10.1
2022-04-24 14:51:37 +02:00
Picnic-Bot
5aa386b5ba Upgrade buildnumber-maven-plugin 1.4 -> 3.0.0 (#69)
See:
- https://github.com/mojohaus/buildnumber-maven-plugin/releases/tag/buildnumber-maven-plugin-3.0.0
- https://github.com/mojohaus/buildnumber-maven-plugin/compare/buildnumber-maven-plugin-1.4...buildnumber-maven-plugin-3.0.0
2022-04-24 14:44:35 +02:00
Picnic-Bot
a480fe908d Upgrade Palantir assertj-error-prone 0.3.0 -> 0.6.0 (#57)
See:
- https://github.com/palantir/assertj-automation/releases/tag/0.4.0
- https://github.com/palantir/assertj-automation/releases/tag/0.5.0
- https://github.com/palantir/assertj-automation/releases/tag/0.6.0
- https://github.com/palantir/assertj-automation/compare/0.3.0...0.6.0
2022-04-24 14:37:29 +02:00
Picnic-Bot
b2316c744c Upgrade Hamcrest 1.3 -> 2.2 (#70)
See:
- https://github.com/hamcrest/JavaHamcrest/releases/tag/v2.1-rc1
- https://github.com/hamcrest/JavaHamcrest/releases/tag/v2.1-rc2
- https://github.com/hamcrest/JavaHamcrest/releases/tag/v2.1-rc3
- https://github.com/hamcrest/JavaHamcrest/releases/tag/v2.1-rc4
- https://github.com/hamcrest/JavaHamcrest/releases/tag/v2.1
- https://github.com/hamcrest/JavaHamcrest/releases/tag/v2.2-rc1
- https://github.com/hamcrest/JavaHamcrest/releases/tag/v2.2
- https://github.com/hamcrest/JavaHamcrest/compare/hamcrest-java-1.3...v2.2
2022-04-24 14:24:54 +02:00
Picnic-Bot
9334babfe8 Upgrade sortpom-maven-plugin 3.0.0 -> 3.0.1 (#48)
See:
- https://github.com/Ekryd/sortpom/wiki/Versions
- https://github.com/Ekryd/sortpom/releases/tag/sortpom-parent-3.0.1
- https://github.com/Ekryd/sortpom/compare/sortpom-parent-3.0.0...sortpom-parent-3.0.1
2022-04-24 14:15:13 +02:00
Picnic-Bot
b6632d393b Upgrade AutoCommon 1.2 -> 1.2.1 (#49)
See:
- https://github.com/google/auto/releases/tag/auto-common-1.2.1
- https://github.com/google/auto/compare/auto-common-1.2...auto-common-1.2.1
2022-04-24 14:01:28 +02:00
Picnic-Bot
53d191ff4f Upgrade NullAway 0.9.2 -> 0.9.6 (#50)
See:
- https://github.com/uber/NullAway/blob/master/CHANGELOG.md
- https://github.com/uber/NullAway/compare/v0.9.2...v0.9.6
2022-04-24 13:19:49 +02:00
Picnic-Bot
b8ddf3ac20 Upgrade Jackson 2.13.0 -> 2.13.2.20220328 (#47)
See:
- https://github.com/FasterXML/jackson/wiki/Jackson-Release-2.13.1
- https://github.com/FasterXML/jackson/wiki/Jackson-Release-2.13.2
- https://github.com/FasterXML/jackson-bom/compare/jackson-bom-2.13.0...jackson-bom-2.13.2.20220328
2022-04-24 13:05:17 +02:00
Rick Ossendrijver
e859c2774f JUnitMethodDeclarationCheck: better handle possibly-problematic method renames (#35)
By flagging affected test methods, but not suggesting a specific rename.
2022-04-12 18:16:21 +02:00
Rick Ossendrijver
1cb4ce97ae Require static import of most java.util.Collections methods (#23)
While there, statically import `ZoneOffset.UTC` in a few places.
2022-04-12 17:48:53 +02:00
Pieter Dirk Soels
92fe96286f Prefer Collectors.joining() over Collectors.joining("") (#44) 2022-04-12 14:25:39 +02:00
Vitor Mussatto
d7531abceb Introduce additional Immutable{List,Map,Set}#of Refaster templates (#40) 2022-04-12 11:51:45 +02:00
João Nogueira
cc7074a62f Introduce RequestParamTypeCheck (#33) 2022-04-12 10:45:34 +02:00
Pieter Dirk Soels
91922454b6 Require static import of com.google.errorprone.refaster.ImportPolicy members (#45) 2022-04-12 10:36:44 +02:00
Rick Ossendrijver
eaa98c985c Introduce assorted AssertJ Refaster templates (#37) 2022-04-11 22:25:14 +02:00
Pieter Dirk Soels
94908d6a37 Require static import of java.util.UUID#randomUUID (#42) 2022-04-11 17:55:57 +02:00
Rick Ossendrijver
e8d9221424 Have PrimitiveComparisonCheck retain relevant generic type parameters (#39) 2022-04-11 17:13:06 +02:00
Rick Ossendrijver
fea8f5b2c0 Suggest assorted canonical ZoneOffset usages (#38)
- Statically import `ZoneOffset.UTC`.
- Prefer `Instant#atOffset` over `OffsetDateTime#ofInstant`.
2022-04-11 10:16:37 +02:00
Rick Ossendrijver
2199accedc Drop or rewrite AssertJBigDecimalTemplates rules (#30) 2022-04-11 10:08:15 +02:00
Rick Ossendrijver
83670aeddf Require static import of java.util.regex.Pattern constants (#36)
The meaning of these constants will be apparent from context.

Note that `Pattern.{compile,matches,quote}` should not be statically imported,
as these methods have rather generic names.
2022-04-11 09:19:40 +02:00
Rick Ossendrijver
e8977bea67 Require static import of BugPattern.{LinkType,ServerityLevel,StandardTags} members (#34) 2022-04-11 08:16:20 +02:00
Pieter Dirk Soels
0d051064bb Prefer Duration.ofX(n) over Duration.of(n, ChronoUnit.X) (#41)
`TimeTemplates` now contains Refaster templates for
`Duration.of{Nanos,Millis,Seconds,Minutes,Hours,Days}(n)`.
2022-04-10 16:02:33 +02:00
Rick Ossendrijver
06913b6a5b Drop obsolete README entry (#29)
This issue is was resolved by google/error-prone#2257.
2022-04-10 15:34:36 +02:00
Rick Ossendrijver
d2bbee3ed9 Introduce IdentityConversionCheck (#27)
And remove any Refaster templates it subsumes.
2022-04-08 13:28:12 +02:00
Rick Ossendrijver
793a70c29b Configure static import policy for AssertThatSetContainsExactlyOneElement Refaster template (#32) 2022-01-24 10:02:34 +01:00
Rick Ossendrijver
63b4fae185 Introduce FluxFlatMapUsageCheck (#26)
And two (semi-)related Refaster templates.
2022-01-15 12:39:34 +01:00
Stephan Schroevers
73f8f056b4 Introduce ScheduledTransactionTraceCheck (#31) 2022-01-10 10:21:20 +01:00
Rick Ossendrijver
c1638066cd Define assorted StaticImportCheck exemptions
While there, require that most `org.springframework.http.MediaType` members are
statically imported.
2022-01-03 10:20:46 +01:00
Rick Ossendrijver
08e99fb54e Use matching lambda parameter names in Refaster before- and after-templates (#24) 2022-01-01 14:04:37 +01:00
210 changed files with 6405 additions and 2008 deletions

38
.github/workflows/build.yaml vendored Normal file
View File

@@ -0,0 +1,38 @@
name: Build and verify
on:
pull_request:
push:
branches:
- 'master'
- 'ibabiankou/experiments'
jobs:
build:
runs-on: ubuntu-22.04
strategy:
matrix:
jdk: [ 11.0.16, 17.0.4 ]
steps:
# We run the build twice for each supported JDK: once against the
# original Error Prone release, using only Error Prone checks available
# on Maven Central, and once against the Picnic Error Prone fork,
# additionally enabling all checks defined in this project and any
# Error Prone checks available only from other artifact repositories.
- name: Check out code
uses: actions/checkout@v3.0.2
- name: Set up JDK
uses: actions/setup-java@v3.4.1
with:
java-version: ${{ matrix.jdk }}
distribution: temurin
cache: maven
- name: Display build environment details
run: mvn --version
- name: Build project against vanilla Error Prone
run: mvn -T1C install
- name: Build project with self-check against Error Prone fork
run: mvn -T1C clean verify -Perror-prone-fork -Pnon-maven-central -Pself-check -s settings.xml
- name: Remove project snapshots
run: mvn build-helper:remove-project-artifact
# XXX: Enable Codecov once we "go public".
# XXX: Enable SonarCloud once we "go public".

1
.mvn/maven.config Normal file
View File

@@ -0,0 +1 @@
--batch-mode --errors --strict-checksums

22
.renovaterc.json Normal file
View File

@@ -0,0 +1,22 @@
{
"$schema": "https://docs.renovatebot.com/renovate-schema.json",
"packageRules": [
{
"matchPackagePatterns": [
"^org\\.springframework:spring-framework-bom$",
"^org\\.springframework\\.boot:spring-boot[a-z-]*$"
],
"separateMinorPatch": true
},
{
"matchPackagePatterns": [
"^com\\.palantir\\.baseline:baseline-error-prone$"
],
"schedule": "* * 1 * *"
}
],
"reviewers": [
"rickie",
"Stephan202"
]
}

View File

@@ -1,38 +0,0 @@
---
dist: bionic
language: java
jdk: openjdk11
addons:
sonarcloud:
organization: picnic-technologies
token: "${SONARCLOUD_TOKEN}"
install:
- mvn io.takari:maven:wrapper
script:
# We run the build twice: once against the original Error Prone release,
# using only Error Prone checks available on Maven Central, and once against
# the Picnic Error Prone fork, additionally enabling all checks defined in
# this project and any Error Prone checks available only from other artifact
# repositories.
- ./mvnw clean install
- ./mvnw clean install -Perror-prone-fork -Pnon-maven-central -Pself-check -s settings.xml
# XXX: Enable SonarCloud once we "go public".
# ./mvnw jacoco:prepare-agent surefire:test jacoco:report sonar:sonar
- ./mvnw jacoco:prepare-agent surefire:test jacoco:report
before_cache:
# Don't cache the artifacts we just generated, for multiple reasons: (1) we
# shouldn't need them next time around and (2) if we do, that indicates a
# dependency issue which might otherwise go unnoticed until next time we bump
# the project's version (i.e., when tagging).
- find "${HOME}/.m2/repository" -depth -name '*-SNAPSHOT' -exec rm -r '{}' \;
cache:
directories:
# The local Maven repository in which third party dependencies are stored.
- ${HOME}/.m2/repository
# The Takari Maven Wrapper's storage for downloaded Maven distributions.
- ${HOME}/.m2/wrapper
# The SonarQube analysis cache.
- ${HOME}/.sonar/cache
# XXX: Enable Codecov once we "go public".
#after_success:
# - bash <(curl -s https://codecov.io/bash)

View File

@@ -0,0 +1,25 @@
#!/usr/bin/env bash
# Compiles the code using Error Prone and applies its suggestions. The set of
# checks applied can optionally be restricted by name.
#
# As this script may modify the project's code, it is important to execute it
# in a clean Git working directory.
set -e -u -o pipefail
if [ "${#}" -gt 1 ]; then
echo "Usage: ./$(basename "${0}") [PatchChecks]"
exit 1
fi
patchChecks=${1:-}
mvn clean test-compile fmt:format \
-T 1.0C \
-Perror-prone \
-Perror-prone-fork \
-Ppatch \
-Pself-check \
-Derror-prone.patch-checks="${patchChecks}" \
-Dverification.skip

View File

@@ -276,10 +276,6 @@ Refaster's expressiveness:
to be lost. In such a case don't statically import the method, so that the
generic type information can be retained. (There may be cases where generic
type information should even be _added_. Find an example.)
- Upon application of a template Refaster can throw a _No binding for
Key{identifier=someAfterTemplateParam}_ exception. When this happens the
template is invalid. Instead perform this check at compile time, such that
such malformed templates cannot be defined in the first place.
- Provide a way to express "match if (not) annotated (with _X_)". See #1 for a
motivating example.
- Provide a way to place match constraints on compile time constants. For

View File

@@ -41,7 +41,7 @@
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>refaster-resource-compiler</artifactId>
<artifactId>refaster-compiler</artifactId>
<!-- This dependency is declared only as a hint to Maven that
compilation depends on it; see the `maven-compiler-plugin`'s
`annotationProcessorPaths` configuration below. -->
@@ -56,6 +56,11 @@
<artifactId>jackson-annotations</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.auto</groupId>
<artifactId>auto-common</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.auto.service</groupId>
<artifactId>auto-service-annotations</artifactId>
@@ -71,11 +76,20 @@
<artifactId>javac</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.googlejavaformat</groupId>
<artifactId>google-java-format</artifactId>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.newrelic.agent.java</groupId>
<artifactId>newrelic-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.projectreactor</groupId>
<artifactId>reactor-core</artifactId>
@@ -116,6 +130,11 @@
<artifactId>jaxb-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
@@ -126,11 +145,6 @@
<artifactId>assertj-core</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
@@ -161,16 +175,6 @@
<artifactId>slf4j-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
@@ -202,7 +206,7 @@
<pluginManagement>
<plugins>
<plugin>
<groupId>com.coveo</groupId>
<groupId>com.spotify.fmt</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<configuration>
<additionalSourceDirectories>
@@ -217,7 +221,7 @@
<annotationProcessorPaths combine.children="append">
<path>
<groupId>${project.groupId}</groupId>
<artifactId>refaster-resource-compiler</artifactId>
<artifactId>refaster-compiler</artifactId>
<version>${project.version}</version>
</path>
<path>
@@ -227,7 +231,7 @@
</path>
</annotationProcessorPaths>
<compilerArgs combine.children="append">
<arg>-Xplugin:RefasterRuleResourceCompiler</arg>
<arg>-Xplugin:RefasterRuleCompiler</arg>
</compilerArgs>
</configuration>
</plugin>

View File

@@ -1,12 +1,12 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.BugPattern.StandardTags.LIKELY_ERROR;
import static com.google.errorprone.matchers.Matchers.isType;
import com.google.auto.service.AutoService;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.AnnotationTreeMatcher;
@@ -27,9 +27,9 @@ import javax.lang.model.element.AnnotationValue;
@BugPattern(
name = "AmbiguousJsonCreator",
summary = "`JsonCreator.Mode` should be set for single-argument creators",
linkType = LinkType.NONE,
severity = SeverityLevel.WARNING,
tags = StandardTags.LIKELY_ERROR)
linkType = NONE,
severity = WARNING,
tags = LIKELY_ERROR)
public final class AmbiguousJsonCreatorCheck extends BugChecker implements AnnotationTreeMatcher {
private static final long serialVersionUID = 1L;
private static final Matcher<AnnotationTree> IS_JSON_CREATOR_ANNOTATION =

View File

@@ -0,0 +1,58 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION;
import static com.google.errorprone.matchers.Matchers.allOf;
import static com.google.errorprone.matchers.Matchers.argument;
import static com.google.errorprone.matchers.Matchers.argumentCount;
import static com.google.errorprone.matchers.Matchers.instanceMethod;
import static com.google.errorprone.matchers.Matchers.nullLiteral;
import com.google.auto.service.AutoService;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.fixes.SuggestedFixes;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.sun.source.tree.MethodInvocationTree;
/**
* A {@link BugChecker} which flags AssertJ {@code isEqualTo(null)} checks for simplification.
*
* <p>This bug checker cannot be replaced with a simple Refaster template, as the Refaster approach
* would require that all overloads of {@link org.assertj.core.api.Assert#isEqualTo(Object)} (such
* as {@link org.assertj.core.api.AbstractStringAssert#isEqualTo(String)}) are explicitly
* enumerated. This bug checker generically matches all such current and future overloads.
*/
@AutoService(BugChecker.class)
@BugPattern(
name = "AssertJIsNull",
summary = "Prefer `.isNull()` over `.isEqualTo(null)`",
linkType = NONE,
severity = SUGGESTION,
tags = SIMPLIFICATION)
public final class AssertJIsNullCheck extends BugChecker implements MethodInvocationTreeMatcher {
private static final long serialVersionUID = 1L;
private static final Matcher<MethodInvocationTree> ASSERT_IS_EQUAL_TO_NULL =
allOf(
instanceMethod().onDescendantOf("org.assertj.core.api.Assert").named("isEqualTo"),
argumentCount(1),
argument(0, nullLiteral()));
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!ASSERT_IS_EQUAL_TO_NULL.matches(tree, state)) {
return Description.NO_MATCH;
}
SuggestedFix.Builder fix =
SuggestedFix.builder().merge(SuggestedFixes.renameMethodInvocation(tree, "isNull", state));
tree.getArguments().forEach(arg -> fix.merge(SuggestedFix.delete(arg)));
return describeMatch(tree, fix.build());
}
}

View File

@@ -1,15 +1,16 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION;
import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.AT_LEAST_ONE;
import static com.google.errorprone.matchers.Matchers.annotations;
import static com.google.errorprone.matchers.Matchers.isType;
import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher;
@@ -28,9 +29,9 @@ import java.util.List;
@BugPattern(
name = "AutowiredConstructor",
summary = "Omit `@Autowired` on a class' sole constructor, as it is redundant",
linkType = LinkType.NONE,
severity = SeverityLevel.SUGGESTION,
tags = StandardTags.SIMPLIFICATION)
linkType = NONE,
severity = SUGGESTION,
tags = SIMPLIFICATION)
public final class AutowiredConstructorCheck extends BugChecker implements ClassTreeMatcher {
private static final long serialVersionUID = 1L;
private static final MultiMatcher<Tree, AnnotationTree> AUTOWIRED_ANNOTATION =
@@ -43,7 +44,7 @@ public final class AutowiredConstructorCheck extends BugChecker implements Class
return Description.NO_MATCH;
}
List<AnnotationTree> annotations =
ImmutableList<AnnotationTree> annotations =
AUTOWIRED_ANNOTATION
.multiMatchResult(Iterables.getOnlyElement(constructors), state)
.matchingNodes();

View File

@@ -1,11 +1,12 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION;
import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.AnnotationTreeMatcher;
@@ -30,9 +31,9 @@ import java.util.regex.Pattern;
@BugPattern(
name = "CanonicalAnnotationSyntax",
summary = "Omit redundant syntax from annotation declarations",
linkType = LinkType.NONE,
severity = SeverityLevel.SUGGESTION,
tags = StandardTags.SIMPLIFICATION)
linkType = NONE,
severity = SUGGESTION,
tags = SIMPLIFICATION)
public final class CanonicalAnnotationSyntaxCheck extends BugChecker
implements AnnotationTreeMatcher {
private static final long serialVersionUID = 1L;

View File

@@ -0,0 +1,119 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.BugPattern.StandardTags.FRAGILE_CODE;
import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod;
import com.google.auto.service.AutoService;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.fixes.SuggestedFixes;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import java.util.stream.Collector;
/**
* A {@link BugChecker} which flags {@link Collector Collectors} that don't clearly express
* (im)mutability.
*
* <p>Replacing such collectors with alternatives that produce immutable collections is preferred.
* Do note that Guava's immutable collections are null-hostile.
*/
@AutoService(BugChecker.class)
@BugPattern(
name = "CollectorMutability",
summary =
"Avoid `Collectors.to{List,Map,Set}` in favour of alternatives that emphasize (im)mutability",
linkType = NONE,
severity = WARNING,
tags = FRAGILE_CODE)
public final class CollectorMutabilityCheck extends BugChecker
implements MethodInvocationTreeMatcher {
private static final long serialVersionUID = 1L;
private static final Matcher<ExpressionTree> COLLECTOR_METHOD =
staticMethod().onClass("java.util.stream.Collectors");
private static final Matcher<ExpressionTree> LIST_COLLECTOR =
staticMethod().anyClass().named("toList");
private static final Matcher<ExpressionTree> MAP_COLLECTOR =
staticMethod().anyClass().named("toMap");
private static final Matcher<ExpressionTree> SET_COLLECTOR =
staticMethod().anyClass().named("toSet");
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!COLLECTOR_METHOD.matches(tree, state)) {
return Description.NO_MATCH;
}
if (LIST_COLLECTOR.matches(tree, state)) {
return suggestToCollectionAlternatives(
tree, "com.google.common.collect.ImmutableList.toImmutableList", "ArrayList", state);
}
if (MAP_COLLECTOR.matches(tree, state)) {
return suggestToMapAlternatives(tree, state);
}
if (SET_COLLECTOR.matches(tree, state)) {
return suggestToCollectionAlternatives(
tree, "com.google.common.collect.ImmutableSet.toImmutableSet", "HashSet", state);
}
return Description.NO_MATCH;
}
private Description suggestToCollectionAlternatives(
MethodInvocationTree tree,
String fullyQualifiedImmutableReplacement,
String mutableReplacement,
VisitorState state) {
SuggestedFix.Builder mutableFix = SuggestedFix.builder();
String toCollectionSelect =
SuggestedFixes.qualifyStaticImport(
"java.util.stream.Collectors.toCollection", mutableFix, state);
return buildDescription(tree)
.addFix(replaceMethodInvocation(tree, fullyQualifiedImmutableReplacement, state))
.addFix(
mutableFix
.addImport(String.format("java.util.%s", mutableReplacement))
.replace(tree, String.format("%s(%s::new)", toCollectionSelect, mutableReplacement))
.build())
.build();
}
private Description suggestToMapAlternatives(MethodInvocationTree tree, VisitorState state) {
int argCount = tree.getArguments().size();
if (argCount > 3) {
return Description.NO_MATCH;
}
return buildDescription(tree)
.addFix(
replaceMethodInvocation(
tree, "com.google.common.collect.ImmutableMap.toImmutableMap", state))
.addFix(
SuggestedFix.builder()
.addImport("java.util.HashMap")
.postfixWith(
tree.getArguments().get(argCount - 1),
(argCount == 2 ? ", (a, b) -> { throw new IllegalStateException(); }" : "")
+ ", HashMap::new")
.build())
.build();
}
private static SuggestedFix replaceMethodInvocation(
MethodInvocationTree tree, String fullyQualifiedReplacement, VisitorState state) {
SuggestedFix.Builder fix = SuggestedFix.builder();
String replacement = SuggestedFixes.qualifyStaticImport(fullyQualifiedReplacement, fix, state);
fix.merge(SuggestedFix.replace(tree.getMethodSelect(), replacement));
return fix.build();
}
}

View File

@@ -1,5 +1,8 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION;
import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.AT_LEAST_ONE;
import static com.google.errorprone.matchers.Matchers.annotations;
import static com.google.errorprone.matchers.Matchers.anyOf;
@@ -7,9 +10,6 @@ import static com.google.errorprone.matchers.Matchers.isType;
import com.google.auto.service.AutoService;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
@@ -28,9 +28,9 @@ import java.util.Optional;
@BugPattern(
name = "EmptyMethod",
summary = "Empty method can likely be deleted",
linkType = LinkType.NONE,
severity = SeverityLevel.SUGGESTION,
tags = StandardTags.SIMPLIFICATION)
linkType = NONE,
severity = SUGGESTION,
tags = SIMPLIFICATION)
public final class EmptyMethodCheck extends BugChecker implements MethodTreeMatcher {
private static final long serialVersionUID = 1L;
private static final Matcher<Tree> PERMITTED_ANNOTATION =

View File

@@ -0,0 +1,161 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.STYLE;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.instanceMethod;
import static java.util.stream.Collectors.joining;
import com.google.auto.service.AutoService;
import com.google.common.base.Splitter;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.util.ASTHelpers;
import com.google.googlejavaformat.java.Formatter;
import com.google.googlejavaformat.java.FormatterException;
import com.google.googlejavaformat.java.ImportOrderer;
import com.google.googlejavaformat.java.JavaFormatterOptions.Style;
import com.google.googlejavaformat.java.RemoveUnusedImports;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.util.Position;
import java.util.List;
import java.util.Optional;
/**
* A {@link BugChecker} which flags improperly formatted Error Prone test code.
*
* <p>All test code should be formatted in accordance with Google Java Format's {@link Formatter}
* output, and imports should be ordered according to the {@link Style#GOOGLE Google} style.
*
* <p>This checker inspects inline code passed to {@code
* com.google.errorprone.CompilationTestHelper} and {@code
* com.google.errorprone.BugCheckerRefactoringTestHelper}. It requires that this code is properly
* formatted and that its imports are organized. Only code that represents the expected output of a
* refactoring operation is allowed to have unused imports, as most {@link BugChecker}s do not (and
* are not able to) remove imports that become obsolete as a result of applying their suggested
* fix(es).
*/
// XXX: Once we target JDK 17 (optionally?) suggest text block fixes.
// XXX: GJF guesses the line separator to be used by inspecting the source. When using text blocks
// this may cause the current unconditional use of `\n` not to be sufficient when building on
// Windows; TBD.
@AutoService(BugChecker.class)
@BugPattern(
name = "ErrorProneTestHelperSourceFormat",
summary = "Test code should follow the Google Java style",
linkType = NONE,
severity = SUGGESTION,
tags = STYLE)
public final class ErrorProneTestHelperSourceFormatCheck extends BugChecker
implements MethodInvocationTreeMatcher {
private static final long serialVersionUID = 1L;
private static final Formatter FORMATTER = new Formatter();
private static final Matcher<ExpressionTree> INPUT_SOURCE_ACCEPTING_METHOD =
anyOf(
instanceMethod()
.onDescendantOf("com.google.errorprone.CompilationTestHelper")
.named("addSourceLines"),
instanceMethod()
.onDescendantOf("com.google.errorprone.BugCheckerRefactoringTestHelper")
.named("addInputLines"));
private static final Matcher<ExpressionTree> OUTPUT_SOURCE_ACCEPTING_METHOD =
instanceMethod()
.onDescendantOf("com.google.errorprone.BugCheckerRefactoringTestHelper.ExpectOutput")
.named("addOutputLines");
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
boolean isOutputSource = OUTPUT_SOURCE_ACCEPTING_METHOD.matches(tree, state);
if (!isOutputSource && !INPUT_SOURCE_ACCEPTING_METHOD.matches(tree, state)) {
return Description.NO_MATCH;
}
List<? extends ExpressionTree> sourceLines =
tree.getArguments().subList(1, tree.getArguments().size());
if (sourceLines.isEmpty()) {
return buildDescription(tree).setMessage("No source code provided").build();
}
int startPos = ASTHelpers.getStartPosition(sourceLines.get(0));
int endPos = state.getEndPosition(sourceLines.get(sourceLines.size() - 1));
/* Attempt to format the source code only if it fully consists of constant expressions. */
return getConstantSourceCode(sourceLines)
.map(source -> flagFormattingIssues(startPos, endPos, source, isOutputSource, state))
.orElse(Description.NO_MATCH);
}
private Description flagFormattingIssues(
int startPos, int endPos, String source, boolean retainUnusedImports, VisitorState state) {
Tree methodInvocation = state.getPath().getLeaf();
String formatted;
try {
formatted = formatSourceCode(source, retainUnusedImports).trim();
} catch (FormatterException e) {
return buildDescription(methodInvocation)
.setMessage(String.format("Source code is malformed: %s", e.getMessage()))
.build();
}
if (source.trim().equals(formatted)) {
return Description.NO_MATCH;
}
if (startPos == Position.NOPOS || endPos == Position.NOPOS) {
/*
* We have insufficient source information to emit a fix, so we only flag the fact that the
* code isn't properly formatted.
*/
return describeMatch(methodInvocation);
}
/*
* The code isn't properly formatted; replace all lines with the properly formatted
* alternatives.
*/
return describeMatch(
methodInvocation,
SuggestedFix.replace(
startPos,
endPos,
Splitter.on('\n')
.splitToStream(formatted)
.map(state::getConstantExpression)
.collect(joining(", "))));
}
private static String formatSourceCode(String source, boolean retainUnusedImports)
throws FormatterException {
String withReorderedImports = ImportOrderer.reorderImports(source, Style.GOOGLE);
String withOptionallyRemovedImports =
retainUnusedImports
? withReorderedImports
: RemoveUnusedImports.removeUnusedImports(withReorderedImports);
return FORMATTER.formatSource(withOptionallyRemovedImports);
}
private static Optional<String> getConstantSourceCode(
List<? extends ExpressionTree> sourceLines) {
StringBuilder source = new StringBuilder();
for (ExpressionTree sourceLine : sourceLines) {
Object value = ASTHelpers.constValue(sourceLine);
if (value == null) {
return Optional.empty();
}
source.append(value).append('\n');
}
return Optional.of(source.toString());
}
}

View File

@@ -2,6 +2,9 @@ package tech.picnic.errorprone.bugpatterns;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.ImmutableSetMultimap.toImmutableSetMultimap;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.BugPattern.StandardTags.FRAGILE_CODE;
import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod;
import static java.util.stream.Collectors.collectingAndThen;
@@ -12,9 +15,6 @@ import com.google.common.collect.Multimaps;
import com.google.common.collect.Ordering;
import com.google.common.collect.Sets;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
@@ -37,9 +37,9 @@ import java.util.stream.Stream;
@BugPattern(
name = "ExplicitEnumOrdering",
summary = "Make sure `Ordering#explicit` lists all of an enum's values",
linkType = LinkType.NONE,
severity = SeverityLevel.WARNING,
tags = StandardTags.FRAGILE_CODE)
linkType = NONE,
severity = WARNING,
tags = FRAGILE_CODE)
public final class ExplicitEnumOrderingCheck extends BugChecker
implements MethodInvocationTreeMatcher {
private static final long serialVersionUID = 1L;

View File

@@ -0,0 +1,86 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.BugPattern.StandardTags.LIKELY_ERROR;
import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod;
import com.google.auto.service.AutoService;
import com.google.common.collect.Iterables;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MemberReferenceTreeMatcher;
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.fixes.SuggestedFixes;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MemberReferenceTree;
import com.sun.source.tree.MethodInvocationTree;
import java.util.function.Function;
import java.util.function.Supplier;
import reactor.core.publisher.Flux;
/**
* A {@link BugChecker} which flags usages of {@link Flux#flatMap(Function)} and {@link
* Flux#flatMapSequential(Function)}.
*
* <p>{@link Flux#flatMap(Function)} and {@link Flux#flatMapSequential(Function)} eagerly perform up
* to {@link reactor.util.concurrent.Queues#SMALL_BUFFER_SIZE} subscriptions. Additionally, the
* former interleaves values as they are emitted, yielding nondeterministic results. In most cases
* {@link Flux#concatMap(Function)} should be preferred, as it produces consistent results and
* avoids potentially saturating the thread pool on which subscription happens. If {@code
* concatMap}'s single-subscription semantics are undesirable one should invoke a {@code flatMap} or
* {@code flatMapSequential} overload with an explicit concurrency level.
*
* <p>NB: The rarely-used overload {@link Flux#flatMap(Function, Function, Supplier)} is not flagged
* by this check because there is no clear alternative to point to.
*/
@AutoService(BugChecker.class)
@BugPattern(
name = "FluxFlatMapUsage",
summary =
"`Flux#flatMap` and `Flux#flatMapSequential` have subtle semantics; "
+ "please use `Flux#concatMap` or explicitly specify the desired amount of concurrency",
linkType = NONE,
severity = ERROR,
tags = LIKELY_ERROR)
public final class FluxFlatMapUsageCheck extends BugChecker
implements MethodInvocationTreeMatcher, MemberReferenceTreeMatcher {
private static final long serialVersionUID = 1L;
private static final String MAX_CONCURRENCY_ARG_NAME = "MAX_CONCURRENCY";
private static final Matcher<ExpressionTree> FLUX_FLATMAP =
instanceMethod()
.onDescendantOf("reactor.core.publisher.Flux")
.namedAnyOf("flatMap", "flatMapSequential")
.withParameters(Function.class.getName());
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!FLUX_FLATMAP.matches(tree, state)) {
return Description.NO_MATCH;
}
return buildDescription(tree)
.addFix(SuggestedFixes.renameMethodInvocation(tree, "concatMap", state))
.addFix(
SuggestedFix.builder()
.postfixWith(
Iterables.getOnlyElement(tree.getArguments()), ", " + MAX_CONCURRENCY_ARG_NAME)
.build())
.build();
}
@Override
public Description matchMemberReference(MemberReferenceTree tree, VisitorState state) {
if (!FLUX_FLATMAP.matches(tree, state)) {
return Description.NO_MATCH;
}
// Method references are expected to occur very infrequently; generating both variants of
// suggested fixes is not worth the trouble.
return describeMatch(tree);
}
}

View File

@@ -1,5 +1,8 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION;
import static com.google.errorprone.matchers.Matchers.allOf;
import static com.google.errorprone.matchers.Matchers.anyMethod;
import static com.google.errorprone.matchers.Matchers.anyOf;
@@ -10,9 +13,6 @@ import static java.util.stream.Collectors.joining;
import com.google.auto.service.AutoService;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
@@ -31,6 +31,7 @@ import com.sun.source.util.SimpleTreeVisitor;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable;
/**
* A {@link BugChecker} which flags string concatenations that produce a format string; in such
@@ -50,9 +51,9 @@ import java.util.Optional;
@BugPattern(
name = "FormatStringConcatenation",
summary = "Defer string concatenation to the invoked method",
linkType = LinkType.NONE,
severity = SeverityLevel.WARNING,
tags = StandardTags.SIMPLIFICATION)
linkType = NONE,
severity = WARNING,
tags = SIMPLIFICATION)
public final class FormatStringConcatenationCheck extends BugChecker
implements MethodInvocationTreeMatcher {
private static final long serialVersionUID = 1L;
@@ -210,6 +211,7 @@ public final class FormatStringConcatenationCheck extends BugChecker
this.formatSpecifier = formatSpecifier;
}
@Nullable
@Override
public Void visitBinary(BinaryTree tree, VisitorState state) {
if (tree.getKind() == Kind.PLUS && isStringTyped(tree, state)) {
@@ -222,11 +224,13 @@ public final class FormatStringConcatenationCheck extends BugChecker
return null;
}
@Nullable
@Override
public Void visitParenthesized(ParenthesizedTree tree, VisitorState state) {
return tree.getExpression().accept(this, state);
}
@Nullable
@Override
protected Void defaultAction(Tree tree, VisitorState state) {
appendExpression(tree);

View File

@@ -0,0 +1,111 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.staticMethod;
import static com.google.errorprone.suppliers.Suppliers.OBJECT_TYPE;
import com.google.auto.service.AutoService;
import com.google.common.primitives.Primitives;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
import com.google.errorprone.bugpatterns.TypesWithUndefinedEquality;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.fixes.SuggestedFixes;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.util.ASTHelpers;
import com.google.errorprone.util.ASTHelpers.TargetType;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.tools.javac.code.Type;
import com.sun.tools.javac.code.Types;
import java.util.Arrays;
import java.util.List;
/** A {@link BugChecker} that flags redundant identity conversions. */
// XXX: Consider detecting cases where a flagged expression is passed to a method, and where removal
// of the identify conversion would cause a different method overload to be selected. Depending on
// the target method such a modification may change the code's semantics or performance.
@AutoService(BugChecker.class)
@BugPattern(
name = "IdentityConversion",
summary = "Avoid or clarify identity conversions",
linkType = NONE,
severity = WARNING,
tags = SIMPLIFICATION)
public final class IdentityConversionCheck extends BugChecker
implements MethodInvocationTreeMatcher {
private static final long serialVersionUID = 1L;
private static final Matcher<ExpressionTree> IS_CONVERSION_METHOD =
anyOf(
staticMethod()
.onClassAny(
"com.google.common.collect.ImmutableBiMap",
"com.google.common.collect.ImmutableList",
"com.google.common.collect.ImmutableListMultimap",
"com.google.common.collect.ImmutableMap",
"com.google.common.collect.ImmutableMultimap",
"com.google.common.collect.ImmutableMultiset",
"com.google.common.collect.ImmutableRangeMap",
"com.google.common.collect.ImmutableRangeSet",
"com.google.common.collect.ImmutableSet",
"com.google.common.collect.ImmutableSetMultimap",
"com.google.common.collect.ImmutableTable")
.named("copyOf"),
staticMethod()
.onClassAny(
Primitives.allWrapperTypes().stream()
.map(Class::getName)
.collect(toImmutableSet()))
.named("valueOf"),
staticMethod().onClass(String.class.getName()).named("valueOf"),
staticMethod().onClass("reactor.adapter.rxjava.RxJava2Adapter"),
staticMethod()
.onClass("reactor.core.publisher.Flux")
.namedAnyOf("concat", "firstWithSignal", "from", "merge"),
staticMethod().onClass("reactor.core.publisher.Mono").namedAnyOf("from", "fromDirect"));
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
List<? extends ExpressionTree> arguments = tree.getArguments();
if (arguments.size() != 1 || !IS_CONVERSION_METHOD.matches(tree, state)) {
return Description.NO_MATCH;
}
ExpressionTree sourceTree = arguments.get(0);
Type sourceType = ASTHelpers.getType(sourceTree);
Type resultType = ASTHelpers.getType(tree);
TargetType targetType = ASTHelpers.targetType(state);
if (sourceType == null || resultType == null || targetType == null) {
return Description.NO_MATCH;
}
if (!state.getTypes().isSameType(sourceType, resultType)
&& !isConvertibleWithWellDefinedEquality(sourceType, targetType.type(), state)) {
return Description.NO_MATCH;
}
return buildDescription(tree)
.setMessage(
"This method invocation appears redundant; remove it or suppress this warning and "
+ "add a comment explaining its purpose")
.addFix(SuggestedFix.replace(tree, Util.treeToString(sourceTree, state)))
.addFix(SuggestedFixes.addSuppressWarnings(state, canonicalName()))
.build();
}
private static boolean isConvertibleWithWellDefinedEquality(
Type sourceType, Type targetType, VisitorState state) {
Types types = state.getTypes();
return !types.isSameType(targetType, OBJECT_TYPE.get(state))
&& types.isConvertible(sourceType, targetType)
&& Arrays.stream(TypesWithUndefinedEquality.values())
.noneMatch(b -> b.matchesType(sourceType, state) || b.matchesType(targetType, state));
}
}

View File

@@ -1,10 +1,17 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION;
import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.AT_LEAST_ONE;
import static com.google.errorprone.matchers.Matchers.allOf;
import static com.google.errorprone.matchers.Matchers.annotations;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.enclosingClass;
import static com.google.errorprone.matchers.Matchers.hasModifier;
import static com.google.errorprone.matchers.Matchers.isType;
import static java.util.function.Predicate.not;
import static tech.picnic.errorprone.bugpatterns.JavaKeywords.isReservedKeyword;
import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableSet;
@@ -16,35 +23,45 @@ import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.fixes.SuggestedFixes;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.Matchers;
import com.google.errorprone.matchers.MultiMatcher;
import com.google.errorprone.predicates.TypePredicate;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.ImportTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Symbol;
import java.util.Optional;
import javax.lang.model.element.Modifier;
import javax.lang.model.element.Name;
/** A {@link BugChecker} which flags non-canonical JUnit method declarations. */
// XXX: Consider introducing a class-level check which enforces that test classes:
// 1. Are named `*Test` or `Abstract*TestCase`.
// 2. If not `abstract`, don't have public methods and subclasses.
// 2. If not `abstract`, are package-private and don't have public methods and subclasses.
// 3. Only have private fields.
// XXX: If implemented, the current logic could flag only `private` JUnit methods.
@AutoService(BugChecker.class)
@BugPattern(
name = "JUnitMethodDeclaration",
summary = "JUnit method declaration can likely be improved",
linkType = BugPattern.LinkType.NONE,
severity = BugPattern.SeverityLevel.SUGGESTION,
tags = BugPattern.StandardTags.SIMPLIFICATION)
linkType = NONE,
severity = SUGGESTION,
tags = SIMPLIFICATION)
public final class JUnitMethodDeclarationCheck extends BugChecker implements MethodTreeMatcher {
private static final long serialVersionUID = 1L;
private static final String TEST_PREFIX = "test";
private static final ImmutableSet<Modifier> ILLEGAL_MODIFIERS =
ImmutableSet.of(Modifier.PRIVATE, Modifier.PROTECTED, Modifier.PUBLIC);
private static final MultiMatcher<MethodTree, AnnotationTree> OVERRIDE_METHOD =
annotations(AT_LEAST_ONE, isType("java.lang.Override"));
private static final Matcher<MethodTree> HAS_UNMODIFIABLE_SIGNATURE =
anyOf(
annotations(AT_LEAST_ONE, isType("java.lang.Override")),
allOf(
Matchers.not(hasModifier(Modifier.FINAL)),
Matchers.not(hasModifier(Modifier.PRIVATE)),
enclosingClass(hasModifier(Modifier.ABSTRACT))));
private static final MultiMatcher<MethodTree, AnnotationTree> TEST_METHOD =
annotations(
AT_LEAST_ONE,
@@ -62,9 +79,7 @@ public final class JUnitMethodDeclarationCheck extends BugChecker implements Met
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
// XXX: Perhaps we should also skip analysis of non-`private` non-`final` methods in abstract
// classes?
if (OVERRIDE_METHOD.matches(tree, state)) {
if (HAS_UNMODIFIABLE_SIGNATURE.matches(tree, state)) {
return Description.NO_MATCH;
}
@@ -73,29 +88,101 @@ public final class JUnitMethodDeclarationCheck extends BugChecker implements Met
return Description.NO_MATCH;
}
SuggestedFix.Builder builder = SuggestedFix.builder();
SuggestedFix.Builder fixBuilder = SuggestedFix.builder();
SuggestedFixes.removeModifiers(tree.getModifiers(), state, ILLEGAL_MODIFIERS)
.ifPresent(builder::merge);
.ifPresent(fixBuilder::merge);
if (isTestMethod) {
// XXX: In theory this rename could clash with an existing method or static import. In that
// case we should emit a warning without a suggested replacement.
tryCanonicalizeMethodName(tree, state).ifPresent(builder::merge);
suggestTestMethodRenameIfApplicable(tree, fixBuilder, state);
}
return builder.isEmpty() ? Description.NO_MATCH : describeMatch(tree, builder.build());
return fixBuilder.isEmpty() ? Description.NO_MATCH : describeMatch(tree, fixBuilder.build());
}
private static Optional<SuggestedFix> tryCanonicalizeMethodName(
MethodTree tree, VisitorState state) {
private void suggestTestMethodRenameIfApplicable(
MethodTree tree, SuggestedFix.Builder fixBuilder, VisitorState state) {
tryCanonicalizeMethodName(tree)
.ifPresent(
newName ->
findMethodRenameBlocker(newName, state)
.ifPresentOrElse(
blocker -> reportMethodRenameBlocker(tree, blocker, state),
() -> fixBuilder.merge(SuggestedFixes.renameMethod(tree, newName, state))));
}
private void reportMethodRenameBlocker(MethodTree tree, String reason, VisitorState state) {
state.reportMatch(
buildDescription(tree)
.setMessage(
String.format(
"This method's name should not redundantly start with `%s` (but note that %s)",
TEST_PREFIX, reason))
.build());
}
/**
* If applicable, returns a human-readable argument against assigning the given name to an
* existing method.
*
* <p>This method implements imperfect heuristics. Things it currently does not consider include
* the following:
*
* <ul>
* <li>Whether the rename would merely introduce a method overload, rather than clashing with an
* existing method declaration.
* <li>Whether the rename would cause a method in a superclass to be overridden.
* <li>Whether the rename would in fact clash with a static import. (It could be that a static
* import of the same name is only referenced from lexical scopes in which the method under
* consideration cannot be referenced directly.)
* </ul>
*/
private static Optional<String> findMethodRenameBlocker(String methodName, VisitorState state) {
if (isMethodInEnclosingClass(methodName, state)) {
return Optional.of(
String.format("a method named `%s` already exists in this class", methodName));
}
if (isSimpleNameStaticallyImported(methodName, state)) {
return Optional.of(String.format("`%s` is already statically imported", methodName));
}
if (isReservedKeyword(methodName)) {
return Optional.of(String.format("`%s` is a reserved keyword", methodName));
}
return Optional.empty();
}
private static boolean isMethodInEnclosingClass(String methodName, VisitorState state) {
return state.findEnclosing(ClassTree.class).getMembers().stream()
.filter(MethodTree.class::isInstance)
.map(MethodTree.class::cast)
.map(MethodTree::getName)
.map(Name::toString)
.anyMatch(methodName::equals);
}
private static boolean isSimpleNameStaticallyImported(String simpleName, VisitorState state) {
return state.getPath().getCompilationUnit().getImports().stream()
.filter(ImportTree::isStatic)
.map(ImportTree::getQualifiedIdentifier)
.map(tree -> getStaticImportSimpleName(tree, state))
.anyMatch(simpleName::contentEquals);
}
private static CharSequence getStaticImportSimpleName(Tree tree, VisitorState state) {
String source = Util.treeToString(tree, state);
return source.subSequence(source.lastIndexOf('.') + 1, source.length());
}
private static Optional<String> tryCanonicalizeMethodName(MethodTree tree) {
return Optional.ofNullable(ASTHelpers.getSymbol(tree))
.map(sym -> sym.getQualifiedName().toString())
.filter(name -> name.startsWith(TEST_PREFIX))
.map(name -> name.substring(TEST_PREFIX.length()))
.filter(not(String::isEmpty))
.map(name -> Character.toLowerCase(name.charAt(0)) + name.substring(1))
.filter(name -> !Character.isDigit(name.charAt(0)))
.map(name -> SuggestedFixes.renameMethod(tree, name, state));
.filter(name -> !Character.isDigit(name.charAt(0)));
}
// XXX: Move to a `MoreMatchers` utility class.

View File

@@ -0,0 +1,112 @@
package tech.picnic.errorprone.bugpatterns;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
final class JavaKeywords {
/**
* List of all reserved keywords in the Java language.
*
* @see <a href="https://docs.oracle.com/javase/specs/jls/se17/html/jls-3.html#jls-3.9">JDK 17 JLS
* section 3.9: Keywords</a>
*/
private static final ImmutableSet<String> RESERVED_KEYWORDS =
ImmutableSet.of(
"_",
"abstract",
"assert",
"boolean",
"break",
"byte",
"case",
"catch",
"char",
"class",
"const",
"continue",
"default",
"do",
"double",
"else",
"enum",
"extends",
"final",
"finally",
"float",
"for",
"goto",
"if",
"implements",
"import",
"instanceof",
"int",
"interface",
"long",
"native",
"new",
"package",
"private",
"protected",
"public",
"return",
"short",
"static",
"strictfp",
"super",
"switch",
"synchronized",
"this",
"throw",
"throws",
"transient",
"try",
"void",
"volatile",
"while");
/**
* List of all contextual keywords in the Java language.
*
* @see <a href="https://docs.oracle.com/javase/specs/jls/se17/html/jls-3.html#jls-3.9">JDK 17 JLS
* section 3.9: Keywords</a>
*/
private static final ImmutableSet<String> CONTEXTUAL_KEYWORDS =
ImmutableSet.of(
"exports",
"module",
"non-sealed",
"open",
"opens",
"permits",
"provides",
"record",
"requires",
"sealed",
"to",
"transitive",
"uses",
"var",
"with",
"yield");
/** List of all keywords in the Java language. */
private static final ImmutableSet<String> ALL_KEYWORDS =
Sets.union(RESERVED_KEYWORDS, CONTEXTUAL_KEYWORDS).immutableCopy();
private JavaKeywords() {}
/** Tells whether the given string is a reserved keyword in the Java language. */
public static boolean isReservedKeyword(String str) {
return RESERVED_KEYWORDS.contains(str);
}
/** Tells whether the given string is a contextual keyword in the Java language. */
public static boolean isContextualKeyword(String str) {
return CONTEXTUAL_KEYWORDS.contains(str);
}
/** Tells whether the given string is a reserved or contextual keyword in the Java language. */
public static boolean isKeyword(String str) {
return ALL_KEYWORDS.contains(str);
}
}

View File

@@ -1,5 +1,8 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.STYLE;
import static java.util.Comparator.comparing;
import static java.util.Comparator.naturalOrder;
import static java.util.stream.Collectors.joining;
@@ -9,9 +12,6 @@ import com.google.common.collect.Comparators;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.ErrorProneFlags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
@@ -37,6 +37,7 @@ import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Stream;
import javax.annotation.Nullable;
/**
* A {@link BugChecker} which flags annotation array listings which aren't sorted lexicographically.
@@ -48,9 +49,9 @@ import java.util.stream.Stream;
@BugPattern(
name = "LexicographicalAnnotationAttributeListing",
summary = "Where possible, sort annotation array attributes lexicographically",
linkType = LinkType.NONE,
severity = SeverityLevel.SUGGESTION,
tags = StandardTags.STYLE)
linkType = NONE,
severity = SUGGESTION,
tags = STYLE)
public final class LexicographicalAnnotationAttributeListingCheck extends BugChecker
implements AnnotationTreeMatcher {
private static final long serialVersionUID = 1L;
@@ -171,18 +172,21 @@ public final class LexicographicalAnnotationAttributeListingCheck extends BugChe
ImmutableList.Builder<ImmutableList<String>> nodes = ImmutableList.builder();
new TreeScanner<Void, Void>() {
@Nullable
@Override
public Void visitIdentifier(IdentifierTree node, Void ctx) {
nodes.add(tokenize(node));
return super.visitIdentifier(node, ctx);
}
@Nullable
@Override
public Void visitLiteral(LiteralTree node, Void ctx) {
nodes.add(tokenize(node));
return super.visitLiteral(node, ctx);
}
@Nullable
@Override
public Void visitPrimitiveType(PrimitiveTypeTree node, Void ctx) {
nodes.add(tokenize(node));

View File

@@ -1,15 +1,15 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.STYLE;
import static java.util.Comparator.comparing;
import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Streams;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
@@ -31,9 +31,9 @@ import java.util.Optional;
@BugPattern(
name = "LexicographicalAnnotationListing",
summary = "Sort annotations lexicographically where possible",
linkType = LinkType.NONE,
severity = SeverityLevel.SUGGESTION,
tags = StandardTags.STYLE)
linkType = NONE,
severity = SUGGESTION,
tags = STYLE)
public final class LexicographicalAnnotationListingCheck extends BugChecker
implements MethodTreeMatcher {
private static final long serialVersionUID = 1L;
@@ -52,7 +52,7 @@ public final class LexicographicalAnnotationListingCheck extends BugChecker
Optional<Fix> fix = tryFixOrdering(originalOrdering, sortedAnnotations, state);
Description.Builder description = buildDescription(tree);
Description.Builder description = buildDescription(originalOrdering.get(0));
fix.ifPresent(description::addFix);
return description.build();
}

View File

@@ -1,14 +1,14 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.STYLE;
import com.google.auto.service.AutoService;
import com.google.common.base.VerifyException;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.LambdaExpressionTreeMatcher;
@@ -53,9 +53,9 @@ import javax.lang.model.element.Name;
@BugPattern(
name = "MethodReferenceUsage",
summary = "Prefer method references over lambda expressions",
linkType = LinkType.NONE,
severity = SeverityLevel.SUGGESTION,
tags = StandardTags.STYLE)
linkType = NONE,
severity = SUGGESTION,
tags = STYLE)
public final class MethodReferenceUsageCheck extends BugChecker
implements LambdaExpressionTreeMatcher {
private static final long serialVersionUID = 1L;

View File

@@ -1,5 +1,8 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.BugPattern.StandardTags.LIKELY_ERROR;
import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.AT_LEAST_ONE;
import static com.google.errorprone.matchers.Matchers.annotations;
import static com.google.errorprone.matchers.Matchers.anyOf;
@@ -7,9 +10,6 @@ import static com.google.errorprone.matchers.Matchers.isType;
import com.google.auto.service.AutoService;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.ClassTreeMatcher;
@@ -26,9 +26,9 @@ import com.sun.source.tree.Tree;
@BugPattern(
name = "MissingRefasterAnnotation",
summary = "The Refaster template contains a method without any Refaster annotations",
linkType = LinkType.NONE,
severity = SeverityLevel.WARNING,
tags = StandardTags.LIKELY_ERROR)
linkType = NONE,
severity = WARNING,
tags = LIKELY_ERROR)
public final class MissingRefasterAnnotationCheck extends BugChecker implements ClassTreeMatcher {
private static final long serialVersionUID = 1L;
private static final MultiMatcher<Tree, AnnotationTree> REFASTER_ANNOTATION =

View File

@@ -1,13 +1,13 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION;
import static com.google.errorprone.matchers.Matchers.staticMethod;
import com.google.auto.service.AutoService;
import com.google.common.collect.Iterables;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
@@ -26,9 +26,9 @@ import java.util.List;
@BugPattern(
name = "MockitoStubbing",
summary = "Don't unnecessarily use Mockito's `eq(...)`",
linkType = LinkType.NONE,
severity = SeverityLevel.SUGGESTION,
tags = StandardTags.SIMPLIFICATION)
linkType = NONE,
severity = SUGGESTION,
tags = SIMPLIFICATION)
public final class MockitoStubbingCheck extends BugChecker implements MethodInvocationTreeMatcher {
private static final long serialVersionUID = 1L;
private static final Matcher<ExpressionTree> MOCKITO_EQ_METHOD =

View File

@@ -1,16 +1,17 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.BugPattern.StandardTags.PERFORMANCE;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod;
import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.joining;
import com.google.auto.service.AutoService;
import com.google.common.base.VerifyException;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
@@ -30,6 +31,7 @@ import com.sun.tools.javac.tree.JCTree.JCMemberReference;
import java.util.Comparator;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Stream;
/**
* A {@link BugChecker} which flags {@code Comparator#comparing*} invocations that can be replaced
@@ -37,16 +39,15 @@ import java.util.function.Function;
*/
// XXX: Add more documentation. Explain how this is useful in the face of refactoring to more
// specific types.
// XXX: Change this checker's name?
@AutoService(BugChecker.class)
@BugPattern(
name = "PrimitiveComparison",
summary =
"Ensure invocations of `Comparator#comparing{,Double,Int,Long}` match the return type"
+ " of the provided function",
linkType = LinkType.NONE,
severity = SeverityLevel.WARNING,
tags = StandardTags.PERFORMANCE)
linkType = NONE,
severity = WARNING,
tags = PERFORMANCE)
public final class PrimitiveComparisonCheck extends BugChecker
implements MethodInvocationTreeMatcher {
private static final long serialVersionUID = 1L;
@@ -77,23 +78,53 @@ public final class PrimitiveComparisonCheck extends BugChecker
}
return getPotentiallyBoxedReturnType(tree.getArguments().get(0))
.flatMap(cmpType -> tryFix(tree, state, cmpType, isStatic))
.flatMap(cmpType -> attemptMethodInvocationReplacement(tree, cmpType, isStatic, state))
.map(fix -> describeMatch(tree, fix))
.orElse(Description.NO_MATCH);
}
private static Optional<Fix> tryFix(
MethodInvocationTree tree, VisitorState state, Type cmpType, boolean isStatic) {
private static Optional<Fix> attemptMethodInvocationReplacement(
MethodInvocationTree tree, Type cmpType, boolean isStatic, VisitorState state) {
return Optional.ofNullable(ASTHelpers.getSymbol(tree))
.map(methodSymbol -> methodSymbol.getSimpleName().toString())
.flatMap(
actualMethodName ->
Optional.of(getPreferredMethod(state, cmpType, isStatic))
Optional.of(getPreferredMethod(cmpType, isStatic, state))
.filter(not(actualMethodName::equals)))
.map(
preferredMethodName ->
prefixTypeArgumentsIfRelevant(preferredMethodName, tree, cmpType, state))
.map(preferredMethodName -> suggestFix(tree, preferredMethodName, state));
}
private static String getPreferredMethod(VisitorState state, Type cmpType, boolean isStatic) {
/**
* Prefixes the given method name with generic type parameters if it replaces a {@code
* Comparator#comparing{,Double,Long,Int}} method which also has generic type parameters.
*
* <p>Such type parameters are retained as they are likely required.
*
* <p>Note that any type parameter to {@code Comparator#thenComparing} is likely redundant, and in
* any case becomes obsolete once that method is replaced with {@code
* Comparator#thenComparing{Double,Long,Int}}. Conversion in the opposite direction does not
* require the introduction of a generic type parameter.
*/
private static String prefixTypeArgumentsIfRelevant(
String preferredMethodName, MethodInvocationTree tree, Type cmpType, VisitorState state) {
if (tree.getTypeArguments().isEmpty() || preferredMethodName.startsWith("then")) {
return preferredMethodName;
}
String typeArguments =
Stream.concat(
Stream.of(Util.treeToString(tree.getTypeArguments().get(0), state)),
Stream.of(cmpType.tsym.getSimpleName())
.filter(u -> "comparing".equals(preferredMethodName)))
.collect(joining(", ", "<", ">"));
return typeArguments + preferredMethodName;
}
private static String getPreferredMethod(Type cmpType, boolean isStatic, VisitorState state) {
Types types = state.getTypes();
Symtab symtab = state.getSymtab();
@@ -128,9 +159,6 @@ public final class PrimitiveComparisonCheck extends BugChecker
}
}
// XXX: We drop explicitly specified generic type information. In case the number of type
// arguments before and after doesn't match, that's for the better. But if we e.g. replace
// `comparingLong` with `comparingInt`, then we should retain it.
private static Fix suggestFix(
MethodInvocationTree tree, String preferredMethodName, VisitorState state) {
ExpressionTree expr = tree.getMethodSelect();

View File

@@ -1,5 +1,8 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION;
import static com.google.errorprone.matchers.Matchers.allOf;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.isNonNullUsingDataflow;
@@ -12,9 +15,6 @@ import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod;
import com.google.auto.service.AutoService;
import com.google.common.collect.Iterables;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.ErrorProneFlags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
@@ -47,9 +47,9 @@ import java.util.Optional;
@BugPattern(
name = "RedundantStringConversion",
summary = "Avoid redundant string conversions when possible",
linkType = LinkType.NONE,
severity = SeverityLevel.SUGGESTION,
tags = StandardTags.SIMPLIFICATION)
linkType = NONE,
severity = SUGGESTION,
tags = SIMPLIFICATION)
public final class RedundantStringConversionCheck extends BugChecker
implements BinaryTreeMatcher, CompoundAssignmentTreeMatcher, MethodInvocationTreeMatcher {
private static final long serialVersionUID = 1L;

View File

@@ -1,12 +1,12 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION;
import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod;
import com.google.auto.service.AutoService;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
@@ -27,9 +27,9 @@ import com.sun.source.tree.MethodInvocationTree;
@BugPattern(
name = "RefasterAnyOfUsage",
summary = "`Refaster#anyOf` should be passed at least two parameters",
linkType = LinkType.NONE,
severity = SeverityLevel.SUGGESTION,
tags = StandardTags.SIMPLIFICATION)
linkType = NONE,
severity = SUGGESTION,
tags = SIMPLIFICATION)
public final class RefasterAnyOfUsageCheck extends BugChecker
implements MethodInvocationTreeMatcher {
private static final long serialVersionUID = 1L;

View File

@@ -2,7 +2,9 @@ package tech.picnic.errorprone.bugpatterns;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableRangeSet.toImmutableRangeSet;
import static java.util.Objects.requireNonNullElseGet;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION;
import static java.util.function.Predicate.not;
import com.google.auto.service.AutoService;
@@ -19,9 +21,6 @@ import com.google.common.collect.TreeRangeSet;
import com.google.common.reflect.ClassPath;
import com.google.common.reflect.ClassPath.ResourceInfo;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.CodeTransformer;
import com.google.errorprone.CompositeCodeTransformer;
import com.google.errorprone.ErrorProneFlags;
@@ -60,9 +59,9 @@ import java.util.stream.Stream;
@BugPattern(
name = "Refaster",
summary = "Write idiomatic code when possible",
linkType = LinkType.NONE,
severity = SeverityLevel.SUGGESTION,
tags = StandardTags.SIMPLIFICATION)
linkType = NONE,
severity = SUGGESTION,
tags = SIMPLIFICATION)
public final class RefasterCheck extends BugChecker implements CompilationUnitTreeMatcher {
private static final long serialVersionUID = 1L;
private static final String REFASTER_TEMPLATE_SUFFIX = ".refaster";
@@ -167,11 +166,12 @@ public final class RefasterCheck extends BugChecker implements CompilationUnitTr
flags
.get(INCLUDED_TEMPLATES_PATTERN_FLAG)
.map(Pattern::compile)
.map(nameFilter -> filterCodeTransformers(allTransformers, nameFilter))
.<ImmutableCollection<CodeTransformer>>map(
nameFilter -> filterCodeTransformers(allTransformers, nameFilter))
.orElseGet(allTransformers::values));
}
private static ImmutableCollection<CodeTransformer> filterCodeTransformers(
private static ImmutableList<CodeTransformer> filterCodeTransformers(
ImmutableListMultimap<String, CodeTransformer> transformers, Pattern nameFilter) {
return transformers.entries().stream()
.filter(e -> nameFilter.matcher(e.getKey()).matches())
@@ -196,10 +196,7 @@ public final class RefasterCheck extends BugChecker implements CompilationUnitTr
private static ImmutableSet<ResourceInfo> getClassPathResources() {
try {
return ClassPath.from(
requireNonNullElseGet(
RefasterCheck.class.getClassLoader(), ClassLoader::getSystemClassLoader))
.getResources();
return ClassPath.from(RefasterCheck.class.getClassLoader()).getResources();
} catch (IOException e) {
throw new UncheckedIOException("Failed to scan classpath for resources", e);
}

View File

@@ -1,20 +1,19 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.BugPattern.StandardTags.LIKELY_ERROR;
import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.ALL;
import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.AT_LEAST_ONE;
import static com.google.errorprone.matchers.Matchers.annotations;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.isSameType;
import static com.google.errorprone.matchers.Matchers.isSubtypeOf;
import static com.google.errorprone.matchers.Matchers.isType;
import static com.google.errorprone.matchers.Matchers.methodHasParameters;
import static com.google.errorprone.matchers.Matchers.not;
import com.google.auto.service.AutoService;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
@@ -33,9 +32,9 @@ import com.sun.source.tree.Tree;
@BugPattern(
name = "RequestMappingAnnotation",
summary = "Make sure all `@RequestMapping` method parameters are annotated",
linkType = LinkType.NONE,
severity = SeverityLevel.WARNING,
tags = StandardTags.LIKELY_ERROR)
linkType = NONE,
severity = WARNING,
tags = LIKELY_ERROR)
public final class RequestMappingAnnotationCheck extends BugChecker implements MethodTreeMatcher {
private static final long serialVersionUID = 1L;
private static final String ANN_PACKAGE_PREFIX = "org.springframework.web.bind.annotation.";
@@ -51,8 +50,11 @@ public final class RequestMappingAnnotationCheck extends BugChecker implements M
isType(ANN_PACKAGE_PREFIX + "PostMapping"),
isType(ANN_PACKAGE_PREFIX + "PutMapping"),
isType(ANN_PACKAGE_PREFIX + "RequestMapping")));
// XXX: Add other parameters as necessary. See
// https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#mvc-ann-arguments.
// XXX: Add other parameters as necessary. Also consider whether it makes sense to have WebMVC-
// and WebFlux-specific logic. See
// https://docs.spring.io/spring-framework/docs/current/reference/html/web.html#mvc-ann-arguments
// and
// https://docs.spring.io/spring-framework/docs/current/reference/html/web-reactive.html#webflux-ann-arguments.
private static final Matcher<MethodTree> LACKS_PARAMETER_ANNOTATION =
not(
methodHasParameters(
@@ -66,10 +68,17 @@ public final class RequestMappingAnnotationCheck extends BugChecker implements M
isType(ANN_PACKAGE_PREFIX + "RequestHeader"),
isType(ANN_PACKAGE_PREFIX + "RequestParam"))),
isSameType("java.io.InputStream"),
isSameType("java.time.ZoneId"),
isSameType("java.util.Locale"),
isSameType("java.util.TimeZone"),
isSameType("javax.servlet.http.HttpServletRequest"),
isSameType("javax.servlet.http.HttpServletResponse"),
isSameType("org.springframework.http.HttpMethod"),
isSubtypeOf("org.springframework.web.context.request.WebRequest"))));
isSameType("org.springframework.web.context.request.NativeWebRequest"),
isSameType("org.springframework.web.context.request.WebRequest"),
isSameType("org.springframework.web.server.ServerWebExchange"),
isSameType("org.springframework.web.util.UriBuilder"),
isSameType("org.springframework.web.util.UriComponentsBuilder"))));
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {

View File

@@ -0,0 +1,45 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.BugPattern.StandardTags.LIKELY_ERROR;
import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.AT_LEAST_ONE;
import static com.google.errorprone.matchers.Matchers.allOf;
import static com.google.errorprone.matchers.Matchers.annotations;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.isSubtypeOf;
import static com.google.errorprone.matchers.Matchers.isType;
import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableCollection;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.VariableTreeMatcher;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.sun.source.tree.VariableTree;
/** A {@link BugChecker} which flags {@code @RequestParam} parameters with an unsupported type. */
@AutoService(BugChecker.class)
@BugPattern(
name = "RequestParamType",
summary = "`@RequestParam` does not support `ImmutableCollection` and `ImmutableMap` subtypes",
linkType = NONE,
severity = ERROR,
tags = LIKELY_ERROR)
public final class RequestParamTypeCheck extends BugChecker implements VariableTreeMatcher {
private static final long serialVersionUID = 1L;
private static final Matcher<VariableTree> HAS_UNSUPPORTED_REQUEST_PARAM =
allOf(
annotations(AT_LEAST_ONE, isType("org.springframework.web.bind.annotation.RequestParam")),
anyOf(isSubtypeOf(ImmutableCollection.class), isSubtypeOf(ImmutableMap.class)));
@Override
public Description matchVariable(VariableTree tree, VisitorState state) {
return HAS_UNSUPPORTED_REQUEST_PARAM.matches(tree, state)
? describeMatch(tree)
: Description.NO_MATCH;
}
}

View File

@@ -0,0 +1,88 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
import static com.google.errorprone.BugPattern.StandardTags.LIKELY_ERROR;
import static com.google.errorprone.matchers.ChildMultiMatcher.MatchType.AT_LEAST_ONE;
import static com.google.errorprone.matchers.Matchers.annotations;
import static com.google.errorprone.matchers.Matchers.hasAnnotation;
import static com.google.errorprone.matchers.Matchers.isType;
import com.google.auto.common.AnnotationMirrors;
import com.google.auto.service.AutoService;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.errorprone.BugPattern;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodTreeMatcher;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.fixes.SuggestedFixes;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.MultiMatcher;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.AnnotationTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
/**
* A {@link BugChecker} which flags methods with Spring's {@code @Scheduled} annotation that lack
* New Relic Agent's {@code @Trace(dispatcher = true)}.
*/
@AutoService(BugChecker.class)
@BugPattern(
name = "ScheduledTransactionTrace",
summary = "Scheduled operation must start a new New Relic transaction",
linkType = NONE,
severity = ERROR,
tags = LIKELY_ERROR)
public final class ScheduledTransactionTraceCheck extends BugChecker implements MethodTreeMatcher {
private static final long serialVersionUID = 1L;
private static final String TRACE_ANNOTATION_FQCN = "com.newrelic.api.agent.Trace";
private static final Matcher<Tree> IS_SCHEDULED =
hasAnnotation("org.springframework.scheduling.annotation.Scheduled");
private static final MultiMatcher<Tree, AnnotationTree> TRACE_ANNOTATION =
annotations(AT_LEAST_ONE, isType(TRACE_ANNOTATION_FQCN));
@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
if (!IS_SCHEDULED.matches(tree, state)) {
return Description.NO_MATCH;
}
ImmutableList<AnnotationTree> traceAnnotations =
TRACE_ANNOTATION.multiMatchResult(tree, state).matchingNodes();
if (traceAnnotations.isEmpty()) {
/* This method completely lacks the `@Trace` annotation; add it. */
return describeMatch(
tree,
SuggestedFix.builder()
.addImport(TRACE_ANNOTATION_FQCN)
.prefixWith(tree, "@Trace(dispatcher = true)")
.build());
}
AnnotationTree traceAnnotation = Iterables.getOnlyElement(traceAnnotations);
if (isCorrectAnnotation(traceAnnotation)) {
return Description.NO_MATCH;
}
/*
* The `@Trace` annotation is present but does not specify `dispatcher = true`. Add or update
* the `dispatcher` annotation element.
*/
return describeMatch(
traceAnnotation,
SuggestedFixes.updateAnnotationArgumentValues(
traceAnnotation, state, "dispatcher", ImmutableList.of("true"))
.build());
}
private static boolean isCorrectAnnotation(AnnotationTree traceAnnotation) {
return Boolean.TRUE.equals(
AnnotationMirrors.getAnnotationValue(
ASTHelpers.getAnnotationMirror(traceAnnotation), "dispatcher")
.getValue());
}
}

View File

@@ -1,15 +1,15 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.common.base.Verify.verify;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.BugPattern.StandardTags.LIKELY_ERROR;
import static com.google.errorprone.matchers.Matchers.isSubtypeOf;
import static com.google.errorprone.matchers.method.MethodMatchers.instanceMethod;
import com.google.auto.service.AutoService;
import com.google.common.base.Splitter;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
@@ -33,9 +33,9 @@ import java.util.Optional;
@BugPattern(
name = "Slf4jLogStatement",
summary = "Make sure SLF4J log statements contain proper placeholders with matching arguments",
linkType = LinkType.NONE,
severity = SeverityLevel.WARNING,
tags = StandardTags.LIKELY_ERROR)
linkType = NONE,
severity = WARNING,
tags = LIKELY_ERROR)
public final class Slf4jLogStatementCheck extends BugChecker
implements MethodInvocationTreeMatcher {
private static final long serialVersionUID = 1L;

View File

@@ -1,6 +1,9 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.common.base.Verify.verify;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.joining;
@@ -9,9 +12,6 @@ import com.google.common.base.VerifyException;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.AnnotationTreeMatcher;
@@ -35,9 +35,9 @@ import java.util.Optional;
name = "SpringMvcAnnotation",
summary =
"Prefer the conciseness of `@{Get,Put,Post,Delete,Patch}Mapping` over `@RequestMapping`",
linkType = LinkType.NONE,
severity = SeverityLevel.SUGGESTION,
tags = StandardTags.SIMPLIFICATION)
linkType = NONE,
severity = SUGGESTION,
tags = SIMPLIFICATION)
public final class SpringMvcAnnotationCheck extends BugChecker implements AnnotationTreeMatcher {
private static final long serialVersionUID = 1L;
private static final String ANN_PACKAGE_PREFIX = "org.springframework.web.bind.annotation.";

View File

@@ -1,5 +1,8 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static com.google.errorprone.BugPattern.StandardTags.SIMPLIFICATION;
import static java.util.Objects.requireNonNull;
import com.google.auto.service.AutoService;
@@ -7,9 +10,6 @@ import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MemberSelectTreeMatcher;
@@ -19,14 +19,18 @@ import com.google.errorprone.fixes.Fix;
import com.google.errorprone.fixes.SuggestedFix;
import com.google.errorprone.fixes.SuggestedFixes;
import com.google.errorprone.matchers.Description;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.source.tree.Tree;
import com.sun.tools.javac.code.Type;
import java.util.Optional;
/** A {@link BugChecker} which flags methods that can and should be statically imported. */
/**
* A {@link BugChecker} which flags methods and constants that can and should be statically
* imported.
*/
// XXX: Tricky cases:
// - `org.springframework.http.MediaType` (do except for `ALL`?)
// - `org.springframework.http.HttpStatus` (not always an improvement, and `valueOf` must
// certainly be excluded)
// - `com.google.common.collect.Tables`
@@ -42,20 +46,28 @@ import java.util.Optional;
@AutoService(BugChecker.class)
@BugPattern(
name = "StaticImport",
summary = "Method should be statically imported",
linkType = LinkType.NONE,
severity = SeverityLevel.SUGGESTION,
tags = StandardTags.SIMPLIFICATION)
summary = "Identifier should be statically imported",
linkType = NONE,
severity = SUGGESTION,
tags = SIMPLIFICATION)
public final class StaticImportCheck extends BugChecker implements MemberSelectTreeMatcher {
private static final long serialVersionUID = 1L;
/**
* Types whose members should be statically imported, unless exempted by {@link
* #STATIC_IMPORT_EXEMPTED_MEMBERS} or {@link #STATIC_IMPORT_EXEMPTED_IDENTIFIERS}.
*/
@VisibleForTesting
static final ImmutableSet<String> STATIC_IMPORT_CANDIDATE_CLASSES =
static final ImmutableSet<String> STATIC_IMPORT_CANDIDATE_TYPES =
ImmutableSet.of(
"com.google.common.base.Preconditions",
"com.google.common.base.Predicates",
"com.google.common.base.Verify",
"com.google.common.collect.MoreCollectors",
"com.google.errorprone.BugPattern.LinkType",
"com.google.errorprone.BugPattern.SeverityLevel",
"com.google.errorprone.BugPattern.StandardTags",
"com.google.errorprone.refaster.ImportPolicy",
"com.mongodb.client.model.Accumulators",
"com.mongodb.client.model.Aggregates",
"com.mongodb.client.model.Filters",
@@ -64,8 +76,10 @@ public final class StaticImportCheck extends BugChecker implements MemberSelectT
"com.mongodb.client.model.Sorts",
"com.mongodb.client.model.Updates",
"java.nio.charset.StandardCharsets",
"java.util.Collections",
"java.util.Comparator",
"java.util.Map.Entry",
"java.util.regex.Pattern",
"java.util.stream.Collectors",
"org.assertj.core.api.Assertions",
"org.assertj.core.api.InstanceOfAssertFactories",
@@ -84,11 +98,13 @@ public final class StaticImportCheck extends BugChecker implements MemberSelectT
"org.springframework.format.annotation.DateTimeFormat.ISO",
"org.springframework.http.HttpHeaders",
"org.springframework.http.HttpMethod",
"org.springframework.http.MediaType",
"org.testng.Assert",
"reactor.function.TupleUtils");
/** Type members that should be statically imported. */
@VisibleForTesting
static final ImmutableSetMultimap<String, String> STATIC_IMPORT_CANDIDATE_METHODS =
static final ImmutableSetMultimap<String, String> STATIC_IMPORT_CANDIDATE_MEMBERS =
ImmutableSetMultimap.<String, String>builder()
.putAll(
"com.google.common.collect.ImmutableListMultimap",
@@ -109,8 +125,10 @@ public final class StaticImportCheck extends BugChecker implements MemberSelectT
.put("com.google.common.collect.ImmutableTable", "toImmutableTable")
.put("com.google.common.collect.Sets", "toImmutableEnumSet")
.put("com.google.common.base.Functions", "identity")
.put("java.time.ZoneOffset", "UTC")
.put("java.util.function.Function", "identity")
.put("java.util.function.Predicate", "not")
.put("java.util.UUID", "randomUUID")
.put("org.junit.jupiter.params.provider.Arguments", "arguments")
.putAll(
"java.util.Objects",
@@ -123,9 +141,57 @@ public final class StaticImportCheck extends BugChecker implements MemberSelectT
.putAll("com.google.common.collect.Comparators", "emptiesFirst", "emptiesLast")
.build();
/**
* Type members that should never be statically imported.
*
* <p>Identifiers listed by {@link #STATIC_IMPORT_EXEMPTED_IDENTIFIERS} should be omitted from
* this collection.
*/
// XXX: Perhaps the set of exempted `java.util.Collections` methods is too strict. For now any
// method name that could be considered "too vague" or could conceivably mean something else in a
// specific context is left out.
@VisibleForTesting
static final ImmutableSetMultimap<String, String> STATIC_IMPORT_EXEMPTED_MEMBERS =
ImmutableSetMultimap.<String, String>builder()
.put("com.mongodb.client.model.Filters", "empty")
.putAll(
"java.util.Collections",
"addAll",
"copy",
"fill",
"list",
"max",
"min",
"nCopies",
"rotate",
"sort",
"swap")
.putAll("java.util.regex.Pattern", "compile", "matches", "quote")
.put("org.springframework.http.MediaType", "ALL")
.build();
/**
* Identifiers that should never be statically imported.
*
* <p>This should be a superset of the identifiers flagged by {@link
* com.google.errorprone.bugpatterns.BadImport}.
*/
@VisibleForTesting
static final ImmutableSet<String> STATIC_IMPORT_EXEMPTED_IDENTIFIERS =
ImmutableSet.of(
"builder",
"create",
"copyOf",
"from",
"getDefaultInstance",
"INSTANCE",
"newBuilder",
"of",
"valueOf");
@Override
public Description matchMemberSelect(MemberSelectTree tree, VisitorState state) {
if (!isCandidate(state)) {
if (!isCandidateContext(state) || !isCandidate(tree)) {
return Description.NO_MATCH;
}
@@ -140,7 +206,7 @@ public final class StaticImportCheck extends BugChecker implements MemberSelectT
.orElse(Description.NO_MATCH);
}
private static boolean isCandidate(VisitorState state) {
private static boolean isCandidateContext(VisitorState state) {
Tree parentTree =
requireNonNull(state.getPath().getParentPath(), "MemberSelectTree lacks enclosing node")
.getLeaf();
@@ -155,6 +221,17 @@ public final class StaticImportCheck extends BugChecker implements MemberSelectT
}
}
private static boolean isCandidate(MemberSelectTree tree) {
String identifier = tree.getIdentifier().toString();
if (STATIC_IMPORT_EXEMPTED_IDENTIFIERS.contains(identifier)) {
return false;
}
Type type = ASTHelpers.getType(tree.getExpression());
return type != null
&& !STATIC_IMPORT_EXEMPTED_MEMBERS.containsEntry(type.toString(), identifier);
}
private static Optional<String> getCandidateSimpleName(StaticImportInfo importInfo) {
String canonicalName = importInfo.canonicalName();
return importInfo
@@ -162,8 +239,8 @@ public final class StaticImportCheck extends BugChecker implements MemberSelectT
.toJavaUtil()
.filter(
name ->
STATIC_IMPORT_CANDIDATE_CLASSES.contains(canonicalName)
|| STATIC_IMPORT_CANDIDATE_METHODS.containsEntry(canonicalName, name));
STATIC_IMPORT_CANDIDATE_TYPES.contains(canonicalName)
|| STATIC_IMPORT_CANDIDATE_MEMBERS.containsEntry(canonicalName, name));
}
private static Optional<Fix> tryStaticImport(

View File

@@ -1,5 +1,8 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.LinkType.NONE;
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.BugPattern.StandardTags.FRAGILE_CODE;
import static com.google.errorprone.matchers.Matchers.allOf;
import static com.google.errorprone.matchers.Matchers.anyOf;
import static com.google.errorprone.matchers.Matchers.enclosingClass;
@@ -10,9 +13,6 @@ import static com.google.errorprone.matchers.Matchers.staticMethod;
import com.google.auto.service.AutoService;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.LinkType;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.BugPattern.StandardTags;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;
@@ -32,9 +32,9 @@ import java.time.LocalTime;
name = "TimeZoneUsage",
summary =
"Derive the current time from an existing `Clock` Spring bean, and don't rely on a `Clock`'s time zone",
linkType = LinkType.NONE,
severity = SeverityLevel.WARNING,
tags = StandardTags.FRAGILE_CODE)
linkType = NONE,
severity = WARNING,
tags = FRAGILE_CODE)
public final class TimeZoneUsageCheck extends BugChecker implements MethodInvocationTreeMatcher {
private static final long serialVersionUID = 1L;
private static final Matcher<ExpressionTree> BANNED_TIME_METHOD =
@@ -59,7 +59,7 @@ public final class TimeZoneUsageCheck extends BugChecker implements MethodInvoca
LocalDateTime.class.getName(),
LocalTime.class.getName())
.named("now"),
staticMethod().onClassAny(Instant.class.getName()).named("now").withParameters());
staticMethod().onClassAny(Instant.class.getName()).named("now").withNoParameters());
@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {

View File

@@ -8,13 +8,22 @@ import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import java.math.BigDecimal;
import org.assertj.core.api.AbstractBigDecimalAssert;
import org.assertj.core.api.BigDecimalAssert;
// XXX: If we add a rule which drops unnecessary `L` suffixes from literal longs, then the `0L`/`1L`
// cases below can go.
/**
* Refaster templates related to AssertJ assertions over {@link BigDecimal}s.
*
* <p>Note that, contrary to collections of Refaster templates for other {@link
* org.assertj.core.api.NumberAssert} subtypes, these templates do not rewrite to/from {@link
* BigDecimalAssert#isEqualTo(Object)} and {@link BigDecimalAssert#isNotEqualTo(Object)}. This is
* because {@link BigDecimal#equals(Object)} considers not only the numeric value of compared
* instances, but also their scale. As a result various seemingly straightforward transformations
* would actually subtly change the assertion's semantics.
*/
final class AssertJBigDecimalTemplates {
private AssertJBigDecimalTemplates() {}
static final class AbstractBigDecimalAssertIsEqualTo {
static final class AbstractBigDecimalAssertIsEqualByComparingTo {
@BeforeTemplate
AbstractBigDecimalAssert<?> before(AbstractBigDecimalAssert<?> bigDecimalAssert, BigDecimal n) {
return Refaster.anyOf(
@@ -24,11 +33,11 @@ final class AssertJBigDecimalTemplates {
@AfterTemplate
AbstractBigDecimalAssert<?> after(AbstractBigDecimalAssert<?> bigDecimalAssert, BigDecimal n) {
return bigDecimalAssert.isEqualTo(n);
return bigDecimalAssert.isEqualByComparingTo(n);
}
}
static final class AbstractBigDecimalAssertIsNotEqualTo {
static final class AbstractBigDecimalAssertIsNotEqualByComparingTo {
@BeforeTemplate
AbstractBigDecimalAssert<?> before(AbstractBigDecimalAssert<?> bigDecimalAssert, BigDecimal n) {
return Refaster.anyOf(
@@ -38,52 +47,7 @@ final class AssertJBigDecimalTemplates {
@AfterTemplate
AbstractBigDecimalAssert<?> after(AbstractBigDecimalAssert<?> bigDecimalAssert, BigDecimal n) {
return bigDecimalAssert.isNotEqualTo(n);
}
}
static final class AbstractBigDecimalAssertIsZero {
@BeforeTemplate
AbstractBigDecimalAssert<?> before(AbstractBigDecimalAssert<?> bigDecimalAssert) {
return Refaster.anyOf(
bigDecimalAssert.isZero(),
bigDecimalAssert.isEqualTo(0L),
bigDecimalAssert.isEqualTo(BigDecimal.ZERO));
}
@AfterTemplate
AbstractBigDecimalAssert<?> after(AbstractBigDecimalAssert<?> bigDecimalAssert) {
return bigDecimalAssert.isEqualTo(0);
}
}
static final class AbstractBigDecimalAssertIsNotZero {
@BeforeTemplate
AbstractBigDecimalAssert<?> before(AbstractBigDecimalAssert<?> bigDecimalAssert) {
return Refaster.anyOf(
bigDecimalAssert.isNotZero(),
bigDecimalAssert.isNotEqualTo(0L),
bigDecimalAssert.isNotEqualTo(BigDecimal.ZERO));
}
@AfterTemplate
AbstractBigDecimalAssert<?> after(AbstractBigDecimalAssert<?> bigDecimalAssert) {
return bigDecimalAssert.isNotEqualTo(0);
}
}
static final class AbstractBigDecimalAssertIsOne {
@BeforeTemplate
AbstractBigDecimalAssert<?> before(AbstractBigDecimalAssert<?> bigDecimalAssert) {
return Refaster.anyOf(
bigDecimalAssert.isOne(),
bigDecimalAssert.isEqualTo(1L),
bigDecimalAssert.isEqualTo(BigDecimal.ONE));
}
@AfterTemplate
AbstractBigDecimalAssert<?> after(AbstractBigDecimalAssert<?> bigDecimalAssert) {
return bigDecimalAssert.isEqualTo(1);
return bigDecimalAssert.isNotEqualByComparingTo(n);
}
}
}

View File

@@ -1,8 +1,8 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.assertj.core.api.Assertions.assertThat;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -59,7 +59,7 @@ final class AssertJBooleanTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractBooleanAssert<?> after(boolean b) {
return assertThat(b).isTrue();
}
@@ -88,7 +88,7 @@ final class AssertJBooleanTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractBooleanAssert<?> after(boolean b) {
return assertThat(b).isFalse();
}

View File

@@ -15,11 +15,7 @@ final class AssertJByteTemplates {
@BeforeTemplate
AbstractByteAssert<?> before(AbstractByteAssert<?> byteAssert, byte n) {
return Refaster.anyOf(
byteAssert.isCloseTo(n, offset((byte) 0)),
byteAssert.isCloseTo(Byte.valueOf(n), offset((byte) 0)),
byteAssert.isCloseTo(n, withPercentage(0)),
byteAssert.isCloseTo(Byte.valueOf(n), withPercentage(0)),
byteAssert.isEqualTo(Byte.valueOf(n)));
byteAssert.isCloseTo(n, offset((byte) 0)), byteAssert.isCloseTo(n, withPercentage(0)));
}
@AfterTemplate
@@ -33,10 +29,7 @@ final class AssertJByteTemplates {
AbstractByteAssert<?> before(AbstractByteAssert<?> byteAssert, byte n) {
return Refaster.anyOf(
byteAssert.isNotCloseTo(n, offset((byte) 0)),
byteAssert.isNotCloseTo(Byte.valueOf(n), offset((byte) 0)),
byteAssert.isNotCloseTo(n, withPercentage(0)),
byteAssert.isNotCloseTo(Byte.valueOf(n), withPercentage(0)),
byteAssert.isNotEqualTo(Byte.valueOf(n)));
byteAssert.isNotCloseTo(n, withPercentage(0)));
}
@AfterTemplate

View File

@@ -1,8 +1,8 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.assertj.core.api.Assertions.assertThat;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -21,7 +21,7 @@ final class AssertJCharSequenceTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(CharSequence charSequence) {
assertThat(charSequence).isEmpty();
}
@@ -36,7 +36,7 @@ final class AssertJCharSequenceTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractAssert<?, ?> after(CharSequence charSequence) {
return assertThat(charSequence).isNotEmpty();
}
@@ -49,7 +49,7 @@ final class AssertJCharSequenceTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractAssert<?, ?> after(CharSequence charSequence, int length) {
return assertThat(charSequence).hasSize(length);
}

View File

@@ -36,11 +36,7 @@ final class AssertJDoubleTemplates {
@BeforeTemplate
AbstractDoubleAssert<?> before(AbstractDoubleAssert<?> doubleAssert, double n) {
return Refaster.anyOf(
doubleAssert.isCloseTo(n, offset(0.0)),
doubleAssert.isCloseTo(Double.valueOf(n), offset(0.0)),
doubleAssert.isCloseTo(n, withPercentage(0.0)),
doubleAssert.isCloseTo(Double.valueOf(n), withPercentage(0.0)),
doubleAssert.isEqualTo(Double.valueOf(n)));
doubleAssert.isCloseTo(n, offset(0.0)), doubleAssert.isCloseTo(n, withPercentage(0.0)));
}
@AfterTemplate
@@ -54,10 +50,7 @@ final class AssertJDoubleTemplates {
AbstractDoubleAssert<?> before(AbstractDoubleAssert<?> doubleAssert, double n) {
return Refaster.anyOf(
doubleAssert.isNotCloseTo(n, offset(0.0)),
doubleAssert.isNotCloseTo(Double.valueOf(n), offset(0.0)),
doubleAssert.isNotCloseTo(n, withPercentage(0.0)),
doubleAssert.isNotCloseTo(Double.valueOf(n), withPercentage(0.0)),
doubleAssert.isNotEqualTo(Double.valueOf(n)));
doubleAssert.isNotCloseTo(n, withPercentage(0.0)));
}
@AfterTemplate

View File

@@ -36,11 +36,7 @@ final class AssertJFloatTemplates {
@BeforeTemplate
AbstractFloatAssert<?> before(AbstractFloatAssert<?> floatAssert, float n) {
return Refaster.anyOf(
floatAssert.isCloseTo(n, offset(0F)),
floatAssert.isCloseTo(Float.valueOf(n), offset(0F)),
floatAssert.isCloseTo(n, withPercentage(0)),
floatAssert.isCloseTo(Float.valueOf(n), withPercentage(0)),
floatAssert.isEqualTo(Float.valueOf(n)));
floatAssert.isCloseTo(n, offset(0F)), floatAssert.isCloseTo(n, withPercentage(0)));
}
@AfterTemplate
@@ -53,11 +49,7 @@ final class AssertJFloatTemplates {
@BeforeTemplate
AbstractFloatAssert<?> before(AbstractFloatAssert<?> floatAssert, float n) {
return Refaster.anyOf(
floatAssert.isNotCloseTo(n, offset(0F)),
floatAssert.isNotCloseTo(Float.valueOf(n), offset(0F)),
floatAssert.isNotCloseTo(n, withPercentage(0)),
floatAssert.isNotCloseTo(Float.valueOf(n), withPercentage(0)),
floatAssert.isNotEqualTo(Float.valueOf(n)));
floatAssert.isNotCloseTo(n, offset(0F)), floatAssert.isNotCloseTo(n, withPercentage(0)));
}
@AfterTemplate

View File

@@ -15,11 +15,7 @@ final class AssertJIntegerTemplates {
@BeforeTemplate
AbstractIntegerAssert<?> before(AbstractIntegerAssert<?> intAssert, int n) {
return Refaster.anyOf(
intAssert.isCloseTo(n, offset(0)),
intAssert.isCloseTo(Integer.valueOf(n), offset(0)),
intAssert.isCloseTo(n, withPercentage(0)),
intAssert.isCloseTo(Integer.valueOf(n), withPercentage(0)),
intAssert.isEqualTo(Integer.valueOf(n)));
intAssert.isCloseTo(n, offset(0)), intAssert.isCloseTo(n, withPercentage(0)));
}
@AfterTemplate
@@ -32,11 +28,7 @@ final class AssertJIntegerTemplates {
@BeforeTemplate
AbstractIntegerAssert<?> before(AbstractIntegerAssert<?> intAssert, int n) {
return Refaster.anyOf(
intAssert.isNotCloseTo(n, offset(0)),
intAssert.isNotCloseTo(Integer.valueOf(n), offset(0)),
intAssert.isNotCloseTo(n, withPercentage(0)),
intAssert.isNotCloseTo(Integer.valueOf(n), withPercentage(0)),
intAssert.isNotEqualTo(Integer.valueOf(n)));
intAssert.isNotCloseTo(n, offset(0)), intAssert.isNotCloseTo(n, withPercentage(0)));
}
@AfterTemplate

View File

@@ -15,11 +15,7 @@ final class AssertJLongTemplates {
@BeforeTemplate
AbstractLongAssert<?> before(AbstractLongAssert<?> longAssert, long n) {
return Refaster.anyOf(
longAssert.isCloseTo(n, offset(0L)),
longAssert.isCloseTo(Long.valueOf(n), offset(0L)),
longAssert.isCloseTo(n, withPercentage(0)),
longAssert.isCloseTo(Long.valueOf(n), withPercentage(0)),
longAssert.isEqualTo(Long.valueOf(n)));
longAssert.isCloseTo(n, offset(0L)), longAssert.isCloseTo(n, withPercentage(0)));
}
@AfterTemplate
@@ -32,11 +28,7 @@ final class AssertJLongTemplates {
@BeforeTemplate
AbstractLongAssert<?> before(AbstractLongAssert<?> longAssert, long n) {
return Refaster.anyOf(
longAssert.isNotCloseTo(n, offset(0L)),
longAssert.isNotCloseTo(Long.valueOf(n), offset(0L)),
longAssert.isNotCloseTo(n, withPercentage(0)),
longAssert.isNotCloseTo(Long.valueOf(n), withPercentage(0)),
longAssert.isNotEqualTo(Long.valueOf(n)));
longAssert.isNotCloseTo(n, offset(0L)), longAssert.isNotCloseTo(n, withPercentage(0)));
}
@AfterTemplate

View File

@@ -0,0 +1,35 @@
package tech.picnic.errorprone.refastertemplates;
import com.google.common.collect.ImmutableMap;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import java.util.Map;
import org.assertj.core.api.AbstractMapAssert;
final class AssertJMapTemplates {
private AssertJMapTemplates() {}
static final class AbstractMapAssertContainsExactlyInAnyOrderEntriesOf<K, V> {
@BeforeTemplate
AbstractMapAssert<?, ?, K, V> before(AbstractMapAssert<?, ?, K, V> mapAssert, Map<K, V> map) {
return mapAssert.isEqualTo(map);
}
@AfterTemplate
AbstractMapAssert<?, ?, K, V> after(AbstractMapAssert<?, ?, K, V> mapAssert, Map<K, V> map) {
return mapAssert.containsExactlyInAnyOrderEntriesOf(map);
}
}
static final class AbstractMapAssertContainsExactlyEntriesOf<K, V> {
@BeforeTemplate
AbstractMapAssert<?, ?, K, V> before(AbstractMapAssert<?, ?, K, V> mapAssert, K key, V value) {
return mapAssert.containsExactlyInAnyOrderEntriesOf(ImmutableMap.of(key, value));
}
@AfterTemplate
AbstractMapAssert<?, ?, K, V> after(AbstractMapAssert<?, ?, K, V> mapAssert, K key, V value) {
return mapAssert.containsExactlyEntriesOf(ImmutableMap.of(key, value));
}
}
}

View File

@@ -1,8 +1,12 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.assertj.core.api.Assertions.assertThat;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import java.math.BigDecimal;
import java.math.BigInteger;
import org.assertj.core.api.AbstractBigDecimalAssert;
@@ -18,7 +22,7 @@ import org.assertj.core.api.NumberAssert;
final class AssertJNumberTemplates {
private AssertJNumberTemplates() {}
static final class NumberIsPositive {
static final class NumberAssertIsPositive {
@BeforeTemplate
AbstractByteAssert<?> before(AbstractByteAssert<?> numberAssert) {
return Refaster.anyOf(
@@ -69,7 +73,7 @@ final class AssertJNumberTemplates {
}
}
static final class NumberIsNotPositive {
static final class NumberAssertIsNotPositive {
@BeforeTemplate
AbstractByteAssert<?> before(AbstractByteAssert<?> numberAssert) {
return Refaster.anyOf(
@@ -120,7 +124,7 @@ final class AssertJNumberTemplates {
}
}
static final class NumberIsNegative {
static final class NumberAssertIsNegative {
@BeforeTemplate
AbstractByteAssert<?> before(AbstractByteAssert<?> numberAssert) {
return Refaster.anyOf(
@@ -171,7 +175,7 @@ final class AssertJNumberTemplates {
}
}
static final class NumberIsNotNegative {
static final class NumberAssertIsNotNegative {
@BeforeTemplate
AbstractByteAssert<?> before(AbstractByteAssert<?> numberAssert) {
return Refaster.anyOf(
@@ -221,4 +225,40 @@ final class AssertJNumberTemplates {
return numberAssert.isNotNegative();
}
}
static final class AssertThatIsOdd {
@BeforeTemplate
AbstractIntegerAssert<?> before(int number) {
return assertThat(number % 2).isEqualTo(1);
}
@BeforeTemplate
AbstractLongAssert<?> before(long number) {
return assertThat(number % 2).isEqualTo(1);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
NumberAssert<?, ?> after(long number) {
return assertThat(number).isOdd();
}
}
static final class AssertThatIsEven {
@BeforeTemplate
AbstractIntegerAssert<?> before(int number) {
return assertThat(number % 2).isEqualTo(0);
}
@BeforeTemplate
AbstractLongAssert<?> before(long number) {
return assertThat(number % 2).isEqualTo(0);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
NumberAssert<?, ?> after(long number) {
return assertThat(number).isEven();
}
}
}

View File

@@ -1,8 +1,8 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.assertj.core.api.Assertions.assertThat;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -21,7 +21,7 @@ final class AssertJObjectTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ObjectAssert<S> after(S object) {
return assertThat(object).isInstanceOf(Refaster.<T>clazz());
}
@@ -34,7 +34,7 @@ final class AssertJObjectTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ObjectAssert<S> after(S object) {
return assertThat(object).isNotInstanceOf(Refaster.<T>clazz());
}
@@ -47,7 +47,7 @@ final class AssertJObjectTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ObjectAssert<S> after(S object1, T object2) {
return assertThat(object1).isEqualTo(object2);
}
@@ -60,7 +60,7 @@ final class AssertJObjectTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ObjectAssert<S> after(S object1, T object2) {
return assertThat(object1).isNotEqualTo(object2);
}
@@ -73,7 +73,7 @@ final class AssertJObjectTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ObjectAssert<T> after(T object, String str) {
return assertThat(object).hasToString(str);
}

View File

@@ -1,8 +1,8 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.assertj.core.api.Assertions.assertThat;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -26,7 +26,7 @@ final class AssertJOptionalTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, T> after(Optional<T> optional) {
return assertThat(optional).get();
}
@@ -53,7 +53,7 @@ final class AssertJOptionalTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
OptionalAssert<T> after(Optional<T> optional) {
return assertThat(optional).isPresent();
}
@@ -80,7 +80,7 @@ final class AssertJOptionalTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
OptionalAssert<T> after(Optional<T> optional) {
return assertThat(optional).isEmpty();
}
@@ -122,7 +122,7 @@ final class AssertJOptionalTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, T> after(Optional<T> optional, Predicate<? super T> predicate) {
return assertThat(optional).get().matches(predicate);
}

View File

@@ -15,11 +15,7 @@ final class AssertJShortTemplates {
@BeforeTemplate
AbstractShortAssert<?> before(AbstractShortAssert<?> shortAssert, short n) {
return Refaster.anyOf(
shortAssert.isCloseTo(n, offset((short) 0)),
shortAssert.isCloseTo(Short.valueOf(n), offset((short) 0)),
shortAssert.isCloseTo(n, withPercentage(0)),
shortAssert.isCloseTo(Short.valueOf(n), withPercentage(0)),
shortAssert.isEqualTo(Short.valueOf(n)));
shortAssert.isCloseTo(n, offset((short) 0)), shortAssert.isCloseTo(n, withPercentage(0)));
}
@AfterTemplate
@@ -33,10 +29,7 @@ final class AssertJShortTemplates {
AbstractShortAssert<?> before(AbstractShortAssert<?> shortAssert, short n) {
return Refaster.anyOf(
shortAssert.isNotCloseTo(n, offset((short) 0)),
shortAssert.isNotCloseTo(Short.valueOf(n), offset((short) 0)),
shortAssert.isNotCloseTo(n, withPercentage(0)),
shortAssert.isNotCloseTo(Short.valueOf(n), withPercentage(0)),
shortAssert.isNotEqualTo(Short.valueOf(n)));
shortAssert.isNotCloseTo(n, withPercentage(0)));
}
@AfterTemplate

View File

@@ -1,8 +1,8 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.assertj.core.api.Assertions.assertThat;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
@@ -31,7 +31,7 @@ final class AssertJStringTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(String string) {
assertThat(string).isEmpty();
}
@@ -56,9 +56,35 @@ final class AssertJStringTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractAssert<?, ?> after(String string) {
return assertThat(string).isNotEmpty();
}
}
static final class AssertThatMatches {
@BeforeTemplate
AbstractAssert<?, ?> before(String string, String regex) {
return assertThat(string.matches(regex)).isTrue();
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractAssert<?, ?> after(String string, String regex) {
return assertThat(string).matches(regex);
}
}
static final class AssertThatDoesNotMatch {
@BeforeTemplate
AbstractAssert<?, ?> before(String string, String regex) {
return assertThat(string.matches(regex)).isFalse();
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractAssert<?, ?> after(String string, String regex) {
return assertThat(string).doesNotMatch(regex);
}
}
}

View File

@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.assertj.core.api.Assertions.assertThat;
import com.google.common.collect.ImmutableBiMap;
@@ -12,7 +13,6 @@ import com.google.common.collect.ImmutableSortedMultiset;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Iterables;
import com.google.common.collect.Multiset;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -97,7 +97,6 @@ import tech.picnic.errorprone.refaster.util.IsArray;
// XXX: Right now we use and import `Offset.offset` and `Percentage.withPercentage`. Use the AssertJ
// methods instead. (Also in the TestNG migration.)
// ^ Also for `Tuple`!
// XXX: Use `assertThatIllegalArgumentException` and variants.
// XXX: `assertThatCode(x).isInstanceOf(clazz)` -> `assertThatThrownBy(x).isInstanceOf(clazz)`
// (etc.)
// XXX: Look into using Assertions#contentOf(URL url, Charset charset) instead of our own test
@@ -137,7 +136,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
OptionalDoubleAssert after(OptionalDouble optional, double expected) {
return assertThat(optional).hasValue(expected);
}
@@ -156,7 +155,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
OptionalIntAssert after(OptionalInt optional, int expected) {
return assertThat(optional).hasValue(expected);
}
@@ -175,7 +174,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
OptionalLongAssert after(OptionalLong optional, long expected) {
return assertThat(optional).hasValue(expected);
}
@@ -365,6 +364,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ObjectEnumerableAssert<?, S> after(Set<S> set, T element) {
return assertThat(set).containsExactly(element);
}
@@ -429,7 +429,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Collection<E> iterable) {
assertThat(iterable).isEmpty();
}
@@ -453,7 +453,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
IterableAssert<E> after(Iterable<E> iterable) {
return assertThat(iterable).isNotEmpty();
}
@@ -471,7 +471,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
IterableAssert<E> after(Iterable<E> iterable, int length) {
return assertThat(iterable).hasSize(length);
}
@@ -484,7 +484,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
IterableAssert<S> after(Iterable<S> iterable, T element) {
return assertThat(iterable).containsExactly(element);
}
@@ -501,7 +501,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
IterableAssert<S> after(Iterable<S> iterable, T element) {
return assertThat(iterable).containsExactly(element);
}
@@ -518,7 +518,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(List<S> list1, List<T> list2) {
return assertThat(list1).containsExactlyElementsOf(list2);
}
@@ -537,7 +537,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractCollectionAssert<?, ?, S, ?> after(Set<S> set1, Set<T> set2) {
return assertThat(set1).hasSameElementsAs(set2);
}
@@ -554,7 +554,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractCollectionAssert<?, ?, S, ?> after(Multiset<S> multiset1, Multiset<T> multiset2) {
return assertThat(multiset1).containsExactlyInAnyOrderElementsOf(multiset2);
}
@@ -632,7 +632,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Map<K, V> map) {
assertThat(map).isEmpty();
}
@@ -669,7 +669,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
MapAssert<K, V> after(Map<K, V> map) {
return assertThat(map).isNotEmpty();
}
@@ -684,7 +684,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
MapAssert<K, V> after(Map<K, V> map, int length) {
return assertThat(map).hasSize(length);
}
@@ -698,7 +698,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
MapAssert<K, V> after(Map<K, V> map1, Map<K, V> map2) {
return assertThat(map1).hasSameSizeAs(map2);
}
@@ -712,7 +712,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
MapAssert<K, V> after(Map<K, V> map, K key) {
return assertThat(map).containsKey(key);
}
@@ -725,7 +725,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
MapAssert<K, V> after(Map<K, V> map, K key) {
return assertThat(map).doesNotContainKey(key);
}
@@ -738,7 +738,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
MapAssert<K, V> after(Map<K, V> map, K key, V value) {
return assertThat(map).containsEntry(key, value);
}
@@ -763,7 +763,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, Iterable<U> iterable) {
return assertThat(stream).containsAnyElementsOf(iterable);
}
@@ -784,7 +784,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, U[] array) {
return assertThat(stream).containsAnyOf(array);
}
@@ -808,7 +808,7 @@ final class AssertJTemplates {
@AfterTemplate
@SuppressWarnings("ObjectEnumerableContainsOneElement" /* Not a true singleton. */)
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, @Repeated U elements) {
return assertThat(stream).containsAnyOf(elements);
}
@@ -829,7 +829,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, Iterable<U> iterable) {
return assertThat(stream).containsAll(iterable);
}
@@ -850,7 +850,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, U[] array) {
return assertThat(stream).contains(array);
}
@@ -873,7 +873,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, @Repeated U elements) {
return assertThat(stream).contains(elements);
}
@@ -888,7 +888,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, Iterable<U> iterable) {
return assertThat(stream).containsExactlyElementsOf(iterable);
}
@@ -903,7 +903,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, U[] array) {
return assertThat(stream).containsExactly(array);
}
@@ -919,7 +919,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, @Repeated U elements) {
return assertThat(stream).containsExactly(elements);
}
@@ -941,7 +941,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, Iterable<U> iterable) {
return assertThat(stream).containsExactlyInAnyOrderElementsOf(iterable);
}
@@ -962,7 +962,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, U[] array) {
return assertThat(stream).containsExactlyInAnyOrder(array);
}
@@ -988,7 +988,7 @@ final class AssertJTemplates {
@AfterTemplate
@SuppressWarnings("ObjectEnumerableContainsExactlyOneElement" /* Not a true singleton. */)
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, @Repeated U elements) {
return assertThat(stream).containsExactlyInAnyOrder(elements);
}
@@ -1009,7 +1009,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, Iterable<U> iterable) {
return assertThat(stream).containsSequence(iterable);
}
@@ -1026,7 +1026,7 @@ final class AssertJTemplates {
@AfterTemplate
@SuppressWarnings("ObjectEnumerableContainsOneElement" /* Not a true singleton. */)
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, @Repeated U elements) {
return assertThat(stream).containsSequence(elements);
}
@@ -1047,7 +1047,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, Iterable<U> iterable) {
return assertThat(stream).containsSubsequence(iterable);
}
@@ -1065,7 +1065,7 @@ final class AssertJTemplates {
@AfterTemplate
@SuppressWarnings("ObjectEnumerableContainsOneElement" /* Not a true singleton. */)
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, @Repeated U elements) {
return assertThat(stream).containsSubsequence(elements);
}
@@ -1086,7 +1086,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, Iterable<U> iterable) {
return assertThat(stream).doesNotContainAnyElementsOf(iterable);
}
@@ -1107,7 +1107,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, U[] array) {
return assertThat(stream).doesNotContain(array);
}
@@ -1130,7 +1130,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, @Repeated U elements) {
return assertThat(stream).doesNotContain(elements);
}
@@ -1151,7 +1151,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, Iterable<U> iterable) {
return assertThat(stream).doesNotContainSequence(iterable);
}
@@ -1169,7 +1169,7 @@ final class AssertJTemplates {
@AfterTemplate
@SuppressWarnings("ObjectEnumerableDoesNotContainOneElement" /* Not a true singleton. */)
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, @Repeated U elements) {
return assertThat(stream).doesNotContainSequence(elements);
}
@@ -1190,7 +1190,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, Iterable<U> iterable) {
return assertThat(stream).hasSameElementsAs(iterable);
}
@@ -1211,7 +1211,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, U[] array) {
return assertThat(stream).containsOnly(array);
}
@@ -1234,7 +1234,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, @Repeated U elements) {
return assertThat(stream).containsOnly(elements);
}
@@ -1267,7 +1267,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, U[] iterable) {
return assertThat(stream).isSubsetOf(iterable);
}
@@ -1290,7 +1290,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ListAssert<S> after(Stream<S> stream, @Repeated U elements) {
return assertThat(stream).isSubsetOf(elements);
}
@@ -1309,7 +1309,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Stream<S> stream) {
assertThat(stream).isEmpty();
}
@@ -1328,7 +1328,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Stream<S> stream) {
assertThat(stream).isNotEmpty();
}
@@ -1341,7 +1341,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Stream<T> stream, int size) {
assertThat(stream).hasSize(size);
}
@@ -1358,7 +1358,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Predicate<T> predicate, T object) {
assertThat(predicate).accepts(object);
}
@@ -1371,7 +1371,7 @@ final class AssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Predicate<T> predicate, T object) {
assertThat(predicate).rejects(object);
}
@@ -2212,7 +2212,7 @@ final class AssertJTemplates {
// }
//
// @AfterTemplate
// @UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
// @UseImportPolicy(STATIC_IMPORT_ALWAYS)
// IterableAssert<E> after(Iterable<E> iterable, E expected) {
// return assertThat(iterable).containsExactly(expected);
// }

View File

@@ -0,0 +1,363 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatIOException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.Repeated;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import java.io.IOException;
import org.assertj.core.api.AbstractObjectAssert;
import org.assertj.core.api.AbstractThrowableAssert;
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
/**
* Refaster templates related to AssertJ assertions over expressions that may throw a {@link
* Throwable} subtype.
*
* <p>For reasons of consistency we prefer {@link
* org.assertj.core.api.Assertions#assertThatThrownBy} over static methods for specific exception
* types. Note that only the most common assertion expressions are rewritten here; covering all
* cases would require the implementation of an Error Prone check instead.
*/
final class AssertJThrowingCallableTemplates {
private AssertJThrowingCallableTemplates() {}
static final class AssertThatThrownByIllegalArgumentException {
@BeforeTemplate
AbstractObjectAssert<?, ?> before(ThrowingCallable throwingCallable) {
return assertThatIllegalArgumentException().isThrownBy(throwingCallable);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(ThrowingCallable throwingCallable) {
return assertThatThrownBy(throwingCallable).isInstanceOf(IllegalArgumentException.class);
}
}
static final class AssertThatThrownByIllegalArgumentExceptionHasMessage {
@BeforeTemplate
@SuppressWarnings(
"AssertThatThrownByIllegalArgumentException" /* Matches strictly more specific expressions. */)
AbstractObjectAssert<?, ?> before(ThrowingCallable throwingCallable, String message) {
return assertThatIllegalArgumentException().isThrownBy(throwingCallable).withMessage(message);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(ThrowingCallable throwingCallable, String message) {
return assertThatThrownBy(throwingCallable)
.isInstanceOf(IllegalArgumentException.class)
.hasMessage(message);
}
}
static final class AssertThatThrownByIllegalArgumentExceptionHasMessageStartingWith {
@BeforeTemplate
@SuppressWarnings(
"AssertThatThrownByIllegalArgumentException" /* Matches strictly more specific expressions. */)
AbstractObjectAssert<?, ?> before(ThrowingCallable throwingCallable, String message) {
return assertThatIllegalArgumentException()
.isThrownBy(throwingCallable)
.withMessageStartingWith(message);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(ThrowingCallable throwingCallable, String message) {
return assertThatThrownBy(throwingCallable)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageStartingWith(message);
}
}
static final class AssertThatThrownByIllegalArgumentExceptionHasMessageContaining {
@BeforeTemplate
@SuppressWarnings(
"AssertThatThrownByIllegalArgumentException" /* Matches strictly more specific expressions. */)
AbstractObjectAssert<?, ?> before(ThrowingCallable throwingCallable, String message) {
return assertThatIllegalArgumentException()
.isThrownBy(throwingCallable)
.withMessageContaining(message);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(ThrowingCallable throwingCallable, String message) {
return assertThatThrownBy(throwingCallable)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining(message);
}
}
static final class AssertThatThrownByIllegalArgumentExceptionHasMessageNotContainingAny {
@BeforeTemplate
@SuppressWarnings(
"AssertThatThrownByIllegalArgumentException" /* Matches strictly more specific expressions. */)
AbstractObjectAssert<?, ?> before(
ThrowingCallable throwingCallable, @Repeated CharSequence values) {
return assertThatIllegalArgumentException()
.isThrownBy(throwingCallable)
.withMessageNotContainingAny(values);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(
ThrowingCallable throwingCallable, @Repeated CharSequence values) {
return assertThatThrownBy(throwingCallable)
.isInstanceOf(IllegalArgumentException.class)
.hasMessageNotContainingAny(values);
}
}
static final class AssertThatThrownByIllegalStateException {
@BeforeTemplate
AbstractObjectAssert<?, ?> before(ThrowingCallable throwingCallable) {
return assertThatIllegalStateException().isThrownBy(throwingCallable);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(ThrowingCallable throwingCallable) {
return assertThatThrownBy(throwingCallable).isInstanceOf(IllegalStateException.class);
}
}
static final class AssertThatThrownByIllegalStateExceptionHasMessage {
@BeforeTemplate
@SuppressWarnings(
"AssertThatThrownByIllegalStateException" /* Matches strictly more specific expressions. */)
AbstractObjectAssert<?, ?> before(ThrowingCallable throwingCallable, String message) {
return assertThatIllegalStateException().isThrownBy(throwingCallable).withMessage(message);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(ThrowingCallable throwingCallable, String message) {
return assertThatThrownBy(throwingCallable)
.isInstanceOf(IllegalStateException.class)
.hasMessage(message);
}
}
static final class AssertThatThrownByIllegalStateExceptionHasMessageStartingWith {
@BeforeTemplate
@SuppressWarnings(
"AssertThatThrownByIllegalStateException" /* Matches strictly more specific expressions. */)
AbstractObjectAssert<?, ?> before(ThrowingCallable throwingCallable, String message) {
return assertThatIllegalStateException()
.isThrownBy(throwingCallable)
.withMessageStartingWith(message);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(ThrowingCallable throwingCallable, String message) {
return assertThatThrownBy(throwingCallable)
.isInstanceOf(IllegalStateException.class)
.hasMessageStartingWith(message);
}
}
static final class AssertThatThrownByIllegalStateExceptionHasMessageContaining {
@BeforeTemplate
@SuppressWarnings(
"AssertThatThrownByIllegalStateException" /* Matches strictly more specific expressions. */)
AbstractObjectAssert<?, ?> before(ThrowingCallable throwingCallable, String message) {
return assertThatIllegalStateException()
.isThrownBy(throwingCallable)
.withMessageContaining(message);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(ThrowingCallable throwingCallable, String message) {
return assertThatThrownBy(throwingCallable)
.isInstanceOf(IllegalStateException.class)
.hasMessageContaining(message);
}
}
static final class AssertThatThrownByIllegalStateExceptionHasMessageNotContaining {
@BeforeTemplate
@SuppressWarnings(
"AssertThatThrownByIllegalStateException" /* Matches strictly more specific expressions. */)
AbstractObjectAssert<?, ?> before(ThrowingCallable throwingCallable, String message) {
return assertThatIllegalStateException()
.isThrownBy(throwingCallable)
.withMessageNotContaining(message);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(ThrowingCallable throwingCallable, String message) {
return assertThatThrownBy(throwingCallable)
.isInstanceOf(IllegalStateException.class)
.hasMessageNotContaining(message);
}
}
static final class AssertThatThrownByNullPointerException {
@BeforeTemplate
AbstractObjectAssert<?, ?> before(ThrowingCallable throwingCallable) {
return assertThatNullPointerException().isThrownBy(throwingCallable);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(ThrowingCallable throwingCallable) {
return assertThatThrownBy(throwingCallable).isInstanceOf(NullPointerException.class);
}
}
static final class AssertThatThrownByNullPointerExceptionHasMessage {
@BeforeTemplate
@SuppressWarnings(
"AssertThatThrownByNullPointerException" /* Matches strictly more specific expressions. */)
AbstractObjectAssert<?, ?> before(ThrowingCallable throwingCallable, String message) {
return assertThatNullPointerException().isThrownBy(throwingCallable).withMessage(message);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(ThrowingCallable throwingCallable, String message) {
return assertThatThrownBy(throwingCallable)
.isInstanceOf(NullPointerException.class)
.hasMessage(message);
}
}
static final class AssertThatThrownByNullPointerExceptionHasMessageStartingWith {
@BeforeTemplate
@SuppressWarnings(
"AssertThatThrownByNullPointerException" /* Matches strictly more specific expressions. */)
AbstractObjectAssert<?, ?> before(ThrowingCallable throwingCallable, String message) {
return assertThatNullPointerException()
.isThrownBy(throwingCallable)
.withMessageStartingWith(message);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(ThrowingCallable throwingCallable, String message) {
return assertThatThrownBy(throwingCallable)
.isInstanceOf(NullPointerException.class)
.hasMessageStartingWith(message);
}
}
static final class AssertThatThrownByIOException {
@BeforeTemplate
AbstractObjectAssert<?, ?> before(ThrowingCallable throwingCallable) {
return assertThatIOException().isThrownBy(throwingCallable);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(ThrowingCallable throwingCallable) {
return assertThatThrownBy(throwingCallable).isInstanceOf(IOException.class);
}
}
static final class AssertThatThrownByIOExceptionHasMessage {
@BeforeTemplate
@SuppressWarnings(
"AssertThatThrownByIOException" /* Matches strictly more specific expressions. */)
AbstractObjectAssert<?, ?> before(ThrowingCallable throwingCallable, String message) {
return assertThatIOException().isThrownBy(throwingCallable).withMessage(message);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(ThrowingCallable throwingCallable, String message) {
return assertThatThrownBy(throwingCallable)
.isInstanceOf(IOException.class)
.hasMessage(message);
}
}
static final class AssertThatThrownBy {
@BeforeTemplate
AbstractObjectAssert<?, ?> before(
Class<? extends Throwable> exceptionType, ThrowingCallable throwingCallable) {
return assertThatExceptionOfType(exceptionType).isThrownBy(throwingCallable);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(
Class<? extends Throwable> exceptionType, ThrowingCallable throwingCallable) {
return assertThatThrownBy(throwingCallable).isInstanceOf(exceptionType);
}
}
static final class AssertThatThrownByHasMessage {
@BeforeTemplate
@SuppressWarnings("AssertThatThrownBy" /* Matches strictly more specific expressions. */)
AbstractObjectAssert<?, ?> before(
Class<? extends Throwable> exceptionType,
ThrowingCallable throwingCallable,
String message) {
return assertThatExceptionOfType(exceptionType)
.isThrownBy(throwingCallable)
.withMessage(message);
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
AbstractObjectAssert<?, ?> after(
Class<? extends Throwable> exceptionType,
ThrowingCallable throwingCallable,
String message) {
return assertThatThrownBy(throwingCallable).isInstanceOf(exceptionType).hasMessage(message);
}
}
// XXX: Drop this template in favour of a generic Error Prone check which flags
// `String.format(...)` arguments to a wide range of format methods.
static final class AbstractThrowableAssertHasMessage {
@BeforeTemplate
AbstractThrowableAssert<?, ? extends Throwable> before(
AbstractThrowableAssert<?, ? extends Throwable> abstractThrowableAssert,
String message,
@Repeated Object parameters) {
return abstractThrowableAssert.hasMessage(String.format(message, parameters));
}
@AfterTemplate
AbstractThrowableAssert<?, ? extends Throwable> after(
AbstractThrowableAssert<?, ? extends Throwable> abstractThrowableAssert,
String message,
@Repeated Object parameters) {
return abstractThrowableAssert.hasMessage(message, parameters);
}
}
// XXX: Drop this template in favour of a generic Error Prone check which flags
// `String.format(...)` arguments to a wide range of format methods.
static final class AbstractThrowableAssertWithFailMessage {
@BeforeTemplate
AbstractThrowableAssert<?, ? extends Throwable> before(
AbstractThrowableAssert<?, ? extends Throwable> abstractThrowableAssert,
String message,
@Repeated Object args) {
return abstractThrowableAssert.withFailMessage(String.format(message, args));
}
@AfterTemplate
AbstractThrowableAssert<?, ? extends Throwable> after(
AbstractThrowableAssert<?, ? extends Throwable> abstractThrowableAssert,
String message,
@Repeated Object args) {
return abstractThrowableAssert.withFailMessage(message, args);
}
}
}

View File

@@ -3,6 +3,8 @@ package tech.picnic.errorprone.refastertemplates;
import static com.google.common.base.Preconditions.checkElementIndex;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.common.collect.Sets.toImmutableEnumSet;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static java.util.Collections.disjoint;
import static java.util.Objects.checkIndex;
import com.google.common.base.Splitter;
@@ -11,7 +13,6 @@ import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
import com.google.common.collect.Sets;
import com.google.common.collect.Streams;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -92,7 +93,7 @@ final class AssortedTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ImmutableSet<T> after(Stream<T> stream) {
return stream.collect(toImmutableEnumSet());
}
@@ -162,7 +163,7 @@ final class AssortedTemplates {
@AfterTemplate
boolean after(Set<T> set1, Set<T> set2) {
return Collections.disjoint(set1, set2);
return disjoint(set1, set2);
}
}
@@ -177,15 +178,15 @@ final class AssortedTemplates {
@BeforeTemplate
boolean before(Collection<T> collection1, Collection<T> collection2) {
return Refaster.anyOf(
Collections.disjoint(ImmutableSet.copyOf(collection1), collection2),
Collections.disjoint(new HashSet<>(collection1), collection2),
Collections.disjoint(collection1, ImmutableSet.copyOf(collection2)),
Collections.disjoint(collection1, new HashSet<>(collection2)));
disjoint(ImmutableSet.copyOf(collection1), collection2),
disjoint(new HashSet<>(collection1), collection2),
disjoint(collection1, ImmutableSet.copyOf(collection2)),
disjoint(collection1, new HashSet<>(collection2)));
}
@AfterTemplate
boolean after(Collection<T> collection1, Collection<T> collection2) {
return Collections.disjoint(collection1, collection2);
return disjoint(collection1, collection2);
}
}

View File

@@ -187,7 +187,7 @@ final class CollectionTemplates {
* Don't call {@link ImmutableCollection#asList()} if the result is going to be streamed; stream
* directly.
*/
static final class ImmutableCollectionAsListToStream<T> {
static final class ImmutableCollectionStream<T> {
@BeforeTemplate
Stream<T> before(ImmutableCollection<T> collection) {
return collection.asList().stream();

View File

@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static java.util.Comparator.comparing;
import static java.util.Comparator.comparingDouble;
import static java.util.Comparator.comparingInt;
@@ -11,7 +12,6 @@ import static java.util.function.Function.identity;
import com.google.common.collect.Comparators;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -38,7 +38,7 @@ final class ComparatorTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Comparator<T> after() {
return naturalOrder();
}
@@ -52,7 +52,7 @@ final class ComparatorTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Comparator<T> after() {
return reverseOrder();
}
@@ -66,7 +66,7 @@ final class ComparatorTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Comparator<T> after(Comparator<T> cmp) {
return cmp;
}
@@ -93,7 +93,7 @@ final class ComparatorTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Comparator<S> after(Comparator<S> cmp, Function<? super S, ? extends T> function) {
return cmp.thenComparing(function, reverseOrder());
}
@@ -180,7 +180,7 @@ final class ComparatorTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Comparator<T> after(Comparator<T> cmp) {
return cmp.thenComparing(naturalOrder());
}

View File

@@ -2,6 +2,7 @@ package tech.picnic.errorprone.refastertemplates;
import static com.google.common.collect.ImmutableListMultimap.flatteningToImmutableListMultimap;
import static com.google.common.collect.ImmutableListMultimap.toImmutableListMultimap;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static java.util.function.Function.identity;
import com.google.common.collect.ImmutableListMultimap;
@@ -12,7 +13,6 @@ import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.SortedSetMultimap;
import com.google.common.collect.Streams;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -92,7 +92,7 @@ final class ImmutableListMultimapTemplates {
*/
static final class EntryToImmutableListMultimap<K, V> {
@BeforeTemplate
ImmutableMultimap<K, V> before(Map.Entry<? extends K, ? extends V> entry) {
ImmutableListMultimap<K, V> before(Map.Entry<? extends K, ? extends V> entry) {
return Refaster.anyOf(
ImmutableListMultimap.<K, V>builder().put(entry).build(),
Stream.of(entry)
@@ -161,7 +161,7 @@ final class ImmutableListMultimapTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ImmutableListMultimap<K, V> after(Stream<E> stream) {
return stream.collect(toImmutableListMultimap(e -> keyFunction(e), e -> valueFunction(e)));
}
@@ -272,17 +272,4 @@ final class ImmutableListMultimapTemplates {
return ImmutableListMultimap.copyOf(Multimaps.transformValues(multimap, transformation));
}
}
/** Don't unnecessarily copy an {@link ImmutableListMultimap}. */
static final class ImmutableListMultimapCopyOfImmutableListMultimap<K, V> {
@BeforeTemplate
ImmutableListMultimap<K, V> before(ImmutableListMultimap<K, V> multimap) {
return ImmutableListMultimap.copyOf(multimap);
}
@AfterTemplate
ImmutableListMultimap<K, V> after(ImmutableListMultimap<K, V> multimap) {
return multimap;
}
}
}

View File

@@ -2,21 +2,20 @@ package tech.picnic.errorprone.refastertemplates;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static java.util.Collections.emptyList;
import static java.util.Collections.singletonList;
import static java.util.Comparator.naturalOrder;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Streams;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
@@ -41,37 +40,6 @@ final class ImmutableListTemplates {
}
}
/** Prefer {@link ImmutableList#of()} over more contrived alternatives. */
static final class EmptyImmutableList<T> {
@BeforeTemplate
ImmutableList<T> before() {
return Refaster.anyOf(
ImmutableList.<T>builder().build(), Stream.<T>empty().collect(toImmutableList()));
}
@AfterTemplate
ImmutableList<T> after() {
return ImmutableList.of();
}
}
/**
* Prefer {@link ImmutableList#of(Object)} over alternatives that don't communicate the
* immutability of the resulting list at the type level.
*/
// XXX: Note that this rewrite rule is incorrect for nullable elements.
static final class SingletonImmutableList<T> {
@BeforeTemplate
List<T> before(T element) {
return Collections.singletonList(element);
}
@AfterTemplate
ImmutableList<T> after(T element) {
return ImmutableList.of(element);
}
}
/**
* Prefer {@link ImmutableList#copyOf(Iterable)} and variants over more contrived alternatives.
*/
@@ -108,21 +76,15 @@ final class ImmutableListTemplates {
}
}
/** Prefer {@link ImmutableList#toImmutableList()} over the more verbose alternative. */
// XXX: Once the code base has been sufficiently cleaned up, we might want to also rewrite
// `Collectors.toList(`), with the caveat that it allows mutation (though this cannot be relied
// upon) as well as nulls. Another option is to explicitly rewrite those variants to
// `Collectors.toSet(ArrayList::new)`.
/** Prefer {@link ImmutableList#toImmutableList()} over less idiomatic alternatives. */
static final class StreamToImmutableList<T> {
@BeforeTemplate
ImmutableList<T> before(Stream<T> stream) {
return Refaster.anyOf(
ImmutableList.copyOf(stream.iterator()),
stream.collect(collectingAndThen(toList(), ImmutableList::copyOf)));
return ImmutableList.copyOf(stream.iterator());
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ImmutableList<T> after(Stream<T> stream) {
return stream.collect(toImmutableList());
}
@@ -181,9 +143,122 @@ final class ImmutableListTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ImmutableList<T> after(Stream<T> stream) {
return stream.collect(toImmutableSet()).asList();
}
}
/**
* Prefer {@link ImmutableList#of()} over more contrived alternatives or alternatives that don't
* communicate the immutability of the resulting list at the type level.
*/
// XXX: The `Stream` variant may be too contrived to warrant inclusion. Review its usage if/when
// this and similar Refaster templates are replaced with an Error Prone check.
static final class ImmutableListOf<T> {
@BeforeTemplate
List<T> before() {
return Refaster.anyOf(
ImmutableList.<T>builder().build(),
Stream.<T>empty().collect(toImmutableList()),
emptyList(),
List.of());
}
@AfterTemplate
ImmutableList<T> after() {
return ImmutableList.of();
}
}
/**
* Prefer {@link ImmutableList#of(Object)} over more contrived alternatives or alternatives that
* don't communicate the immutability of the resulting list at the type level.
*/
// XXX: Note that the replacement of `Collections#singletonList` is incorrect for nullable
// elements.
static final class ImmutableListOf1<T> {
@BeforeTemplate
List<T> before(T e1) {
return Refaster.anyOf(
ImmutableList.<T>builder().add(e1).build(), singletonList(e1), List.of(e1));
}
@AfterTemplate
ImmutableList<T> after(T e1) {
return ImmutableList.of(e1);
}
}
/**
* Prefer {@link ImmutableList#of(Object, Object)} over alternatives that don't communicate the
* immutability of the resulting list at the type level.
*/
// XXX: Consider writing an Error Prone check which also flags straightforward
// `ImmutableList.builder()` usages.
static final class ImmutableListOf2<T> {
@BeforeTemplate
List<T> before(T e1, T e2) {
return List.of(e1, e2);
}
@AfterTemplate
ImmutableList<T> after(T e1, T e2) {
return ImmutableList.of(e1, e2);
}
}
/**
* Prefer {@link ImmutableList#of(Object, Object, Object)} over alternatives that don't
* communicate the immutability of the resulting list at the type level.
*/
// XXX: Consider writing an Error Prone check which also flags straightforward
// `ImmutableList.builder()` usages.
static final class ImmutableListOf3<T> {
@BeforeTemplate
List<T> before(T e1, T e2, T e3) {
return List.of(e1, e2, e3);
}
@AfterTemplate
ImmutableList<T> after(T e1, T e2, T e3) {
return ImmutableList.of(e1, e2, e3);
}
}
/**
* Prefer {@link ImmutableList#of(Object, Object, Object, Object)} over alternatives that don't
* communicate the immutability of the resulting list at the type level.
*/
// XXX: Consider writing an Error Prone check which also flags straightforward
// `ImmutableList.builder()` usages.
static final class ImmutableListOf4<T> {
@BeforeTemplate
List<T> before(T e1, T e2, T e3, T e4) {
return List.of(e1, e2, e3, e4);
}
@AfterTemplate
ImmutableList<T> after(T e1, T e2, T e3, T e4) {
return ImmutableList.of(e1, e2, e3, e4);
}
}
/**
* Prefer {@link ImmutableList#of(Object, Object, Object, Object, Object)} over alternatives that
* don't communicate the immutability of the resulting list at the type level.
*/
// XXX: Consider writing an Error Prone check which also flags straightforward
// `ImmutableList.builder()` usages.
static final class ImmutableListOf5<T> {
@BeforeTemplate
List<T> before(T e1, T e2, T e3, T e4, T e5) {
return List.of(e1, e2, e3, e4, e5);
}
@AfterTemplate
ImmutableList<T> after(T e1, T e2, T e3, T e4, T e5) {
return ImmutableList.of(e1, e2, e3, e4, e5);
}
}
}

View File

@@ -1,12 +1,14 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.common.collect.ImmutableMap.toImmutableMap;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static java.util.Collections.emptyMap;
import static java.util.Collections.singletonMap;
import static java.util.function.Function.identity;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.google.common.collect.Streams;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -14,7 +16,6 @@ import com.google.errorprone.refaster.annotation.MayOptionallyUse;
import com.google.errorprone.refaster.annotation.Placeholder;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
@@ -40,41 +41,6 @@ final class ImmutableMapTemplates {
}
}
/** Prefer {@link ImmutableMap#of()} over more contrived alternatives. */
static final class EmptyImmutableMap<K, V> {
@BeforeTemplate
ImmutableMap<K, V> before() {
return ImmutableMap.<K, V>builder().build();
}
@AfterTemplate
ImmutableMap<K, V> after() {
return ImmutableMap.of();
}
}
/**
* Prefer {@link ImmutableMap#of(Object, Object)} over more contrived alternatives and
* alternatives that don't communicate the immutability of the resulting map at the type level..
*/
// XXX: One can define variants for more than one key-value pair, but at some point the builder
// actually produces nicer code. So it's not clear we should add Refaster templates for those
// variants.
// XXX: Note that the `singletonMap` rewrite rule is incorrect for nullable elements.
static final class PairToImmutableMap<K, V> {
@BeforeTemplate
Map<K, V> before(K key, V value) {
return Refaster.anyOf(
ImmutableMap.<K, V>builder().put(key, value).build(),
Collections.singletonMap(key, value));
}
@AfterTemplate
ImmutableMap<K, V> after(K key, V value) {
return ImmutableMap.of(key, value);
}
}
/** Prefer {@link ImmutableMap#of(Object, Object)} over more contrived alternatives. */
static final class EntryToImmutableMap<K, V> {
@BeforeTemplate
@@ -178,7 +144,7 @@ final class ImmutableMapTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ImmutableMap<K, V> after(Stream<E> stream) {
return stream.collect(toImmutableMap(e -> keyFunction(e), e -> valueFunction(e)));
}
@@ -242,16 +208,108 @@ final class ImmutableMapTemplates {
}
}
/** Don't unnecessarily copy an {@link ImmutableMap}. */
static final class ImmutableMapCopyOfImmutableMap<K, V> {
/**
* Prefer {@link ImmutableMap#of()} over more contrived alternatives or alternatives that don't
* communicate the immutability of the resulting map at the type level.
*/
static final class ImmutableMapOf<K, V> {
@BeforeTemplate
ImmutableMap<K, V> before(ImmutableMap<K, V> map) {
return ImmutableMap.copyOf(map);
Map<K, V> before() {
return Refaster.anyOf(ImmutableMap.<K, V>builder().build(), emptyMap(), Map.of());
}
@AfterTemplate
ImmutableMap<K, V> after(ImmutableMap<K, V> map) {
return map;
ImmutableMap<K, V> after() {
return ImmutableMap.of();
}
}
/**
* Prefer {@link ImmutableMap#of(Object, Object)} over more contrived alternatives or alternatives
* that don't communicate the immutability of the resulting map at the type level.
*/
// XXX: Note that the replacement of `Collections#singletonMap` is incorrect for nullable
// elements.
static final class ImmutableMapOf1<K, V> {
@BeforeTemplate
Map<K, V> before(K k1, V v1) {
return Refaster.anyOf(
ImmutableMap.<K, V>builder().put(k1, v1).build(), singletonMap(k1, v1), Map.of(k1, v1));
}
@AfterTemplate
ImmutableMap<K, V> after(K k1, V v1) {
return ImmutableMap.of(k1, v1);
}
}
/**
* Prefer {@link ImmutableMap#of(Object, Object, Object, Object)} over alternatives that don't
* communicate the immutability of the resulting map at the type level.
*/
// XXX: Also rewrite the `ImmutableMap.builder()` variant?
static final class ImmutableMapOf2<K, V> {
@BeforeTemplate
Map<K, V> before(K k1, V v1, K k2, V v2) {
return Map.of(k1, v1, k2, v2);
}
@AfterTemplate
ImmutableMap<K, V> after(K k1, V v1, K k2, V v2) {
return ImmutableMap.of(k1, v1, k2, v2);
}
}
/**
* Prefer {@link ImmutableMap#of(Object, Object, Object, Object, Object, Object)} over
* alternatives that don't communicate the immutability of the resulting map at the type level.
*/
// XXX: Also rewrite the `ImmutableMap.builder()` variant?
static final class ImmutableMapOf3<K, V> {
@BeforeTemplate
Map<K, V> before(K k1, V v1, K k2, V v2, K k3, V v3) {
return Map.of(k1, v1, k2, v2, k3, v3);
}
@AfterTemplate
ImmutableMap<K, V> after(K k1, V v1, K k2, V v2, K k3, V v3) {
return ImmutableMap.of(k1, v1, k2, v2, k3, v3);
}
}
/**
* Prefer {@link ImmutableMap#of(Object, Object, Object, Object, Object, Object, Object, Object)}
* over alternatives that don't communicate the immutability of the resulting map at the type
* level.
*/
// XXX: Also rewrite the `ImmutableMap.builder()` variant?
static final class ImmutableMapOf4<K, V> {
@BeforeTemplate
Map<K, V> before(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
return Map.of(k1, v1, k2, v2, k3, v3, k4, v4);
}
@AfterTemplate
ImmutableMap<K, V> after(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4) {
return ImmutableMap.of(k1, v1, k2, v2, k3, v3, k4, v4);
}
}
/**
* Prefer {@link ImmutableMap#of(Object, Object, Object, Object, Object, Object, Object, Object,
* Object, Object)} over alternatives that don't communicate the immutability of the resulting map
* at the type level.
*/
// XXX: Also rewrite the `ImmutableMap.builder()` variant?
static final class ImmutableMapOf5<K, V> {
@BeforeTemplate
Map<K, V> before(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
return Map.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5);
}
@AfterTemplate
ImmutableMap<K, V> after(K k1, V v1, K k2, V v2, K k3, V v3, K k4, V v4, K k5, V v5) {
return ImmutableMap.of(k1, v1, k2, v2, k3, v3, k4, v4, k5, v5);
}
}

View File

@@ -1,12 +1,10 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.common.collect.ImmutableMultiset.toImmutableMultiset;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.Streams;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -90,28 +88,13 @@ final class ImmutableMultisetTemplates {
static final class StreamToImmutableMultiset<T> {
@BeforeTemplate
ImmutableMultiset<T> before(Stream<T> stream) {
return Refaster.anyOf(
ImmutableMultiset.copyOf(stream.iterator()),
stream.collect(collectingAndThen(toList(), ImmutableMultiset::copyOf)));
return ImmutableMultiset.copyOf(stream.iterator());
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ImmutableMultiset<T> after(Stream<T> stream) {
return stream.collect(toImmutableMultiset());
}
}
/** Don't unnecessarily copy an {@link ImmutableMultiset}. */
static final class ImmutableMultisetCopyOfImmutableMultiset<T> {
@BeforeTemplate
ImmutableMultiset<T> before(ImmutableMultiset<T> multiset) {
return ImmutableMultiset.copyOf(multiset);
}
@AfterTemplate
ImmutableMultiset<T> after(ImmutableMultiset<T> multiset) {
return multiset;
}
}
}

View File

@@ -2,6 +2,7 @@ package tech.picnic.errorprone.refastertemplates;
import static com.google.common.collect.ImmutableSetMultimap.flatteningToImmutableSetMultimap;
import static com.google.common.collect.ImmutableSetMultimap.toImmutableSetMultimap;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.ListMultimap;
@@ -10,7 +11,6 @@ import com.google.common.collect.Multimaps;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.SortedSetMultimap;
import com.google.common.collect.Streams;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -138,7 +138,7 @@ final class ImmutableSetMultimapTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ImmutableSetMultimap<K, V> after(Stream<E> stream) {
return stream.collect(toImmutableSetMultimap(e -> keyFunction(e), e -> valueFunction(e)));
}
@@ -162,7 +162,7 @@ final class ImmutableSetMultimapTemplates {
@AfterTemplate
ImmutableSetMultimap<K, V2> after(Multimap<K, V1> multimap) {
return ImmutableSetMultimap.copyOf(
Multimaps.transformValues(multimap, v -> valueTransformation(v)));
Multimaps.transformValues(multimap, e -> valueTransformation(e)));
}
}
@@ -215,17 +215,4 @@ final class ImmutableSetMultimapTemplates {
return ImmutableSetMultimap.copyOf(Multimaps.transformValues(multimap, transformation));
}
}
/** Don't unnecessarily copy an {@link ImmutableSetMultimap}. */
static final class ImmutableSetMultimapCopyOfImmutableSetMultimap<K, V> {
@BeforeTemplate
ImmutableSetMultimap<K, V> before(ImmutableSetMultimap<K, V> multimap) {
return ImmutableSetMultimap.copyOf(multimap);
}
@AfterTemplate
ImmutableSetMultimap<K, V> after(ImmutableSetMultimap<K, V> multimap) {
return multimap;
}
}
}

View File

@@ -1,21 +1,19 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toSet;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets.SetView;
import com.google.common.collect.Streams;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.Set;
import java.util.stream.Stream;
@@ -39,37 +37,6 @@ final class ImmutableSetTemplates {
}
}
/** Prefer {@link ImmutableSet#of()} over more contrived alternatives. */
static final class EmptyImmutableSet<T> {
@BeforeTemplate
ImmutableSet<T> before() {
return Refaster.anyOf(
ImmutableSet.<T>builder().build(), Stream.<T>empty().collect(toImmutableSet()));
}
@AfterTemplate
ImmutableSet<T> after() {
return ImmutableSet.of();
}
}
/**
* Prefer {@link ImmutableSet#of(Object)} over alternatives that don't communicate the
* immutability of the resulting set at the type level.
*/
// XXX: Note that this rewrite rule is incorrect for nullable elements.
static final class SingletonImmutableSet<T> {
@BeforeTemplate
Set<T> before(T element) {
return Collections.singleton(element);
}
@AfterTemplate
ImmutableSet<T> after(T element) {
return ImmutableSet.of(element);
}
}
/** Prefer {@link ImmutableSet#copyOf(Iterable)} and variants over more contrived alternatives. */
static final class IterableToImmutableSet<T> {
@BeforeTemplate
@@ -105,40 +72,20 @@ final class ImmutableSetTemplates {
}
/** Prefer {@link ImmutableSet#toImmutableSet()} over less idiomatic alternatives. */
// XXX: Once the code base has been sufficiently cleaned up, we might want to also rewrite
// `Collectors.toSet(`), with the caveat that it allows mutation (though this cannot be relied
// upon) as well as nulls. Another option is to explicitly rewrite those variants to
// `Collectors.toSet(HashSet::new)`.
static final class StreamToImmutableSet<T> {
@BeforeTemplate
ImmutableSet<T> before(Stream<T> stream) {
return Refaster.anyOf(
ImmutableSet.copyOf(stream.iterator()),
stream.distinct().collect(toImmutableSet()),
stream.collect(collectingAndThen(toList(), ImmutableSet::copyOf)),
stream.collect(collectingAndThen(toSet(), ImmutableSet::copyOf)));
ImmutableSet.copyOf(stream.iterator()), stream.distinct().collect(toImmutableSet()));
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ImmutableSet<T> after(Stream<T> stream) {
return stream.collect(toImmutableSet());
}
}
/** Don't unnecessarily copy an {@link ImmutableSet}. */
static final class ImmutableSetCopyOfImmutableSet<T> {
@BeforeTemplate
ImmutableSet<T> before(ImmutableSet<T> set) {
return ImmutableSet.copyOf(set);
}
@AfterTemplate
ImmutableSet<T> after(ImmutableSet<T> set) {
return set;
}
}
/** Prefer {@link SetView#immutableCopy()} over the more verbose alternative. */
static final class ImmutableSetCopyOfSetView<T> {
@BeforeTemplate
@@ -151,4 +98,115 @@ final class ImmutableSetTemplates {
return set.immutableCopy();
}
}
/**
* Prefer {@link ImmutableSet#of()} over more contrived alternatives or alternatives that don't
* communicate the immutability of the resulting set at the type level.
*/
// XXX: The `Stream` variant may be too contrived to warrant inclusion. Review its usage if/when
// this and similar Refaster templates are replaced with an Error Prone check.
static final class ImmutableSetOf<T> {
@BeforeTemplate
Set<T> before() {
return Refaster.anyOf(
ImmutableSet.<T>builder().build(),
Stream.<T>empty().collect(toImmutableSet()),
emptySet(),
Set.of());
}
@AfterTemplate
ImmutableSet<T> after() {
return ImmutableSet.of();
}
}
/**
* Prefer {@link ImmutableSet#of(Object)} over more contrived alternatives or alternatives that
* don't communicate the immutability of the resulting set at the type level.
*/
// XXX: Note that the replacement of `Collections#singleton` is incorrect for nullable elements.
static final class ImmutableSetOf1<T> {
@BeforeTemplate
Set<T> before(T e1) {
return Refaster.anyOf(ImmutableSet.<T>builder().add(e1).build(), singleton(e1), Set.of(e1));
}
@AfterTemplate
ImmutableSet<T> after(T e1) {
return ImmutableSet.of(e1);
}
}
/**
* Prefer {@link ImmutableSet#of(Object, Object)} over alternatives that don't communicate the
* immutability of the resulting set at the type level.
*/
// XXX: Consider writing an Error Prone check which also flags straightforward
// `ImmutableSet.builder()` usages.
static final class ImmutableSetOf2<T> {
@BeforeTemplate
Set<T> before(T e1, T e2) {
return Set.of(e1, e2);
}
@AfterTemplate
ImmutableSet<T> after(T e1, T e2) {
return ImmutableSet.of(e1, e2);
}
}
/**
* Prefer {@link ImmutableSet#of(Object, Object, Object)} over alternatives that don't communicate
* the immutability of the resulting set at the type level.
*/
// XXX: Consider writing an Error Prone check which also flags straightforward
// `ImmutableSet.builder()` usages.
static final class ImmutableSetOf3<T> {
@BeforeTemplate
Set<T> before(T e1, T e2, T e3) {
return Set.of(e1, e2, e3);
}
@AfterTemplate
ImmutableSet<T> after(T e1, T e2, T e3) {
return ImmutableSet.of(e1, e2, e3);
}
}
/**
* Prefer {@link ImmutableSet#of(Object, Object, Object, Object)} over alternatives that don't
* communicate the immutability of the resulting set at the type level.
*/
// XXX: Consider writing an Error Prone check which also flags straightforward
// `ImmutableSet.builder()` usages.
static final class ImmutableSetOf4<T> {
@BeforeTemplate
Set<T> before(T e1, T e2, T e3, T e4) {
return Set.of(e1, e2, e3, e4);
}
@AfterTemplate
ImmutableSet<T> after(T e1, T e2, T e3, T e4) {
return ImmutableSet.of(e1, e2, e3, e4);
}
}
/**
* Prefer {@link ImmutableSet#of(Object, Object, Object, Object, Object)} over alternatives that
* don't communicate the immutability of the resulting set at the type level.
*/
// XXX: Consider writing an Error Prone check which also flags straightforward
// `ImmutableSet.builder()` usages.
static final class ImmutableSetOf5<T> {
@BeforeTemplate
Set<T> before(T e1, T e2, T e3, T e4, T e5) {
return Set.of(e1, e2, e3, e4, e5);
}
@AfterTemplate
ImmutableSet<T> after(T e1, T e2, T e3, T e4, T e5) {
return ImmutableSet.of(e1, e2, e3, e4, e5);
}
}
}

View File

@@ -1,14 +1,11 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.common.collect.ImmutableSortedMultiset.toImmutableSortedMultiset;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static java.util.Comparator.naturalOrder;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;
import com.google.common.collect.ImmutableMultiset;
import com.google.common.collect.ImmutableSortedMultiset;
import com.google.common.collect.Streams;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -93,14 +90,14 @@ final class ImmutableSortedMultisetTemplates {
// `reverseOrder`.) Worth the hassle?
static final class IterableToImmutableSortedMultiset<T extends Comparable<? super T>> {
@BeforeTemplate
ImmutableMultiset<T> before(T[] iterable) {
ImmutableSortedMultiset<T> before(T[] iterable) {
return Refaster.anyOf(
ImmutableSortedMultiset.<T>naturalOrder().add(iterable).build(),
Arrays.stream(iterable).collect(toImmutableSortedMultiset(naturalOrder())));
}
@BeforeTemplate
ImmutableMultiset<T> before(Iterator<T> iterable) {
ImmutableSortedMultiset<T> before(Iterator<T> iterable) {
return Refaster.anyOf(
ImmutableSortedMultiset.copyOf(naturalOrder(), iterable),
ImmutableSortedMultiset.<T>naturalOrder().addAll(iterable).build(),
@@ -108,7 +105,7 @@ final class ImmutableSortedMultisetTemplates {
}
@BeforeTemplate
ImmutableMultiset<T> before(Iterable<T> iterable) {
ImmutableSortedMultiset<T> before(Iterable<T> iterable) {
return Refaster.anyOf(
ImmutableSortedMultiset.copyOf(naturalOrder(), iterable),
ImmutableSortedMultiset.<T>naturalOrder().addAll(iterable).build(),
@@ -134,13 +131,11 @@ final class ImmutableSortedMultisetTemplates {
static final class StreamToImmutableSortedMultiset<T extends Comparable<? super T>> {
@BeforeTemplate
ImmutableSortedMultiset<T> before(Stream<T> stream) {
return Refaster.anyOf(
ImmutableSortedMultiset.copyOf(stream.iterator()),
stream.collect(collectingAndThen(toList(), ImmutableSortedMultiset::copyOf)));
return ImmutableSortedMultiset.copyOf(stream.iterator());
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ImmutableSortedMultiset<T> after(Stream<T> stream) {
return stream.collect(toImmutableSortedMultiset(naturalOrder()));
}

View File

@@ -1,14 +1,11 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static java.util.Comparator.naturalOrder;
import static java.util.stream.Collectors.collectingAndThen;
import static java.util.stream.Collectors.toList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSortedSet;
import com.google.common.collect.Streams;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -91,14 +88,14 @@ final class ImmutableSortedSetTemplates {
// `reverseOrder`.) Worth the hassle?
static final class IterableToImmutableSortedSet<T extends Comparable<? super T>> {
@BeforeTemplate
ImmutableSet<T> before(T[] iterable) {
ImmutableSortedSet<T> before(T[] iterable) {
return Refaster.anyOf(
ImmutableSortedSet.<T>naturalOrder().add(iterable).build(),
Arrays.stream(iterable).collect(toImmutableSortedSet(naturalOrder())));
}
@BeforeTemplate
ImmutableSet<T> before(Iterator<T> iterable) {
ImmutableSortedSet<T> before(Iterator<T> iterable) {
return Refaster.anyOf(
ImmutableSortedSet.copyOf(naturalOrder(), iterable),
ImmutableSortedSet.<T>naturalOrder().addAll(iterable).build(),
@@ -106,7 +103,7 @@ final class ImmutableSortedSetTemplates {
}
@BeforeTemplate
ImmutableSet<T> before(Iterable<T> iterable) {
ImmutableSortedSet<T> before(Iterable<T> iterable) {
return Refaster.anyOf(
ImmutableSortedSet.copyOf(naturalOrder(), iterable),
ImmutableSortedSet.<T>naturalOrder().addAll(iterable).build(),
@@ -134,13 +131,11 @@ final class ImmutableSortedSetTemplates {
static final class StreamToImmutableSortedSet<T extends Comparable<? super T>> {
@BeforeTemplate
ImmutableSortedSet<T> before(Stream<T> stream) {
return Refaster.anyOf(
ImmutableSortedSet.copyOf(stream.iterator()),
stream.collect(collectingAndThen(toList(), ImmutableSortedSet::copyOf)));
return ImmutableSortedSet.copyOf(stream.iterator());
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
ImmutableSortedSet<T> after(Stream<T> stream) {
return stream.collect(toImmutableSortedSet(naturalOrder()));
}

View File

@@ -1,8 +1,8 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.junit.jupiter.params.provider.Arguments.arguments;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.Repeated;
@@ -21,7 +21,7 @@ final class JUnitTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Arguments after(@Repeated T objects) {
return arguments(objects);
}

View File

@@ -1,12 +1,12 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static java.util.Comparator.comparing;
import static java.util.Comparator.naturalOrder;
import static java.util.Map.Entry.comparingByKey;
import static java.util.Map.Entry.comparingByValue;
import com.google.common.collect.Maps;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -51,7 +51,7 @@ final class MapEntryTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Comparator<Map.Entry<K, V>> after() {
return comparingByKey();
}
@@ -65,7 +65,7 @@ final class MapEntryTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Comparator<Map.Entry<K, V>> after(Comparator<? super K> cmp) {
return comparingByKey(cmp);
}
@@ -80,7 +80,7 @@ final class MapEntryTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Comparator<Map.Entry<K, V>> after() {
return comparingByValue();
}
@@ -94,7 +94,7 @@ final class MapEntryTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Comparator<Map.Entry<K, V>> after(Comparator<? super V> cmp) {
return comparingByValue(cmp);
}

View File

@@ -1,10 +1,10 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
@@ -26,7 +26,7 @@ final class MockitoTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
VerificationMode after() {
return never();
}
@@ -43,7 +43,7 @@ final class MockitoTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
T after(T mock) {
return verify(mock);
}

View File

@@ -1,9 +1,9 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static java.util.Objects.requireNonNullElse;
import com.google.common.base.MoreObjects;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
@@ -24,7 +24,7 @@ final class NullTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
T after(T first, T second) {
return requireNonNullElse(first, second);
}

View File

@@ -1,7 +1,8 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import com.google.common.collect.Streams;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -103,7 +104,7 @@ final class OptionalTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Optional<T> after(Iterator<T> it) {
return Streams.stream(it).findFirst();
}
@@ -165,6 +166,11 @@ final class OptionalTemplates {
}
}
/**
* Prefer {@link Optional#map} over a {@link Optional#flatMap} which wraps the result of a
* transformation in an {@link Optional}; the former operation transforms {@code null} to {@link
* Optional#empty()}.
*/
abstract static class MapToNullable<T, S> {
@Placeholder
abstract S toNullableFunction(@MayOptionallyUse T element);

View File

@@ -1,9 +1,10 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.common.collect.MoreCollectors.toOptional;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.assertj.core.api.Assertions.assertThat;
import com.google.common.collect.MoreCollectors;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -142,6 +143,35 @@ final class ReactorTemplates {
}
}
/** Prefer {@link Flux#concatMap(Function)} over more contrived alternatives. */
static final class FluxConcatMap<T, S> {
@BeforeTemplate
Flux<S> before(Flux<T> flux, Function<? super T, ? extends Publisher<? extends S>> function) {
return Refaster.anyOf(flux.flatMap(function, 1), flux.flatMapSequential(function, 1));
}
@AfterTemplate
Flux<S> after(Flux<T> flux, Function<? super T, ? extends Publisher<? extends S>> function) {
return flux.concatMap(function);
}
}
/**
* Prefer {@link Flux#concatMapIterable(Function)} over {@link Flux#flatMapIterable(Function)}, as
* the former has equivalent semantics but a clearer name.
*/
static final class FluxConcatMapIterable<T, S> {
@BeforeTemplate
Flux<S> before(Flux<T> flux, Function<? super T, ? extends Iterable<? extends S>> function) {
return flux.flatMapIterable(function);
}
@AfterTemplate
Flux<S> after(Flux<T> flux, Function<? super T, ? extends Iterable<? extends S>> function) {
return flux.concatMapIterable(function);
}
}
/**
* Don't use {@link Mono#flatMapMany(Function)} to implicitly convert a {@link Mono} to a {@link
* Flux}.
@@ -174,19 +204,6 @@ final class ReactorTemplates {
}
}
/** Don't unnecessarily invoke {@link Flux#concat(Publisher)}. */
static final class FluxIdentity<T> {
@BeforeTemplate
Flux<T> before(Flux<T> flux) {
return Flux.concat(flux);
}
@AfterTemplate
Flux<T> after(Flux<T> flux) {
return flux;
}
}
/**
* Prefer a collection using {@link MoreCollectors#toOptional()} over more contrived alternatives.
*/
@@ -201,12 +218,38 @@ final class ReactorTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Mono<Optional<T>> after(Mono<T> mono) {
return mono.flux().collect(toOptional());
}
}
/** Prefer {@link Mono#cast(Class)} over {@link Mono#map(Function)} with a cast. */
static final class MonoCast<T, S> {
@BeforeTemplate
Mono<S> before(Mono<T> mono) {
return mono.map(Refaster.<S>clazz()::cast);
}
@AfterTemplate
Mono<S> after(Mono<T> mono) {
return mono.cast(Refaster.<S>clazz());
}
}
/** Prefer {@link Flux#cast(Class)} over {@link Flux#map(Function)} with a cast. */
static final class FluxCast<T, S> {
@BeforeTemplate
Flux<S> before(Flux<T> flux) {
return flux.map(Refaster.<S>clazz()::cast);
}
@AfterTemplate
Flux<S> after(Flux<T> flux) {
return flux.cast(Refaster.<S>clazz());
}
}
/** Prefer {@link PublisherProbe#empty()}} over more verbose alternatives. */
static final class PublisherProbeEmpty<T> {
@BeforeTemplate
@@ -304,7 +347,9 @@ final class ReactorTemplates {
static final class StepVerifierLastStepVerifyErrorClass<T extends Throwable> {
@BeforeTemplate
Duration before(StepVerifier.LastStep step, Class<T> clazz) {
return step.expectError(clazz).verify();
return Refaster.anyOf(
step.expectError(clazz).verify(),
step.verifyErrorSatisfies(t -> assertThat(t).isInstanceOf(clazz)));
}
@AfterTemplate

View File

@@ -41,11 +41,9 @@ final class RxJava2AdapterTemplates {
@BeforeTemplate
Publisher<T> before(Flowable<T> flowable) {
return Refaster.anyOf(
Flux.from(flowable),
flowable.compose(Flux::from),
flowable.to(Flux::from),
flowable.as(Flux::from),
RxJava2Adapter.flowableToFlux(flowable),
flowable.compose(RxJava2Adapter::flowableToFlux),
flowable.to(RxJava2Adapter::flowableToFlux));
}
@@ -67,7 +65,6 @@ final class RxJava2AdapterTemplates {
Flowable.fromPublisher(flux),
flux.transform(Flowable::fromPublisher),
flux.as(Flowable::fromPublisher),
RxJava2Adapter.fluxToFlowable(flux),
flux.transform(RxJava2Adapter::fluxToFlowable));
}
@@ -140,7 +137,6 @@ final class RxJava2AdapterTemplates {
Flowable.fromPublisher(mono),
mono.transform(Flowable::fromPublisher),
mono.as(Flowable::fromPublisher),
RxJava2Adapter.monoToFlowable(mono),
mono.transform(RxJava2Adapter::monoToFlowable));
}

View File

@@ -1,11 +1,12 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static java.util.Comparator.naturalOrder;
import static java.util.Comparator.reverseOrder;
import static java.util.function.Predicate.not;
import static java.util.stream.Collectors.joining;
import com.google.common.collect.Streams;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
@@ -18,12 +19,31 @@ import java.util.Objects;
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.Stream;
/** Refaster templates related to expressions dealing with {@link Stream}s. */
final class StreamTemplates {
private StreamTemplates() {}
/**
* Prefer {@link Collectors#joining()} over {@link Collectors#joining(CharSequence)} with an empty
* delimiter string.
*/
static final class Joining {
@BeforeTemplate
Collector<CharSequence, ?, String> before() {
return joining("");
}
@AfterTemplate
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Collector<CharSequence, ?, String> after() {
return joining();
}
}
/** Prefer {@link Stream#empty()} over less clear alternatives. */
static final class EmptyStream<T> {
@BeforeTemplate
@@ -216,7 +236,7 @@ final class StreamTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Optional<T> after(Stream<T> stream) {
return stream.min(naturalOrder());
}
@@ -242,7 +262,7 @@ final class StreamTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
Optional<T> after(Stream<T> stream) {
return stream.max(naturalOrder());
}

View File

@@ -1,5 +1,6 @@
package tech.picnic.errorprone.refastertemplates;
import static com.google.errorprone.refaster.ImportPolicy.STATIC_IMPORT_ALWAYS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.fail;
@@ -17,7 +18,6 @@ import static org.testng.Assert.assertTrue;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.DoNotCall;
import com.google.errorprone.refaster.ImportPolicy;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
@@ -96,7 +96,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(String message) {
fail(message);
}
@@ -110,7 +110,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(String message, Throwable throwable) {
fail(message, throwable);
}
@@ -123,7 +123,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(boolean condition) {
assertThat(condition).isTrue();
}
@@ -136,7 +136,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(boolean condition, String message) {
assertThat(condition).withFailMessage(message).isTrue();
}
@@ -149,7 +149,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(boolean condition) {
assertThat(condition).isFalse();
}
@@ -162,7 +162,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(boolean condition, String message) {
assertThat(condition).withFailMessage(message).isFalse();
}
@@ -175,7 +175,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object object) {
assertThat(object).isNull();
}
@@ -188,7 +188,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object object, String message) {
assertThat(object).withFailMessage(message).isNull();
}
@@ -201,7 +201,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object object) {
assertThat(object).isNotNull();
}
@@ -214,7 +214,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object object, String message) {
assertThat(object).withFailMessage(message).isNotNull();
}
@@ -227,7 +227,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object actual, Object expected) {
assertThat(actual).isSameAs(expected);
}
@@ -240,7 +240,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object actual, Object expected, String message) {
assertThat(actual).withFailMessage(message).isSameAs(expected);
}
@@ -253,7 +253,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object actual, Object expected) {
assertThat(actual).isNotSameAs(expected);
}
@@ -266,7 +266,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object actual, Object expected, String message) {
assertThat(actual).withFailMessage(message).isNotSameAs(expected);
}
@@ -329,7 +329,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object actual, Object expected) {
assertThat(actual).isEqualTo(expected);
}
@@ -392,7 +392,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object actual, Object expected, String message) {
assertThat(actual).withFailMessage(message).isEqualTo(expected);
}
@@ -405,7 +405,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(float actual, float expected, float delta) {
assertThat(actual).isCloseTo(expected, offset(delta));
}
@@ -418,7 +418,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(float actual, float expected, float delta, String message) {
assertThat(actual).withFailMessage(message).isCloseTo(expected, offset(delta));
}
@@ -431,7 +431,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(double actual, double expected, double delta) {
assertThat(actual).isCloseTo(expected, offset(delta));
}
@@ -444,7 +444,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(double actual, double expected, double delta, String message) {
assertThat(actual).withFailMessage(message).isCloseTo(expected, offset(delta));
}
@@ -497,7 +497,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object[] actual, Object[] expected) {
assertThat(actual).containsExactly(expected);
}
@@ -550,7 +550,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object[] actual, Object[] expected, String message) {
assertThat(actual).withFailMessage(message).containsExactly(expected);
}
@@ -563,7 +563,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object[] actual, Object[] expected) {
assertThat(actual).containsExactlyInAnyOrder(expected);
}
@@ -576,7 +576,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object[] actual, Object[] expected, String message) {
assertThat(actual).withFailMessage(message).containsExactlyInAnyOrder(expected);
}
@@ -589,7 +589,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
<S, T extends S> void after(Iterator<S> actual, Iterator<T> expected) {
// XXX: This is not `null`-safe.
// XXX: The `ImmutableList.copyOf` should actually *not* be imported statically.
@@ -604,7 +604,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
<S, T extends S> void after(Iterator<S> actual, Iterator<T> expected, String message) {
// XXX: This is not `null`-safe.
// XXX: The `ImmutableList.copyOf` should actually *not* be imported statically.
@@ -629,7 +629,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
<S, T extends S> void after(Iterable<S> actual, Iterable<T> expected) {
assertThat(actual).containsExactlyElementsOf(expected);
}
@@ -647,7 +647,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
<S, T extends S> void after(Iterable<S> actual, Iterable<T> expected, String message) {
assertThat(actual).withFailMessage(message).containsExactlyElementsOf(expected);
}
@@ -660,7 +660,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
<S, T extends S> void after(Set<S> actual, Set<T> expected) {
assertThat(actual).hasSameElementsAs(expected);
}
@@ -673,7 +673,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
<S, T extends S> void after(Set<S> actual, Set<T> expected, String message) {
assertThat(actual).withFailMessage(message).hasSameElementsAs(expected);
}
@@ -741,7 +741,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object actual, Object expected) {
assertThat(actual).isNotEqualTo(expected);
}
@@ -809,7 +809,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(Object actual, Object expected, String message) {
assertThat(actual).withFailMessage(message).isNotEqualTo(expected);
}
@@ -822,7 +822,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(float actual, float expected, float delta) {
assertThat(actual).isNotCloseTo(expected, offset(delta));
}
@@ -835,7 +835,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(float actual, float expected, float delta, String message) {
assertThat(actual).withFailMessage(message).isNotCloseTo(expected, offset(delta));
}
@@ -848,7 +848,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(double actual, double expected, double delta) {
assertThat(actual).isNotCloseTo(expected, offset(delta));
}
@@ -861,7 +861,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(double actual, double expected, double delta, String message) {
assertThat(actual).withFailMessage(message).isNotCloseTo(expected, offset(delta));
}
@@ -874,7 +874,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(ThrowingCallable runnable) {
assertThatThrownBy(runnable);
}
@@ -887,7 +887,7 @@ final class TestNGToAssertJTemplates {
}
@AfterTemplate
@UseImportPolicy(ImportPolicy.STATIC_IMPORT_ALWAYS)
@UseImportPolicy(STATIC_IMPORT_ALWAYS)
void after(ThrowingCallable runnable, Class<T> clazz) {
assertThatThrownBy(runnable).isInstanceOf(clazz);
}

View File

@@ -1,5 +1,7 @@
package tech.picnic.errorprone.refastertemplates;
import static java.time.ZoneOffset.UTC;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.AlsoNegation;
@@ -17,6 +19,7 @@ import java.time.ZoneOffset;
import java.time.chrono.ChronoLocalDate;
import java.time.chrono.ChronoLocalDateTime;
import java.time.chrono.ChronoZonedDateTime;
import java.time.temporal.ChronoUnit;
import java.time.temporal.TemporalUnit;
/** Refaster templates related to expressions dealing with time. */
@@ -49,13 +52,26 @@ final class TimeTemplates {
ZoneId.of("UTC"),
ZoneId.of("+0"),
ZoneId.of("-0"),
ZoneOffset.UTC.normalized(),
ZoneId.from(ZoneOffset.UTC));
UTC.normalized(),
ZoneId.from(UTC));
}
@AfterTemplate
ZoneOffset after() {
return ZoneOffset.UTC;
return UTC;
}
}
/** Prefer {@link Instant#atOffset(ZoneOffset)} over the more verbose alternative. */
static final class InstantAtOffset {
@BeforeTemplate
OffsetDateTime before(Instant instant, ZoneOffset zoneOffset) {
return OffsetDateTime.ofInstant(instant, zoneOffset);
}
@AfterTemplate
OffsetDateTime after(Instant instant, ZoneOffset zoneOffset) {
return instant.atOffset(zoneOffset);
}
}
@@ -64,7 +80,7 @@ final class TimeTemplates {
@BeforeTemplate
@SuppressWarnings("TimeZoneUsage")
Clock before() {
return Clock.system(ZoneOffset.UTC);
return Clock.system(UTC);
}
@AfterTemplate
@@ -310,8 +326,86 @@ final class TimeTemplates {
}
}
/** Prefer {@link Duration#ofDays(long)} over alternative representations. */
static final class DurationOfDays {
@BeforeTemplate
Duration before(long amount) {
return Duration.of(amount, ChronoUnit.DAYS);
}
@AfterTemplate
Duration after(long amount) {
return Duration.ofDays(amount);
}
}
/** Prefer {@link Duration#ofHours(long)} over alternative representations. */
static final class DurationOfHours {
@BeforeTemplate
Duration before(long amount) {
return Duration.of(amount, ChronoUnit.HOURS);
}
@AfterTemplate
Duration after(long amount) {
return Duration.ofHours(amount);
}
}
/** Prefer {@link Duration#ofMillis(long)} over alternative representations. */
static final class DurationOfMillis {
@BeforeTemplate
Duration before(long amount) {
return Duration.of(amount, ChronoUnit.MILLIS);
}
@AfterTemplate
Duration after(long amount) {
return Duration.ofMillis(amount);
}
}
/** Prefer {@link Duration#ofMinutes(long)} over alternative representations. */
static final class DurationOfMinutes {
@BeforeTemplate
Duration before(long amount) {
return Duration.of(amount, ChronoUnit.MINUTES);
}
@AfterTemplate
Duration after(long amount) {
return Duration.ofMinutes(amount);
}
}
/** Prefer {@link Duration#ofNanos(long)} over alternative representations. */
static final class DurationOfNanos {
@BeforeTemplate
Duration before(long amount) {
return Duration.of(amount, ChronoUnit.NANOS);
}
@AfterTemplate
Duration after(long amount) {
return Duration.ofNanos(amount);
}
}
/** Prefer {@link Duration#ofSeconds(long)} over alternative representations. */
static final class DurationOfSeconds {
@BeforeTemplate
Duration before(long amount) {
return Duration.of(amount, ChronoUnit.SECONDS);
}
@AfterTemplate
Duration after(long amount) {
return Duration.ofSeconds(amount);
}
}
/**
* Don't unnecessarily convert two and from milliseconds. (This way nanosecond precision is
* Don't unnecessarily convert to and from milliseconds. (This way nanosecond precision is
* retained.)
*
* <p><strong>Warning:</strong> this rewrite rule increases precision!
@@ -329,7 +423,7 @@ final class TimeTemplates {
}
/**
* Don't unnecessarily convert two and from milliseconds. (This way nanosecond precision is
* Don't unnecessarily convert to and from milliseconds. (This way nanosecond precision is
* retained.)
*
* <p><strong>Warning:</strong> this rewrite rule increases precision!

View File

@@ -1,12 +1,25 @@
package tech.picnic.errorprone.refastertemplates;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.HEAD;
import static org.springframework.http.HttpMethod.OPTIONS;
import static org.springframework.http.HttpMethod.PATCH;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.http.HttpMethod.PUT;
import static org.springframework.web.reactive.function.BodyInserters.fromValue;
import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.Repeated;
import java.util.function.Function;
import org.springframework.http.HttpMethod;
import org.springframework.test.web.reactive.server.WebTestClient;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.reactive.function.client.WebClient.RequestBodySpec;
import org.springframework.web.reactive.function.client.WebClient.RequestBodyUriSpec;
import org.springframework.web.reactive.function.client.WebClient.RequestHeadersSpec;
import org.springframework.web.reactive.function.client.WebClient.RequestHeadersUriSpec;
/**
* Refaster templates related to expressions dealing with {@link
@@ -23,7 +36,7 @@ final class WebClientTemplates {
}
@BeforeTemplate
WebTestClient.RequestHeadersSpec<?> before2(
WebTestClient.RequestHeadersSpec<?> before(
WebTestClient.RequestBodySpec requestBodySpec, T value) {
return requestBodySpec.body(fromValue(value));
}
@@ -33,4 +46,159 @@ final class WebClientTemplates {
return requestBodySpec.bodyValue(value);
}
}
/**
* Prefer {@link WebClient#get()} over {@link WebClient#method(HttpMethod)} with {@link
* HttpMethod#GET}.
*/
static final class WebClientGet {
@BeforeTemplate
RequestHeadersSpec<?> before(WebClient webClient) {
return webClient.method(GET);
}
@BeforeTemplate
WebTestClient.RequestHeadersSpec<?> before(WebTestClient webClient) {
return webClient.method(GET);
}
@AfterTemplate
RequestHeadersSpec<?> after(WebClient webClient) {
return webClient.get();
}
}
/**
* Prefer {@link WebClient#head()} over {@link WebClient#method(HttpMethod)} with {@link
* HttpMethod#HEAD}.
*/
static final class WebClientHead {
@BeforeTemplate
RequestHeadersSpec<?> before(WebClient webClient) {
return webClient.method(HEAD);
}
@BeforeTemplate
WebTestClient.RequestHeadersSpec<?> before(WebTestClient webClient) {
return webClient.method(HEAD);
}
@AfterTemplate
RequestHeadersSpec<?> after(WebClient webClient) {
return webClient.head();
}
}
/**
* Prefer {@link WebClient#options()} over {@link WebClient#method(HttpMethod)} with {@link
* HttpMethod#OPTIONS}.
*/
static final class WebClientOptions {
@BeforeTemplate
RequestHeadersSpec<?> before(WebClient webClient) {
return webClient.method(OPTIONS);
}
@BeforeTemplate
WebTestClient.RequestHeadersSpec<?> before(WebTestClient webClient) {
return webClient.method(OPTIONS);
}
@AfterTemplate
RequestHeadersSpec<?> after(WebClient webClient) {
return webClient.options();
}
}
/**
* Prefer {@link WebClient#patch()} over {@link WebClient#method(HttpMethod)} with {@link
* HttpMethod#PATCH}.
*/
static final class WebClientPatch {
@BeforeTemplate
RequestBodyUriSpec before(WebClient webClient) {
return webClient.method(PATCH);
}
@BeforeTemplate
WebTestClient.RequestBodyUriSpec before(WebTestClient webClient) {
return webClient.method(PATCH);
}
@AfterTemplate
RequestBodyUriSpec after(WebClient webClient) {
return webClient.patch();
}
}
/**
* Prefer {@link WebClient#post()} over {@link WebClient#method(HttpMethod)} with {@link
* HttpMethod#POST}.
*/
static final class WebClientPost {
@BeforeTemplate
RequestBodyUriSpec before(WebClient webClient) {
return webClient.method(POST);
}
@BeforeTemplate
WebTestClient.RequestBodyUriSpec before(WebTestClient webClient) {
return webClient.method(POST);
}
@AfterTemplate
RequestBodyUriSpec after(WebClient webClient) {
return webClient.post();
}
}
/**
* Prefer {@link WebClient#put()} over {@link WebClient#method(HttpMethod)} with {@link
* HttpMethod#PUT}.
*/
static final class WebClientPut {
@BeforeTemplate
RequestBodyUriSpec before(WebClient webClient) {
return webClient.method(PUT);
}
@BeforeTemplate
WebTestClient.RequestBodyUriSpec before(WebTestClient webClient) {
return webClient.method(PUT);
}
@AfterTemplate
RequestBodyUriSpec after(WebClient webClient) {
return webClient.put();
}
}
/** Don't unnecessarily use {@link RequestHeadersUriSpec#uri(Function)}. */
abstract static class RequestHeadersUriSpecUri {
@BeforeTemplate
RequestHeadersSpec<?> before(
RequestHeadersUriSpec<?> requestHeadersUriSpec,
String path,
@Repeated Object uriVariables) {
return requestHeadersUriSpec.uri(
uriBuilder -> uriBuilder.path(path).build(Refaster.asVarargs(uriVariables)));
}
@BeforeTemplate
WebTestClient.RequestHeadersSpec<?> before(
WebTestClient.RequestHeadersUriSpec<?> requestHeadersUriSpec,
String path,
@Repeated Object uriVariables) {
return requestHeadersUriSpec.uri(
uriBuilder -> uriBuilder.path(path).build(Refaster.asVarargs(uriVariables)));
}
@AfterTemplate
RequestHeadersSpec<?> after(
RequestHeadersUriSpec<?> requestHeadersUriSpec,
String path,
@Repeated Object uriVariables) {
return requestHeadersUriSpec.uri(path, Refaster.asVarargs(uriVariables));
}
}
}

View File

@@ -112,7 +112,6 @@ final class AmbiguousJsonCreatorCheckTest {
" return new F(s);",
" }",
" }",
"",
"}")
.doTest();
}

View File

@@ -6,7 +6,7 @@ import com.google.common.collect.ImmutableList;
import java.util.Optional;
import org.junit.jupiter.api.Test;
public final class AnnotationAttributeMatcherTest {
final class AnnotationAttributeMatcherTest {
@Test
void withoutListings() {
AnnotationAttributeMatcher matcher =

View File

@@ -0,0 +1,64 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
final class AssertJIsNullCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(AssertJIsNullCheck.class, getClass());
private final BugCheckerRefactoringTestHelper refactoringTestHelper =
BugCheckerRefactoringTestHelper.newInstance(AssertJIsNullCheck.class, getClass());
@Test
void identification() {
compilationTestHelper
.addSourceLines(
"A.java",
"import static org.assertj.core.api.Assertions.assertThat;",
"",
"class A {",
" void m() {",
" assertThat(1).isEqualTo(1);",
" // BUG: Diagnostic contains:",
" assertThat(1).isEqualTo(null);",
" // BUG: Diagnostic contains:",
" assertThat(\"foo\").isEqualTo(null);",
" isEqualTo(null);",
" }",
"",
" private boolean isEqualTo(Object value) {",
" return value.equals(\"bar\");",
" }",
"}")
.doTest();
}
@Test
void replacement() {
refactoringTestHelper
.addInputLines(
"A.java",
"import static org.assertj.core.api.Assertions.assertThat;",
"",
"class A {",
" void m() {",
" assertThat(1).isEqualTo(null);",
" assertThat(\"foo\").isEqualTo(null);",
" }",
"}")
.addOutputLines(
"A.java",
"import static org.assertj.core.api.Assertions.assertThat;",
"",
"class A {",
" void m() {",
" assertThat(1).isNull();",
" assertThat(\"foo\").isNull();",
" }",
"}")
.doTest(TEXT_MATCH);
}
}

View File

@@ -5,7 +5,7 @@ import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
public final class AutowiredConstructorCheckTest {
final class AutowiredConstructorCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(AutowiredConstructorCheck.class, getClass());
private final BugCheckerRefactoringTestHelper refactoringTestHelper =
@@ -27,27 +27,34 @@ public final class AutowiredConstructorCheckTest {
" }",
"",
" class B {",
" @Autowired void setProperty(Object o) {}",
" @Autowired",
" void setProperty(Object o) {}",
" }",
"",
" class C {",
" // BUG: Diagnostic contains:",
" @Autowired C() {}",
" @Autowired",
" C() {}",
" }",
"",
" class D {",
" // BUG: Diagnostic contains:",
" @Autowired D(String x) {}",
" @Autowired",
" D(String x) {}",
" }",
"",
" class E {",
" @Autowired E() {}",
" @Autowired",
" E() {}",
"",
" E(String x) {}",
" }",
"",
" class F {",
" F() {}",
" @Autowired F(String x) {}",
"",
" @Autowired",
" F(String x) {}",
" }",
"",
" class G {",
@@ -55,7 +62,8 @@ public final class AutowiredConstructorCheckTest {
" }",
"",
" class H {",
" @SafeVarargs H(List<String>... lists) {}",
" @SafeVarargs",
" H(List<String>... lists) {}",
" }",
"}")
.doTest();
@@ -70,11 +78,14 @@ public final class AutowiredConstructorCheckTest {
"",
"interface Container {",
" class A {",
" @Autowired @Deprecated A() {}",
" @Autowired",
" @Deprecated",
" A() {}",
" }",
"",
" class B {",
" @Autowired B(String x) {}",
" @Autowired",
" B(String x) {}",
" }",
"}")
.addOutputLines(
@@ -83,10 +94,13 @@ public final class AutowiredConstructorCheckTest {
"",
"interface Container {",
" class A {",
" @Deprecated A() {}",
"",
" @Deprecated",
" A() {}",
" }",
"",
" class B {",
"",
" B(String x) {}",
" }",
"}")

View File

@@ -5,7 +5,7 @@ import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
public final class CanonicalAnnotationSyntaxCheckTest {
final class CanonicalAnnotationSyntaxCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(CanonicalAnnotationSyntaxCheck.class, getClass());
private final BugCheckerRefactoringTestHelper refactoringTestHelper =
@@ -23,63 +23,110 @@ public final class CanonicalAnnotationSyntaxCheckTest {
"interface A {",
" @interface Foo {",
" int[] value() default {};",
"",
" int[] value2() default {};",
" }",
"",
" @pkg.A.Foo A minimal1();",
" @A.Foo A minimal2();",
" @Foo A minimal3();",
" @pkg.A.Foo",
" A minimal1();",
"",
" @A.Foo",
" A minimal2();",
"",
" @Foo",
" A minimal3();",
"",
" // BUG: Diagnostic contains:",
" @pkg.A.Foo() A functional1();",
" @pkg.A.Foo()",
" A functional1();",
" // BUG: Diagnostic contains:",
" @A.Foo() A functional2();",
" @A.Foo()",
" A functional2();",
" // BUG: Diagnostic contains:",
" @Foo() A functional3();",
" @Foo()",
" A functional3();",
"",
" @pkg.A.Foo(1) A simple1();",
" @A.Foo(1) A simple2();",
" @Foo(1) A simple3();",
" @pkg.A.Foo(1)",
" A simple1();",
"",
" @A.Foo(1)",
" A simple2();",
"",
" @Foo(1)",
" A simple3();",
"",
" // BUG: Diagnostic contains:",
" @pkg.A.Foo({1}) A singleton1();",
" @pkg.A.Foo({1})",
" A singleton1();",
" // BUG: Diagnostic contains:",
" @A.Foo({1}) A singleton2();",
" @A.Foo({1})",
" A singleton2();",
" // BUG: Diagnostic contains:",
" @Foo({1}) A singleton3();",
" @Foo({1})",
" A singleton3();",
"",
" // BUG: Diagnostic contains:",
" @pkg.A.Foo(value = 1) A verbose1();",
" @pkg.A.Foo(value = 1)",
" A verbose1();",
" // BUG: Diagnostic contains:",
" @A.Foo(value = 1) A verbose2();",
" @A.Foo(value = 1)",
" A verbose2();",
" // BUG: Diagnostic contains:",
" @Foo(value = 1) A verbose3();",
" @Foo(value = 1)",
" A verbose3();",
"",
" @pkg.A.Foo(value2 = 2) A custom1();",
" @A.Foo(value2 = 2) A custom2();",
" @Foo(value2 = 2) A custom3();",
" @pkg.A.Foo(value2 = 2)",
" A custom1();",
"",
" @A.Foo(value2 = 2)",
" A custom2();",
"",
" @Foo(value2 = 2)",
" A custom3();",
"",
" // BUG: Diagnostic contains:",
" @pkg.A.Foo(value2 = {2}) A customSingleton1();",
" @pkg.A.Foo(value2 = {2})",
" A customSingleton1();",
" // BUG: Diagnostic contains:",
" @A.Foo(value2 = {2}) A customSingleton2();",
" @A.Foo(value2 = {2})",
" A customSingleton2();",
" // BUG: Diagnostic contains:",
" @Foo(value2 = {2}) A customSingleton3();",
" @Foo(value2 = {2})",
" A customSingleton3();",
"",
" @pkg.A.Foo(value2 = {2, 2}) A customPair1();",
" @A.Foo(value2 = {2, 2}) A customPair2();",
" @Foo(value2 = {2, 2}) A customPair3();",
" @pkg.A.Foo(value2 = {2, 2})",
" A customPair1();",
"",
" @pkg.A.Foo(value = 1, value2 = 2) A extended1();",
" @A.Foo(value = 1, value2 = 2) A extended2();",
" @Foo(value = 1, value2 = 2) A extended3();",
" @A.Foo(value2 = {2, 2})",
" A customPair2();",
"",
" @Foo(value2 = {2, 2})",
" A customPair3();",
"",
" @pkg.A.Foo(value = 1, value2 = 2)",
" A extended1();",
"",
" @A.Foo(value = 1, value2 = 2)",
" A extended2();",
"",
" @Foo(value = 1, value2 = 2)",
" A extended3();",
"",
" // BUG: Diagnostic contains:",
" @pkg.A.Foo({1, 1,}) A trailingComma1();",
" @pkg.A.Foo({",
" 1, 1,",
" })",
" A trailingComma1();",
" // BUG: Diagnostic contains:",
" @A.Foo({1, 1,}) A trailingComma2();",
" @A.Foo({",
" 1, 1,",
" })",
" A trailingComma2();",
" // BUG: Diagnostic contains:",
" @Foo({1, 1,}) A trailingComma3();",
" @Foo({",
" 1, 1,",
" })",
" A trailingComma3();",
"}")
.doTest();
}
@@ -96,28 +143,67 @@ public final class CanonicalAnnotationSyntaxCheckTest {
"interface A {",
" @interface Foo {",
" String[] value() default {};",
"",
" int[] value2() default {};",
" }",
"",
" @pkg.A.Foo() A functional1();",
" @A.Foo() A functional2();",
" @Foo() A functional3();",
" @pkg.A.Foo()",
" A functional1();",
"",
" @pkg.A.Foo(value = \"foo\") A verbose1();",
" @A.Foo(value = \"a'b\") A verbose2();",
" @Foo(value = \"a\" + \"\\nb\") A verbose3();",
" @A.Foo()",
" A functional2();",
"",
" @pkg.A.Foo(value = {\"foo\"}) A moreVerbose1();",
" @A.Foo(value = {\"a'b\"}) A moreVerbose2();",
" @Foo(value = {\"a\" + \"\\nb\"}) A moreVerbose3();",
" @Foo()",
" A functional3();",
"",
" @pkg.A.Foo(value = {\"foo\", \"bar\"}, value2 = {2}) A extended1();",
" @A.Foo(value = {\"a'b\", \"c'd\"}, value2 = {2}) A extended2();",
" @Foo(value = {\"a\" + \"\\nb\", \"c\" + \"\\nd\"}, value2 = {2}) A extended3();",
" @pkg.A.Foo(value = \"foo\")",
" A verbose1();",
"",
" @pkg.A.Foo({\"foo\", \"bar\",}) A trailingComma1();",
" @A.Foo({\"a'b\", \"c'd\",}) A trailingComma2();",
" @Foo({\"a\" + \"\\nb\", \"c\" + \"\\nd\",}) A trailingComma3();",
" @A.Foo(value = \"a'b\")",
" A verbose2();",
"",
" @Foo(value = \"a\" + \"\\nb\")",
" A verbose3();",
"",
" @pkg.A.Foo(value = {\"foo\"})",
" A moreVerbose1();",
"",
" @A.Foo(value = {\"a'b\"})",
" A moreVerbose2();",
"",
" @Foo(value = {\"a\" + \"\\nb\"})",
" A moreVerbose3();",
"",
" @pkg.A.Foo(",
" value = {\"foo\", \"bar\"},",
" value2 = {2})",
" A extended1();",
"",
" @A.Foo(",
" value = {\"a'b\", \"c'd\"},",
" value2 = {2})",
" A extended2();",
"",
" @Foo(",
" value = {\"a\" + \"\\nb\", \"c\" + \"\\nd\"},",
" value2 = {2})",
" A extended3();",
"",
" @pkg.A.Foo({",
" \"foo\", \"bar\",",
" })",
" A trailingComma1();",
"",
" @A.Foo({",
" \"a'b\", \"c'd\",",
" })",
" A trailingComma2();",
"",
" @Foo({",
" \"a\" + \"\\nb\",",
" \"c\" + \"\\nd\",",
" })",
" A trailingComma3();",
"}")
.addOutputLines(
"out/pkg/A.java",
@@ -128,28 +214,60 @@ public final class CanonicalAnnotationSyntaxCheckTest {
"interface A {",
" @interface Foo {",
" String[] value() default {};",
"",
" int[] value2() default {};",
" }",
"",
" @pkg.A.Foo A functional1();",
" @A.Foo A functional2();",
" @Foo A functional3();",
" @pkg.A.Foo",
" A functional1();",
"",
" @pkg.A.Foo(\"foo\") A verbose1();",
" @A.Foo(\"a'b\") A verbose2();",
" @Foo(\"a\" + \"\\nb\") A verbose3();",
" @A.Foo",
" A functional2();",
"",
" @pkg.A.Foo(\"foo\") A moreVerbose1();",
" @A.Foo(\"a'b\") A moreVerbose2();",
" @Foo(\"a\" + \"\\nb\") A moreVerbose3();",
" @Foo",
" A functional3();",
"",
" @pkg.A.Foo(value = {\"foo\", \"bar\"}, value2 = 2) A extended1();",
" @A.Foo(value = {\"a'b\", \"c'd\"}, value2 = 2) A extended2();",
" @Foo(value = {\"a\" + \"\\nb\", \"c\" + \"\\nd\"}, value2 = 2) A extended3();",
" @pkg.A.Foo(\"foo\")",
" A verbose1();",
"",
" @pkg.A.Foo({\"foo\", \"bar\"}) A trailingComma1();",
" @A.Foo({\"a'b\", \"c'd\"}) A trailingComma2();",
" @Foo({\"a\" + \"\\nb\", \"c\" + \"\\nd\"}) A trailingComma3();",
" @A.Foo(\"a'b\")",
" A verbose2();",
"",
" @Foo(\"a\" + \"\\nb\")",
" A verbose3();",
"",
" @pkg.A.Foo(\"foo\")",
" A moreVerbose1();",
"",
" @A.Foo(\"a'b\")",
" A moreVerbose2();",
"",
" @Foo(\"a\" + \"\\nb\")",
" A moreVerbose3();",
"",
" @pkg.A.Foo(",
" value = {\"foo\", \"bar\"},",
" value2 = 2)",
" A extended1();",
"",
" @A.Foo(",
" value = {\"a'b\", \"c'd\"},",
" value2 = 2)",
" A extended2();",
"",
" @Foo(",
" value = {\"a\" + \"\\nb\", \"c\" + \"\\nd\"},",
" value2 = 2)",
" A extended3();",
"",
" @pkg.A.Foo({\"foo\", \"bar\"})",
" A trailingComma1();",
"",
" @A.Foo({\"a'b\", \"c'd\"})",
" A trailingComma2();",
"",
" @Foo({\"a\" + \"\\nb\", \"c\" + \"\\nd\"})",
" A trailingComma3();",
"}")
.doTest(TestMode.TEXT_MATCH);
}

View File

@@ -0,0 +1,200 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers.SECOND;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
final class CollectorMutabilityCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(CollectorMutabilityCheck.class, getClass());
private final BugCheckerRefactoringTestHelper refactoringTestHelper =
BugCheckerRefactoringTestHelper.newInstance(CollectorMutabilityCheck.class, getClass());
@Test
void identification() {
compilationTestHelper
.addSourceLines(
"A.java",
"import static com.google.common.collect.ImmutableList.toImmutableList;",
"import static com.google.common.collect.ImmutableMap.toImmutableMap;",
"import static com.google.common.collect.ImmutableSet.toImmutableSet;",
"import static java.util.stream.Collectors.toCollection;",
"import static java.util.stream.Collectors.toList;",
"import static java.util.stream.Collectors.toMap;",
"import static java.util.stream.Collectors.toSet;",
"",
"import java.util.ArrayList;",
"import java.util.HashMap;",
"import java.util.HashSet;",
"import java.util.stream.Collectors;",
"import java.util.stream.Stream;",
"import reactor.core.publisher.Flux;",
"",
"class A {",
" void m() {",
" // BUG: Diagnostic contains:",
" Flux.just(1).collect(Collectors.toList());",
" // BUG: Diagnostic contains:",
" Flux.just(2).collect(toList());",
" Flux.just(3).collect(toImmutableList());",
" Flux.just(4).collect(toCollection(ArrayList::new));",
"",
" // BUG: Diagnostic contains:",
" Flux.just(\"foo\").collect(Collectors.toMap(String::getBytes, String::length));",
" // BUG: Diagnostic contains:",
" Flux.just(\"bar\").collect(toMap(String::getBytes, String::length));",
" Flux.just(\"baz\").collect(toImmutableMap(String::getBytes, String::length));",
" // BUG: Diagnostic contains:",
" Flux.just(\"qux\").collect(toMap(String::getBytes, String::length, (a, b) -> a));",
" Flux.just(\"quux\").collect(toImmutableMap(String::getBytes, String::length, (a, b) -> a));",
" Flux.just(\"quuz\").collect(toMap(String::getBytes, String::length, (a, b) -> a, HashMap::new));",
"",
" // BUG: Diagnostic contains:",
" Stream.of(1).collect(Collectors.toSet());",
" // BUG: Diagnostic contains:",
" Stream.of(2).collect(toSet());",
" Stream.of(3).collect(toImmutableSet());",
" Stream.of(4).collect(toCollection(HashSet::new));",
"",
" Flux.just(\"foo\").collect(Collectors.joining());",
" }",
"}")
.doTest();
}
@Test
void replacementFirstSuggestedFix() {
refactoringTestHelper
.addInputLines(
"A.java",
"import static java.util.stream.Collectors.toList;",
"import static java.util.stream.Collectors.toMap;",
"import static java.util.stream.Collectors.toSet;",
"",
"import java.util.stream.Collectors;",
"import java.util.stream.Stream;",
"import reactor.core.publisher.Flux;",
"",
"class A {",
" void m() {",
" Flux.just(1).collect(Collectors.toList());",
" Flux.just(2).collect(toList());",
"",
" Stream.of(\"foo\").collect(Collectors.toMap(String::getBytes, String::length));",
" Stream.of(\"bar\").collect(toMap(String::getBytes, String::length));",
" Flux.just(\"baz\").collect(Collectors.toMap(String::getBytes, String::length, (a, b) -> b));",
" Flux.just(\"qux\").collect(toMap(String::getBytes, String::length, (a, b) -> b));",
"",
" Stream.of(1).collect(Collectors.toSet());",
" Stream.of(2).collect(toSet());",
" }",
"}")
.addOutputLines(
"A.java",
"import static com.google.common.collect.ImmutableList.toImmutableList;",
"import static com.google.common.collect.ImmutableMap.toImmutableMap;",
"import static com.google.common.collect.ImmutableSet.toImmutableSet;",
"import static java.util.stream.Collectors.toList;",
"import static java.util.stream.Collectors.toMap;",
"import static java.util.stream.Collectors.toSet;",
"",
"import java.util.stream.Collectors;",
"import java.util.stream.Stream;",
"import reactor.core.publisher.Flux;",
"",
"class A {",
" void m() {",
" Flux.just(1).collect(toImmutableList());",
" Flux.just(2).collect(toImmutableList());",
"",
" Stream.of(\"foo\").collect(toImmutableMap(String::getBytes, String::length));",
" Stream.of(\"bar\").collect(toImmutableMap(String::getBytes, String::length));",
" Flux.just(\"baz\").collect(toImmutableMap(String::getBytes, String::length, (a, b) -> b));",
" Flux.just(\"qux\").collect(toImmutableMap(String::getBytes, String::length, (a, b) -> b));",
"",
" Stream.of(1).collect(toImmutableSet());",
" Stream.of(2).collect(toImmutableSet());",
" }",
"}")
.doTest(TestMode.TEXT_MATCH);
}
@Test
void replacementSecondSuggestedFix() {
refactoringTestHelper
.setFixChooser(SECOND)
.addInputLines(
"A.java",
"import static java.util.stream.Collectors.toList;",
"import static java.util.stream.Collectors.toMap;",
"import static java.util.stream.Collectors.toSet;",
"",
"import java.util.stream.Collectors;",
"import java.util.stream.Stream;",
"import reactor.core.publisher.Flux;",
"",
"class A {",
" void m() {",
" Flux.just(1).collect(Collectors.toList());",
" Flux.just(2).collect(toList());",
"",
" Stream.of(\"foo\").collect(Collectors.toMap(String::getBytes, String::length));",
" Stream.of(\"bar\").collect(toMap(String::getBytes, String::length));",
" Flux.just(\"baz\").collect(Collectors.toMap(String::getBytes, String::length, (a, b) -> b));",
" Flux.just(\"qux\").collect(toMap(String::getBytes, String::length, (a, b) -> b));",
"",
" Stream.of(1).collect(Collectors.toSet());",
" Stream.of(2).collect(toSet());",
" }",
"}")
.addOutputLines(
"A.java",
"import static java.util.stream.Collectors.toCollection;",
"import static java.util.stream.Collectors.toList;",
"import static java.util.stream.Collectors.toMap;",
"import static java.util.stream.Collectors.toSet;",
"",
"import java.util.ArrayList;",
"import java.util.HashMap;",
"import java.util.HashSet;",
"import java.util.stream.Collectors;",
"import java.util.stream.Stream;",
"import reactor.core.publisher.Flux;",
"",
"class A {",
" void m() {",
" Flux.just(1).collect(toCollection(ArrayList::new));",
" Flux.just(2).collect(toCollection(ArrayList::new));",
"",
" Stream.of(\"foo\")",
" .collect(",
" Collectors.toMap(",
" String::getBytes,",
" String::length,",
" (a, b) -> {",
" throw new IllegalStateException();",
" },",
" HashMap::new));",
" Stream.of(\"bar\")",
" .collect(",
" toMap(",
" String::getBytes,",
" String::length,",
" (a, b) -> {",
" throw new IllegalStateException();",
" },",
" HashMap::new));",
" Flux.just(\"baz\")",
" .collect(Collectors.toMap(String::getBytes, String::length, (a, b) -> b, HashMap::new));",
" Flux.just(\"qux\").collect(toMap(String::getBytes, String::length, (a, b) -> b, HashMap::new));",
"",
" Stream.of(1).collect(toCollection(HashSet::new));",
" Stream.of(2).collect(toCollection(HashSet::new));",
" }",
"}")
.doTest(TestMode.TEXT_MATCH);
}
}

View File

@@ -5,7 +5,7 @@ import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
public final class EmptyMethodCheckTest {
final class EmptyMethodCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(EmptyMethodCheck.class, getClass());
private final BugCheckerRefactoringTestHelper refactoringTestHelper =

View File

@@ -0,0 +1,151 @@
package tech.picnic.errorprone.bugpatterns;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
final class ErrorProneTestHelperSourceFormatCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(ErrorProneTestHelperSourceFormatCheck.class, getClass());
private final BugCheckerRefactoringTestHelper refactoringTestHelper =
BugCheckerRefactoringTestHelper.newInstance(
ErrorProneTestHelperSourceFormatCheck.class, getClass());
@Test
void identification() {
compilationTestHelper
.addSourceLines(
"A.java",
"import com.google.errorprone.BugCheckerRefactoringTestHelper;",
"import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;",
"import com.google.errorprone.CompilationTestHelper;",
"import tech.picnic.errorprone.bugpatterns.EmptyMethodCheck;",
"",
"class A {",
" private final CompilationTestHelper compilationTestHelper =",
" CompilationTestHelper.newInstance(EmptyMethodCheck.class, getClass());",
" private final BugCheckerRefactoringTestHelper refactoringTestHelper =",
" BugCheckerRefactoringTestHelper.newInstance(EmptyMethodCheck.class, getClass());",
"",
" void m() {",
" compilationTestHelper",
" // BUG: Diagnostic contains: No source code provided",
" .addSourceLines(\"A.java\")",
" // BUG: Diagnostic contains: Source code is malformed:",
" .addSourceLines(\"B.java\", \"class B {\")",
" // Well-formed code, so not flagged.",
" .addSourceLines(\"C.java\", \"class C {}\")",
" // Malformed code, but not compile-time constant, so not flagged.",
" .addSourceLines(\"D.java\", \"class D {\" + getClass())",
" // BUG: Diagnostic contains: Test code should follow the Google Java style",
" .addSourceLines(\"E.java\", \"class E { }\")",
" .doTest();",
"",
" refactoringTestHelper",
" // BUG: Diagnostic contains: Test code should follow the Google Java style",
" .addInputLines(\"in/A.java\", \"class A { }\")",
" // BUG: Diagnostic contains: Test code should follow the Google Java style",
" .addOutputLines(\"out/A.java\", \"class A { }\")",
" // BUG: Diagnostic contains: Test code should follow the Google Java style",
" .addInputLines(\"in/B.java\", \"import java.util.Map;\", \"\", \"class B {}\")",
" // Unused import, but in an output file, so not flagged.",
" .addOutputLines(\"out/B.java\", \"import java.util.Map;\", \"\", \"class B {}\")",
" .doTest(TestMode.TEXT_MATCH);",
" }",
"}")
.doTest();
}
@Test
void replacement() {
/*
* Verifies that import sorting and code formatting is performed unconditionally, while unused
* imports are removed unless part of a `BugCheckerRefactoringTestHelper` expected output file.
*/
refactoringTestHelper
.addInputLines(
"in/A.java",
"import com.google.errorprone.BugCheckerRefactoringTestHelper;",
"import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;",
"import com.google.errorprone.CompilationTestHelper;",
"import tech.picnic.errorprone.bugpatterns.EmptyMethodCheck;",
"",
"class A {",
" private final CompilationTestHelper compilationTestHelper =",
" CompilationTestHelper.newInstance(EmptyMethodCheck.class, getClass());",
" private final BugCheckerRefactoringTestHelper refactoringTestHelper =",
" BugCheckerRefactoringTestHelper.newInstance(EmptyMethodCheck.class, getClass());",
"",
" void m() {",
" compilationTestHelper",
" .addSourceLines(",
" \"A.java\",",
" \"import java.util.Map;\",",
" \"import java.util.Collection;\",",
" \"import java.util.List;\",",
" \"\",",
" \"interface A extends List<A>, Map<A,A> { }\")",
" .doTest();",
"",
" refactoringTestHelper",
" .addInputLines(",
" \"in/A.java\",",
" \"import java.util.Map;\",",
" \"import java.util.Collection;\",",
" \"import java.util.List;\",",
" \"\",",
" \"interface A extends List<A>, Map<A,A> { }\")",
" .addOutputLines(",
" \"out/A.java\",",
" \"import java.util.Map;\",",
" \"import java.util.Collection;\",",
" \"import java.util.List;\",",
" \"\",",
" \"interface A extends List<A>, Map<A,A> { }\")",
" .doTest(TestMode.TEXT_MATCH);",
" }",
"}")
.addOutputLines(
"out/A.java",
"import com.google.errorprone.BugCheckerRefactoringTestHelper;",
"import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;",
"import com.google.errorprone.CompilationTestHelper;",
"import tech.picnic.errorprone.bugpatterns.EmptyMethodCheck;",
"",
"class A {",
" private final CompilationTestHelper compilationTestHelper =",
" CompilationTestHelper.newInstance(EmptyMethodCheck.class, getClass());",
" private final BugCheckerRefactoringTestHelper refactoringTestHelper =",
" BugCheckerRefactoringTestHelper.newInstance(EmptyMethodCheck.class, getClass());",
"",
" void m() {",
" compilationTestHelper",
" .addSourceLines(",
" \"A.java\",",
" \"import java.util.List;\",",
" \"import java.util.Map;\",",
" \"\",",
" \"interface A extends List<A>, Map<A, A> {}\")",
" .doTest();",
"",
" refactoringTestHelper",
" .addInputLines(",
" \"in/A.java\",",
" \"import java.util.List;\",",
" \"import java.util.Map;\",",
" \"\",",
" \"interface A extends List<A>, Map<A, A> {}\")",
" .addOutputLines(",
" \"out/A.java\",",
" \"import java.util.Collection;\",",
" \"import java.util.List;\",",
" \"import java.util.Map;\",",
" \"\",",
" \"interface A extends List<A>, Map<A, A> {}\")",
" .doTest(TestMode.TEXT_MATCH);",
" }",
"}")
.doTest(TestMode.TEXT_MATCH);
}
}

View File

@@ -12,14 +12,14 @@ final class ExplicitEnumOrderingCheckTest {
compilationTestHelper
.addSourceLines(
"A.java",
"import static java.lang.annotation.RetentionPolicy.SOURCE;",
"import static java.lang.annotation.RetentionPolicy.CLASS;",
"import static java.lang.annotation.RetentionPolicy.RUNTIME;",
"import static java.lang.annotation.RetentionPolicy.SOURCE;",
"import static java.time.chrono.IsoEra.BCE;",
"import static java.time.chrono.IsoEra.CE;",
"",
"import com.google.common.collect.Ordering;",
"import com.google.common.collect.ImmutableList;",
"import com.google.common.collect.Ordering;",
"import java.lang.annotation.RetentionPolicy;",
"import java.time.chrono.IsoEra;",
"",

View File

@@ -0,0 +1,122 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugCheckerRefactoringTestHelper.newInstance;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
final class FluxFlatMapUsageCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(FluxFlatMapUsageCheck.class, getClass());
private final BugCheckerRefactoringTestHelper refactoringTestHelper =
newInstance(FluxFlatMapUsageCheck.class, getClass());
@Test
void identification() {
compilationTestHelper
.addSourceLines(
"A.java",
"import java.util.function.BiFunction;",
"import java.util.function.Function;",
"import reactor.core.publisher.Flux;",
"import reactor.core.publisher.Mono;",
"",
"class A {",
" void m() {",
" // BUG: Diagnostic contains:",
" Flux.just(1).flatMap(Flux::just);",
" // BUG: Diagnostic contains:",
" Flux.just(1).<String>flatMap(i -> Flux.just(String.valueOf(i)));",
" // BUG: Diagnostic contains:",
" Flux.just(1).flatMapSequential(Flux::just);",
" // BUG: Diagnostic contains:",
" Flux.just(1).<String>flatMapSequential(i -> Flux.just(String.valueOf(i)));",
"",
" Mono.just(1).flatMap(Mono::just);",
" Flux.just(1).concatMap(Flux::just);",
"",
" Flux.just(1).flatMap(Flux::just, 1);",
" Flux.just(1).flatMap(Flux::just, 1, 1);",
" Flux.just(1).flatMap(Flux::just, throwable -> Flux.empty(), Flux::empty);",
"",
" Flux.just(1).flatMapSequential(Flux::just, 1);",
" Flux.just(1).flatMapSequential(Flux::just, 1, 1);",
"",
" // BUG: Diagnostic contains:",
" this.<String, Flux<String>>sink(Flux::flatMap);",
" // BUG: Diagnostic contains:",
" this.<Integer, Flux<Integer>>sink(Flux::<Integer>flatMap);",
"",
" // BUG: Diagnostic contains:",
" this.<String, Flux<String>>sink(Flux::flatMapSequential);",
" // BUG: Diagnostic contains:",
" this.<Integer, Flux<Integer>>sink(Flux::<Integer>flatMapSequential);",
"",
" this.<String, Mono<String>>sink(Mono::flatMap);",
" }",
"",
" private <T, P> void sink(BiFunction<P, Function<T, P>, P> fun) {}",
"}")
.doTest();
}
@Test
void replacementFirstSuggestedFix() {
refactoringTestHelper
.setFixChooser(FixChoosers.FIRST)
.addInputLines(
"in/A.java",
"import reactor.core.publisher.Flux;",
"",
"class A {",
" void m() {",
" Flux.just(1).flatMap(Flux::just);",
" Flux.just(1).flatMapSequential(Flux::just);",
" }",
"}")
.addOutputLines(
"out/A.java",
"import reactor.core.publisher.Flux;",
"",
"class A {",
" void m() {",
" Flux.just(1).concatMap(Flux::just);",
" Flux.just(1).concatMap(Flux::just);",
" }",
"}")
.doTest();
}
@Test
void replacementSecondSuggestedFix() {
refactoringTestHelper
.setFixChooser(FixChoosers.SECOND)
.addInputLines(
"in/A.java",
"import reactor.core.publisher.Flux;",
"",
"class A {",
" private static final int MAX_CONCURRENCY = 8;",
"",
" void m() {",
" Flux.just(1).flatMap(Flux::just);",
" Flux.just(1).flatMapSequential(Flux::just);",
" }",
"}")
.addOutputLines(
"out/A.java",
"import reactor.core.publisher.Flux;",
"",
"class A {",
" private static final int MAX_CONCURRENCY = 8;",
"",
" void m() {",
" Flux.just(1).flatMap(Flux::just, MAX_CONCURRENCY);",
" Flux.just(1).flatMapSequential(Flux::just, MAX_CONCURRENCY);",
" }",
"}")
.doTest();
}
}

View File

@@ -5,7 +5,7 @@ import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
public final class FormatStringConcatenationCheckTest {
final class FormatStringConcatenationCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(FormatStringConcatenationCheck.class, getClass());
private final BugCheckerRefactoringTestHelper refactoringTestHelper =
@@ -23,8 +23,8 @@ public final class FormatStringConcatenationCheckTest {
"import static org.assertj.core.api.Assertions.assertThat;",
"import static org.assertj.core.api.SoftAssertions.assertSoftly;",
"",
"import java.util.Locale;",
"import java.util.Formatter;",
"import java.util.Locale;",
"import org.assertj.core.api.Assertions;",
"import org.assertj.core.api.BDDAssertions;",
"import org.assertj.core.api.Fail;",
@@ -269,9 +269,9 @@ public final class FormatStringConcatenationCheckTest {
" LOG.error(\"{} \" + toString(), \"arg\");",
"",
" // BUG: Diagnostic contains:",
" LOG.error((Marker) null,\"str \" + toString());",
" LOG.error((Marker) null, \"str \" + toString());",
" // BUG: Diagnostic contains:",
" LOG.error((Marker) null,\"{} \" + toString(), \"arg\");",
" LOG.error((Marker) null, \"{} \" + toString(), \"arg\");",
"",
" // BUG: Diagnostic contains:",
" LOG.info(\"str \" + toString());",
@@ -279,9 +279,9 @@ public final class FormatStringConcatenationCheckTest {
" LOG.info(\"{} \" + toString(), \"arg\");",
"",
" // BUG: Diagnostic contains:",
" LOG.info((Marker) null,\"str \" + toString());",
" LOG.info((Marker) null, \"str \" + toString());",
" // BUG: Diagnostic contains:",
" LOG.info((Marker) null,\"{} \" + toString(), \"arg\");",
" LOG.info((Marker) null, \"{} \" + toString(), \"arg\");",
"",
" // BUG: Diagnostic contains:",
" LOG.trace(\"str \" + toString());",
@@ -289,9 +289,9 @@ public final class FormatStringConcatenationCheckTest {
" LOG.trace(\"{} \" + toString(), \"arg\");",
"",
" // BUG: Diagnostic contains:",
" LOG.trace((Marker) null,\"str \" + toString());",
" LOG.trace((Marker) null, \"str \" + toString());",
" // BUG: Diagnostic contains:",
" LOG.trace((Marker) null,\"{} \" + toString(), \"arg\");",
" LOG.trace((Marker) null, \"{} \" + toString(), \"arg\");",
"",
" // BUG: Diagnostic contains:",
" LOG.warn(\"str \" + toString());",
@@ -299,9 +299,9 @@ public final class FormatStringConcatenationCheckTest {
" LOG.warn(\"{} \" + toString(), \"arg\");",
"",
" // BUG: Diagnostic contains:",
" LOG.warn((Marker) null,\"str \" + toString());",
" LOG.warn((Marker) null, \"str \" + toString());",
" // BUG: Diagnostic contains:",
" LOG.warn((Marker) null,\"{} \" + toString(), \"arg\");",
" LOG.warn((Marker) null, \"{} \" + toString(), \"arg\");",
" }",
"}")
.doTest();
@@ -316,7 +316,6 @@ public final class FormatStringConcatenationCheckTest {
"import static org.assertj.core.api.Assertions.assertThat;",
"",
"import java.util.Locale;",
"import java.util.Formatter;",
"import org.slf4j.Logger;",
"import org.slf4j.LoggerFactory;",
"import org.slf4j.Marker;",
@@ -366,7 +365,6 @@ public final class FormatStringConcatenationCheckTest {
"import static org.assertj.core.api.Assertions.assertThat;",
"",
"import java.util.Locale;",
"import java.util.Formatter;",
"import org.slf4j.Logger;",
"import org.slf4j.LoggerFactory;",
"import org.slf4j.Marker;",
@@ -400,6 +398,7 @@ public final class FormatStringConcatenationCheckTest {
" String.format(\"{} \" + toString(), \"arg\");",
" String.format(Locale.ROOT, \"{} \" + toString(), \"arg\");",
" }",
"",
" void slf4j() {",
" LOG.debug(\"str {}\", toString());",
" LOG.debug((Marker) null, \"str {}\", toString());",

View File

@@ -0,0 +1,302 @@
package tech.picnic.errorprone.bugpatterns;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.FixChoosers;
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
final class IdentityConversionCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(IdentityConversionCheck.class, getClass());
private final BugCheckerRefactoringTestHelper refactoringTestHelper =
BugCheckerRefactoringTestHelper.newInstance(IdentityConversionCheck.class, getClass());
@Test
void identification() {
compilationTestHelper
.addSourceLines(
"Foo.java",
"import com.google.common.collect.ImmutableBiMap;",
"import com.google.common.collect.ImmutableList;",
"import com.google.common.collect.ImmutableListMultimap;",
"import com.google.common.collect.ImmutableMap;",
"import com.google.common.collect.ImmutableMultimap;",
"import com.google.common.collect.ImmutableMultiset;",
"import com.google.common.collect.ImmutableRangeMap;",
"import com.google.common.collect.ImmutableRangeSet;",
"import com.google.common.collect.ImmutableSet;",
"import com.google.common.collect.ImmutableSetMultimap;",
"import com.google.common.collect.ImmutableTable;",
"import reactor.adapter.rxjava.RxJava2Adapter;",
"import reactor.core.publisher.Flux;",
"import reactor.core.publisher.Mono;",
"",
"public final class Foo {",
" public void foo() {",
" // BUG: Diagnostic contains:",
" Boolean b1 = Boolean.valueOf(Boolean.FALSE);",
" // BUG: Diagnostic contains:",
" Boolean b2 = Boolean.valueOf(false);",
" // BUG: Diagnostic contains:",
" boolean b3 = Boolean.valueOf(Boolean.FALSE);",
" // BUG: Diagnostic contains:",
" boolean b4 = Boolean.valueOf(false);",
"",
" // BUG: Diagnostic contains:",
" Byte byte1 = Byte.valueOf((Byte) Byte.MIN_VALUE);",
" // BUG: Diagnostic contains:",
" Byte byte2 = Byte.valueOf(Byte.MIN_VALUE);",
" // BUG: Diagnostic contains:",
" byte byte3 = Byte.valueOf((Byte) Byte.MIN_VALUE);",
" // BUG: Diagnostic contains:",
" byte byte4 = Byte.valueOf(Byte.MIN_VALUE);",
"",
" // BUG: Diagnostic contains:",
" Character c1 = Character.valueOf((Character) 'a');",
" // BUG: Diagnostic contains:",
" Character c2 = Character.valueOf('a');",
" // BUG: Diagnostic contains:",
" char c3 = Character.valueOf((Character) 'a');",
" // BUG: Diagnostic contains:",
" char c4 = Character.valueOf('a');",
"",
" // BUG: Diagnostic contains:",
" Double d1 = Double.valueOf((Double) 0.0);",
" // BUG: Diagnostic contains:",
" Double d2 = Double.valueOf(0.0);",
" // BUG: Diagnostic contains:",
" double d3 = Double.valueOf((Double) 0.0);",
" // BUG: Diagnostic contains:",
" double d4 = Double.valueOf(0.0);",
"",
" // BUG: Diagnostic contains:",
" Float f1 = Float.valueOf((Float) 0.0F);",
" // BUG: Diagnostic contains:",
" Float f2 = Float.valueOf(0.0F);",
" // BUG: Diagnostic contains:",
" float f3 = Float.valueOf((Float) 0.0F);",
" // BUG: Diagnostic contains:",
" float f4 = Float.valueOf(0.0F);",
"",
" // BUG: Diagnostic contains:",
" Integer i1 = Integer.valueOf((Integer) 1);",
" // BUG: Diagnostic contains:",
" Integer i2 = Integer.valueOf(1);",
" // BUG: Diagnostic contains:",
" int i3 = Integer.valueOf((Integer) 1);",
" // BUG: Diagnostic contains:",
" int i4 = Integer.valueOf(1);",
"",
" // BUG: Diagnostic contains:",
" Long l1 = Long.valueOf((Long) 1L);",
" // BUG: Diagnostic contains:",
" Long l2 = Long.valueOf(1L);",
" // BUG: Diagnostic contains:",
" long l3 = Long.valueOf((Long) 1L);",
" // BUG: Diagnostic contains:",
" long l4 = Long.valueOf(1L);",
"",
" Long l5 = Long.valueOf((Integer) 1);",
" Long l6 = Long.valueOf(1);",
" // BUG: Diagnostic contains:",
" long l7 = Long.valueOf((Integer) 1);",
" // BUG: Diagnostic contains:",
" long l8 = Long.valueOf(1);",
"",
" // BUG: Diagnostic contains:",
" Short s1 = Short.valueOf((Short) Short.MIN_VALUE);",
" // BUG: Diagnostic contains:",
" Short s2 = Short.valueOf(Short.MIN_VALUE);",
" // BUG: Diagnostic contains:",
" short s3 = Short.valueOf((Short) Short.MIN_VALUE);",
" // BUG: Diagnostic contains:",
" short s4 = Short.valueOf(Short.MIN_VALUE);",
"",
" String str1 = String.valueOf(0);",
" // BUG: Diagnostic contains:",
" String str2 = String.valueOf(\"1\");",
"",
" // BUG: Diagnostic contains:",
" ImmutableBiMap<Object, Object> o1 = ImmutableBiMap.copyOf(ImmutableBiMap.of());",
" // BUG: Diagnostic contains:",
" ImmutableList<Object> o2 = ImmutableList.copyOf(ImmutableList.of());",
" ImmutableListMultimap<Object, Object> o3 =",
" // BUG: Diagnostic contains:",
" ImmutableListMultimap.copyOf(ImmutableListMultimap.of());",
" // BUG: Diagnostic contains:",
" ImmutableMap<Object, Object> o4 = ImmutableMap.copyOf(ImmutableMap.of());",
" // BUG: Diagnostic contains:",
" ImmutableMultimap<Object, Object> o5 = ImmutableMultimap.copyOf(ImmutableMultimap.of());",
" // BUG: Diagnostic contains:",
" ImmutableMultiset<Object> o6 = ImmutableMultiset.copyOf(ImmutableMultiset.of());",
" // BUG: Diagnostic contains:",
" ImmutableRangeMap<String, Object> o7 = ImmutableRangeMap.copyOf(ImmutableRangeMap.of());",
" // BUG: Diagnostic contains:",
" ImmutableRangeSet<String> o8 = ImmutableRangeSet.copyOf(ImmutableRangeSet.of());",
" // BUG: Diagnostic contains:",
" ImmutableSet<Object> o9 = ImmutableSet.copyOf(ImmutableSet.of());",
" ImmutableSetMultimap<Object, Object> o10 =",
" // BUG: Diagnostic contains:",
" ImmutableSetMultimap.copyOf(ImmutableSetMultimap.of());",
" // BUG: Diagnostic contains:",
" ImmutableTable<Object, Object, Object> o11 = ImmutableTable.copyOf(ImmutableTable.of());",
"",
" // BUG: Diagnostic contains:",
" Flux<Integer> flux1 = Flux.just(1).flatMap(e -> RxJava2Adapter.fluxToFlowable(Flux.just(2)));",
" // BUG: Diagnostic contains:",
" Flux<Integer> flux2 = Flux.concat(Flux.just(1));",
" // BUG: Diagnostic contains:",
" Flux<Integer> flux3 = Flux.firstWithSignal(Flux.just(1));",
" // BUG: Diagnostic contains:",
" Flux<Integer> flux4 = Flux.from(Flux.just(1));",
" // BUG: Diagnostic contains:",
" Flux<Integer> flux5 = Flux.merge(Flux.just(1));",
"",
" // BUG: Diagnostic contains:",
" Mono<Integer> m1 = Mono.from(Mono.just(1));",
" // BUG: Diagnostic contains:",
" Mono<Integer> m2 = Mono.fromDirect(Mono.just(1));",
" }",
"}")
.doTest();
}
@Test
void replacementFirstSuggestedFix() {
refactoringTestHelper
.setFixChooser(FixChoosers.FIRST)
.addInputLines(
"Foo.java",
"import static org.mockito.Mockito.when;",
"",
"import com.google.common.collect.ImmutableCollection;",
"import com.google.common.collect.ImmutableList;",
"import com.google.common.collect.ImmutableSet;",
"import java.util.ArrayList;",
"import java.util.Collection;",
"import org.reactivestreams.Publisher;",
"import reactor.adapter.rxjava.RxJava2Adapter;",
"import reactor.core.publisher.Flux;",
"import reactor.core.publisher.Mono;",
"",
"public final class Foo {",
" public void foo() {",
" ImmutableSet<Object> set1 = ImmutableSet.copyOf(ImmutableSet.of());",
" ImmutableSet<Object> set2 = ImmutableSet.copyOf(ImmutableList.of());",
"",
" ImmutableCollection<Integer> list1 = ImmutableList.copyOf(ImmutableList.of(1));",
" ImmutableCollection<Integer> list2 = ImmutableList.copyOf(new ArrayList<>(ImmutableList.of(1)));",
"",
" Collection<Integer> c1 = ImmutableSet.copyOf(ImmutableSet.of(1));",
" Collection<Integer> c2 = ImmutableList.copyOf(new ArrayList<>(ImmutableList.of(1)));",
"",
" Flux<Integer> f1 = Flux.just(1).flatMap(e -> RxJava2Adapter.fluxToFlowable(Flux.just(2)));",
" Flux<Integer> f2 = Flux.concat(Flux.just(3));",
" Publisher<Integer> f3 = Flux.firstWithSignal(Flux.just(4));",
" Publisher<Integer> f4 = Flux.from(Flux.just(5));",
" Publisher<Integer> f5 = Flux.merge(Flux.just(6));",
"",
" Mono<Integer> m1 = Mono.from(Mono.just(7));",
" Publisher<Integer> m2 = Mono.fromDirect(Mono.just(8));",
"",
" bar(Flux.concat(Flux.just(9)));",
" bar(Mono.from(Mono.just(10)));",
"",
" Object o1 = ImmutableSet.copyOf(ImmutableList.of());",
" Object o2 = ImmutableSet.copyOf(ImmutableSet.of());",
"",
" when(\"foo\".contains(\"f\")).thenAnswer(inv -> ImmutableSet.copyOf(ImmutableList.of(1)));",
" }",
"",
" void bar(Publisher<Integer> publisher) {}",
"}")
.addOutputLines(
"Foo.java",
"import static org.mockito.Mockito.when;",
"",
"import com.google.common.collect.ImmutableCollection;",
"import com.google.common.collect.ImmutableList;",
"import com.google.common.collect.ImmutableSet;",
"import java.util.ArrayList;",
"import java.util.Collection;",
"import org.reactivestreams.Publisher;",
"import reactor.adapter.rxjava.RxJava2Adapter;",
"import reactor.core.publisher.Flux;",
"import reactor.core.publisher.Mono;",
"",
"public final class Foo {",
" public void foo() {",
" ImmutableSet<Object> set1 = ImmutableSet.of();",
" ImmutableSet<Object> set2 = ImmutableSet.copyOf(ImmutableList.of());",
"",
" ImmutableCollection<Integer> list1 = ImmutableList.of(1);",
" ImmutableCollection<Integer> list2 = ImmutableList.copyOf(new ArrayList<>(ImmutableList.of(1)));",
"",
" Collection<Integer> c1 = ImmutableSet.of(1);",
" Collection<Integer> c2 = ImmutableList.copyOf(new ArrayList<>(ImmutableList.of(1)));",
"",
" Flux<Integer> f1 = Flux.just(1).flatMap(e -> Flux.just(2));",
" Flux<Integer> f2 = Flux.just(3);",
" Publisher<Integer> f3 = Flux.just(4);",
" Publisher<Integer> f4 = Flux.just(5);",
" Publisher<Integer> f5 = Flux.just(6);",
"",
" Mono<Integer> m1 = Mono.just(7);",
" Publisher<Integer> m2 = Mono.just(8);",
"",
" bar(Flux.just(9));",
" bar(Mono.just(10));",
"",
" Object o1 = ImmutableSet.copyOf(ImmutableList.of());",
" Object o2 = ImmutableSet.of();",
"",
" when(\"foo\".contains(\"f\")).thenAnswer(inv -> ImmutableSet.copyOf(ImmutableList.of(1)));",
" }",
"",
" void bar(Publisher<Integer> publisher) {}",
"}")
.doTest(TestMode.TEXT_MATCH);
}
@Test
void replacementSecondSuggestedFix() {
refactoringTestHelper
.setFixChooser(FixChoosers.SECOND)
.addInputLines(
"Foo.java",
"import com.google.common.collect.ImmutableCollection;",
"import com.google.common.collect.ImmutableList;",
"import com.google.common.collect.ImmutableSet;",
"import java.util.ArrayList;",
"",
"public final class Foo {",
" public void foo() {",
" ImmutableSet<Object> set1 = ImmutableSet.copyOf(ImmutableSet.of());",
" ImmutableSet<Object> set2 = ImmutableSet.copyOf(ImmutableList.of());",
"",
" ImmutableCollection<Integer> list1 = ImmutableList.copyOf(ImmutableList.of(1));",
" ImmutableCollection<Integer> list2 = ImmutableList.copyOf(new ArrayList<>(ImmutableList.of(1)));",
" }",
"}")
.addOutputLines(
"Foo.java",
"import com.google.common.collect.ImmutableCollection;",
"import com.google.common.collect.ImmutableList;",
"import com.google.common.collect.ImmutableSet;",
"import java.util.ArrayList;",
"",
"public final class Foo {",
" public void foo() {",
" @SuppressWarnings(\"IdentityConversion\")",
" ImmutableSet<Object> set1 = ImmutableSet.copyOf(ImmutableSet.of());",
" ImmutableSet<Object> set2 = ImmutableSet.copyOf(ImmutableList.of());",
"",
" @SuppressWarnings(\"IdentityConversion\")",
" ImmutableCollection<Integer> list1 = ImmutableList.copyOf(ImmutableList.of(1));",
" ImmutableCollection<Integer> list2 = ImmutableList.copyOf(new ArrayList<>(ImmutableList.of(1)));",
" }",
"}")
.doTest(TestMode.TEXT_MATCH);
}
}

View File

@@ -5,7 +5,7 @@ import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
public final class JUnitMethodDeclarationCheckTest {
final class JUnitMethodDeclarationCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(JUnitMethodDeclarationCheck.class, getClass());
private final BugCheckerRefactoringTestHelper refactoringTestHelper =
@@ -16,6 +16,8 @@ public final class JUnitMethodDeclarationCheckTest {
compilationTestHelper
.addSourceLines(
"A.java",
"import static org.junit.jupiter.params.provider.Arguments.arguments;",
"",
"import org.junit.jupiter.api.AfterAll;",
"import org.junit.jupiter.api.AfterEach;",
"import org.junit.jupiter.api.BeforeAll;",
@@ -24,63 +26,137 @@ public final class JUnitMethodDeclarationCheckTest {
"import org.junit.jupiter.params.ParameterizedTest;",
"",
"class A {",
" @BeforeAll void setUp1() {}",
" // BUG: Diagnostic contains:",
" @BeforeAll public void setUp2() {}",
" // BUG: Diagnostic contains:",
" @BeforeAll protected void setUp3() {}",
" // BUG: Diagnostic contains:",
" @BeforeAll private void setUp4() {}",
" {",
" arguments();",
" }",
"",
" @BeforeEach void setup5() {}",
" // BUG: Diagnostic contains:",
" @BeforeEach public void setUp6() {}",
" // BUG: Diagnostic contains:",
" @BeforeEach protected void setUp7() {}",
" // BUG: Diagnostic contains:",
" @BeforeEach private void setUp8() {}",
" @BeforeAll",
" void setUp1() {}",
"",
" @AfterEach void tearDown1() {}",
" @BeforeAll",
" // BUG: Diagnostic contains:",
" @AfterEach public void tearDown2() {}",
" // BUG: Diagnostic contains:",
" @AfterEach protected void tearDown3() {}",
" // BUG: Diagnostic contains:",
" @AfterEach private void tearDown4() {}",
" public void setUp2() {}",
"",
" @AfterAll void tearDown5() {}",
" @BeforeAll",
" // BUG: Diagnostic contains:",
" @AfterAll public void tearDown6() {}",
" // BUG: Diagnostic contains:",
" @AfterAll protected void tearDown7() {}",
" // BUG: Diagnostic contains:",
" @AfterAll private void tearDown8() {}",
" protected void setUp3() {}",
"",
" @Test void method1() {}",
" @BeforeAll",
" // BUG: Diagnostic contains:",
" @Test void testMethod2() {}",
" // BUG: Diagnostic contains:",
" @Test public void method3() {}",
" // BUG: Diagnostic contains:",
" @Test protected void method4() {}",
" // BUG: Diagnostic contains:",
" @Test private void method5() {}",
" private void setUp4() {}",
"",
" @ParameterizedTest void method6() {}",
" // BUG: Diagnostic contains:",
" @ParameterizedTest void testMethod7() {}",
" // BUG: Diagnostic contains:",
" @ParameterizedTest public void method8() {}",
" // BUG: Diagnostic contains:",
" @ParameterizedTest protected void method9() {}",
" // BUG: Diagnostic contains:",
" @ParameterizedTest private void method10() {}",
" @BeforeEach",
" void setup5() {}",
"",
" @BeforeEach",
" // BUG: Diagnostic contains:",
" public void setUp6() {}",
"",
" @BeforeEach",
" // BUG: Diagnostic contains:",
" protected void setUp7() {}",
"",
" @BeforeEach",
" // BUG: Diagnostic contains:",
" private void setUp8() {}",
"",
" @AfterEach",
" void tearDown1() {}",
"",
" @AfterEach",
" // BUG: Diagnostic contains:",
" public void tearDown2() {}",
"",
" @AfterEach",
" // BUG: Diagnostic contains:",
" protected void tearDown3() {}",
"",
" @AfterEach",
" // BUG: Diagnostic contains:",
" private void tearDown4() {}",
"",
" @AfterAll",
" void tearDown5() {}",
"",
" @AfterAll",
" // BUG: Diagnostic contains:",
" public void tearDown6() {}",
"",
" @AfterAll",
" // BUG: Diagnostic contains:",
" protected void tearDown7() {}",
"",
" @AfterAll",
" // BUG: Diagnostic contains:",
" private void tearDown8() {}",
"",
" @Test",
" void method1() {}",
"",
" @Test",
" // BUG: Diagnostic contains:",
" void testMethod2() {}",
"",
" @Test",
" // BUG: Diagnostic contains:",
" public void method3() {}",
"",
" @Test",
" // BUG: Diagnostic contains:",
" protected void method4() {}",
"",
" @Test",
" // BUG: Diagnostic contains:",
" private void method5() {}",
"",
" @ParameterizedTest",
" void method6() {}",
"",
" @ParameterizedTest",
" // BUG: Diagnostic contains:",
" void testMethod7() {}",
"",
" @ParameterizedTest",
" // BUG: Diagnostic contains:",
" public void method8() {}",
"",
" @ParameterizedTest",
" // BUG: Diagnostic contains:",
" protected void method9() {}",
"",
" @ParameterizedTest",
" // BUG: Diagnostic contains:",
" private void method10() {}",
"",
" @BeforeEach",
" @BeforeAll",
" @AfterEach",
" @AfterAll",
" void testNonTestMethod1() {}",
"",
" @BeforeEach @BeforeAll @AfterEach @AfterAll void testNonTestMethod1() {}",
" public void testNonTestMethod2() {}",
"",
" protected void testNonTestMethod3() {}",
"",
" private void testNonTestMethod4() {}",
" @Test void test5() {}",
"",
" @Test",
" void test5() {}",
"",
" @Test",
" // BUG: Diagnostic contains: (but note that a method named `overload` already exists in this",
" // class)",
" void testOverload() {}",
"",
" void overload() {}",
"",
" @Test",
" // BUG: Diagnostic contains: (but note that `arguments` is already statically imported)",
" void testArguments() {}",
"",
" @Test",
" // BUG: Diagnostic contains: (but note that `public` is a reserved keyword)",
" void testPublic() {}",
"}")
.addSourceLines(
"B.java",
@@ -92,36 +168,138 @@ public final class JUnitMethodDeclarationCheckTest {
"import org.junit.jupiter.params.ParameterizedTest;",
"",
"class B extends A {",
" @Override @BeforeAll void setUp1() {}",
" @Override @BeforeAll public void setUp2() {}",
" @Override @BeforeAll protected void setUp3() {}",
" @Override",
" @BeforeAll",
" void setUp1() {}",
"",
" @Override @BeforeEach void setup5() {}",
" @Override @BeforeEach public void setUp6() {}",
" @Override @BeforeEach protected void setUp7() {}",
" @Override",
" @BeforeAll",
" public void setUp2() {}",
"",
" @Override @AfterEach void tearDown1() {}",
" @Override @AfterEach public void tearDown2() {}",
" @Override @AfterEach protected void tearDown3() {}",
" @Override",
" @BeforeAll",
" protected void setUp3() {}",
"",
" @Override @AfterAll void tearDown5() {}",
" @Override @AfterAll public void tearDown6() {}",
" @Override @AfterAll protected void tearDown7() {}",
" @Override",
" @BeforeEach",
" void setup5() {}",
"",
" @Override @Test void method1() {}",
" @Override @Test void testMethod2() {}",
" @Override @Test public void method3() {}",
" @Override @Test protected void method4() {}",
" @Override",
" @BeforeEach",
" public void setUp6() {}",
"",
" @Override @ParameterizedTest void method6() {}",
" @Override @ParameterizedTest void testMethod7() {}",
" @Override @ParameterizedTest public void method8() {}",
" @Override @ParameterizedTest protected void method9() {}",
" @Override",
" @BeforeEach",
" protected void setUp7() {}",
"",
" @Override @BeforeEach @BeforeAll @AfterEach @AfterAll void testNonTestMethod1() {}",
" @Override public void testNonTestMethod2() {}",
" @Override protected void testNonTestMethod3() {}",
" @Override @Test void test5() {}",
" @Override",
" @AfterEach",
" void tearDown1() {}",
"",
" @Override",
" @AfterEach",
" public void tearDown2() {}",
"",
" @Override",
" @AfterEach",
" protected void tearDown3() {}",
"",
" @Override",
" @AfterAll",
" void tearDown5() {}",
"",
" @Override",
" @AfterAll",
" public void tearDown6() {}",
"",
" @Override",
" @AfterAll",
" protected void tearDown7() {}",
"",
" @Override",
" @Test",
" void method1() {}",
"",
" @Override",
" @Test",
" void testMethod2() {}",
"",
" @Override",
" @Test",
" public void method3() {}",
"",
" @Override",
" @Test",
" protected void method4() {}",
"",
" @Override",
" @ParameterizedTest",
" void method6() {}",
"",
" @Override",
" @ParameterizedTest",
" void testMethod7() {}",
"",
" @Override",
" @ParameterizedTest",
" public void method8() {}",
"",
" @Override",
" @ParameterizedTest",
" protected void method9() {}",
"",
" @Override",
" @BeforeEach",
" @BeforeAll",
" @AfterEach",
" @AfterAll",
" void testNonTestMethod1() {}",
"",
" @Override",
" public void testNonTestMethod2() {}",
"",
" @Override",
" protected void testNonTestMethod3() {}",
"",
" @Override",
" @Test",
" void test5() {}",
"",
" @Override",
" @Test",
" void testOverload() {}",
"",
" @Override",
" void overload() {}",
"",
" @Override",
" @Test",
" void testArguments() {}",
"",
" @Override",
" @Test",
" void testPublic() {}",
"}")
.addSourceLines(
"C.java",
"import org.junit.jupiter.api.AfterAll;",
"import org.junit.jupiter.api.BeforeAll;",
"import org.junit.jupiter.api.Test;",
"",
"abstract class C {",
" @BeforeAll",
" public void setUp() {}",
"",
" @Test",
" void testMethod1() {}",
"",
" @AfterAll",
" // BUG: Diagnostic contains:",
" private void tearDown() {}",
"",
" @Test",
" // BUG: Diagnostic contains:",
" final void testMethod2() {}",
"}")
.doTest();
}
@@ -131,6 +309,8 @@ public final class JUnitMethodDeclarationCheckTest {
refactoringTestHelper
.addInputLines(
"in/A.java",
"import static org.junit.jupiter.params.provider.Arguments.arguments;",
"",
"import org.junit.jupiter.api.AfterAll;",
"import org.junit.jupiter.api.AfterEach;",
"import org.junit.jupiter.api.BeforeAll;",
@@ -140,20 +320,52 @@ public final class JUnitMethodDeclarationCheckTest {
"import org.junit.jupiter.params.ParameterizedTest;",
"",
"class A {",
" @BeforeAll public void setUp1() {}",
" @BeforeEach protected void setUp2() {}",
" @AfterEach private void setUp3() {}",
" @AfterAll private void setUp4() {}",
" {",
" arguments();",
" }",
"",
" @Test void testFoo() {}",
" @ParameterizedTest void testBar() {}",
" @BeforeAll",
" public void setUp1() {}",
"",
" @Test public void baz() {}",
" @RepeatedTest(2) private void qux() {}",
" @ParameterizedTest protected void quux() {}",
" @BeforeEach",
" protected void setUp2() {}",
"",
" @AfterEach",
" private void setUp3() {}",
"",
" @AfterAll",
" private void setUp4() {}",
"",
" @Test",
" void testFoo() {}",
"",
" @ParameterizedTest",
" void testBar() {}",
"",
" @Test",
" public void baz() {}",
"",
" @RepeatedTest(2)",
" private void qux() {}",
"",
" @ParameterizedTest",
" protected void quux() {}",
"",
" @Test",
" public void testOverload() {}",
"",
" void overload() {}",
"",
" @Test",
" protected void testArguments() {}",
"",
" @Test",
" private void testClass() {}",
"}")
.addOutputLines(
"out/A.java",
"import static org.junit.jupiter.params.provider.Arguments.arguments;",
"",
"import org.junit.jupiter.api.AfterAll;",
"import org.junit.jupiter.api.AfterEach;",
"import org.junit.jupiter.api.BeforeAll;",
@@ -163,17 +375,47 @@ public final class JUnitMethodDeclarationCheckTest {
"import org.junit.jupiter.params.ParameterizedTest;",
"",
"class A {",
" @BeforeAll void setUp1() {}",
" @BeforeEach void setUp2() {}",
" @AfterEach void setUp3() {}",
" @AfterAll void setUp4() {}",
" {",
" arguments();",
" }",
"",
" @Test void foo() {}",
" @ParameterizedTest void bar() {}",
" @BeforeAll",
" void setUp1() {}",
"",
" @Test void baz() {}",
" @RepeatedTest(2) void qux() {}",
" @ParameterizedTest void quux() {}",
" @BeforeEach",
" void setUp2() {}",
"",
" @AfterEach",
" void setUp3() {}",
"",
" @AfterAll",
" void setUp4() {}",
"",
" @Test",
" void foo() {}",
"",
" @ParameterizedTest",
" void bar() {}",
"",
" @Test",
" void baz() {}",
"",
" @RepeatedTest(2)",
" void qux() {}",
"",
" @ParameterizedTest",
" void quux() {}",
"",
" @Test",
" void testOverload() {}",
"",
" void overload() {}",
"",
" @Test",
" void testArguments() {}",
"",
" @Test",
" void testClass() {}",
"}")
.doTest(TestMode.TEXT_MATCH);
}

View File

@@ -6,7 +6,7 @@ import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
public final class LexicographicalAnnotationAttributeListingCheckTest {
final class LexicographicalAnnotationAttributeListingCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(
LexicographicalAnnotationAttributeListingCheck.class, getClass());
@@ -26,8 +26,8 @@ public final class LexicographicalAnnotationAttributeListingCheckTest {
compilationTestHelper
.addSourceLines(
"A.java",
"import static java.math.RoundingMode.UP;",
"import static java.math.RoundingMode.DOWN;",
"import static java.math.RoundingMode.UP;",
"",
"import com.fasterxml.jackson.annotation.JsonPropertyOrder;",
"import io.swagger.annotations.ApiImplicitParam;",
@@ -40,9 +40,13 @@ public final class LexicographicalAnnotationAttributeListingCheckTest {
"interface A {",
" @interface Foo {",
" String[] value() default {};",
"",
" int[] ints() default {};",
"",
" Class<?>[] cls() default {};",
"",
" RoundingMode[] enums() default {};",
"",
" Bar[] anns() default {};",
" }",
"",
@@ -50,50 +54,95 @@ public final class LexicographicalAnnotationAttributeListingCheckTest {
" String[] value() default {};",
" }",
"",
" @Foo({}) A noString();",
" @Foo({\"a\"}) A oneString();",
" @Foo({\"a\", \"b\"}) A sortedStrings();",
" // BUG: Diagnostic contains:",
" @Foo({\"b\", \"a\"}) A unsortedString();",
" @Foo({\"ab\", \"Ac\"}) A sortedStringCaseInsensitive();",
" // BUG: Diagnostic contains:",
" @Foo({\"ac\", \"Ab\"}) A unsortedStringCaseInsensitive();",
" @Foo({\"A\", \"a\"}) A sortedStringCaseInsensitiveWithTotalOrderFallback();",
" // BUG: Diagnostic contains:",
" @Foo({\"a\", \"A\"}) A unsortedStringCaseInsensitiveWithTotalOrderFallback();",
" @Foo({})",
" A noString();",
"",
" @Foo(ints = {}) A noInts();",
" @Foo(ints = {0}) A oneInt();",
" @Foo(ints = {0, 1}) A sortedInts();",
" @Foo(ints = {1, 0}) A unsortedInts();",
" @Foo({\"a\"})",
" A oneString();",
"",
" @Foo(cls = {}) A noClasses();",
" @Foo(cls = {int.class}) A oneClass();",
" @Foo(cls = {int.class, long.class}) A sortedClasses();",
" @Foo({\"a\", \"b\"})",
" A sortedStrings();",
" // BUG: Diagnostic contains:",
" @Foo(cls = {long.class, int.class}) A unsortedClasses();",
" @Foo({\"b\", \"a\"})",
" A unsortedString();",
"",
" @Foo(enums = {}) A noEnums();",
" @Foo(enums = {DOWN}) A oneEnum();",
" @Foo(enums = {DOWN, UP}) A sortedEnums();",
" @Foo({\"ab\", \"Ac\"})",
" A sortedStringCaseInsensitive();",
" // BUG: Diagnostic contains:",
" @Foo(enums = {UP, DOWN}) A unsortedEnums();",
" @Foo({\"ac\", \"Ab\"})",
" A unsortedStringCaseInsensitive();",
"",
" @Foo(anns = {}) A noAnns();",
" @Foo(anns = {@Bar(\"a\")}) A oneAnn();",
" @Foo(anns = {@Bar(\"a\"), @Bar(\"b\")}) A sortedAnns();",
" @Foo({\"A\", \"a\"})",
" A sortedStringCaseInsensitiveWithTotalOrderFallback();",
" // BUG: Diagnostic contains:",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")}) A unsortedAnns();",
" // BUG: Diagnostic contains:",
" @Foo(anns = {@Bar(\"a\"), @Bar({\"b\", \"a\"})}) A unsortedInnderAnns();",
" @Foo({\"a\", \"A\"})",
" A unsortedStringCaseInsensitiveWithTotalOrderFallback();",
"",
" @Foo({\"a=foo\", \"a.b=bar\", \"a.c=baz\"}) A hierarchicallySorted();",
" // BUG: Diagnostic contains:",
" @Foo({\"a.b=bar\", \"a.c=baz\", \"a=foo\"}) A hierarchicallyUnsorted();",
" @Foo(ints = {})",
" A noInts();",
"",
" @JsonPropertyOrder({\"field2\", \"field1\"}) A dto();",
" @ApiImplicitParams({@ApiImplicitParam(\"p2\"), @ApiImplicitParam(\"p1\")}) A firstEndpoint();",
" @Parameters({@Parameter(name = \"p2\"), @Parameter(name = \"p1\")}) A secondEndpoint();",
" @Foo(ints = {0})",
" A oneInt();",
"",
" @Foo(ints = {0, 1})",
" A sortedInts();",
"",
" @Foo(ints = {1, 0})",
" A unsortedInts();",
"",
" @Foo(cls = {})",
" A noClasses();",
"",
" @Foo(cls = {int.class})",
" A oneClass();",
"",
" @Foo(cls = {int.class, long.class})",
" A sortedClasses();",
" // BUG: Diagnostic contains:",
" @Foo(cls = {long.class, int.class})",
" A unsortedClasses();",
"",
" @Foo(enums = {})",
" A noEnums();",
"",
" @Foo(enums = {DOWN})",
" A oneEnum();",
"",
" @Foo(enums = {DOWN, UP})",
" A sortedEnums();",
" // BUG: Diagnostic contains:",
" @Foo(enums = {UP, DOWN})",
" A unsortedEnums();",
"",
" @Foo(anns = {})",
" A noAnns();",
"",
" @Foo(anns = {@Bar(\"a\")})",
" A oneAnn();",
"",
" @Foo(anns = {@Bar(\"a\"), @Bar(\"b\")})",
" A sortedAnns();",
" // BUG: Diagnostic contains:",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")})",
" A unsortedAnns();",
" // BUG: Diagnostic contains:",
" @Foo(anns = {@Bar(\"a\"), @Bar({\"b\", \"a\"})})",
" A unsortedInnderAnns();",
"",
" @Foo({\"a=foo\", \"a.b=bar\", \"a.c=baz\"})",
" A hierarchicallySorted();",
" // BUG: Diagnostic contains:",
" @Foo({\"a.b=bar\", \"a.c=baz\", \"a=foo\"})",
" A hierarchicallyUnsorted();",
"",
" @JsonPropertyOrder({\"field2\", \"field1\"})",
" A dto();",
"",
" @ApiImplicitParams({@ApiImplicitParam(\"p2\"), @ApiImplicitParam(\"p1\")})",
" A firstEndpoint();",
"",
" @Parameters({@Parameter(name = \"p2\"), @Parameter(name = \"p1\")})",
" A secondEndpoint();",
"",
" @XmlType(propOrder = {\"field2\", \"field1\"})",
" class Dummy {}",
@@ -109,16 +158,19 @@ public final class LexicographicalAnnotationAttributeListingCheckTest {
refactoringTestHelper
.addInputLines(
"in/A.java",
"import static java.math.RoundingMode.UP;",
"import static java.math.RoundingMode.DOWN;",
"import static java.math.RoundingMode.UP;",
"",
"import java.math.RoundingMode;",
"",
"interface A {",
" @interface Foo {",
" String[] value() default {};",
"",
" Class<?>[] cls() default {};",
"",
" RoundingMode[] enums() default {};",
"",
" Bar[] anns() default {};",
" }",
"",
@@ -126,24 +178,36 @@ public final class LexicographicalAnnotationAttributeListingCheckTest {
" String[] value() default {};",
" }",
"",
" @Foo({\"b\", \"a\"}) A unsortedString();",
" @Foo(cls = {long.class, int.class}) A unsortedClasses();",
" @Foo(enums = {UP, DOWN}) A unsortedEnums();",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")}) A unsortedAnns();",
" @Foo(anns = {@Bar(\"a\"), @Bar({\"b\", \"a\"})}) A unsortedInnderAnns();",
" @Foo({\"b\", \"a\"})",
" A unsortedString();",
"",
" @Foo(cls = {long.class, int.class})",
" A unsortedClasses();",
"",
" @Foo(enums = {UP, DOWN})",
" A unsortedEnums();",
"",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")})",
" A unsortedAnns();",
"",
" @Foo(anns = {@Bar(\"a\"), @Bar({\"b\", \"a\"})})",
" A unsortedInnderAnns();",
"}")
.addOutputLines(
"out/A.java",
"import static java.math.RoundingMode.UP;",
"import static java.math.RoundingMode.DOWN;",
"import static java.math.RoundingMode.UP;",
"",
"import java.math.RoundingMode;",
"",
"interface A {",
" @interface Foo {",
" String[] value() default {};",
"",
" Class<?>[] cls() default {};",
"",
" RoundingMode[] enums() default {};",
"",
" Bar[] anns() default {};",
" }",
"",
@@ -151,11 +215,20 @@ public final class LexicographicalAnnotationAttributeListingCheckTest {
" String[] value() default {};",
" }",
"",
" @Foo({\"a\", \"b\"}) A unsortedString();",
" @Foo(cls = {int.class, long.class}) A unsortedClasses();",
" @Foo(enums = {DOWN, UP}) A unsortedEnums();",
" @Foo(anns = {@Bar(\"a\"), @Bar(\"b\")}) A unsortedAnns();",
" @Foo(anns = {@Bar(\"a\"), @Bar({\"a\", \"b\"})}) A unsortedInnderAnns();",
" @Foo({\"a\", \"b\"})",
" A unsortedString();",
"",
" @Foo(cls = {int.class, long.class})",
" A unsortedClasses();",
"",
" @Foo(enums = {DOWN, UP})",
" A unsortedEnums();",
"",
" @Foo(anns = {@Bar(\"a\"), @Bar(\"b\")})",
" A unsortedAnns();",
"",
" @Foo(anns = {@Bar(\"a\"), @Bar({\"a\", \"b\"})})",
" A unsortedInnderAnns();",
"}")
.doTest(TestMode.TEXT_MATCH);
}
@@ -171,28 +244,40 @@ public final class LexicographicalAnnotationAttributeListingCheckTest {
"interface A {",
" @interface Foo {",
" String[] value() default {};",
"",
" String[] value2() default {};",
" }",
"",
" @interface Bar {",
" String[] value() default {};",
"",
" String[] value2() default {};",
" }",
"",
" @interface Baz {",
" String[] value() default {};",
"",
" String[] value2() default {};",
" }",
"",
" // BUG: Diagnostic contains:",
" @Foo({\"b\", \"a\"}) A fooValue();",
" @Foo({\"b\", \"a\"})",
" A fooValue();",
" // BUG: Diagnostic contains:",
" @Foo(value2 = {\"b\", \"a\"}) A fooValue2();",
" @Bar({\"b\", \"a\"}) A barValue();",
" @Foo(value2 = {\"b\", \"a\"})",
" A fooValue2();",
"",
" @Bar({\"b\", \"a\"})",
" A barValue();",
" // BUG: Diagnostic contains:",
" @Bar(value2 = {\"b\", \"a\"}) A barValue2();",
" @Baz({\"b\", \"a\"}) A bazValue();",
" @Baz(value2 = {\"b\", \"a\"}) A bazValue2();",
" @Bar(value2 = {\"b\", \"a\"})",
" A barValue2();",
"",
" @Baz({\"b\", \"a\"})",
" A bazValue();",
"",
" @Baz(value2 = {\"b\", \"a\"})",
" A bazValue2();",
"}")
.doTest();
}

View File

@@ -7,7 +7,7 @@ import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
public final class LexicographicalAnnotationListingCheckTest {
final class LexicographicalAnnotationListingCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(LexicographicalAnnotationListingCheck.class, getClass())
.expectErrorMessage(
@@ -27,7 +27,9 @@ public final class LexicographicalAnnotationListingCheckTest {
" @Repeatable(Foos.class)",
" @interface Foo {",
" String[] value() default {};",
"",
" int[] ints() default {};",
"",
" Bar[] anns() default {};",
" }",
"",
@@ -40,33 +42,70 @@ public final class LexicographicalAnnotationListingCheckTest {
" }",
"",
" @interface Foos {",
" Foo[] value();",
" Foo[] value();",
" }",
"",
" // BUG: Diagnostic matches: X",
" @Foo @Bar A unsortedSimpleCase();",
" @Foo",
" @Bar",
" A unsortedSimpleCase();",
" // BUG: Diagnostic matches: X",
" @Foo() @Bar() A unsortedWithParens();",
" @Foo() A onlyOneAnnotation();",
" @Bar @Foo() A sortedAnnotationsOneWithParens();",
" @Foo()",
" @Bar()",
" A unsortedWithParens();",
"",
" @Foo()",
" A onlyOneAnnotation();",
"",
" @Bar",
" @Foo()",
" A sortedAnnotationsOneWithParens();",
"",
" // BUG: Diagnostic matches: X",
" @Foo @Baz @Bar A threeUnsortedAnnotationsSameInitialLetter();",
" @Foo",
" @Baz",
" @Bar",
" A threeUnsortedAnnotationsSameInitialLetter();",
" // BUG: Diagnostic matches: X",
" @Bar @Foo() @Baz A firstOrderedWithTwoUnsortedAnnotations();",
" @Bar @Baz @Foo() A threeSortedAnnotations();",
" @Bar",
" @Foo()",
" @Baz",
" A firstOrderedWithTwoUnsortedAnnotations();",
"",
" @Bar",
" @Baz",
" @Foo()",
" A threeSortedAnnotations();",
"",
" // BUG: Diagnostic matches: X",
" @Foo({\"b\"}) @Bar({\"a\"}) A unsortedWithStringAttributes();",
" @Foo({\"b\"})",
" @Bar({\"a\"})",
" A unsortedWithStringAttributes();",
" // BUG: Diagnostic matches: X",
" @Baz(str = {\"a\", \"b\"}) @Foo(ints = {1, 0}) @Bar A unsortedWithAttributes();",
" @Baz(str = {\"a\", \"b\"})",
" @Foo(ints = {1, 0})",
" @Bar",
" A unsortedWithAttributes();",
" // BUG: Diagnostic matches: X",
" @Bar @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")}) @Baz A unsortedWithNestedBar();",
" @Bar @Baz @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")}) A sortedWithNestedBar();",
" @Bar",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")})",
" @Baz",
" A unsortedWithNestedBar();",
"",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")}) @Foo(ints = {1, 2}) @Foo({\"b\"}) A sortedRepeatableAnnotation();",
" @Bar",
" @Baz",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")})",
" A sortedWithNestedBar();",
"",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")})",
" @Foo(ints = {1, 2})",
" @Foo({\"b\"})",
" A sortedRepeatableAnnotation();",
" // BUG: Diagnostic matches: X",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")}) @Bar @Foo(ints = {1, 2}) A unsortedRepeatableAnnotation();",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")})",
" @Bar",
" @Foo(ints = {1, 2})",
" A unsortedRepeatableAnnotation();",
"}")
.doTest();
}
@@ -77,11 +116,14 @@ public final class LexicographicalAnnotationListingCheckTest {
.addInputLines(
"in/A.java",
"import java.lang.annotation.Repeatable;",
"",
"interface A {",
" @Repeatable(Foos.class)",
" @interface Foo {",
" String[] value() default {};",
"",
" int[] ints() default {};",
"",
" Bar[] anns() default {};",
" }",
"",
@@ -94,29 +136,56 @@ public final class LexicographicalAnnotationListingCheckTest {
" }",
"",
" @interface Foos {",
" Foo[] value();",
" Foo[] value();",
" }",
"",
" @Bar A singleAnnotation();",
" @Bar @Foo A sortedAnnotations();",
" @Foo @Bar A unsortedAnnotations();",
" @Foo() @Baz() @Bar A unsortedAnnotationsWithSomeParens();",
" @Bar",
" A singleAnnotation();",
"",
" @Bar @Baz(str = {\"a\", \"b\"}) @Foo() A unsortedAnnotationsOneContainingAttributes();",
" @Baz(str = {\"a\", \"b\"}) @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")}) @Bar({\"b\"}) A unsortedAnnotationsWithAttributes();",
" @Bar",
" @Foo",
" A sortedAnnotations();",
"",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")}) @Foo(ints = {1, 2}) @Foo({\"b\"}) A sortedRepeatableAnnotation();",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")}) @Bar @Foo(ints = {1, 2}) A unsortedRepeatableAnnotation();",
" @Foo",
" @Bar",
" A unsortedAnnotations();",
"",
" @Foo()",
" @Baz()",
" @Bar",
" A unsortedAnnotationsWithSomeParens();",
"",
" @Bar",
" @Baz(str = {\"a\", \"b\"})",
" @Foo()",
" A unsortedAnnotationsOneContainingAttributes();",
"",
" @Baz(str = {\"a\", \"b\"})",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")})",
" @Bar({\"b\"})",
" A unsortedAnnotationsWithAttributes();",
"",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")})",
" @Foo(ints = {1, 2})",
" @Foo({\"b\"})",
" A sortedRepeatableAnnotation();",
"",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")})",
" @Bar",
" @Foo(ints = {1, 2})",
" A unsortedRepeatableAnnotation();",
"}")
.addOutputLines(
"out/A.java",
"import java.lang.annotation.Repeatable;",
"",
"interface A {",
" @Repeatable(Foos.class)",
" @interface Foo {",
" String[] value() default {};",
"",
" int[] ints() default {};",
"",
" Bar[] anns() default {};",
" }",
"",
@@ -129,19 +198,44 @@ public final class LexicographicalAnnotationListingCheckTest {
" }",
"",
" @interface Foos {",
" Foo[] value();",
" Foo[] value();",
" }",
" @Bar A singleAnnotation();",
" @Bar @Foo A sortedAnnotations();",
" @Bar @Foo A unsortedAnnotations();",
" @Bar @Baz() @Foo() A unsortedAnnotationsWithSomeParens();",
"",
" @Bar @Baz(str = {\"a\", \"b\"}) @Foo() A unsortedAnnotationsOneContainingAttributes();",
" @Bar({\"b\"}) @Baz(str = {\"a\", \"b\"}) @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")}) A unsortedAnnotationsWithAttributes();",
" @Bar",
" A singleAnnotation();",
"",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")}) @Foo(ints = {1, 2}) @Foo({\"b\"}) A sortedRepeatableAnnotation();",
" @Bar @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")}) @Foo(ints = {1, 2}) A unsortedRepeatableAnnotation();",
" @Bar",
" @Foo",
" A sortedAnnotations();",
"",
" @Bar",
" @Foo",
" A unsortedAnnotations();",
"",
" @Bar",
" @Baz()",
" @Foo()",
" A unsortedAnnotationsWithSomeParens();",
"",
" @Bar",
" @Baz(str = {\"a\", \"b\"})",
" @Foo()",
" A unsortedAnnotationsOneContainingAttributes();",
"",
" @Bar({\"b\"})",
" @Baz(str = {\"a\", \"b\"})",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")})",
" A unsortedAnnotationsWithAttributes();",
"",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")})",
" @Foo(ints = {1, 2})",
" @Foo({\"b\"})",
" A sortedRepeatableAnnotation();",
"",
" @Bar",
" @Foo(anns = {@Bar(\"b\"), @Bar(\"a\")})",
" @Foo(ints = {1, 2})",
" A unsortedRepeatableAnnotation();",
"}")
.doTest(TestMode.TEXT_MATCH);
}

View File

@@ -1,10 +1,10 @@
package tech.picnic.errorprone.bugpatterns;
import static com.google.errorprone.BugPattern.SeverityLevel.SUGGESTION;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import com.google.common.collect.ImmutableList;
import com.google.errorprone.BugPattern;
import com.google.errorprone.BugPattern.SeverityLevel;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.VisitorState;
import com.google.errorprone.bugpatterns.BugChecker;
@@ -15,11 +15,11 @@ import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import org.junit.jupiter.api.Test;
public final class MethodMatcherFactoryTest {
final class MethodMatcherFactoryTest {
/** A {@link BugChecker} which flags method invocations matched by {@link #TEST_MATCHER}. */
@BugPattern(
name = "MatchedMethodsFlagger",
severity = SeverityLevel.SUGGESTION,
severity = SUGGESTION,
summary = "Flags methods matched by the test matcher.")
public static final class MatchedMethodsFlagger extends BugChecker
implements MethodInvocationTreeMatcher {
@@ -68,13 +68,21 @@ public final class MethodMatcherFactoryTest {
"",
"public class A {",
" public void m1() {}",
"",
" public void m1(String s) {}",
"",
" public void m1(int i, int j) {}",
"",
" public void m2() {}",
"",
" public void m2(String s) {}",
"",
" public void m2(int i, int j) {}",
"",
" public void m3() {}",
"",
" public void m3(String s) {}",
"",
" public void m3(int i, int j) {}",
"}")
.addSourceLines(
@@ -83,13 +91,21 @@ public final class MethodMatcherFactoryTest {
"",
"public class B {",
" public void m1() {}",
"",
" public void m1(String s) {}",
"",
" public void m1(int i, int j) {}",
"",
" public void m2() {}",
"",
" public void m2(String s) {}",
"",
" public void m2(int i, int j) {}",
"",
" public void m3() {}",
"",
" public void m3(String s) {}",
"",
" public void m3(int i, int j) {}",
"}")
.addSourceLines(
@@ -98,13 +114,21 @@ public final class MethodMatcherFactoryTest {
"",
"public class A {",
" public static void m1() {}",
"",
" public static void m1(String s) {}",
"",
" public static void m1(int i, int j) {}",
"",
" public static void m2() {}",
"",
" public static void m2(String s) {}",
"",
" public static void m2(int i, int j) {}",
"",
" public static void m3() {}",
"",
" public static void m3(String s) {}",
"",
" public static void m3(int i, int j) {}",
"}")
.addSourceLines(
@@ -113,13 +137,21 @@ public final class MethodMatcherFactoryTest {
"",
"public class B {",
" public static void m1() {}",
"",
" public static void m1(String s) {}",
"",
" public static void m1(int i, int j) {}",
"",
" public static void m2() {}",
"",
" public static void m2(String s) {}",
"",
" public static void m2(int i, int j) {}",
"",
" public static void m3() {}",
"",
" public static void m3(String s) {}",
"",
" public static void m3(int i, int j) {}",
"}")
.addSourceLines(

View File

@@ -5,7 +5,7 @@ import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
public final class MethodReferenceUsageCheckTest {
final class MethodReferenceUsageCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(MethodReferenceUsageCheck.class, getClass());
private final BugCheckerRefactoringTestHelper refactoringTestHelper =
@@ -17,37 +17,62 @@ public final class MethodReferenceUsageCheckTest {
.addSourceLines(
"A.java",
"import com.google.common.collect.Streams;",
"import java.util.Map;",
"import java.util.HashMap;",
"import java.util.stream.Stream;",
"import java.util.Map;",
"import java.util.function.IntConsumer;",
"import java.util.function.IntFunction;",
"import java.util.stream.Stream;",
"",
"class A {",
" private final Stream<Integer> s = Stream.of(1);",
" private final Map<Integer, Integer> m = new HashMap<>();",
" private final Runnable thrower = () -> { throw new RuntimeException(); };",
" private final Runnable thrower =",
" () -> {",
" throw new RuntimeException();",
" };",
"",
" void unaryExternalStaticFunctionCalls() {",
" s.forEach(String::valueOf);",
" // BUG: Diagnostic contains:",
" s.forEach(v -> String.valueOf(v));",
" // BUG: Diagnostic contains:",
" s.forEach((v) -> { String.valueOf(v); });",
" // BUG: Diagnostic contains:",
" s.forEach((Integer v) -> { { String.valueOf(v); } });",
" s.forEach(v -> { String.valueOf(v); String.valueOf(v); });",
" s.forEach(",
" // BUG: Diagnostic contains:",
" (v) -> {",
" String.valueOf(v);",
" });",
" s.forEach(",
" // BUG: Diagnostic contains:",
" (Integer v) -> {",
" {",
" String.valueOf(v);",
" }",
" });",
" s.forEach(",
" v -> {",
" String.valueOf(v);",
" String.valueOf(v);",
" });",
"",
" s.map(String::valueOf);",
" // BUG: Diagnostic contains:",
" s.map(v -> String.valueOf(v));",
" // BUG: Diagnostic contains:",
" s.map((v) -> (String.valueOf(v)));",
" // BUG: Diagnostic contains:",
" s.map((Integer v) -> { return String.valueOf(v); });",
" // BUG: Diagnostic contains:",
" s.map((final Integer v) -> { return (String.valueOf(v)); });",
" s.map(v -> { String.valueOf(v); return String.valueOf(v); });",
" s.map(",
" // BUG: Diagnostic contains:",
" (Integer v) -> {",
" return String.valueOf(v);",
" });",
" s.map(",
" // BUG: Diagnostic contains:",
" (final Integer v) -> {",
" return (String.valueOf(v));",
" });",
" s.map(",
" v -> {",
" String.valueOf(v);",
" return String.valueOf(v);",
" });",
"",
" s.findFirst().orElseGet(() -> Integer.valueOf(\"0\"));",
" m.forEach((k, v) -> String.valueOf(v));",
@@ -59,14 +84,34 @@ public final class MethodReferenceUsageCheckTest {
" // BUG: Diagnostic contains:",
" m.forEach((k, v) -> m.put(k, v));",
" m.forEach((k, v) -> m.put(v, k));",
" // BUG: Diagnostic contains:",
" m.forEach((Integer k, Integer v) -> { m.put(k, v); });",
" m.forEach((k, v) -> { m.put(k, k); });",
" // BUG: Diagnostic contains:",
" m.forEach((final Integer k, final Integer v) -> { { m.put(k, v); } });",
" m.forEach((k, v) -> { { m.put(v, v); } });",
" m.forEach(",
" // BUG: Diagnostic contains:",
" (Integer k, Integer v) -> {",
" m.put(k, v);",
" });",
" m.forEach(",
" (k, v) -> {",
" m.put(k, k);",
" });",
" m.forEach(",
" // BUG: Diagnostic contains:",
" (final Integer k, final Integer v) -> {",
" {",
" m.put(k, v);",
" }",
" });",
" m.forEach(",
" (k, v) -> {",
" {",
" m.put(v, v);",
" }",
" });",
" m.forEach((k, v) -> new HashMap<Integer, Integer>().put(k, v));",
" m.forEach((k, v) -> { m.put(k, v); m.put(k, v); });",
" m.forEach(",
" (k, v) -> {",
" m.put(k, v);",
" m.put(k, v);",
" });",
"",
" Streams.zip(s, s, m::put);",
" // BUG: Diagnostic contains:",
@@ -75,20 +120,45 @@ public final class MethodReferenceUsageCheckTest {
" // BUG: Diagnostic contains:",
" Streams.zip(s, s, (Integer a, Integer b) -> (m.put(a, b)));",
" Streams.zip(s, s, (a, b) -> (m.put(a, a)));",
" // BUG: Diagnostic contains:",
" Streams.zip(s, s, (final Integer a, final Integer b) -> { return m.put(a, b); });",
" Streams.zip(s, s, (a, b) -> { return m.put(b, b); });",
" // BUG: Diagnostic contains:",
" Streams.zip(s, s, (a, b) -> { return (m.put(a, b)); });",
" Streams.zip(s, s, (a, b) -> { return (m.put(b, a)); });",
" Streams.zip(s, s, (a, b) -> { m.put(a, b); return m.put(a, b); });",
" Streams.zip(",
" s,",
" s,",
" // BUG: Diagnostic contains:",
" (final Integer a, final Integer b) -> {",
" return m.put(a, b);",
" });",
" Streams.zip(",
" s,",
" s,",
" (a, b) -> {",
" return m.put(b, b);",
" });",
" Streams.zip(",
" s,",
" s,",
" // BUG: Diagnostic contains:",
" (a, b) -> {",
" return (m.put(a, b));",
" });",
" Streams.zip(",
" s,",
" s,",
" (a, b) -> {",
" return (m.put(b, a));",
" });",
" Streams.zip(",
" s,",
" s,",
" (a, b) -> {",
" m.put(a, b);",
" return m.put(a, b);",
" });",
" }",
"",
" void nullaryExternalInstanceFunctionCalls() {",
" s.map(Integer::doubleValue);",
" // BUG: Diagnostic contains:",
" s.map(i -> i.doubleValue());",
// `s.map(Integer::toString)` is ambiguous
" s.map(i -> i.toString());",
" s.map(i -> s.toString());",
"",
@@ -108,24 +178,36 @@ public final class MethodReferenceUsageCheckTest {
" s.forEach(this::ivoid1);",
" // BUG: Diagnostic contains:",
" s.forEach(v -> ivoid1(v));",
" // BUG: Diagnostic contains:",
" s.forEach(v -> { ivoid1(v); });",
" s.forEach(",
" // BUG: Diagnostic contains:",
" v -> {",
" ivoid1(v);",
" });",
" s.forEach(this::iint1);",
" // BUG: Diagnostic contains:",
" s.forEach(v -> iint1(v));",
" // BUG: Diagnostic contains:",
" s.forEach(v -> { iint1(v); });",
" s.forEach(",
" // BUG: Diagnostic contains:",
" v -> {",
" iint1(v);",
" });",
"",
" s.forEach(A::svoid1);",
" // BUG: Diagnostic contains:",
" s.forEach(v -> svoid1(v));",
" // BUG: Diagnostic contains:",
" s.forEach(v -> { svoid1(v); });",
" s.forEach(",
" // BUG: Diagnostic contains:",
" v -> {",
" svoid1(v);",
" });",
" s.forEach(A::sint1);",
" // BUG: Diagnostic contains:",
" s.forEach(v -> sint1(v));",
" // BUG: Diagnostic contains:",
" s.forEach(v -> { sint1(v); });",
" s.forEach(",
" // BUG: Diagnostic contains:",
" v -> {",
" sint1(v);",
" });",
"",
" s.forEach(v -> ivoid2(v, v));",
" s.forEach(v -> iint2(v, v));",
@@ -140,28 +222,43 @@ public final class MethodReferenceUsageCheckTest {
" m.forEach(this::ivoid2);",
" // BUG: Diagnostic contains:",
" m.forEach((k, v) -> ivoid2(k, v));",
" // BUG: Diagnostic contains:",
" m.forEach((k, v) -> { ivoid2(k, v); });",
" m.forEach(",
" // BUG: Diagnostic contains:",
" (k, v) -> {",
" ivoid2(k, v);",
" });",
" m.forEach(this::iint2);",
" // BUG: Diagnostic contains:",
" m.forEach((k, v) -> iint2(k, v));",
" // BUG: Diagnostic contains:",
" m.forEach((k, v) -> { iint2(k, v); });",
" m.forEach(",
" // BUG: Diagnostic contains:",
" (k, v) -> {",
" iint2(k, v);",
" });",
"",
" m.forEach(A::svoid2);",
" // BUG: Diagnostic contains:",
" m.forEach((k, v) -> svoid2(k, v));",
" // BUG: Diagnostic contains:",
" m.forEach((k, v) -> { svoid2(k, v); });",
" m.forEach(",
" // BUG: Diagnostic contains:",
" (k, v) -> {",
" svoid2(k, v);",
" });",
" m.forEach(A::sint2);",
" // BUG: Diagnostic contains:",
" m.forEach((k, v) -> sint2(k, v));",
" // BUG: Diagnostic contains:",
" m.forEach((k, v) -> { sint2(k, v); });",
" m.forEach(",
" // BUG: Diagnostic contains:",
" (k, v) -> {",
" sint2(k, v);",
" });",
" }",
"",
" void functionCallsWhoseReplacementWouldBeAmbiguous() {",
" receiver(i -> { Integer.toString(i); });",
" receiver(",
" i -> {",
" Integer.toString(i);",
" });",
" }",
"",
" void assortedOtherEdgeCases() {",
@@ -175,23 +272,47 @@ public final class MethodReferenceUsageCheckTest {
" TernaryOp o7 = (a, b, c) -> b.concat(c);",
" }",
"",
" void receiver(IntFunction<?> op) { }",
" void receiver(IntConsumer op) { }",
" void receiver(IntFunction<?> op) {}",
"",
" void ivoid0() { }",
" void ivoid1(int a) { }",
" void ivoid2(int a, int b) { }",
" int iint0() { return 0; }",
" int iint1(int a) { return 0; }",
" int iint2(int a, int b) { return 0; }",
" void receiver(IntConsumer op) {}",
"",
" static void svoid0() { }",
" static void svoid1(int a) { }",
" static void svoid2(int a, int b) { }",
" static void svoid3(int a, int b, int c) { }",
" static int sint0() { return 0; }",
" static int sint1(int a) { return 0; }",
" static int sint2(int a, int b) { return 0; }",
" void ivoid0() {}",
"",
" void ivoid1(int a) {}",
"",
" void ivoid2(int a, int b) {}",
"",
" int iint0() {",
" return 0;",
" }",
"",
" int iint1(int a) {",
" return 0;",
" }",
"",
" int iint2(int a, int b) {",
" return 0;",
" }",
"",
" static void svoid0() {}",
"",
" static void svoid1(int a) {}",
"",
" static void svoid2(int a, int b) {}",
"",
" static void svoid3(int a, int b, int c) {}",
"",
" static int sint0() {",
" return 0;",
" }",
"",
" static int sint1(int a) {",
" return 0;",
" }",
"",
" static int sint2(int a, int b) {",
" return 0;",
" }",
"",
" interface TernaryOp {",
" String collect(String a, String b, String c);",
@@ -210,9 +331,6 @@ public final class MethodReferenceUsageCheckTest {
"import java.util.Collections;",
"import java.util.List;",
"import java.util.Map;",
// Don't import `java.util.Set`; it should be added.
"import java.util.function.IntConsumer;",
"import java.util.function.IntFunction;",
"import java.util.function.IntSupplier;",
"import java.util.function.Supplier;",
"import java.util.stream.Stream;",
@@ -245,12 +363,19 @@ public final class MethodReferenceUsageCheckTest {
" Stream.of((Map<?, ?>) null).map(Map::keySet).map(s -> s.size());",
" }",
"",
" @Override int iint0() { return 0; }",
" @Override",
" int iint0() {",
" return 0;",
" }",
" }",
"",
" int iint0() { return 0; }",
" int iint0() {",
" return 0;",
" }",
"",
" static int sint0() { return 0; }",
" static int sint0() {",
" return 0;",
" }",
"}")
.addOutputLines(
"out/A.java",
@@ -260,8 +385,6 @@ public final class MethodReferenceUsageCheckTest {
"import java.util.List;",
"import java.util.Map;",
"import java.util.Set;",
"import java.util.function.IntConsumer;",
"import java.util.function.IntFunction;",
"import java.util.function.IntSupplier;",
"import java.util.function.Supplier;",
"import java.util.stream.Stream;",
@@ -294,12 +417,19 @@ public final class MethodReferenceUsageCheckTest {
" Stream.of((Map<?, ?>) null).map(Map::keySet).map(Set::size);",
" }",
"",
" @Override int iint0() { return 0; }",
" @Override",
" int iint0() {",
" return 0;",
" }",
" }",
"",
" int iint0() { return 0; }",
" int iint0() {",
" return 0;",
" }",
"",
" static int sint0() { return 0; }",
" static int sint0() {",
" return 0;",
" }",
"}")
.doTest(TestMode.TEXT_MATCH);
}

View File

@@ -5,7 +5,7 @@ import static com.google.common.base.Predicates.containsPattern;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
public final class MissingRefasterAnnotationCheckTest {
final class MissingRefasterAnnotationCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(MissingRefasterAnnotationCheck.class, getClass())
.expectErrorMessage(

View File

@@ -5,7 +5,7 @@ import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
public final class MockitoStubbingCheckTest {
final class MockitoStubbingCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(MockitoStubbingCheck.class, getClass());
private final BugCheckerRefactoringTestHelper refactoringTestHelper =
@@ -42,8 +42,10 @@ public final class MockitoStubbingCheckTest {
" doAnswer(inv -> null).when(biConsumer).accept(0, \"foo\");",
" doAnswer(inv -> null).when(biConsumer).accept(eq(0), notNull());",
" doAnswer(inv -> null).when(biConsumer).accept(notNull(), eq(\"foo\"));",
" // BUG: Diagnostic contains:",
" doAnswer(inv -> null).when(biConsumer).accept(ArgumentMatchers.eq(0), ArgumentMatchers.eq(\"foo\"));",
" doAnswer(inv -> null)",
" .when(biConsumer)",
" // BUG: Diagnostic contains:",
" .accept(ArgumentMatchers.eq(0), ArgumentMatchers.eq(\"foo\"));",
" // BUG: Diagnostic contains:",
" doAnswer(inv -> null).when(biConsumer).accept(eq(hashCode()), eq(toString()));",
" }",
@@ -71,7 +73,9 @@ public final class MockitoStubbingCheckTest {
" doAnswer(inv -> null).when(consumer).accept(eq(toString()));",
"",
" BiConsumer<Integer, String> biConsumer = mock(BiConsumer.class);",
" doAnswer(inv -> null).when(biConsumer).accept(ArgumentMatchers.eq(0), ArgumentMatchers.eq(\"foo\"));",
" doAnswer(inv -> null)",
" .when(biConsumer)",
" .accept(ArgumentMatchers.eq(0), ArgumentMatchers.eq(\"foo\"));",
" doAnswer(inv -> null).when(biConsumer).accept(eq(hashCode()), eq(toString()));",
" }",
"}")

View File

@@ -5,7 +5,7 @@ import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
public final class PrimitiveComparisonCheckTest {
final class PrimitiveComparisonCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(PrimitiveComparisonCheck.class, getClass());
private final BugCheckerRefactoringTestHelper refactoringTestHelper =
@@ -99,10 +99,21 @@ public final class PrimitiveComparisonCheckTest {
" cmp().thenComparingDouble(o -> Byte.valueOf((byte) 0));",
" }",
"",
" private Comparator<Object> cmp() { return null; }",
" private byte toPrimitive(Object o) { return 0; }",
" private Byte toBoxed(Object o) { return 0; }",
" private Function<Object, Byte> toBoxed() { return o -> 0; }",
" private Comparator<Object> cmp() {",
" return null;",
" }",
"",
" private byte toPrimitive(Object o) {",
" return 0;",
" }",
"",
" private Byte toBoxed(Object o) {",
" return 0;",
" }",
"",
" private Function<Object, Byte> toBoxed() {",
" return o -> 0;",
" }",
"}")
.doTest();
}
@@ -191,11 +202,25 @@ public final class PrimitiveComparisonCheckTest {
" cmp().thenComparingDouble(o -> Integer.valueOf(0));",
" }",
"",
" private Comparator<Object> cmp() { return null; }",
" private int toPrimitive(Object o) { return 0; }",
" private Integer toBoxed(Object o) { return 0; }",
" private Function<Object, Integer> toBoxed() { return o -> 0; }",
" private ToIntFunction<Object> toPrimitive() { return o -> 0; }",
" private Comparator<Object> cmp() {",
" return null;",
" }",
"",
" private int toPrimitive(Object o) {",
" return 0;",
" }",
"",
" private Integer toBoxed(Object o) {",
" return 0;",
" }",
"",
" private Function<Object, Integer> toBoxed() {",
" return o -> 0;",
" }",
"",
" private ToIntFunction<Object> toPrimitive() {",
" return o -> 0;",
" }",
"}")
.doTest();
}
@@ -268,11 +293,25 @@ public final class PrimitiveComparisonCheckTest {
" cmp().thenComparingDouble(o -> Long.valueOf(0));",
" }",
"",
" private Comparator<Object> cmp() { return null; }",
" private long toPrimitive(Object o) { return 0L; }",
" private Long toBoxed(Object o) { return 0L; }",
" private Function<Object, Long> toBoxed() { return o -> 0L; }",
" private ToLongFunction<Object> toPrimitive() { return o -> 0L; }",
" private Comparator<Object> cmp() {",
" return null;",
" }",
"",
" private long toPrimitive(Object o) {",
" return 0L;",
" }",
"",
" private Long toBoxed(Object o) {",
" return 0L;",
" }",
"",
" private Function<Object, Long> toBoxed() {",
" return o -> 0L;",
" }",
"",
" private ToLongFunction<Object> toPrimitive() {",
" return o -> 0L;",
" }",
"}")
.doTest();
}
@@ -326,10 +365,21 @@ public final class PrimitiveComparisonCheckTest {
" cmp().thenComparingDouble(o -> Float.valueOf(0));",
" }",
"",
" private Comparator<Object> cmp() { return null; }",
" private float toPrimitive(Object o) { return 0.0f; }",
" private Float toBoxed(Object o) { return 0.0f; }",
" private Function<Object, Float> toBoxed() { return o -> 0.0f; }",
" private Comparator<Object> cmp() {",
" return null;",
" }",
"",
" private float toPrimitive(Object o) {",
" return 0.0f;",
" }",
"",
" private Float toBoxed(Object o) {",
" return 0.0f;",
" }",
"",
" private Function<Object, Float> toBoxed() {",
" return o -> 0.0f;",
" }",
"}")
.doTest();
}
@@ -386,11 +436,25 @@ public final class PrimitiveComparisonCheckTest {
" cmp().thenComparingDouble(o -> Double.valueOf(0));",
" }",
"",
" private Comparator<Object> cmp() { return null; }",
" private double toPrimitive(Object o) { return 0.0; }",
" private Double toBoxed(Object o) { return 0.0; }",
" private Function<Object, Double> toBoxed() { return o -> 0.0; }",
" private ToDoubleFunction<Object> toPrimitive() { return o -> 0.0; }",
" private Comparator<Object> cmp() {",
" return null;",
" }",
"",
" private double toPrimitive(Object o) {",
" return 0.0;",
" }",
"",
" private Double toBoxed(Object o) {",
" return 0.0;",
" }",
"",
" private Function<Object, Double> toBoxed() {",
" return o -> 0.0;",
" }",
"",
" private ToDoubleFunction<Object> toPrimitive() {",
" return o -> 0.0;",
" }",
"}")
.doTest();
}
@@ -420,14 +484,17 @@ public final class PrimitiveComparisonCheckTest {
" cmp().thenComparing(toStr(), cmp());",
" }",
"",
" private Comparator<Object> cmp() { return null; }",
" private Function<Object, String> toStr() { return String::valueOf; }",
" private Comparator<Object> cmp() {",
" return null;",
" }",
"",
" private Function<Object, String> toStr() {",
" return String::valueOf;",
" }",
"}")
.doTest();
}
// XXX: If the explicit `<A, BoxedPrimitive>` generic type information was necessary, then this
// replacement drops too much information.
@Test
void replacementWithPrimitiveVariants() {
refactoringTestHelper
@@ -436,21 +503,35 @@ public final class PrimitiveComparisonCheckTest {
"import java.util.Comparator;",
"",
"interface A extends Comparable<A> {",
" Comparator<A> bCmp = Comparator.<A, Byte>comparing(o -> (byte) 0);",
" Comparator<A> cCmp = Comparator.<A, Character>comparing(o -> (char) 0);",
" Comparator<A> sCmp = Comparator.<A, Short>comparing(o -> (short) 0);",
" Comparator<A> iCmp = Comparator.<A, Integer>comparing(o -> 0);",
" Comparator<A> lCmp = Comparator.<A, Long>comparing(o -> 0L);",
" Comparator<A> fCmp = Comparator.<A, Float>comparing(o -> 0.0f);",
" Comparator<A> dCmp = Comparator.<A, Double>comparing(o -> 0.0);",
" Comparator<A> bCmp = Comparator.comparing(o -> (byte) 0);",
" Comparator<A> bCmp2 = Comparator.<A, Byte>comparing(o -> (byte) 0);",
" Comparator<A> cCmp = Comparator.comparing(o -> (char) 0);",
" Comparator<A> cCmp2 = Comparator.<A, Character>comparing(o -> (char) 0);",
" Comparator<A> sCmp = Comparator.comparing(o -> (short) 0);",
" Comparator<A> sCmp2 = Comparator.<A, Short>comparing(o -> (short) 0);",
" Comparator<A> iCmp = Comparator.comparing(o -> 0);",
" Comparator<A> iCmp2 = Comparator.<A, Integer>comparing(o -> 0);",
" Comparator<A> lCmp = Comparator.comparing(o -> 0L);",
" Comparator<A> lCmp2 = Comparator.<A, Long>comparing(o -> 0L);",
" Comparator<A> fCmp = Comparator.comparing(o -> 0.0f);",
" Comparator<A> fCmp2 = Comparator.<A, Float>comparing(o -> 0.0f);",
" Comparator<A> dCmp = Comparator.comparing(o -> 0.0);",
" Comparator<A> dCmp2 = Comparator.<A, Double>comparing(o -> 0.0);",
"",
" default void m() {",
" bCmp.<Byte>thenComparing(o -> (byte) 0);",
" bCmp.thenComparing(o -> (byte) 0);",
" cCmp.<Character>thenComparing(o -> (char) 0);",
" cCmp.thenComparing(o -> (char) 0);",
" sCmp.<Short>thenComparing(o -> (short) 0);",
" sCmp.thenComparing(o -> (short) 0);",
" iCmp.<Integer>thenComparing(o -> 0);",
" iCmp.thenComparing(o -> 0);",
" lCmp.<Long>thenComparing(o -> 0L);",
" lCmp.thenComparing(o -> 0L);",
" fCmp.<Float>thenComparing(o -> 0.0f);",
" fCmp.thenComparing(o -> 0.0f);",
" dCmp.<Double>thenComparing(o -> 0.0);",
" dCmp.thenComparing(o -> 0.0);",
" }",
"}")
@@ -460,28 +541,40 @@ public final class PrimitiveComparisonCheckTest {
"",
"interface A extends Comparable<A> {",
" Comparator<A> bCmp = Comparator.comparingInt(o -> (byte) 0);",
" Comparator<A> bCmp2 = Comparator.<A>comparingInt(o -> (byte) 0);",
" Comparator<A> cCmp = Comparator.comparingInt(o -> (char) 0);",
" Comparator<A> cCmp2 = Comparator.<A>comparingInt(o -> (char) 0);",
" Comparator<A> sCmp = Comparator.comparingInt(o -> (short) 0);",
" Comparator<A> sCmp2 = Comparator.<A>comparingInt(o -> (short) 0);",
" Comparator<A> iCmp = Comparator.comparingInt(o -> 0);",
" Comparator<A> iCmp2 = Comparator.<A>comparingInt(o -> 0);",
" Comparator<A> lCmp = Comparator.comparingLong(o -> 0L);",
" Comparator<A> lCmp2 = Comparator.<A>comparingLong(o -> 0L);",
" Comparator<A> fCmp = Comparator.comparingDouble(o -> 0.0f);",
" Comparator<A> fCmp2 = Comparator.<A>comparingDouble(o -> 0.0f);",
" Comparator<A> dCmp = Comparator.comparingDouble(o -> 0.0);",
" Comparator<A> dCmp2 = Comparator.<A>comparingDouble(o -> 0.0);",
"",
" default void m() {",
" bCmp.thenComparingInt(o -> (byte) 0);",
" bCmp.thenComparingInt(o -> (byte) 0);",
" cCmp.thenComparingInt(o -> (char) 0);",
" cCmp.thenComparingInt(o -> (char) 0);",
" sCmp.thenComparingInt(o -> (short) 0);",
" sCmp.thenComparingInt(o -> (short) 0);",
" iCmp.thenComparingInt(o -> 0);",
" iCmp.thenComparingInt(o -> 0);",
" lCmp.thenComparingLong(o -> 0L);",
" lCmp.thenComparingLong(o -> 0L);",
" fCmp.thenComparingDouble(o -> 0.0f);",
" fCmp.thenComparingDouble(o -> 0.0f);",
" dCmp.thenComparingDouble(o -> 0.0);",
" dCmp.thenComparingDouble(o -> 0.0);",
" }",
"}")
.doTest(TestMode.TEXT_MATCH);
}
// XXX: If the explicit `<A>` generic type information was necessary, then this replacement drops
// too much information.
@Test
void replacementWithBoxedVariants() {
refactoringTestHelper
@@ -490,13 +583,20 @@ public final class PrimitiveComparisonCheckTest {
"import java.util.Comparator;",
"",
"interface A extends Comparable<A> {",
" Comparator<A> bCmp = Comparator.<A>comparingInt(o -> Byte.valueOf((byte) 0));",
" Comparator<A> cCmp = Comparator.<A>comparingInt(o -> Character.valueOf((char) 0));",
" Comparator<A> sCmp = Comparator.<A>comparingInt(o -> Short.valueOf((short) 0));",
" Comparator<A> iCmp = Comparator.<A>comparingInt(o -> Integer.valueOf(0));",
" Comparator<A> lCmp = Comparator.<A>comparingLong(o -> Long.valueOf(0));",
" Comparator<A> fCmp = Comparator.<A>comparingDouble(o -> Float.valueOf(0));",
" Comparator<A> dCmp = Comparator.<A>comparingDouble(o -> Double.valueOf(0));",
" Comparator<A> bCmp = Comparator.comparingInt(o -> Byte.valueOf((byte) 0));",
" Comparator<A> bCmp2 = Comparator.<A>comparingInt(o -> Byte.valueOf((byte) 0));",
" Comparator<A> cCmp = Comparator.comparingInt(o -> Character.valueOf((char) 0));",
" Comparator<A> cCmp2 = Comparator.<A>comparingInt(o -> Character.valueOf((char) 0));",
" Comparator<A> sCmp = Comparator.comparingInt(o -> Short.valueOf((short) 0));",
" Comparator<A> sCmp2 = Comparator.<A>comparingInt(o -> Short.valueOf((short) 0));",
" Comparator<A> iCmp = Comparator.comparingInt(o -> Integer.valueOf(0));",
" Comparator<A> iCmp2 = Comparator.<A>comparingInt(o -> Integer.valueOf(0));",
" Comparator<A> lCmp = Comparator.comparingLong(o -> Long.valueOf(0));",
" Comparator<A> lCmp2 = Comparator.<A>comparingLong(o -> Long.valueOf(0));",
" Comparator<A> fCmp = Comparator.comparingDouble(o -> Float.valueOf(0));",
" Comparator<A> fCmp2 = Comparator.<A>comparingDouble(o -> Float.valueOf(0));",
" Comparator<A> dCmp = Comparator.comparingDouble(o -> Double.valueOf(0));",
" Comparator<A> dCmp2 = Comparator.<A>comparingDouble(o -> Double.valueOf(0));",
"",
" default void m() {",
" bCmp.thenComparingInt(o -> Byte.valueOf((byte) 0));",
@@ -514,12 +614,19 @@ public final class PrimitiveComparisonCheckTest {
"",
"interface A extends Comparable<A> {",
" Comparator<A> bCmp = Comparator.comparing(o -> Byte.valueOf((byte) 0));",
" Comparator<A> bCmp2 = Comparator.<A, Byte>comparing(o -> Byte.valueOf((byte) 0));",
" Comparator<A> cCmp = Comparator.comparing(o -> Character.valueOf((char) 0));",
" Comparator<A> cCmp2 = Comparator.<A, Character>comparing(o -> Character.valueOf((char) 0));",
" Comparator<A> sCmp = Comparator.comparing(o -> Short.valueOf((short) 0));",
" Comparator<A> sCmp2 = Comparator.<A, Short>comparing(o -> Short.valueOf((short) 0));",
" Comparator<A> iCmp = Comparator.comparing(o -> Integer.valueOf(0));",
" Comparator<A> iCmp2 = Comparator.<A, Integer>comparing(o -> Integer.valueOf(0));",
" Comparator<A> lCmp = Comparator.comparing(o -> Long.valueOf(0));",
" Comparator<A> lCmp2 = Comparator.<A, Long>comparing(o -> Long.valueOf(0));",
" Comparator<A> fCmp = Comparator.comparing(o -> Float.valueOf(0));",
" Comparator<A> fCmp2 = Comparator.<A, Float>comparing(o -> Float.valueOf(0));",
" Comparator<A> dCmp = Comparator.comparing(o -> Double.valueOf(0));",
" Comparator<A> dCmp2 = Comparator.<A, Double>comparing(o -> Double.valueOf(0));",
"",
" default void m() {",
" bCmp.thenComparing(o -> Byte.valueOf((byte) 0));",
@@ -654,22 +761,30 @@ public final class PrimitiveComparisonCheckTest {
"import java.util.Comparator;",
"",
"interface A extends Comparable<A> {",
" Comparator<A> bCmp = Comparator.<A, A>comparing(o -> o).thenComparingInt(o -> Byte.valueOf((byte) 0));",
" Comparator<A> cCmp = Comparator.<A, A>comparing(o -> o).thenComparingInt(o -> Character.valueOf((char) 0));",
" Comparator<A> sCmp = Comparator.<A, A>comparing(o -> o).thenComparingInt(o -> Short.valueOf((short) 0));",
" Comparator<A> bCmp =",
" Comparator.<A, A>comparing(o -> o).thenComparingInt(o -> Byte.valueOf((byte) 0));",
" Comparator<A> cCmp =",
" Comparator.<A, A>comparing(o -> o).thenComparingInt(o -> Character.valueOf((char) 0));",
" Comparator<A> sCmp =",
" Comparator.<A, A>comparing(o -> o).thenComparingInt(o -> Short.valueOf((short) 0));",
" Comparator<A> iCmp = Comparator.<A, A>comparing(o -> o).thenComparingInt(o -> Integer.valueOf(0));",
" Comparator<A> lCmp = Comparator.<A, A>comparing(o -> o).thenComparingLong(o -> Long.valueOf(0));",
" Comparator<A> fCmp = Comparator.<A, A>comparing(o -> o).thenComparingDouble(o -> Float.valueOf(0));",
" Comparator<A> dCmp = Comparator.<A, A>comparing(o -> o).thenComparingDouble(o -> Double.valueOf(0));",
" Comparator<A> fCmp =",
" Comparator.<A, A>comparing(o -> o).thenComparingDouble(o -> Float.valueOf(0));",
" Comparator<A> dCmp =",
" Comparator.<A, A>comparing(o -> o).thenComparingDouble(o -> Double.valueOf(0));",
"}")
.addOutputLines(
"out/A.java",
"import java.util.Comparator;",
"",
"interface A extends Comparable<A> {",
" Comparator<A> bCmp = Comparator.<A, A>comparing(o -> o).thenComparing(o -> Byte.valueOf((byte) 0));",
" Comparator<A> cCmp = Comparator.<A, A>comparing(o -> o).thenComparing(o -> Character.valueOf((char) 0));",
" Comparator<A> sCmp = Comparator.<A, A>comparing(o -> o).thenComparing(o -> Short.valueOf((short) 0));",
" Comparator<A> bCmp =",
" Comparator.<A, A>comparing(o -> o).thenComparing(o -> Byte.valueOf((byte) 0));",
" Comparator<A> cCmp =",
" Comparator.<A, A>comparing(o -> o).thenComparing(o -> Character.valueOf((char) 0));",
" Comparator<A> sCmp =",
" Comparator.<A, A>comparing(o -> o).thenComparing(o -> Short.valueOf((short) 0));",
" Comparator<A> iCmp = Comparator.<A, A>comparing(o -> o).thenComparing(o -> Integer.valueOf(0));",
" Comparator<A> lCmp = Comparator.<A, A>comparing(o -> o).thenComparing(o -> Long.valueOf(0));",
" Comparator<A> fCmp = Comparator.<A, A>comparing(o -> o).thenComparing(o -> Float.valueOf(0));",

View File

@@ -9,7 +9,7 @@ import org.junit.jupiter.api.Test;
// XXX: The tests below show that `String.valueOf((String) null)` may be simplified, but
// `String.valueOf(null)` may not. That is because the latter matches `String#valueOf(char[])`. We
// could special-case `null` arguments, but that doesn't seem worth the trouble.
public final class RedundantStringConversionCheckTest {
final class RedundantStringConversionCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(RedundantStringConversionCheck.class, getClass());
private final CompilationTestHelper customizedCompilationTestHelper =
@@ -124,7 +124,7 @@ public final class RedundantStringConversionCheckTest {
" s + String.valueOf((String) null),",
" s + String.valueOf(null),",
" s + String.valueOf(new char[0]),",
"",
" //",
" 42 + this.toString(),",
" 42 + super.toString(),",
" 42 + i.toString(),",
@@ -147,7 +147,7 @@ public final class RedundantStringConversionCheckTest {
" String.valueOf((String) null) + s,",
" String.valueOf(null) + s,",
" String.valueOf(new char[0]) + s,",
"",
" //",
" this.toString() + 42,",
" super.toString() + 42,",
" i.toString() + 42,",
@@ -175,10 +175,7 @@ public final class RedundantStringConversionCheckTest {
"",
" int[] m2() {",
" return new int[] {",
" 1 + 1,",
" 1 - 1,",
" 1 * 1,",
" 1 / 1,",
" 1 + 1, 1 - 1, 1 * 1, 1 / 1,",
" };",
" }",
"}")
@@ -284,9 +281,9 @@ public final class RedundantStringConversionCheckTest {
compilationTestHelper
.addSourceLines(
"A.java",
"import static com.google.common.base.Preconditions.checkState;",
"import static com.google.common.base.Preconditions.checkArgument;",
"import static com.google.common.base.Preconditions.checkNotNull;",
"import static com.google.common.base.Preconditions.checkState;",
"import static com.google.common.base.Verify.verify;",
"import static com.google.common.base.Verify.verifyNotNull;",
"",

View File

@@ -5,7 +5,7 @@ import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
public final class RefasterAnyOfUsageCheckTest {
final class RefasterAnyOfUsageCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(RefasterAnyOfUsageCheck.class, getClass());
private final BugCheckerRefactoringTestHelper refactoringTestHelper =

View File

@@ -13,12 +13,13 @@ import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import org.junit.ComparisonFailure;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
public final class RefasterCheckTest {
final class RefasterCheckTest {
/** The names of all Refaster template groups defined in this module. */
private static final ImmutableSet<String> TEMPLATE_GROUPS =
ImmutableSet.of(
@@ -34,10 +35,12 @@ public final class RefasterCheckTest {
"AssertJInteger",
"AssertJLong",
"AssertJNumber",
"AssertJMap",
"AssertJObject",
"AssertJOptional",
"AssertJShort",
"AssertJString",
"AssertJThrowingCallable",
"Assorted",
"BigDecimal",
"Collection",
@@ -137,8 +140,7 @@ public final class RefasterCheckTest {
assertThatCode(() -> verifyRefactoring(group, namePattern(group, template)))
.withFailMessage(
"Template %s does not affect the tests for group %s; is it tested?", template, group)
.isInstanceOf(AssertionError.class)
.hasMessageFindingMatch("^(diff|expected):");
.isInstanceOf(ComparisonFailure.class);
}
private static ImmutableSetMultimap<String, String> indexTemplateNamesByGroup(
@@ -166,7 +168,24 @@ public final class RefasterCheckTest {
private BugCheckerRefactoringTestHelper createRestrictedRefactoringTestHelper(
String namePattern) {
return BugCheckerRefactoringTestHelper.newInstance(RefasterCheck.class, getClass())
return BugCheckerRefactoringTestHelper.newInstance(
RefasterCheck.class, getRefasterTemplateCollectionClass())
.setArgs("-XepOpt:Refaster:NamePattern=" + namePattern);
}
/**
* Returns an arbitrary Refaster template collection class inside the {@code
* tech.picnic.errorprone.refastertemplates} package, enabling {@link
* BugCheckerRefactoringTestHelper} to load test resources from said package.
*/
private Class<?> getRefasterTemplateCollectionClass() {
try {
return Class.forName(
"tech.picnic.errorprone.refastertemplates.AssortedTemplates",
/* initialize= */ false,
getClass().getClassLoader());
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Failed to load Refaster template collection class", e);
}
}
}

View File

@@ -3,7 +3,7 @@ package tech.picnic.errorprone.bugpatterns;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
public final class RequestMappingAnnotationCheckTest {
final class RequestMappingAnnotationCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(RequestMappingAnnotationCheck.class, getClass());
@@ -13,6 +13,9 @@ public final class RequestMappingAnnotationCheckTest {
.addSourceLines(
"A.java",
"import java.io.InputStream;",
"import java.time.ZoneId;",
"import java.util.Locale;",
"import java.util.TimeZone;",
"import javax.servlet.http.HttpServletRequest;",
"import javax.servlet.http.HttpServletResponse;",
"import org.springframework.http.HttpMethod;",
@@ -25,47 +28,100 @@ public final class RequestMappingAnnotationCheckTest {
"import org.springframework.web.bind.annotation.RequestBody;",
"import org.springframework.web.bind.annotation.RequestHeader;",
"import org.springframework.web.bind.annotation.RequestMapping;",
"import org.springframework.web.bind.annotation.RequestMethod;",
"import org.springframework.web.bind.annotation.RequestParam;",
"import org.springframework.web.context.request.NativeWebRequest;",
"import org.springframework.web.context.request.WebRequest;",
"import org.springframework.web.server.ServerWebExchange;",
"import org.springframework.web.util.UriBuilder;",
"import org.springframework.web.util.UriComponentsBuilder;",
"",
"interface A {",
" A noMapping();",
"",
" A noMapping(String param);",
" @DeleteMapping A properNoParameters();",
" @GetMapping A properPathVariable(@PathVariable String param);",
" @PatchMapping A properRequestBody(@RequestBody String body);",
" @PostMapping A properRequestHeader(@RequestHeader String header);",
" @PutMapping A properRequestParam(@RequestParam String param);",
" @RequestMapping A properInputStream(InputStream input);",
" @RequestMapping A properHttpServletRequest(HttpServletRequest request);",
" @RequestMapping A properHttpServletResponse(HttpServletResponse response);",
" @RequestMapping A properHttpMethod(HttpMethod method);",
" @RequestMapping A properWebRequest(WebRequest request);",
"",
" // BUG: Diagnostic contains:",
" @DeleteMapping A delete(String param);",
" @DeleteMapping",
" A properNoParameters();",
"",
" // BUG: Diagnostic contains:",
" @GetMapping A get(String param);",
" @GetMapping",
" A properPathVariable(@PathVariable String param);",
"",
" // BUG: Diagnostic contains:",
" @PatchMapping A patch(String param);",
" @PatchMapping",
" A properRequestBody(@RequestBody String body);",
"",
" // BUG: Diagnostic contains:",
" @PostMapping A post(String param);",
" @PostMapping",
" A properRequestHeader(@RequestHeader String header);",
"",
" // BUG: Diagnostic contains:",
" @PutMapping A put(String param);",
" @PutMapping",
" A properRequestParam(@RequestParam String param);",
"",
" // BUG: Diagnostic contains:",
" @RequestMapping A requestMultiple(String param, String param2);",
" @RequestMapping",
" A properInputStream(InputStream input);",
"",
" // BUG: Diagnostic contains:",
" @RequestMapping A requestFirstParamViolation(String param, @PathVariable String param2);",
" @RequestMapping",
" A properZoneId(ZoneId zoneId);",
"",
" @RequestMapping",
" A properLocale(Locale locale);",
"",
" @RequestMapping",
" A properTimeZone(TimeZone timeZone);",
"",
" @RequestMapping",
" A properHttpServletRequest(HttpServletRequest request);",
"",
" @RequestMapping",
" A properHttpServletResponse(HttpServletResponse response);",
"",
" @RequestMapping",
" A properHttpMethod(HttpMethod method);",
"",
" @RequestMapping",
" A properNativeWebRequest(NativeWebRequest request);",
"",
" @RequestMapping",
" A properWebRequest(WebRequest request);",
"",
" @RequestMapping",
" A properServerWebExchange(ServerWebExchange exchange);",
"",
" @RequestMapping",
" A properServerUriBuilder(UriBuilder builder);",
"",
" @RequestMapping",
" A properServerUriComponentsBuilder(UriComponentsBuilder builder);",
"",
" @DeleteMapping",
" // BUG: Diagnostic contains:",
" @RequestMapping A requestSecondParamViolation(@RequestBody String param, String param2);",
" A delete(String param);",
"",
" @GetMapping",
" // BUG: Diagnostic contains:",
" A get(String param);",
"",
" @PatchMapping",
" // BUG: Diagnostic contains:",
" A patch(String param);",
"",
" @PostMapping",
" // BUG: Diagnostic contains:",
" A post(String param);",
"",
" @PutMapping",
" // BUG: Diagnostic contains:",
" A put(String param);",
"",
" @RequestMapping",
" // BUG: Diagnostic contains:",
" A requestMultiple(String param, String param2);",
"",
" @RequestMapping",
" // BUG: Diagnostic contains:",
" A requestFirstParamViolation(String param, @PathVariable String param2);",
"",
" @RequestMapping",
" // BUG: Diagnostic contains:",
" A requestSecondParamViolation(@RequestBody String param, String param2);",
"}")
.doTest();
}

View File

@@ -0,0 +1,66 @@
package tech.picnic.errorprone.bugpatterns;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
final class RequestParamTypeCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(RequestParamTypeCheck.class, getClass());
@Test
void identification() {
compilationTestHelper
.addSourceLines(
"A.java",
"import com.google.common.collect.ImmutableBiMap;",
"import com.google.common.collect.ImmutableList;",
"import com.google.common.collect.ImmutableMap;",
"import com.google.common.collect.ImmutableSet;",
"import java.util.List;",
"import java.util.Map;",
"import java.util.Set;",
"import javax.annotation.Nullable;",
"import org.springframework.web.bind.annotation.DeleteMapping;",
"import org.springframework.web.bind.annotation.GetMapping;",
"import org.springframework.web.bind.annotation.PostMapping;",
"import org.springframework.web.bind.annotation.PutMapping;",
"import org.springframework.web.bind.annotation.RequestBody;",
"import org.springframework.web.bind.annotation.RequestParam;",
"",
"interface A {",
" @PostMapping",
" A properRequestParam(@RequestBody String body);",
"",
" @GetMapping",
" A properRequestParam(@RequestParam int param);",
"",
" @GetMapping",
" A properRequestParam(@RequestParam List<String> param);",
"",
" @PostMapping",
" A properRequestParam(@RequestBody String body, @RequestParam Set<String> param);",
"",
" @PutMapping",
" A properRequestParam(@RequestBody String body, @RequestParam Map<String, String> param);",
"",
" @GetMapping",
" // BUG: Diagnostic contains:",
" A get(@RequestParam ImmutableBiMap<String, String> param);",
"",
" @PostMapping",
" // BUG: Diagnostic contains:",
" A post(@Nullable @RequestParam ImmutableList<String> param);",
"",
" @PutMapping",
" // BUG: Diagnostic contains:",
" A put(@RequestBody String body, @RequestParam ImmutableSet<String> param);",
"",
" @DeleteMapping",
" // BUG: Diagnostic contains:",
" A delete(@RequestBody String body, @RequestParam ImmutableMap<String, String> param);",
"",
" void negative(ImmutableSet<Integer> set, ImmutableMap<String, String> map);",
"}")
.doTest();
}
}

View File

@@ -0,0 +1,94 @@
package tech.picnic.errorprone.bugpatterns;
import com.google.errorprone.BugCheckerRefactoringTestHelper;
import com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode;
import com.google.errorprone.CompilationTestHelper;
import org.junit.jupiter.api.Test;
final class ScheduledTransactionTraceCheckTest {
private final CompilationTestHelper compilationTestHelper =
CompilationTestHelper.newInstance(ScheduledTransactionTraceCheck.class, getClass());
private final BugCheckerRefactoringTestHelper refactoringTestHelper =
BugCheckerRefactoringTestHelper.newInstance(ScheduledTransactionTraceCheck.class, getClass());
@Test
void identification() {
compilationTestHelper
.addSourceLines(
"A.java",
"import com.newrelic.api.agent.Trace;",
"import org.springframework.scheduling.annotation.Scheduled;",
"",
"class A {",
" void notScheduled() {}",
"",
" @Scheduled(fixedDelay = 1)",
" // BUG: Diagnostic contains:",
" void scheduledButNotTraced() {}",
"",
" @Scheduled(fixedDelay = 1)",
" // BUG: Diagnostic contains:",
" @Trace",
" void scheduledButImproperlyTraced1() {}",
"",
" @Scheduled(fixedDelay = 1)",
" // BUG: Diagnostic contains:",
" @Trace(dispatcher = false)",
" void scheduledButImproperlyTraced2() {}",
"",
" @Scheduled(fixedDelay = 1)",
" @Trace(dispatcher = true)",
" void scheduledAndProperlyTraced() {}",
"}")
.doTest();
}
@Test
void replacement() {
refactoringTestHelper
.addInputLines(
"in/A.java",
"import com.newrelic.api.agent.Trace;",
"import org.springframework.scheduling.annotation.Scheduled;",
"",
"class A {",
" @Scheduled(fixedDelay = 1)",
" void scheduledButNotTraced() {}",
"",
" @Scheduled(fixedDelay = 1)",
" @Trace",
" void scheduledButImproperlyTraced1() {}",
"",
" @Scheduled(fixedDelay = 1)",
" @Trace(dispatcher = false)",
" void scheduledButImproperlyTraced2() {}",
"",
" @Scheduled(fixedDelay = 1)",
" @Trace(leaf = true)",
" void scheduledButImproperlyTraced3() {}",
"}")
.addOutputLines(
"out/A.java",
"import com.newrelic.api.agent.Trace;",
"import org.springframework.scheduling.annotation.Scheduled;",
"",
"class A {",
" @Trace(dispatcher = true)",
" @Scheduled(fixedDelay = 1)",
" void scheduledButNotTraced() {}",
"",
" @Scheduled(fixedDelay = 1)",
" @Trace(dispatcher = true)",
" void scheduledButImproperlyTraced1() {}",
"",
" @Scheduled(fixedDelay = 1)",
" @Trace(dispatcher = true)",
" void scheduledButImproperlyTraced2() {}",
"",
" @Scheduled(fixedDelay = 1)",
" @Trace(dispatcher = true, leaf = true)",
" void scheduledButImproperlyTraced3() {}",
"}")
.doTest(TestMode.TEXT_MATCH);
}
}

Some files were not shown because too many files have changed in this diff Show More