Skip to content

Commit fa33321

Browse files
committed
Add threeten-extra dependency, if needed
1 parent 9cf2fe6 commit fa33321

File tree

6 files changed

+157
-8
lines changed

6 files changed

+157
-8
lines changed

src/main/java/org/openrewrite/java/migrate/joda/JodaTimeVisitor.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,11 @@ public Javadoc visitReference(Javadoc.Reference reference, ExecutionContext ctx)
170170
if (!isJodaVarRef(ident)) {
171171
return super.visitIdentifier(ident, ctx);
172172
}
173-
Optional<NamedVariable> mayBeVar = findVarInScope(ident.getSimpleName());
174-
if (!mayBeVar.isPresent() || acc.getUnsafeVars().contains(mayBeVar.get())) {
175-
return ident;
173+
if (this.safeMigration) {
174+
Optional<NamedVariable> mayBeVar = findVarInScope(ident.getSimpleName());
175+
if (!mayBeVar.isPresent() || acc.getUnsafeVars().contains(mayBeVar.get())) {
176+
return ident;
177+
}
176178
}
177179

178180
JavaType.FullyQualified jodaType = ((JavaType.Class) ident.getType());

src/main/java/org/openrewrite/java/migrate/joda/templates/IntervalTemplates.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,15 @@ public class IntervalTemplates implements Templates {
3333
private final MethodMatcher intervalWithDateTimeAndDuration = new MethodMatcher(JODA_INTERVAL + " <constructor>(" + JODA_READABLE_INSTANT + ", " + JODA_READABLE_DURATION + ")");
3434

3535
private final JavaTemplate intervalTemplate = JavaTemplate.builder("Interval.of(Instant.ofEpochMilli(#{any(long)}), Instant.ofEpochMilli(#{any(long)}))")
36-
.javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra"))
36+
.javaParser(JavaParser.fromJavaVersion().classpath("threeten"))
3737
.imports(JAVA_INSTANT, THREE_TEN_EXTRA_INTERVAL)
3838
.build();
3939
private final JavaTemplate intervalWithDateTimeTemplate = JavaTemplate.builder("Interval.of(#{any(" + JAVA_DATE_TIME + ")}.toInstant(), #{any(" + JAVA_DATE_TIME + ")}.toInstant())")
40-
.javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra"))
40+
.javaParser(JavaParser.fromJavaVersion().classpath("threeten"))
4141
.imports(THREE_TEN_EXTRA_INTERVAL)
4242
.build();
4343
private final JavaTemplate intervalWithDateTimeAndDurationTemplate = JavaTemplate.builder("Interval.of(#{any(" + JAVA_DATE_TIME + ")}.toInstant(), #{any(" + JAVA_DURATION + ")})")
44-
.javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra"))
44+
.javaParser(JavaParser.fromJavaVersion().classpath("threeten"))
4545
.imports(THREE_TEN_EXTRA_INTERVAL)
4646
.build();
4747

src/main/java/org/openrewrite/java/migrate/joda/templates/TimeClassMap.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ public class TimeClassMap {
3535
put(JODA_TIME_FORMATTER, javaTypeClass(JAVA_TIME_FORMATTER, object));
3636
put(JODA_DURATION, javaTypeClass(JAVA_DURATION, object));
3737
put(JODA_READABLE_DURATION, javaTypeClass(JAVA_DURATION, object));
38+
put(JODA_INTERVAL, javaTypeClass(THREE_TEN_EXTRA_INTERVAL, object));
3839
}
3940
};
4041

src/main/java/org/openrewrite/java/migrate/joda/templates/VarTemplates.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
*/
1616
package org.openrewrite.java.migrate.joda.templates;
1717

18+
import org.openrewrite.java.JavaParser;
1819
import org.openrewrite.java.JavaTemplate;
1920
import org.openrewrite.java.tree.J;
2021
import org.openrewrite.java.tree.JavaType;
@@ -66,8 +67,9 @@ public static Optional<JavaTemplate> getTemplate(J.VariableDeclarations variable
6667
}
6768
}
6869
return Optional.of(JavaTemplate.builder(template.toString())
69-
.imports(typeName)
70-
.build());
70+
.imports(typeName)
71+
.javaParser(JavaParser.fromJavaVersion().classpath("threeten"))
72+
.build());
7173
}
7274

