Skip to content

Commit def2188

Browse files
author
Vincent Potucek
committed
[prone] Add TimeRulesRecipes
Signed-off-by: Vincent Potucek <[email protected]>
1 parent 4bc97ad commit def2188

File tree

18 files changed

+70
-32
lines changed

18 files changed

+70
-32
lines changed

.github/actions/build/action.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,8 @@ runs:
5959
version=$(sed -n 's/version=\(.*\)/\1/p' gradle.properties)
6060
echo "Version is $version"
6161
echo "version=$version" >> $GITHUB_OUTPUT
62+
- name: SanityCheck
63+
id: build
64+
if: ${{ inputs.publish == 'false' }}
65+
shell: bash
66+
run: ./gradlew rewriteDryRun -Dorg.gradle.jvmargs=-Xmx8G

build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ plugins {
77
id 'com.gradleup.shadow' version "9.2.2" apply false
88
id 'me.champeau.jmh' version '0.7.2' apply false
99
id 'io.spring.nullability' version '0.0.8' apply false
10+
id 'org.openrewrite.rewrite' version '7.20.0' apply false
1011
}
1112

1213
ext {
@@ -16,6 +17,8 @@ ext {
1617

1718
description = "Spring Framework"
1819

20+
apply from: "$rootDir/gradle/rewrite.gradle"
21+
1922
configure(allprojects) { project ->
2023
apply plugin: "org.springframework.build.localdev"
2124
group = "org.springframework"

gradle/rewrite.gradle

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apply plugin: 'org.openrewrite.rewrite'
2+
3+
rewrite {
4+
activeRecipe('org.springframework.openrewrite.SanityCheck')
5+
setExportDatatables(true)
6+
setFailOnDryRunResults(true)
7+
}
8+
9+
dependencies {
10+
rewrite(platform('org.openrewrite.recipe:rewrite-recipe-bom:3.18.0'))
11+
rewrite('org.openrewrite.recipe:rewrite-rewrite:0.15.0')
12+
}

rewrite.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
---
2+
type: specs.openrewrite.org/v1beta/recipe
3+
name: org.springframework.openrewrite.SanityCheck
4+
displayName: Apply all Java & Gradle best practices
5+
description: Comprehensive code quality recipe combining modernization, security, and best practices.
6+
tags:
7+
- java
8+
- gradle
9+
- static-analysis
10+
- cleanup
11+
recipeList:
12+
- org.openrewrite.gradle.EnableGradleBuildCache
13+
- org.openrewrite.gradle.EnableGradleParallelExecution
14+
- org.openrewrite.gradle.GradleBestPractices
15+
- tech.picnic.errorprone.refasterrules.TimeRulesRecipes
16+
# TBD
17+
# - org.openrewrite.java.migrate.Java8toJava11 # https://github.com/google/error-prone/pull/5328
18+
# - org.openrewrite.java.migrate.UpgradeToJava17 # https://github.com/checkstyle/checkstyle/pull/17730
19+
---

spring-context/src/main/java/org/springframework/format/datetime/standard/DateTimeConverters.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ private static ZonedDateTime calendarToZonedDateTime(Calendar source) {
8181
return gc.toZonedDateTime();
8282
}
8383
else {
84-
return ZonedDateTime.ofInstant(Instant.ofEpochMilli(source.getTimeInMillis()),
85-
source.getTimeZone().toZoneId());
84+
return Instant.ofEpochMilli(source.getTimeInMillis()).atZone(source.getTimeZone().toZoneId());
8685
}
8786
}
8887

spring-context/src/main/java/org/springframework/scheduling/support/CronTrigger.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public String getExpression() {
115115
public @Nullable Instant nextExecution(TriggerContext triggerContext) {
116116
Instant timestamp = determineLatestTimestamp(triggerContext);
117117
ZoneId zone = (this.zoneId != null ? this.zoneId : triggerContext.getClock().getZone());
118-
ZonedDateTime zonedTimestamp = ZonedDateTime.ofInstant(timestamp, zone);
118+
ZonedDateTime zonedTimestamp = timestamp.atZone(zone);
119119
ZonedDateTime nextTimestamp = this.expression.next(zonedTimestamp);
120120
return (nextTimestamp != null ? nextTimestamp.toInstant() : null);
121121
}

spring-context/src/test/java/org/springframework/scheduling/concurrent/DefaultManagedTaskSchedulerTests.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.time.Duration;
2020
import java.time.Instant;
21-
import java.time.temporal.ChronoUnit;
2221

2322
import org.assertj.core.api.ThrowableAssert.ThrowingCallable;
2423
import org.junit.jupiter.api.Test;
@@ -53,28 +52,28 @@ void scheduleWithInstantAndNoScheduledExecutorProvidesDedicatedException() {
5352
void scheduleAtFixedRateWithStartTimeAndDurationAndNoScheduledExecutorProvidesDedicatedException() {
5453
DefaultManagedTaskScheduler scheduler = new DefaultManagedTaskScheduler();
5554
assertNoExecutorException(() -> scheduler.scheduleAtFixedRate(
56-
NO_OP, Instant.now(), Duration.of(1, ChronoUnit.MINUTES)));
55+
NO_OP, Instant.now(), Duration.ofMinutes(1)));
5756
}
5857

5958
@Test
6059
void scheduleAtFixedRateWithDurationAndNoScheduledExecutorProvidesDedicatedException() {
6160
DefaultManagedTaskScheduler scheduler = new DefaultManagedTaskScheduler();
6261
assertNoExecutorException(() -> scheduler.scheduleAtFixedRate(
63-
NO_OP, Duration.of(1, ChronoUnit.MINUTES)));
62+
NO_OP, Duration.ofMinutes(1)));
6463
}
6564

