Skip to content

Commit 39507e5

Browse files
authored
Joda time to Java time: Add few more templates for migration (#619)
1 parent 3b3f34f commit 39507e5

File tree

7 files changed

+158
-4
lines changed

7 files changed

+158
-4
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ public class AllTemplates {
3434
private static final MethodMatcher ANY_TIME_FORMATTER = new MethodMatcher(JODA_TIME_FORMATTER + " *(..)");
3535
private static final MethodMatcher ANY_NEW_DURATION = new MethodMatcher(JODA_DURATION + "<constructor>(..)");
3636
private static final MethodMatcher ANY_DURATION = new MethodMatcher(JODA_DURATION + " *(..)");
37+
private static final MethodMatcher ANY_BASE_DURATION = new MethodMatcher(JODA_BASE_DURATION + " *(..)");
3738
private static final MethodMatcher ANY_ABSTRACT_INSTANT = new MethodMatcher(JODA_ABSTRACT_INSTANT + " *(..)");
3839
private static final MethodMatcher ANY_ABSTRACT_DATE_TIME = new MethodMatcher(JODA_ABSTRACT_DATE_TIME + " *(..)");
3940
private static final MethodMatcher ANY_ABSTRACT_DURATION = new MethodMatcher(JODA_ABSTRACT_DURATION + " *(..)");
4041
private static final MethodMatcher ANY_INSTANT = new MethodMatcher(JODA_INSTANT + " *(..)");
4142
private static final MethodMatcher ANY_NEW_INSTANT = new MethodMatcher(JODA_INSTANT + "<constructor>(..)");
4243
private static final MethodMatcher ANY_NEW_INTERVAL = new MethodMatcher(JODA_INTERVAL + "<constructor>(..)");
4344
private static final MethodMatcher ANY_ABSTRACT_INTERVAL = new MethodMatcher(JODA_ABSTRACT_INTERVAL + " *(..)");
45+
private static final MethodMatcher ANY_BASE_INTERVAL = new MethodMatcher(JODA_BASE_INTERVAL + " *(..)");
4446

4547
private static List<MatcherAndTemplates> templates = new ArrayList<MatcherAndTemplates>() {
4648
{
@@ -54,11 +56,13 @@ public class AllTemplates {
5456
add(new MatcherAndTemplates(ANY_DATE_TIME, new DateTimeTemplates()));
5557
add(new MatcherAndTemplates(ANY_NEW_DURATION, new DurationTemplates()));
5658
add(new MatcherAndTemplates(ANY_DURATION, new DurationTemplates()));
59+
add(new MatcherAndTemplates(ANY_BASE_DURATION, new BaseDurationTemplates()));
5760
add(new MatcherAndTemplates(ANY_DATE_TIMEZONE, new TimeZoneTemplates()));
5861
add(new MatcherAndTemplates(ANY_INSTANT, new InstantTemplates()));
5962
add(new MatcherAndTemplates(ANY_NEW_INSTANT, new InstantTemplates()));
6063
add(new MatcherAndTemplates(ANY_NEW_INTERVAL, new IntervalTemplates()));
6164
add(new MatcherAndTemplates(ANY_ABSTRACT_INTERVAL, new AbstractIntervalTemplates()));
65+
add(new MatcherAndTemplates(ANY_BASE_INTERVAL, new BaseIntervalTemplates()));
6266
}
6367
};
6468

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.templates;
17+
18+
import lombok.Getter;
19+
import org.openrewrite.java.JavaTemplate;
20+
import org.openrewrite.java.MethodMatcher;
21+
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
25+
import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JAVA_DURATION;
26+
import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JODA_BASE_DURATION;
27+
28+
public class BaseDurationTemplates implements Templates {
29+
private final MethodMatcher getMillis = new MethodMatcher(JODA_BASE_DURATION + " getMillis()");
30+
31+
private final JavaTemplate getMillisTemplate = JavaTemplate.builder("#{any(" + JAVA_DURATION + ")}.toMillis()").build();
32+
33+
@Getter
34+
private final List<MethodTemplate> templates = new ArrayList<MethodTemplate>() {
35+
{
36+
add(new MethodTemplate(getMillis, getMillisTemplate));
37+
}
38+
};
39+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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.templates;
17+
18+
import lombok.Getter;
19+
import org.openrewrite.java.JavaParser;
20+
import org.openrewrite.java.JavaTemplate;
21+
import org.openrewrite.java.MethodMatcher;
22+
23+
import java.util.ArrayList;
24+
import java.util.List;
25+
26+
import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JODA_BASE_INTERVAL;
27+
import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.THREE_TEN_EXTRA_INTERVAL;
28+
29+
public class BaseIntervalTemplates implements Templates {
30+
private final MethodMatcher getStartMillis = new MethodMatcher(JODA_BASE_INTERVAL + " getStartMillis()");
31+
private final MethodMatcher getEndMillis = new MethodMatcher(JODA_BASE_INTERVAL + " getEndMillis()");
32+
33+
private final JavaTemplate getStartMillisTemplate = JavaTemplate.builder("#{any(" + THREE_TEN_EXTRA_INTERVAL + ")}.getStart().toEpochMilli()")
34+
.javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra"))
35+
.build();
36+
private final JavaTemplate getEndMillisTemplate = JavaTemplate.builder("#{any(" + THREE_TEN_EXTRA_INTERVAL + ")}.getEnd().toEpochMilli()")
37+
.javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra"))
38+
.build();
39+
40+
@Getter
41+
private final List<MethodTemplate> templates = new ArrayList<MethodTemplate>() {
42+
{
43+
add(new MethodTemplate(getStartMillis, getStartMillisTemplate));
44+
add(new MethodTemplate(getEndMillis, getEndMillisTemplate));
45+
}
46+
};
47+
}

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"))
36+
.javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra"))
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"))
40+
.javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra"))
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"))
44+
.javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra"))
4545
.imports(THREE_TEN_EXTRA_INTERVAL)
4646
.build();
4747

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,12 @@ public class TimeClassNames {
3939
public static final String JODA_DURATION_FIELD_TYPE = JODA_TIME_PKG + ".DurationFieldType";
4040
public static final String JODA_DURATION = JODA_TIME_PKG + ".Duration";
4141
public static final String JODA_READABLE_DURATION = JODA_TIME_PKG + ".ReadableDuration";
42+
public static final String JODA_BASE_DURATION = JODA_TIME_PKG + ".base.BaseDuration";
4243
public static final String JODA_ABSTRACT_INSTANT = JODA_TIME_PKG + ".base.AbstractInstant";
4344
public static final String JODA_READABLE_INSTANT = JODA_TIME_PKG + ".ReadableInstant";
4445
public static final String JODA_INSTANT = JODA_TIME_PKG + ".Instant";
4546
public static final String JODA_INTERVAL = JODA_TIME_PKG + ".Interval";
47+
public static final String JODA_BASE_INTERVAL = JODA_TIME_PKG + ".base.BaseInterval";
4648

4749
// Java Time classes
4850
public static final String JAVA_TIME_PKG = "java.time";

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public static Optional<JavaTemplate> getTemplate(J.VariableDeclarations variable
6868
}
6969
return Optional.of(JavaTemplate.builder(template.toString())
7070
.imports(typeName)
71-
.javaParser(JavaParser.fromJavaVersion().classpath("threeten"))
71+
.javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra"))
7272
.build());
7373
}
7474

src/test/java/org/openrewrite/java/migrate/joda/JodaTimeVisitorTest.java

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,4 +872,66 @@ public void foo() {
872872
)
873873
);
874874
}
875+
876+
@Test
877+
void migrateBaseDuration() {
878+
// language=java
879+
rewriteRun(
880+
java(
881+
"""
882+
import org.joda.time.Duration;
883+
884+
class A {
885+
public void foo() {
886+
Duration d = new Duration(100);
887+
d.getMillis();
888+
}
889+
}
890+
""",
891+
"""
892+
import java.time.Duration;
893+
894+
class A {
895+
public void foo() {
896+
Duration d = Duration.ofMillis(100);
897+
d.toMillis();
898+
}
899+
}
900+
"""
901+
)
902+
);
903+
}
904+
905+
@Test
906+
void migrateBaseInterval() {
907+
// language=java
908+
rewriteRun(
909+
java(
910+
"""
911+
import org.joda.time.Interval;
912+
913+
class A {
914+
public void foo() {
915+
Interval i = new Interval(50, 100);
916+
long s = i.getStartMillis();
917+
long e = i.getEndMillis();
918+
}
919+
}
920+
""",
921+
"""
922+
import org.threeten.extra.Interval;
923+
924+
import java.time.Instant;
925+
926+
class A {
927+
public void foo() {
928+
Interval i = Interval.of(Instant.ofEpochMilli(50), Instant.ofEpochMilli(100));
929+
long s = i.getStart().toEpochMilli();
930+
long e = i.getEnd().toEpochMilli();
931+
}
932+
}
933+
"""
934+
)
935+
);
936+
}
875937
}

0 commit comments

Comments
 (0)