diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/StaticImportCheck.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/StaticImportCheck.java index 96b6fcf60c6..d5625a4c8e9 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/StaticImportCheck.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/bugpatterns/StaticImportCheck.java @@ -119,6 +119,7 @@ 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("org.junit.jupiter.params.provider.Arguments", "arguments") diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/TimeTemplates.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/TimeTemplates.java index 31693ef8ff0..e9d0e1d8cf6 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/TimeTemplates.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refastertemplates/TimeTemplates.java @@ -59,6 +59,19 @@ ZoneOffset after() { } } + /** 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); + } + } + /** Use {@link Clock#systemUTC()} when possible. */ static final class UtcClock { @BeforeTemplate diff --git a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportCheckTest.java b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportCheckTest.java index 0a0485ade59..df22f275347 100644 --- a/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportCheckTest.java +++ b/error-prone-contrib/src/test/java/tech/picnic/errorprone/bugpatterns/StaticImportCheckTest.java @@ -113,6 +113,7 @@ void replacement() { "import com.google.common.collect.ImmutableMap;", "import com.google.common.collect.ImmutableSet;", "import java.nio.charset.StandardCharsets;", + "import java.time.ZoneOffset;", "import java.util.Objects;", "import org.junit.jupiter.params.provider.Arguments;", "import org.springframework.format.annotation.DateTimeFormat;", @@ -143,6 +144,9 @@ void replacement() { " MediaType.APPLICATION_XHTML_XML,", " MediaType.TEXT_HTML,", " MediaType.valueOf(\"image/webp\"));", + "", + " ZoneOffset z1 = ZoneOffset.UTC;", + " ZoneOffset z2 = ZoneOffset.MIN;", " }", "", " void m2(", @@ -163,6 +167,7 @@ void replacement() { "import static com.google.common.collect.ImmutableMap.toImmutableMap;", "import static com.google.common.collect.ImmutableSet.toImmutableSet;", "import static java.nio.charset.StandardCharsets.UTF_8;", + "import static java.time.ZoneOffset.UTC;", "import static java.util.Objects.requireNonNull;", "import static java.util.function.Predicate.not;", "import static org.junit.jupiter.params.provider.Arguments.arguments;", @@ -177,6 +182,7 @@ void replacement() { "import com.google.common.collect.ImmutableMap;", "import com.google.common.collect.ImmutableSet;", "import java.nio.charset.StandardCharsets;", + "import java.time.ZoneOffset;", "import java.util.Objects;", "import org.junit.jupiter.params.provider.Arguments;", "import org.springframework.boot.test.context.SpringBootTest;", @@ -207,6 +213,9 @@ void replacement() { " APPLICATION_XHTML_XML,", " TEXT_HTML,", " MediaType.valueOf(\"image/webp\"));", + "", + " ZoneOffset z1 = UTC;", + " ZoneOffset z2 = ZoneOffset.MIN;", " }", "", " void m2(", diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/TimeTemplatesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/TimeTemplatesTestInput.java index 84394c9d449..691213de428 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/TimeTemplatesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/TimeTemplatesTestInput.java @@ -34,6 +34,10 @@ ImmutableSet testUtcConstant() { ZoneId.from(ZoneOffset.UTC)); } + OffsetDateTime testInstantAtOffset() { + return OffsetDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC); + } + Clock testUtcClock() { return Clock.system(ZoneOffset.UTC); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/TimeTemplatesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/TimeTemplatesTestOutput.java index b514b0f358d..3fb98c7338a 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/TimeTemplatesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/bugpatterns/TimeTemplatesTestOutput.java @@ -34,6 +34,10 @@ ImmutableSet testUtcConstant() { ZoneOffset.UTC); } + OffsetDateTime testInstantAtOffset() { + return Instant.EPOCH.atOffset(ZoneOffset.UTC); + } + Clock testUtcClock() { return Clock.systemUTC(); }