6665
@Test
6766
void scheduleWithFixedDelayWithStartTimeAndDurationAndNoScheduledExecutorProvidesDedicatedException() {
6867
DefaultManagedTaskScheduler scheduler = new DefaultManagedTaskScheduler();
6968
assertNoExecutorException(() -> scheduler.scheduleWithFixedDelay(
70-
NO_OP, Instant.now(), Duration.of(1, ChronoUnit.MINUTES)));
69+
NO_OP, Instant.now(), Duration.ofMinutes(1)));
7170
}
7271

7372
@Test
7473
void scheduleWithFixedDelayWithDurationAndNoScheduledExecutorProvidesDedicatedException() {
7574
DefaultManagedTaskScheduler scheduler = new DefaultManagedTaskScheduler();
7675
assertNoExecutorException(() -> scheduler.scheduleWithFixedDelay(
77-
NO_OP, Duration.of(1, ChronoUnit.MINUTES)));
76+
NO_OP, Duration.ofMinutes(1)));
7877
}
7978

8079
private void assertNoExecutorException(ThrowingCallable callable) {

spring-test/src/main/java/org/springframework/test/http/HttpHeadersAssert.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.time.Instant;
2020
import java.time.ZoneId;
21+
import java.time.ZoneOffset;
2122
import java.time.ZonedDateTime;
2223
import java.time.temporal.ChronoUnit;
2324
import java.util.Arrays;
@@ -42,7 +43,7 @@
4243
*/
4344
public class HttpHeadersAssert extends AbstractObjectAssert<HttpHeadersAssert, HttpHeaders> {
4445

45-
private static final ZoneId GMT = ZoneId.of("GMT");
46+
private static final ZoneId GMT = ZoneOffset.UTC;
4647

4748

4849
private final AbstractCollectionAssert<?, Collection<? extends String>, String, ObjectAssert<String>> namesAssert;
@@ -173,7 +174,7 @@ public HttpHeadersAssert hasValue(String name, Instant value) {
173174
containsHeader(name);
174175
Assertions.assertThat(this.actual.getFirstZonedDateTime(name))
175176
.as("check primary date value for HTTP header '%s'", name)
176-
.isCloseTo(ZonedDateTime.ofInstant(value, GMT), Assertions.within(999, ChronoUnit.MILLIS));
177+
.isCloseTo(value.atZone(GMT), Assertions.within(999, ChronoUnit.MILLIS));
177178
return this.myself;
178179
}
179180

spring-test/src/test/java/org/springframework/test/web/servlet/client/RestTestClientTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818

1919
import java.net.URI;
2020
import java.nio.charset.StandardCharsets;
21-
import java.time.ZoneId;
21+
import java.time.ZoneOffset;
2222
import java.time.ZonedDateTime;
2323
import java.util.Map;
2424

@@ -263,7 +263,7 @@ void testAcceptCharset() {
263263
@Test
264264
void testIfModifiedSince() {
265265
RestTestClientTests.this.client.get().uri("/test")
266-
.ifModifiedSince(ZonedDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneId.of("GMT")))
266+
.ifModifiedSince(ZonedDateTime.of(1970, 1, 1, 0, 0, 0, 0, ZoneOffset.UTC))
267267
.exchange()
268268
.expectStatus().isOk()
269269
.expectBody().jsonPath("$.headers.If-Modified-Since").isEqualTo("Thu, 01 Jan 1970 00:00:00 GMT");

spring-test/src/test/java/org/springframework/test/web/servlet/result/HeaderResultMatchersTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
package org.springframework.test.web.servlet.result;
1818

19-
import java.time.ZoneId;
19+
import java.time.ZoneOffset;
2020
import java.time.ZonedDateTime;
2121

2222
import org.junit.jupiter.api.Test;
@@ -45,7 +45,7 @@ public class HeaderResultMatchersTests {
4545
@Test // SPR-17330
4646
public void matchDateFormattedWithHttpHeaders() throws Exception {
4747

48-
long epochMilli = ZonedDateTime.of(2018, 10, 5, 0, 0, 0, 0, ZoneId.of("GMT")).toInstant().toEpochMilli();
48+
long epochMilli = ZonedDateTime.of(2018, 10, 5, 0, 0, 0, 0, ZoneOffset.UTC).toInstant().toEpochMilli();
4949
HttpHeaders headers = new HttpHeaders();
5050
headers.setDate("myDate", epochMilli);
5151
this.response.setHeader("d", headers.getFirst("myDate"));

0 commit comments

Comments
 (0)