7375
public static Optional<JavaTemplate> getTemplate(J.Assignment assignment) {
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#
2+
# Copyright 2021 the original author or authors.
3+
# <p>
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
# <p>
8+
# https://www.apache.org/licenses/LICENSE-2.0
9+
# <p>
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
#
16+
---
17+
type: specs.openrewrite.org/v1beta/recipe
18+
name: org.openrewrite.java.migrate.joda.NoJodaTime
19+
displayName: Prefer the Java standard library instead of JodaTime
20+
description: >-
21+
Before Java 8, Java lacked a robust date and time library, leading to the widespread use of Joda-Time to fill this
22+
gap. With the release of Java 8, the java.time package was introduced, incorporating most of Joda-Time's concepts.
23+
Features deemed too specialized or bulky for java.time were included in the ThreeTen-Extra library. This recipe
24+
migrates JodaTime types to java.time and threeten-extra types.
25+
tags:
26+
- joda-time
27+
recipeList:
28+
- org.openrewrite.java.migrate.joda.JodaTimeRecipe
29+
- org.openrewrite.java.dependencies.AddDependency:
30+
groupId: org.threeten
31+
artifactId: threeten-extra
32+
version: 1.8.0
33+
onlyIfUsing: org.joda.time.*Interval*
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/*
2+
* Copyright 2024 the original author or authors.
3+
* <p>
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
* <p>
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.openrewrite.java.migrate.joda;
17+
18+
import org.junit.jupiter.api.Test;
19+
import org.openrewrite.java.JavaParser;
20+
import org.openrewrite.test.RecipeSpec;
21+
import org.openrewrite.test.RewriteTest;
22+
23+
import static org.openrewrite.java.Assertions.*;
24+
import static org.openrewrite.maven.Assertions.pomXml;
25+
26+
public class NoJodaTimeTest implements RewriteTest {
27+
28+
@Override
29+
public void defaults(RecipeSpec spec) {
30+
spec
31+
.recipeFromResource("/META-INF/rewrite/no-joda-time.yml", "org.openrewrite.java.migrate.joda.NoJodaTime")
32+
.parser(JavaParser.fromJavaVersion().classpath("joda-time", "threeten-extra"));
33+
}
34+
@Test
35+
void migrateJodaTime() {
36+
rewriteRun(
37+
mavenProject("foo",
38+
srcMainJava(
39+
// language=java
40+
java(
41+
"""
42+
import org.joda.time.DateTime;
43+
import org.joda.time.Interval;
44+
45+
public class A {
46+
public void foo() {
47+
DateTime dt = new DateTime();
48+
DateTime dt1 = new DateTime().plusDays(1);
49+
Interval i = new Interval(dt, dt1);
50+
System.out.println(i.toDuration());
51+
}
52+
}
53+
""",
54+
"""
55+
import org.threeten.extra.Interval;
56+
57+
import java.time.ZonedDateTime;
58+
59+
public class A {
60+
public void foo() {
61+
ZonedDateTime dt = ZonedDateTime.now();
62+
ZonedDateTime dt1 = ZonedDateTime.now().plusDays(1);
63+
Interval i = Interval.of(dt.toInstant(), dt1.toInstant());
64+
System.out.println(i.toDuration());
65+
}
66+
}
67+
"""
68+
),
69+
//language=xml
70+
pomXml(
71+
"""
72+
<project>
73+
<modelVersion>4.0.0</modelVersion>
74+
<groupId>com.example.foobar</groupId>
75+
<artifactId>foobar-core</artifactId>
76+
<version>1.0.0</version>
77+
<dependencies>
78+
<dependency>
79+
<groupId>joda-time</groupId>
80+
<artifactId>joda-time</artifactId>
81+
<version>2.12.3</version>
82+
</dependency>
83+
</dependencies>
84+
</project>
85+
""",
86+
"""
87+
<project>
88+
<modelVersion>4.0.0</modelVersion>
89+
<groupId>com.example.foobar</groupId>
90+
<artifactId>foobar-core</artifactId>
91+
<version>1.0.0</version>
92+
<dependencies>
93+
<dependency>
94+
<groupId>joda-time</groupId>
95+
<artifactId>joda-time</artifactId>
96+
<version>2.12.3</version>
97+
</dependency>
98+
<dependency>
99+
<groupId>org.threeten</groupId>
100+
<artifactId>threeten-extra</artifactId>
101+
<version>1.8.0</version>
102+
</dependency>
103+
</dependencies>
104+
</project>
105+
"""
106+
)
107+
)
108+
)
109+
);
110+
}
111+
}

0 commit comments

Comments
 (0)