Skip to content

Commit

Permalink
StaticImport: Add ZoneOffset.UTC and add template to TimeTemplates
Browse files Browse the repository at this point in the history
  • Loading branch information
rickie committed Feb 7, 2022
1 parent 793a70c commit 82943cf
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;",
Expand Down Expand Up @@ -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(",
Expand All @@ -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;",
Expand All @@ -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;",
Expand Down Expand Up @@ -207,6 +213,9 @@ void replacement() {
" APPLICATION_XHTML_XML,",
" TEXT_HTML,",
" MediaType.valueOf(\"image/webp\"));",
"",
" ZoneOffset z1 = UTC;",
" ZoneOffset z2 = ZoneOffset.MIN;",
" }",
"",
" void m2(",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ ImmutableSet<ZoneId> testUtcConstant() {
ZoneId.from(ZoneOffset.UTC));
}

OffsetDateTime testInstantAtOffset() {
return OffsetDateTime.ofInstant(Instant.EPOCH, ZoneOffset.UTC);
}

Clock testUtcClock() {
return Clock.system(ZoneOffset.UTC);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ ImmutableSet<ZoneId> testUtcConstant() {
ZoneOffset.UTC);
}

OffsetDateTime testInstantAtOffset() {
return Instant.EPOCH.atOffset(ZoneOffset.UTC);
}

Clock testUtcClock() {
return Clock.systemUTC();
}
Expand Down

0 comments on commit 82943cf

Please sign in to comment.