diff --git a/build.gradle.kts b/build.gradle.kts index 0da963c0cd..d8b5f48297 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -61,8 +61,6 @@ dependencies { testImplementation("org.assertj:assertj-core:latest.release") testImplementation("com.google.errorprone:error_prone_annotations:latest.release") - testImplementation("joda-time:joda-time:2.12.3") - testImplementation("org.threeten:threeten-extra:1.8.0") testRuntimeOnly("com.fasterxml.jackson.datatype:jackson-datatype-jsr353") testRuntimeOnly("com.fasterxml.jackson.core:jackson-core") diff --git a/src/main/java/org/openrewrite/java/migrate/joda/JodaTimeFlowSpec.java b/src/main/java/org/openrewrite/java/migrate/joda/JodaTimeFlowSpec.java deleted file mode 100644 index ffeb680545..0000000000 --- a/src/main/java/org/openrewrite/java/migrate/joda/JodaTimeFlowSpec.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Copyright 2024 the original author or authors. - *
- * Licensed under the Moderne Source Available License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *
- * https://docs.moderne.io/licensing/moderne-source-available-license - *
- * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.openrewrite.java.migrate.joda; - -import lombok.NonNull; -import org.openrewrite.analysis.dataflow.DataFlowNode; -import org.openrewrite.analysis.dataflow.DataFlowSpec; -import org.openrewrite.java.tree.J; -import org.openrewrite.java.tree.JavaType; - -import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JODA_CLASS_PATTERN; - -class JodaTimeFlowSpec extends DataFlowSpec { - - @Override - public boolean isSource(@NonNull DataFlowNode srcNode) { - Object value = srcNode.getCursor().getParentTreeCursor().getValue(); - - if (value instanceof J.Assignment && ((J.Assignment) value).getVariable() instanceof J.Identifier) { - return isJodaType(((J.Assignment) value).getVariable().getType()); - } - - if (value instanceof J.VariableDeclarations.NamedVariable) { - return isJodaType(((J.VariableDeclarations.NamedVariable) value).getType()); - } - - if (value instanceof J.VariableDeclarations) { - if (srcNode.getCursor().getParentTreeCursor().getParentTreeCursor().getValue() instanceof J.MethodDeclaration) { - return isJodaType(((J.VariableDeclarations) value).getType()); - } - } - return false; - } - - @Override - public boolean isSink(@NonNull DataFlowNode sinkNode) { - Object value = sinkNode.getCursor().getValue(); - Object parent = sinkNode.getCursor().getParentTreeCursor().getValue(); - if (parent instanceof J.MethodInvocation) { - J.MethodInvocation method = (J.MethodInvocation) parent; - return (method.getSelect() != null && method.getSelect().equals(value)) || - method.getArguments().stream().anyMatch(a -> a.equals(value)); - } - return parent instanceof J.VariableDeclarations.NamedVariable || - parent instanceof J.NewClass || - parent instanceof J.Assignment || - parent instanceof J.Return; - } - - static boolean isJodaType(JavaType type) { - if (!(type instanceof JavaType.Class)) { - return false; - } - return type.isAssignableFrom(JODA_CLASS_PATTERN); - } -} diff --git a/src/main/java/org/openrewrite/java/migrate/joda/JodaTimeRecipe.java b/src/main/java/org/openrewrite/java/migrate/joda/JodaTimeRecipe.java deleted file mode 100644 index 7bb2571067..0000000000 --- a/src/main/java/org/openrewrite/java/migrate/joda/JodaTimeRecipe.java +++ /dev/null @@ -1,89 +0,0 @@ -/* - * Copyright 2024 the original author or authors. - *
- * Licensed under the Moderne Source Available License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - *
- * https://docs.moderne.io/licensing/moderne-source-available-license - *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda;
-
-import lombok.Getter;
-import org.jspecify.annotations.Nullable;
-import org.openrewrite.ExecutionContext;
-import org.openrewrite.Preconditions;
-import org.openrewrite.ScanningRecipe;
-import org.openrewrite.TreeVisitor;
-import org.openrewrite.java.search.UsesType;
-import org.openrewrite.java.tree.J;
-import org.openrewrite.java.tree.J.VariableDeclarations.NamedVariable;
-import org.openrewrite.java.tree.JavaType;
-
-import java.util.*;
-
-import static java.util.Collections.emptyList;
-
-public class JodaTimeRecipe extends ScanningRecipe
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda;
-
-import fj.data.Option;
-import lombok.Getter;
-import lombok.NonNull;
-import lombok.RequiredArgsConstructor;
-import lombok.Value;
-import org.jspecify.annotations.Nullable;
-import org.openrewrite.Cursor;
-import org.openrewrite.ExecutionContext;
-import org.openrewrite.analysis.dataflow.Dataflow;
-import org.openrewrite.analysis.dataflow.analysis.SinkFlowSummary;
-import org.openrewrite.java.JavaIsoVisitor;
-import org.openrewrite.java.JavadocVisitor;
-import org.openrewrite.java.tree.*;
-import org.openrewrite.java.tree.J.VariableDeclarations.NamedVariable;
-
-import java.util.*;
-import java.util.concurrent.atomic.AtomicBoolean;
-
-import static java.util.Collections.emptyList;
-import static java.util.Collections.emptySet;
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JODA_CLASS_PATTERN;
-
-class JodaTimeScanner extends ScopeAwareVisitor {
-
- @Getter
- private final JodaTimeRecipe.Accumulator acc;
-
- private final Map
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda;
-
-import lombok.NonNull;
-import org.jspecify.annotations.Nullable;
-import org.openrewrite.ExecutionContext;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.JavadocVisitor;
-import org.openrewrite.java.migrate.joda.templates.AllTemplates;
-import org.openrewrite.java.migrate.joda.templates.MethodTemplate;
-import org.openrewrite.java.migrate.joda.templates.TimeClassMap;
-import org.openrewrite.java.migrate.joda.templates.VarTemplates;
-import org.openrewrite.java.tree.*;
-import org.openrewrite.java.tree.J.VariableDeclarations.NamedVariable;
-
-import java.util.LinkedList;
-import java.util.List;
-import java.util.Optional;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.*;
-
-class JodaTimeVisitor extends ScopeAwareVisitor {
-
- private final boolean safeMigration;
- private final JodaTimeRecipe.Accumulator acc;
-
- public JodaTimeVisitor(JodaTimeRecipe.Accumulator acc, boolean safeMigration, LinkedList
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda;
-
-import lombok.EqualsAndHashCode;
-import lombok.Value;
-import lombok.With;
-import org.openrewrite.java.tree.J.VariableDeclarations.NamedVariable;
-import org.openrewrite.marker.Marker;
-
-import java.util.HashSet;
-import java.util.Set;
-import java.util.UUID;
-
-/**
- * A marker to indicate whether an expression is safe to migrate
- * and variables that are referenced in the expression.
- */
-@Value
-@With
-public class SafeCheckMarker implements Marker {
- @EqualsAndHashCode.Include
- UUID id;
- boolean isSafe;
- Set
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda;
-
-import lombok.AllArgsConstructor;
-import lombok.Value;
-import org.openrewrite.Cursor;
-import org.openrewrite.ExecutionContext;
-import org.openrewrite.java.JavaVisitor;
-import org.openrewrite.java.tree.J;
-import org.openrewrite.java.tree.J.VariableDeclarations.NamedVariable;
-
-import java.util.HashSet;
-import java.util.LinkedList;
-import java.util.Optional;
-import java.util.Set;
-
-@AllArgsConstructor
-class ScopeAwareVisitor extends JavaVisitor
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-@NullMarked
-@NonNullFields
-package org.openrewrite.java.migrate.joda;
-
-import org.jspecify.annotations.NullMarked;
-import org.openrewrite.internal.lang.NonNullFields;
diff --git a/src/main/java/org/openrewrite/java/migrate/joda/templates/AbstractDateTimeTemplates.java b/src/main/java/org/openrewrite/java/migrate/joda/templates/AbstractDateTimeTemplates.java
deleted file mode 100644
index f561541776..0000000000
--- a/src/main/java/org/openrewrite/java/migrate/joda/templates/AbstractDateTimeTemplates.java
+++ /dev/null
@@ -1,69 +0,0 @@
-/*
- * Copyright 2024 the original author or authors.
- *
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import lombok.Getter;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.MethodMatcher;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JAVA_DATE_TIME;
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JODA_ABSTRACT_DATE_TIME;
-
-public class AbstractDateTimeTemplates implements Templates {
- private final MethodMatcher getDayOfMonth = new MethodMatcher(JODA_ABSTRACT_DATE_TIME + " getDayOfMonth()");
- private final MethodMatcher getDayOfWeek = new MethodMatcher(JODA_ABSTRACT_DATE_TIME + " getDayOfWeek()");
- private final MethodMatcher getHourOfDay = new MethodMatcher(JODA_ABSTRACT_DATE_TIME + " getHourOfDay()");
- private final MethodMatcher getMillisOfSecond = new MethodMatcher(JODA_ABSTRACT_DATE_TIME + " getMillisOfSecond()");
- private final MethodMatcher getMinuteOfDay = new MethodMatcher(JODA_ABSTRACT_DATE_TIME + " getMinuteOfDay()");
- private final MethodMatcher getMinuteOfHour = new MethodMatcher(JODA_ABSTRACT_DATE_TIME + " getMinuteOfHour()");
- private final MethodMatcher getMonthOfYear = new MethodMatcher(JODA_ABSTRACT_DATE_TIME + " getMonthOfYear()");
- private final MethodMatcher getSecondOfDay = new MethodMatcher(JODA_ABSTRACT_DATE_TIME + " getSecondOfDay()");
- private final MethodMatcher getSecondOfMinute = new MethodMatcher(JODA_ABSTRACT_DATE_TIME + " getSecondOfMinute()");
- private final MethodMatcher getWeekOfWeekyear = new MethodMatcher(JODA_ABSTRACT_DATE_TIME + " getWeekOfWeekyear()");
- private final MethodMatcher toString = new MethodMatcher(JODA_ABSTRACT_DATE_TIME + " toString()");
-
- private final JavaTemplate getDayOfMonthTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.getDayOfMonth()").build();
- private final JavaTemplate getDayOfWeekTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.getDayOfWeek().getValue()").build();
- private final JavaTemplate getHourOfDayTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.getHour()").build();
- private final JavaTemplate getMillisOfSecondTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.get(ChronoField.MILLI_OF_SECOND)").imports("java.time.temporal.ChronoField").build();
- private final JavaTemplate getMinuteOfDayTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.get(ChronoField.MINUTE_OF_DAY)").imports("java.time.temporal.ChronoField").build();
- private final JavaTemplate getMinuteOfHourTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.getMinute()").build();
- private final JavaTemplate getMonthOfYearTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.getMonthValue()").build();
- private final JavaTemplate getSecondOfDayTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.get(ChronoField.SECOND_OF_DAY)").imports("java.time.temporal.ChronoField").build();
- private final JavaTemplate getSecondOfMinuteTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.getSecond()").build();
- private final JavaTemplate getWeekOfWeekyearTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.get(ChronoField.ALIGNED_WEEK_OF_YEAR)").imports("java.time.temporal.ChronoField").build();
- private final JavaTemplate toStringTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.toString()").build();
-
- @Getter
- private final List
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import lombok.Getter;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.MethodMatcher;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JAVA_DURATION;
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JODA_DURATION;
-
-public class AbstractDurationTemplates implements Templates {
- private final MethodMatcher isLongerThan = new MethodMatcher(JODA_DURATION + " isLongerThan(..)");
- private final MethodMatcher toPeriod = new MethodMatcher(JODA_DURATION + " toPeriod()");
-
- private final JavaTemplate isLongerThanTemplate = JavaTemplate.builder("#{any(" + JAVA_DURATION + ")}.compareTo(#{any(" + JAVA_DURATION + ")}) > 0").build();
- private final JavaTemplate toPeriodTemplate = JavaTemplate.builder("#{any(" + JAVA_DURATION + ")}.toPeriod()").build();
-
- @Getter
- private final List
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import lombok.Getter;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.MethodMatcher;
-import org.openrewrite.java.tree.Expression;
-import org.openrewrite.java.tree.J;
-import org.openrewrite.java.tree.MethodCall;
-
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
-import java.util.regex.Pattern;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.*;
-
-public class AbstractInstantTemplates implements Templates {
- private final MethodMatcher equals = new MethodMatcher(JODA_ABSTRACT_INSTANT + " equals(java.lang.Object)");
- private final MethodMatcher getZone = new MethodMatcher(JODA_ABSTRACT_INSTANT + " getZone()");
- private final MethodMatcher isAfterLong = new MethodMatcher(JODA_ABSTRACT_INSTANT + " isAfter(long)");
- private final MethodMatcher isAfter = new MethodMatcher(JODA_ABSTRACT_INSTANT + " isAfter(org.joda.time.ReadableInstant)");
- private final MethodMatcher isBeforeLong = new MethodMatcher(JODA_ABSTRACT_INSTANT + " isBefore(long)");
- private final MethodMatcher isBefore = new MethodMatcher(JODA_ABSTRACT_INSTANT + " isBefore(org.joda.time.ReadableInstant)");
- private final MethodMatcher isBeforeNow = new MethodMatcher(JODA_ABSTRACT_INSTANT + " isBeforeNow()");
- private final MethodMatcher isEqualLong = new MethodMatcher(JODA_ABSTRACT_INSTANT + " isEqual(long)");
- private final MethodMatcher isEqualReadableInstant = new MethodMatcher(JODA_ABSTRACT_INSTANT + " isEqual(org.joda.time.ReadableInstant)");
- private final MethodMatcher toDate = new MethodMatcher(JODA_ABSTRACT_INSTANT + " toDate()");
- private final MethodMatcher toInstant = new MethodMatcher(JODA_ABSTRACT_INSTANT + " toInstant()");
- private final MethodMatcher toString = new MethodMatcher(JODA_ABSTRACT_INSTANT + " toString()");
- private final MethodMatcher toStringFormatter = new MethodMatcher(JODA_ABSTRACT_INSTANT + " toString(org.joda.time.format.DateTimeFormatter)");
-
- private final JavaTemplate equalsTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.equals(#{any(java.lang.Object)})").build();
- private final JavaTemplate getZoneTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.getZone()").build();
- private final JavaTemplate isAfterLongTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.isAfter(Instant.ofEpochMilli(#{any(long)}).atZone(ZoneId.systemDefault()))")
- .imports(JAVA_INSTANT, JAVA_ZONE_ID).build();
- private final JavaTemplate isAfterLongTemplateWithInstant = JavaTemplate.builder("#{any(" + JAVA_INSTANT + ")}.isAfter(Instant.ofEpochMilli(#{any(long)}))")
- .imports(JAVA_INSTANT).build();
- private final JavaTemplate isAfterTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.isAfter(#{any(" + JAVA_DATE_TIME + ")})").build();
- private final JavaTemplate isAfterTemplateWithInstant = JavaTemplate.builder("#{any(" + JAVA_INSTANT + ")}.isAfter(#{any(" + JAVA_INSTANT + ")})").build();
- private final JavaTemplate isBeforeLongTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.isBefore(Instant.ofEpochMilli(#{any(long)}).atZone(ZoneId.systemDefault()))")
- .imports(JAVA_INSTANT, JAVA_ZONE_ID).build();
- private final JavaTemplate isBeforeLongTemplateWithInstant = JavaTemplate.builder("#{any(" + JAVA_INSTANT + ")}.isBefore(Instant.ofEpochMilli(#{any(long)}))")
- .imports(JAVA_INSTANT).build();
- private final JavaTemplate isBeforTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.isBefore(#{any(" + JAVA_DATE_TIME + ")})").build();
- private final JavaTemplate isBeforeTemplateWithInstant = JavaTemplate.builder("#{any(" + JAVA_INSTANT + ")}.isBefore(#{any(" + JAVA_INSTANT + ")})").build();
- private final JavaTemplate isBeforeNowTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.isBefore(ZonedDateTime.now())")
- .imports(JAVA_DATE_TIME).build();
- private final JavaTemplate isEqualLongTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.isEqual(Instant.ofEpochMilli(#{any(long)}).atZone(ZoneId.systemDefault()))")
- .imports(JAVA_INSTANT, JAVA_ZONE_ID).build();
- private final JavaTemplate isEqualReadableInstantTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.isEqual(#{any(" + JAVA_DATE_TIME + ")})").build();
- private final JavaTemplate toDateTemplate = JavaTemplate.builder("Date.from(#{any(" + JAVA_DATE_TIME + ")}.toInstant())")
- .imports(JAVA_UTIL_DATE)
- .build();
- private final JavaTemplate toInstantTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.toInstant()").build();
- private final JavaTemplate toStringTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.toString()").build();
- private final JavaTemplate toStringFormatterTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.format(#{any(" + JAVA_TIME_FORMATTER + ")})").build();
-
- @Getter
- private final List
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import lombok.Getter;
-import org.openrewrite.java.JavaParser;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.MethodMatcher;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.*;
-
-public class AbstractIntervalTemplates implements Templates {
- private final MethodMatcher getStart = new MethodMatcher(JODA_ABSTRACT_INTERVAL + " getStart()");
- private final MethodMatcher getEnd = new MethodMatcher(JODA_ABSTRACT_INTERVAL + " getEnd()");
- private final MethodMatcher toDuration = new MethodMatcher(JODA_ABSTRACT_INTERVAL + " toDuration()");
- private final MethodMatcher toDurationMillis = new MethodMatcher(JODA_ABSTRACT_INTERVAL + " toDurationMillis()");
- private final MethodMatcher contains = new MethodMatcher(JODA_ABSTRACT_INTERVAL + " contains(long)");
-
- private final JavaTemplate getStartTemplate = JavaTemplate.builder("#{any(" + THREE_TEN_EXTRA_INTERVAL + ")}.getStart().atZone(ZoneId.systemDefault())")
- .javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra"))
- .imports(JAVA_ZONE_ID)
- .build();
- private final JavaTemplate getEndTemplate = JavaTemplate.builder("#{any(" + THREE_TEN_EXTRA_INTERVAL + ")}.getEnd().atZone(ZoneId.systemDefault())")
- .javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra"))
- .imports(JAVA_ZONE_ID)
- .build();
- private final JavaTemplate toDurationTemplate = JavaTemplate.builder("#{any(" + THREE_TEN_EXTRA_INTERVAL + ")}.toDuration()")
- .javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra"))
- .build();
- private final JavaTemplate toDurationMillisTemplate = JavaTemplate.builder("#{any(" + THREE_TEN_EXTRA_INTERVAL + ")}.toDuration().toMillis()")
- .javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra"))
- .build();
- private final JavaTemplate containsTemplate = JavaTemplate.builder("#{any(" + THREE_TEN_EXTRA_INTERVAL + ")}.contains(Instant.ofEpochMilli(#{any(long)}))")
- .javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra"))
- .imports(JAVA_INSTANT)
- .build();
-
- @Getter
- private final List
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import lombok.Value;
-import org.openrewrite.java.MethodMatcher;
-import org.openrewrite.java.tree.MethodCall;
-
-import java.util.ArrayList;
-import java.util.List;
-import java.util.Optional;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.*;
-
-public class AllTemplates {
- private static final MethodMatcher ANY_BASE_DATETIME = new MethodMatcher(JODA_BASE_DATE_TIME + " *(..)");
- private static final MethodMatcher ANY_NEW_DATE_TIME = new MethodMatcher(JODA_DATE_TIME + "
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import lombok.Getter;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.MethodMatcher;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JODA_BASE_DATE_TIME;
-
-public class BaseDateTime implements Templates {
- private final MethodMatcher getMillis = new MethodMatcher(JODA_BASE_DATE_TIME + " getMillis()");
-
- private final JavaTemplate getMillisTemplate = JavaTemplate.builder("#{any(java.time.ZonedDateTime)}.toInstant().toEpochMilli()")
- .build();
-
- @Getter
- private final List
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import lombok.Getter;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.MethodMatcher;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JAVA_DURATION;
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JODA_BASE_DURATION;
-
-public class BaseDurationTemplates implements Templates {
- private final MethodMatcher getMillis = new MethodMatcher(JODA_BASE_DURATION + " getMillis()");
-
- private final JavaTemplate getMillisTemplate = JavaTemplate.builder("#{any(" + JAVA_DURATION + ")}.toMillis()").build();
-
- @Getter
- private final List
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import lombok.Getter;
-import org.openrewrite.java.JavaParser;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.MethodMatcher;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JODA_BASE_INTERVAL;
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.THREE_TEN_EXTRA_INTERVAL;
-
-public class BaseIntervalTemplates implements Templates {
- private final MethodMatcher getStartMillis = new MethodMatcher(JODA_BASE_INTERVAL + " getStartMillis()");
- private final MethodMatcher getEndMillis = new MethodMatcher(JODA_BASE_INTERVAL + " getEndMillis()");
-
- private final JavaTemplate getStartMillisTemplate = JavaTemplate.builder("#{any(" + THREE_TEN_EXTRA_INTERVAL + ")}.getStart().toEpochMilli()")
- .javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra"))
- .build();
- private final JavaTemplate getEndMillisTemplate = JavaTemplate.builder("#{any(" + THREE_TEN_EXTRA_INTERVAL + ")}.getEnd().toEpochMilli()")
- .javaParser(JavaParser.fromJavaVersion().classpath("threeten-extra"))
- .build();
-
- @Getter
- private final List
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import lombok.Getter;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.MethodMatcher;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.*;
-
-public class DateTimeFormatTemplates implements Templates {
- private final MethodMatcher forPattern = new MethodMatcher(JODA_TIME_FORMAT + " forPattern(String)");
- private final MethodMatcher shortDate = new MethodMatcher(JODA_TIME_FORMAT + " shortDate()");
- private final MethodMatcher mediumDate = new MethodMatcher(JODA_TIME_FORMAT + " mediumDate()");
- private final MethodMatcher longDate = new MethodMatcher(JODA_TIME_FORMAT + " longDate()");
- private final MethodMatcher fullDate = new MethodMatcher(JODA_TIME_FORMAT + " fullDate()");
- private final MethodMatcher shortTime = new MethodMatcher(JODA_TIME_FORMAT + " shortTime()");
- private final MethodMatcher mediumTime = new MethodMatcher(JODA_TIME_FORMAT + " mediumTime()");
- private final MethodMatcher longTime = new MethodMatcher(JODA_TIME_FORMAT + " longTime()");
- private final MethodMatcher fullTime = new MethodMatcher(JODA_TIME_FORMAT + " fullTime()");
- private final MethodMatcher shortDateTime = new MethodMatcher(JODA_TIME_FORMAT + " shortDateTime()");
- private final MethodMatcher mediumDateTime = new MethodMatcher(JODA_TIME_FORMAT + " mediumDateTime()");
- private final MethodMatcher longDateTime = new MethodMatcher(JODA_TIME_FORMAT + " longDateTime()");
- private final MethodMatcher fullDateTime = new MethodMatcher(JODA_TIME_FORMAT + " fullDateTime()");
-
- private final JavaTemplate ofPatternTemplate = JavaTemplate.builder("DateTimeFormatter.ofPattern(#{any(String)})")
- .imports("java.time.format.DateTimeFormatter")
- .build();
- private final JavaTemplate shortDateTemplate = JavaTemplate.builder("DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)")
- .imports(JAVA_TIME_FORMATTER, JAVA_TIME_FORMAT_STYLE)
- .build();
- private final JavaTemplate mediumDateTemplate = JavaTemplate.builder("DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)")
- .imports(JAVA_TIME_FORMATTER, JAVA_TIME_FORMAT_STYLE)
- .build();
- private final JavaTemplate longDateTemplate = JavaTemplate.builder("DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG)")
- .imports(JAVA_TIME_FORMATTER, JAVA_TIME_FORMAT_STYLE)
- .build();
- private final JavaTemplate fullDateTemplate = JavaTemplate.builder("DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL)")
- .imports(JAVA_TIME_FORMATTER, JAVA_TIME_FORMAT_STYLE)
- .build();
- private final JavaTemplate shortTimeTemplate = JavaTemplate.builder("DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT)")
- .imports(JAVA_TIME_FORMATTER, JAVA_TIME_FORMAT_STYLE)
- .build();
- private final JavaTemplate mediumTimeTemplate = JavaTemplate.builder("DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM)")
- .imports(JAVA_TIME_FORMATTER, JAVA_TIME_FORMAT_STYLE)
- .build();
- private final JavaTemplate longTimeTemplate = JavaTemplate.builder("DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG)")
- .imports(JAVA_TIME_FORMATTER, JAVA_TIME_FORMAT_STYLE)
- .build();
- private final JavaTemplate fullTimeTemplate = JavaTemplate.builder("DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL)")
- .imports(JAVA_TIME_FORMATTER, JAVA_TIME_FORMAT_STYLE)
- .build();
- private final JavaTemplate shortDateTimeTemplate = JavaTemplate.builder("DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT)")
- .imports(JAVA_TIME_FORMATTER, JAVA_TIME_FORMAT_STYLE)
- .build();
- private final JavaTemplate mediumDateTimeTemplate = JavaTemplate.builder("DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.MEDIUM)")
- .imports(JAVA_TIME_FORMATTER, JAVA_TIME_FORMAT_STYLE)
- .build();
- private final JavaTemplate longDateTimeTemplate = JavaTemplate.builder("DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.LONG)")
- .imports(JAVA_TIME_FORMATTER, JAVA_TIME_FORMAT_STYLE)
- .build();
- private final JavaTemplate fullDateTimeTemplate = JavaTemplate.builder("DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.FULL)")
- .imports(JAVA_TIME_FORMATTER, JAVA_TIME_FORMAT_STYLE)
- .build();
-
- @Getter
- private final List
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import lombok.Getter;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.MethodMatcher;
-import org.openrewrite.java.tree.Expression;
-import org.openrewrite.java.tree.J;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.*;
-
-public class DateTimeFormatterTemplates implements Templates {
- private final MethodMatcher parseDateTime = new MethodMatcher(JODA_TIME_FORMATTER + " parseDateTime(java.lang.String)");
- private final MethodMatcher parseMillis = new MethodMatcher(JODA_TIME_FORMATTER + " parseMillis(java.lang.String)");
- private final MethodMatcher printLong = new MethodMatcher(JODA_TIME_FORMATTER + " print(long)");
- private final MethodMatcher printDateTime = new MethodMatcher(JODA_TIME_FORMATTER + " print(org.joda.time.ReadableInstant)");
- private final MethodMatcher withZone = new MethodMatcher(JODA_TIME_FORMATTER + " withZone(org.joda.time.DateTimeZone)");
- private final MethodMatcher withZoneUTC = new MethodMatcher(JODA_TIME_FORMATTER + " withZoneUTC()");
-
- private final JavaTemplate parseDateTimeTemplate = JavaTemplate.builder("ZonedDateTime.parse(#{any(java.lang.String)}, #{any(" + JAVA_TIME_FORMATTER + ")})")
- .imports(JAVA_DATE_TIME).build();
- private final JavaTemplate parseMillisTemplate = JavaTemplate.builder("ZonedDateTime.parse(#{any(java.lang.String)}, #{any(" + JAVA_TIME_FORMATTER + ")}).toInstant().toEpochMilli()")
- .imports(JAVA_DATE_TIME).build();
- private final JavaTemplate printLongTemplate = JavaTemplate.builder("ZonedDateTime.ofInstant(Instant.ofEpochMilli(#{any(long)}), ZoneId.systemDefault()).format( #{any(" + JAVA_TIME_FORMATTER + ")})")
- .imports(JAVA_DATE_TIME, JAVA_INSTANT, JAVA_ZONE_ID)
- .build();
- private final JavaTemplate printDateTimeTemplate = JavaTemplate.builder("#{any(" + JAVA_DATE_TIME + ")}.format(#{any(" + JAVA_TIME_FORMATTER + ")})").build();
- private final JavaTemplate withZoneTemplate = JavaTemplate.builder("#{any(" + JAVA_TIME_FORMATTER + ")}.withZone(#{any(" + JAVA_ZONE_ID + ")})").build();
- private final JavaTemplate withZoneUTCTemplate = JavaTemplate.builder("#{any(" + JAVA_TIME_FORMATTER + ")}.withZone(ZoneOffset.UTC)")
- .imports(JAVA_ZONE_OFFSET).build();
-
- @Getter
- private final List
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import lombok.Getter;
-import lombok.NoArgsConstructor;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.MethodMatcher;
-import org.openrewrite.java.tree.Expression;
-import org.openrewrite.java.tree.J;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.*;
-
-@NoArgsConstructor
-public class DateTimeTemplates implements Templates {
- private final MethodMatcher newDateTime = new MethodMatcher(JODA_DATE_TIME + "
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import lombok.Getter;
-import lombok.NoArgsConstructor;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.MethodMatcher;
-import org.openrewrite.java.tree.Expression;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.*;
-
-@NoArgsConstructor
-public class DurationTemplates implements Templates {
- private final MethodMatcher parse = new MethodMatcher(JODA_DURATION + " parse(String)");
- private final MethodMatcher standardDays = new MethodMatcher(JODA_DURATION + " standardDays(long)");
- private final MethodMatcher standardHours = new MethodMatcher(JODA_DURATION + " standardHours(long)");
- private final MethodMatcher standardMinutes = new MethodMatcher(JODA_DURATION + " standardMinutes(long)");
- private final MethodMatcher standardSeconds = new MethodMatcher(JODA_DURATION + " standardSeconds(long)");
- private final MethodMatcher millis = new MethodMatcher(JODA_DURATION + " millis(long)");
-
- private final MethodMatcher newDuration = new MethodMatcher(JODA_DURATION + "
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import lombok.Getter;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.MethodMatcher;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JAVA_INSTANT;
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.JODA_INSTANT;
-
-public class InstantTemplates implements Templates {
- private final MethodMatcher constructor = new MethodMatcher(JODA_INSTANT + "
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import lombok.Getter;
-import org.openrewrite.java.JavaParser;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.MethodMatcher;
-import org.openrewrite.java.tree.Expression;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.*;
-
-public class IntervalTemplates implements Templates {
- private final MethodMatcher interval = new MethodMatcher(JODA_INTERVAL + "
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import lombok.Value;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.MethodMatcher;
-import org.openrewrite.java.tree.Expression;
-import org.openrewrite.java.tree.J;
-import org.openrewrite.java.tree.MethodCall;
-
-import java.util.function.Function;
-
-@Value
-public class MethodTemplate {
- MethodMatcher matcher;
- JavaTemplate template;
- Function
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import org.openrewrite.java.tree.MethodCall;
-
-import java.util.List;
-
-public interface Templates {
- List
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import org.jspecify.annotations.Nullable;
-import org.openrewrite.java.tree.JavaType;
-
-import java.util.HashMap;
-import java.util.Map;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.*;
-
-public class TimeClassMap {
-
- private static final JavaType.Class object = JavaType.ShallowClass.build("java.lang.Object");
-
- private final Map
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import java.util.regex.Pattern;
-
-public class TimeClassNames {
- public static final Pattern JODA_CLASS_PATTERN = Pattern.compile("org\\.joda\\.time\\..*");
-
- // java util
- public static final String JAVA_UTIL_DATE = "java.util.Date";
-
- // Joda-Time classes
- public static final String JODA_TIME_PKG = "org.joda.time";
- public static final String JODA_ABSTRACT_DATE_TIME = JODA_TIME_PKG + ".base.AbstractDateTime";
- public static final String JODA_ABSTRACT_DURATION = JODA_TIME_PKG + ".base.AbstractDuration";
- public static final String JODA_ABSTRACT_INTERVAL = JODA_TIME_PKG + ".base.AbstractInterval";
- public static final String JODA_BASE_DATE_TIME = JODA_TIME_PKG + ".base.BaseDateTime";
- public static final String JODA_DATE_TIME = JODA_TIME_PKG + ".DateTime";
- public static final String JODA_DATE_TIME_ZONE = JODA_TIME_PKG + ".DateTimeZone";
- public static final String JODA_TIME_FORMAT = JODA_TIME_PKG + ".format.DateTimeFormat";
- public static final String JODA_TIME_FORMATTER = JODA_TIME_PKG + ".format.DateTimeFormatter";
- public static final String JODA_LOCAL_DATE = JODA_TIME_PKG + ".LocalDate";
- public static final String JODA_LOCAL_TIME = JODA_TIME_PKG + ".LocalTime";
- public static final String JODA_DATE_TIME_FIELD_TYPE = JODA_TIME_PKG + ".DateTimeFieldType";
- public static final String JODA_DURATION_FIELD_TYPE = JODA_TIME_PKG + ".DurationFieldType";
- public static final String JODA_DURATION = JODA_TIME_PKG + ".Duration";
- public static final String JODA_READABLE_DURATION = JODA_TIME_PKG + ".ReadableDuration";
- public static final String JODA_BASE_DURATION = JODA_TIME_PKG + ".base.BaseDuration";
- public static final String JODA_ABSTRACT_INSTANT = JODA_TIME_PKG + ".base.AbstractInstant";
- public static final String JODA_READABLE_INSTANT = JODA_TIME_PKG + ".ReadableInstant";
- public static final String JODA_INSTANT = JODA_TIME_PKG + ".Instant";
- public static final String JODA_INTERVAL = JODA_TIME_PKG + ".Interval";
- public static final String JODA_BASE_INTERVAL = JODA_TIME_PKG + ".base.BaseInterval";
-
- // Java Time classes
- public static final String JAVA_TIME_PKG = "java.time";
- public static final String JAVA_DATE_TIME = JAVA_TIME_PKG + ".ZonedDateTime";
- public static final String JAVA_DURATION = JAVA_TIME_PKG + ".Duration";
- public static final String JAVA_ZONE_OFFSET = JAVA_TIME_PKG + ".ZoneOffset";
- public static final String JAVA_ZONE_ID = JAVA_TIME_PKG + ".ZoneId";
- public static final String JAVA_INSTANT = JAVA_TIME_PKG + ".Instant";
- public static final String JAVA_TIME_FORMATTER = JAVA_TIME_PKG + ".format.DateTimeFormatter";
- public static final String JAVA_TIME_FORMAT_STYLE = JAVA_TIME_PKG + ".format.FormatStyle";
- public static final String JAVA_TEMPORAL_ADJUSTER = JAVA_TIME_PKG + ".temporal.TemporalAdjuster";
- public static final String JAVA_LOCAL_DATE = JAVA_TIME_PKG + ".LocalDate";
- public static final String JAVA_LOCAL_TIME = JAVA_TIME_PKG + ".LocalTime";
- public static final String JAVA_TEMPORAL_ISO_FIELDS = JAVA_TIME_PKG + ".temporal.IsoFields";
- public static final String JAVA_CHRONO_FIELD = JAVA_TIME_PKG + ".temporal.ChronoField";
-
- // ThreeTen-Extra classes
- public static final String THREE_TEN_EXTRA_PKG = "org.threeten.extra";
- public static final String THREE_TEN_EXTRA_INTERVAL = THREE_TEN_EXTRA_PKG + ".Interval";
-}
diff --git a/src/main/java/org/openrewrite/java/migrate/joda/templates/TimeZoneTemplates.java b/src/main/java/org/openrewrite/java/migrate/joda/templates/TimeZoneTemplates.java
deleted file mode 100644
index b5ba559e66..0000000000
--- a/src/main/java/org/openrewrite/java/migrate/joda/templates/TimeZoneTemplates.java
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Copyright 2024 the original author or authors.
- *
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import lombok.Getter;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.MethodMatcher;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.*;
-
-public class TimeZoneTemplates implements Templates {
- private final MethodMatcher zoneForID = new MethodMatcher(JODA_DATE_TIME_ZONE + " forID(String)");
- private final MethodMatcher zoneForOffsetHours = new MethodMatcher(JODA_DATE_TIME_ZONE + " forOffsetHours(int)");
- private final MethodMatcher zoneForOffsetHoursMinutes = new MethodMatcher(JODA_DATE_TIME_ZONE + " forOffsetHoursMinutes(int, int)");
- private final MethodMatcher zoneForTimeZone = new MethodMatcher(JODA_DATE_TIME_ZONE + " forTimeZone(java.util.TimeZone)");
-
- private final JavaTemplate zoneIdOfTemplate = JavaTemplate.builder("ZoneId.of(#{any(String)})")
- .imports(JAVA_ZONE_ID)
- .build();
- private final JavaTemplate zoneOffsetHoursTemplate = JavaTemplate.builder("ZoneOffset.ofHours(#{any(int)})")
- .imports(JAVA_ZONE_OFFSET)
- .build();
- private final JavaTemplate zoneOffsetHoursMinutesTemplate = JavaTemplate.builder("ZoneOffset.ofHoursMinutes(#{any(int)}, #{any(int)})")
- .imports(JAVA_ZONE_OFFSET)
- .build();
- private final JavaTemplate timeZoneToZoneIdTemplate = JavaTemplate.builder("#{any(java.util.TimeZone)}.toZoneId()")
- .build();
-
- @Getter
- private final List
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda.templates;
-
-import org.openrewrite.java.JavaParser;
-import org.openrewrite.java.JavaTemplate;
-import org.openrewrite.java.tree.J;
-import org.openrewrite.java.tree.JavaType;
-
-import java.util.HashMap;
-import java.util.Map;
-import java.util.Optional;
-
-import static org.openrewrite.java.migrate.joda.templates.TimeClassNames.*;
-
-public class VarTemplates {
-
- private static Map
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-@NullMarked
-@NonNullFields
-package org.openrewrite.java.migrate.joda.templates;
-
-import org.jspecify.annotations.NullMarked;
-import org.openrewrite.internal.lang.NonNullFields;
diff --git a/src/main/resources/META-INF/rewrite/examples.yml b/src/main/resources/META-INF/rewrite/examples.yml
index d85099aadb..af428868b7 100644
--- a/src/main/resources/META-INF/rewrite/examples.yml
+++ b/src/main/resources/META-INF/rewrite/examples.yml
@@ -5828,100 +5828,6 @@ examples:
language: java
---
type: specs.openrewrite.org/v1beta/example
-recipeName: org.openrewrite.java.migrate.joda.JodaTimeRecipe
-examples:
-- description: ''
- sources:
- - before: |
- import org.joda.time.DateTime;
-
- class A {
- public void foo() {
- DateTime dt = new DateTime();
- System.out.println(dt.toDateTime());
- }
- }
- after: |
- import java.time.ZonedDateTime;
-
- class A {
- public void foo() {
- ZonedDateTime dt = ZonedDateTime.now();
- System.out.println(dt);
- }
- }
- language: java
----
-type: specs.openrewrite.org/v1beta/example
-recipeName: org.openrewrite.java.migrate.joda.NoJodaTime
-examples:
-- description: ''
- sources:
- - before: |
- import org.joda.time.DateTime;
- import org.joda.time.Interval;
-
- class A {
- void foo() {
- DateTime dt = new DateTime();
- DateTime dt1 = new DateTime().plusDays(1);
- Interval i = new Interval(dt, dt1);
- System.out.println(i.toDuration());
- }
- }
- after: |
- import org.threeten.extra.Interval;
-
- import java.time.ZonedDateTime;
-
- class A {
- void foo() {
- ZonedDateTime dt = ZonedDateTime.now();
- ZonedDateTime dt1 = ZonedDateTime.now().plusDays(1);
- Interval i = Interval.of(dt.toInstant(), dt1.toInstant());
- System.out.println(i.toDuration());
- }
- }
- language: java
- - before: foo
- language: mavenProject
- - before: |
-
-# Licensed under the Moderne Source Available License (the "License");
-# you may not use this file except in compliance with the License.
-# You may obtain a copy of the License at
-#
-# https://docs.moderne.io/licensing/moderne-source-available-license
-#
-# Unless required by applicable law or agreed to in writing, software
-# distributed under the License is distributed on an "AS IS" BASIS,
-# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-# See the License for the specific language governing permissions and
-# limitations under the License.
-#
-
----
-type: specs.openrewrite.org/v1beta/recipe
-name: org.openrewrite.java.migrate.joda.NoJodaTime
-displayName: Prefer the Java standard library instead of Joda-Time
-description: >-
- Before Java 8, Java lacked a robust date and time library, leading to the widespread use of Joda-Time to fill this
- gap. With the release of Java 8, the `java.time` package was introduced, incorporating most of Joda-Time's concepts.
- Features deemed too specialized or bulky for `java.time` were included in the ThreeTen-Extra library. This recipe
- migrates Joda-Time types to `java.time` and `threeten-extra` types.
-tags:
- - joda-time
-recipeList:
- - org.openrewrite.java.dependencies.AddDependency:
- groupId: org.threeten
- artifactId: threeten-extra
- version: 1.8.0
- onlyIfUsing: org.joda.time.*Interval*
- - org.openrewrite.java.migrate.joda.JodaTimeRecipe
diff --git a/src/test/java/org/openrewrite/java/migrate/joda/JodaTimeFlowSpecTest.java b/src/test/java/org/openrewrite/java/migrate/joda/JodaTimeFlowSpecTest.java
deleted file mode 100644
index 66949495d1..0000000000
--- a/src/test/java/org/openrewrite/java/migrate/joda/JodaTimeFlowSpecTest.java
+++ /dev/null
@@ -1,146 +0,0 @@
-/*
- * Copyright 2024 the original author or authors.
- *
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda;
-
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.parallel.Execution;
-import org.junit.jupiter.api.parallel.ExecutionMode;
-import org.openrewrite.Cursor;
-import org.openrewrite.DocumentExample;
-import org.openrewrite.ExecutionContext;
-import org.openrewrite.analysis.dataflow.Dataflow;
-import org.openrewrite.java.JavaIsoVisitor;
-import org.openrewrite.java.JavaParser;
-import org.openrewrite.java.tree.Expression;
-import org.openrewrite.java.tree.J;
-import org.openrewrite.marker.SearchResult;
-import org.openrewrite.test.RecipeSpec;
-import org.openrewrite.test.RewriteTest;
-
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import static java.util.stream.Collectors.joining;
-import static org.openrewrite.java.Assertions.java;
-import static org.openrewrite.test.RewriteTest.toRecipe;
-
-@Execution(ExecutionMode.SAME_THREAD)
-class JodaTimeFlowSpecTest implements RewriteTest {
- @Override
- public void defaults(RecipeSpec spec) {
- spec
- .recipe(toRecipe(() -> new JavaIsoVisitor<>() {
- Map
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda;
-
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.parallel.Execution;
-import org.junit.jupiter.api.parallel.ExecutionMode;
-import org.openrewrite.DocumentExample;
-import org.openrewrite.java.JavaParser;
-import org.openrewrite.test.RecipeSpec;
-import org.openrewrite.test.RewriteTest;
-
-import static org.openrewrite.java.Assertions.java;
-
-@Execution(ExecutionMode.SAME_THREAD)
-class JodaTimeRecipeTest implements RewriteTest {
- @Override
- public void defaults(RecipeSpec spec) {
- spec
- .recipe(new JodaTimeRecipe())
- .parser(JavaParser.fromJavaVersion().classpath("joda-time"));
- }
-
- @DocumentExample
- @Test
- void migrateSafeVariable() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
-
- class A {
- public void foo() {
- DateTime dt = new DateTime();
- System.out.println(dt.toDateTime());
- }
- }
- """,
- """
- import java.time.ZonedDateTime;
-
- class A {
- public void foo() {
- ZonedDateTime dt = ZonedDateTime.now();
- System.out.println(dt);
- }
- }
- """
- )
- );
- }
-
- @Test
- void dontChangeClassVariable() {
- // not supported yet
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
-
- class A {
- public void foo() {
- DateTime dt = new DateTime();
- System.out.println(dt.toDateTime());
- System.out.println(new B().dateTime.toDateTime());
- }
- public static class B {
- DateTime dateTime = new DateTime();
- }
- }
- """,
- """
- import org.joda.time.DateTime;
-
- import java.time.ZonedDateTime;
-
- class A {
- public void foo() {
- ZonedDateTime dt = ZonedDateTime.now();
- System.out.println(dt);
- System.out.println(new B().dateTime.toDateTime());
- }
- public static class B {
- DateTime dateTime = new DateTime();
- }
- }
- """
- )
- );
- }
-
- @Test
- void safeMethodParamMigrationAcrossClassBoundary() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
-
- class A {
- public void foo() {
- new B().print(new DateTime());
- System.out.println(new B().dateTime); // dateTime is class variable, not handled yet
- }
- }
-
- class B {
- DateTime dateTime = new DateTime();
- public void print(DateTime dateTime) {
- System.out.println(dateTime);
- }
- }
- """,
- """
- import org.joda.time.DateTime;
-
- import java.time.ZonedDateTime;
-
- class A {
- public void foo() {
- new B().print(ZonedDateTime.now());
- System.out.println(new B().dateTime); // dateTime is class variable, not handled yet
- }
- }
-
- class B {
- DateTime dateTime = new DateTime();
- public void print(ZonedDateTime dateTime) {
- System.out.println(dateTime);
- }
- }
- """
- )
- );
- }
-
- @Test
- void noUnsafeVar() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
- import org.joda.time.DateTimeZone;
- import java.util.Date;
-
- class A {
- public void foo(String city) {
- DateTimeZone dtz;
- if ("london".equals(city)) {
- dtz = DateTimeZone.forID("Europe/London");
- } else {
- dtz = DateTimeZone.forID("America/New_York");
- }
- DateTime dt = new DateTime(dtz);
- print(dt.toDate());
- }
- private void print(Date date) {
- System.out.println(date);
- }
- }
- """,
- """
- import java.time.ZoneId;
- import java.time.ZonedDateTime;
- import java.util.Date;
-
- class A {
- public void foo(String city) {
- ZoneId dtz;
- if ("london".equals(city)) {
- dtz = ZoneId.of("Europe/London");
- } else {
- dtz = ZoneId.of("America/New_York");
- }
- ZonedDateTime dt = ZonedDateTime.now(dtz);
- print(Date.from(dt.toInstant()));
- }
- private void print(Date date) {
- System.out.println(date);
- }
- }
- """
- )
- );
- }
-
- @Test
- void localVarUsedReferencedInReturnStatement() {
- // language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
- import org.joda.time.DateTimeZone;
-
- class A {
- public DateTime foo(String city) {
- DateTimeZone dtz;
- if ("london".equals(city)) {
- dtz = DateTimeZone.forID("Europe/London");
- } else {
- dtz = DateTimeZone.forID("America/New_York");
- }
- DateTime dt = new DateTime(dtz);
- return dt.plus(2);
- }
- }
- """,
- """
- import java.time.Duration;
- import java.time.ZoneId;
- import java.time.ZonedDateTime;
-
- class A {
- public ZonedDateTime foo(String city) {
- ZoneId dtz;
- if ("london".equals(city)) {
- dtz = ZoneId.of("Europe/London");
- } else {
- dtz = ZoneId.of("America/New_York");
- }
- ZonedDateTime dt = ZonedDateTime.now(dtz);
- return dt.plus(Duration.ofMillis(2));
- }
- }
- """
- )
- );
- }
-
- @Test
- void migrateSafeMethodParam() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
-
- class A {
- public void foo() {
- new Bar().bar(new DateTime());
- }
-
- private static class Bar {
- public void bar(DateTime dt) {
- dt.getMillis();
- }
- }
- }
- """,
- """
- import java.time.ZonedDateTime;
-
- class A {
- public void foo() {
- new Bar().bar(ZonedDateTime.now());
- }
-
- private static class Bar {
- public void bar(ZonedDateTime dt) {
- dt.toInstant().toEpochMilli();
- }
- }
- }
- """
- )
- );
- }
-
- @Test
- void migrationWithRecord() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
-
- record A(String x) {
- public void foo() {
- new Bar().bar(new DateTime());
- }
-
- private static class Bar {
- public void bar(DateTime dt) {
- dt.getMillis();
- }
- }
- }
- """,
- """
- import java.time.ZonedDateTime;
-
- record A(String x) {
- public void foo() {
- new Bar().bar(ZonedDateTime.now());
- }
-
- private static class Bar {
- public void bar(ZonedDateTime dt) {
- dt.toInstant().toEpochMilli();
- }
- }
- }
- """
- )
- );
- }
-
- @Test
- void migrateMethodWithSafeReturnExpression() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
- import org.joda.time.Interval;
-
- class A {
- public DateTime foo(DateTime dt) {
- Interval interval = new Interval(dt, dt.plusDays(1));
- return interval.getEnd();
- }
-
- private static class Bar {
- public void bar(DateTime dt) {
- DateTime d = foo(dt);
- System.out.println(d.getMillis());
- }
- }
- }
- """,
- """
- import org.threeten.extra.Interval;
-
- import java.time.ZoneId;
- import java.time.ZonedDateTime;
-
- class A {
- public ZonedDateTime foo(ZonedDateTime dt) {
- Interval interval = Interval.of(dt.toInstant(), dt.plusDays(1).toInstant());
- return interval.getEnd().atZone(ZoneId.systemDefault());
- }
-
- private static class Bar {
- public void bar(ZonedDateTime dt) {
- ZonedDateTime d = foo(dt);
- System.out.println(d.toInstant().toEpochMilli());
- }
- }
- }
- """
- )
- );
- }
-
- @Test
- void migrateMethodWithSafeReturnExpressionAndUnsafeParam() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
- import org.joda.time.Interval;
-
- class A {
- public DateTime foo(DateTime dt) {
- DateTime d = dt.toDateMidnight();
- DateTime d2 = DateTime.now();
- Interval interval = new Interval(d2, d2.plusDays(1));
- return interval.getEnd();
- }
-
- private static class Bar {
- public void bar() {
- DateTime d = foo(new DateTime());
- System.out.println(d.getMillis());
- }
- }
- }
- """,
- """
- import org.joda.time.DateTime;
- import org.threeten.extra.Interval;
-
- import java.time.ZoneId;
- import java.time.ZonedDateTime;
-
- class A {
- public ZonedDateTime foo(DateTime dt) {
- DateTime d = dt.toDateMidnight();
- ZonedDateTime d2 = ZonedDateTime.now();
- Interval interval = Interval.of(d2.toInstant(), d2.plusDays(1).toInstant());
- return interval.getEnd().atZone(ZoneId.systemDefault());
- }
-
- private static class Bar {
- public void bar() {
- ZonedDateTime d = foo(new DateTime());
- System.out.println(d.toInstant().toEpochMilli());
- }
- }
- }
- """
- )
- );
- }
-
- @Test
- void doNotMigrateUnsafeMethodParam() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
-
- class A {
- public void foo() {
- new Bar().bar(new DateTime());
- }
-
- private static class Bar {
- public void bar(DateTime dt) {
- dt.toDateMidnight(); // template doesn't exist for toDateMidnight
- }
- }
- }
- """
- )
- );
- }
-
- @Test
- void dontMigrateMethodInvocationIfSelectExprIsNotMigrated() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.Interval;
-
- class A {
- private Query query = new Query();
- public void foo() {
- query.interval().getEndMillis();
- }
- static class Query {
- private Interval interval;
-
- public Interval interval() {
- return interval;
- }
- }
- }
- """
- )
- );
- }
-
- @Test
- void migrateWithMethodReferenceInComment() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
-
- class A {
- public void foo() {
- /**
- * some method reference in comment
- * {@link java.util.List#add(DateTime)}
- */
- System.out.println(new DateTime());
- }
- }
- """,
- """
- import java.time.ZonedDateTime;
-
- class A {
- public void foo() {
- /**
- * some method reference in comment
- * {@link java.util.List#add(DateTime)}
- */
- System.out.println(ZonedDateTime.now());
- }
- }
- """
- )
- );
- }
-}
diff --git a/src/test/java/org/openrewrite/java/migrate/joda/JodaTimeScannerTest.java b/src/test/java/org/openrewrite/java/migrate/joda/JodaTimeScannerTest.java
deleted file mode 100644
index dc5c926838..0000000000
--- a/src/test/java/org/openrewrite/java/migrate/joda/JodaTimeScannerTest.java
+++ /dev/null
@@ -1,427 +0,0 @@
-/*
- * Copyright 2024 the original author or authors.
- *
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda;
-
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.parallel.Execution;
-import org.junit.jupiter.api.parallel.ExecutionMode;
-import org.openrewrite.java.JavaParser;
-import org.openrewrite.java.tree.J;
-import org.openrewrite.test.RecipeSpec;
-import org.openrewrite.test.RewriteTest;
-
-import java.util.Map;
-
-import static org.assertj.core.api.Assertions.assertThat;
-import static org.openrewrite.java.Assertions.java;
-import static org.openrewrite.test.RewriteTest.toRecipe;
-
-@Execution(ExecutionMode.SAME_THREAD)
-class JodaTimeScannerTest implements RewriteTest {
- @Override
- public void defaults(RecipeSpec spec) {
- spec.parser(JavaParser.fromJavaVersion().classpath("joda-time"));
- }
-
- @Test
- void noUnsafeVar() {
- JodaTimeScanner scanner = new JodaTimeScanner(new JodaTimeRecipe.Accumulator());
- // language=java
- rewriteRun(
- spec -> spec.recipe(toRecipe(() -> scanner)),
- java(
- """
- import org.joda.time.DateTime;
- import org.joda.time.DateTimeZone;
- import java.util.Date;
-
- class A {
- public void foo(String city) {
- DateTimeZone dtz;
- if ("london".equals(city)) {
- dtz = DateTimeZone.forID("Europe/London");
- } else {
- dtz = DateTimeZone.forID("America/New_York");
- }
- DateTime dt = new DateTime(dtz);
- print(dt.toDate());
- }
- private void print(Date date) {
- System.out.println(date);
- }
- }
- """
- )
- );
- assertThat(scanner.getAcc().getUnsafeVars()).isEmpty();
- }
-
- @Test
- void hasUnsafeVars() {
- JodaTimeScanner scanner = new JodaTimeScanner(new JodaTimeRecipe.Accumulator());
- // language=java
- rewriteRun(
- spec -> spec.recipe(toRecipe(() -> scanner)),
- java(
- """
- import org.joda.time.DateTime;
- import org.joda.time.DateTimeZone;
-
- class A {
- DateTime dateTime; // class Variable not handled yet
- public void foo(String city) {
- DateTimeZone dtz;
- if ("london".equals(city)) {
- dtz = DateTimeZone.forID("Europe/London");
- } else {
- dtz = DateTimeZone.forID("America/New_York");
- }
- dateTime = new DateTime(dtz);
- print(dateTime.toDateTime());
- }
- private void print(DateTime dt) {
- System.out.println(dt);
- }
- }
- """
- )
- );
- // The variable 'dtz' is unsafe due to the class variable 'dateTime'.
- // The parameter 'dt' in the 'print' method is also unsafe because one of its method calls is unsafe.
- assertThat(scanner.getAcc().getUnsafeVars()).hasSize(3);
- for (J.VariableDeclarations.NamedVariable var : scanner.getAcc().getUnsafeVars()) {
- assertThat("dtz".equals(var.getSimpleName()) ||
- "dt".equals(var.getSimpleName()) ||
- "dateTime".equals(var.getSimpleName())).isTrue();
- }
- }
-
- @Test
- void localVarReferencingClassVar() { // not supported yet
- JodaTimeScanner scanner = new JodaTimeScanner(new JodaTimeRecipe.Accumulator());
- // language=java
- rewriteRun(
- spec -> spec.recipe(toRecipe(() -> scanner)),
- java(
- """
- import org.joda.time.DateTime;
- import org.joda.time.DateTimeZone;
-
- class A {
- DateTime dateTime;
- public void foo(String city) {
- DateTimeZone dtz;
- if ("london".equals(city)) {
- dtz = DateTimeZone.forID("Europe/London");
- } else {
- dtz = DateTimeZone.forID("America/New_York");
- }
- DateTime dt = dateTime.minus(2);
- System.out.println(dt);
- }
- }
- """
- )
- );
- // The local variable dt is unsafe due to class var datetime.
- assertThat(scanner.getAcc().getUnsafeVars()).hasSize(2);
- for (J.VariableDeclarations.NamedVariable var : scanner.getAcc().getUnsafeVars()) {
- assertThat("dateTime".equals(var.getSimpleName()) || "dt".equals(var.getSimpleName())).isTrue();
- }
- }
-
- @Test
- void localVarUsedReferencedInReturnStatement() {
- JodaTimeScanner scanner = new JodaTimeScanner(new JodaTimeRecipe.Accumulator());
- // language=java
- rewriteRun(
- spec -> spec.recipe(toRecipe(() -> scanner)),
- java(
- """
- import org.joda.time.DateTime;
- import org.joda.time.DateTimeZone;
-
- class A {
- public DateTime foo(String city) {
- DateTimeZone dtz;
- if ("london".equals(city)) {
- dtz = DateTimeZone.forID("Europe/London");
- } else {
- dtz = DateTimeZone.forID("America/New_York");
- }
- DateTime dt = new DateTime(dtz);
- return dt.plus(2);
- }
- }
- """
- )
- );
- assertThat(scanner.getAcc().getUnsafeVars()).isEmpty();
- }
-
- @Test
- void unsafeMethodParam() {
- JodaTimeScanner scanner = new JodaTimeScanner(new JodaTimeRecipe.Accumulator());
- // language=java
- rewriteRun(
- spec -> spec.recipe(toRecipe(() -> scanner)),
- java(
- """
- import org.joda.time.DateTime;
-
- class A {
- public void foo() {
- new Bar().bar(new DateTime());
- }
-
- private static class Bar {
- public void bar(DateTime dt) {
- dt.toDateMidnight();
- }
- }
- }
- """
- )
- );
- // The bar method parameter dt is unsafe because migration of toDateMidnight() is not yet implemented.
- assertThat(scanner.getAcc().getUnsafeVars()).hasSize(1);
- for (J.VariableDeclarations.NamedVariable var : scanner.getAcc().getUnsafeVars()) {
- assertThat(var.getSimpleName()).isEqualTo("dt");
- }
- }
-
- @Test
- void detectUnsafeVarsInInitializer() {
- JodaTimeScanner scanner = new JodaTimeScanner(new JodaTimeRecipe.Accumulator());
- // language=java
- rewriteRun(
- spec -> spec.recipe(toRecipe(() -> scanner)),
- java(
- """
- import org.joda.time.Period;
- import java.util.stream.Collectors;
- import java.util.stream.Stream;
- import java.util.List;
-
- class A {
- public Period period() {
- return new Period();
- }
-
- public void foo() {
- List
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda;
-
-import org.junit.jupiter.api.Test;
-import org.junit.jupiter.api.parallel.Execution;
-import org.junit.jupiter.api.parallel.ExecutionMode;
-import org.openrewrite.DocumentExample;
-import org.openrewrite.java.JavaParser;
-import org.openrewrite.test.RecipeSpec;
-import org.openrewrite.test.RewriteTest;
-
-import java.util.LinkedList;
-
-import static org.openrewrite.java.Assertions.java;
-import static org.openrewrite.test.RewriteTest.toRecipe;
-
-@Execution(ExecutionMode.SAME_THREAD)
-class JodaTimeVisitorTest implements RewriteTest {
- @Override
- public void defaults(RecipeSpec spec) {
- spec
- .recipe(toRecipe(() -> new JodaTimeVisitor(new JodaTimeRecipe.Accumulator(), true, new LinkedList<>())))
- .parser(JavaParser.fromJavaVersion().classpath("joda-time", "threeten-extra"));
- }
-
- @DocumentExample
- @Test
- void migrateNewDateTime() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
- import org.joda.time.DateTimeZone;
- import java.util.TimeZone;
-
- class A {
- public void foo() {
- System.out.println(new DateTime());
- System.out.println(new DateTime(DateTimeZone.UTC));
- System.out.println(new DateTime(1234567890L));
- System.out.println(new DateTime(1234567890L, DateTimeZone.forID("America/New_York")));
- System.out.println(new DateTime(2024, 9, 30, 12, 58));
- System.out.println(new DateTime(2024, 9, 30, 12, 58, DateTimeZone.forOffsetHours(2)));
- System.out.println(new DateTime(2024, 9, 30, 13, 3, 15));
- System.out.println(new DateTime(2024, 9, 30, 13, 3, 15, DateTimeZone.forOffsetHoursMinutes(5, 30)));
- System.out.println(new DateTime(2024, 9, 30, 13, 49, 15, 545));
- System.out.println(new DateTime(2024, 9, 30, 13, 49, 15, 545, DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/New_York"))));
- }
- }
- """,
- """
- import java.time.Instant;
- import java.time.ZoneId;
- import java.time.ZoneOffset;
- import java.time.ZonedDateTime;
- import java.util.TimeZone;
-
- class A {
- public void foo() {
- System.out.println(ZonedDateTime.now());
- System.out.println(ZonedDateTime.now(ZoneOffset.UTC));
- System.out.println(ZonedDateTime.ofInstant(Instant.ofEpochMilli(1234567890L), ZoneId.systemDefault()));
- System.out.println(ZonedDateTime.ofInstant(Instant.ofEpochMilli(1234567890L), ZoneId.of("America/New_York")));
- System.out.println(ZonedDateTime.of(2024, 9, 30, 12, 58, 0, 0, ZoneId.systemDefault()));
- System.out.println(ZonedDateTime.of(2024, 9, 30, 12, 58, 0, 0, ZoneOffset.ofHours(2)));
- System.out.println(ZonedDateTime.of(2024, 9, 30, 13, 3, 15, 0, ZoneId.systemDefault()));
- System.out.println(ZonedDateTime.of(2024, 9, 30, 13, 3, 15, 0, ZoneOffset.ofHoursMinutes(5, 30)));
- System.out.println(ZonedDateTime.of(2024, 9, 30, 13, 49, 15, 545 * 1_000_000, ZoneId.systemDefault()));
- System.out.println(ZonedDateTime.of(2024, 9, 30, 13, 49, 15, 545 * 1_000_000, TimeZone.getTimeZone("America/New_York").toZoneId()));
- }
- }
- """
- )
- );
- }
-
- @Test
- void migrateDateTimeStaticCalls() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
- import org.joda.time.DateTimeZone;
- import org.joda.time.format.DateTimeFormat;
- import java.util.TimeZone;
-
- class A {
- public void foo() {
- System.out.println(DateTime.now());
- System.out.println(DateTime.now(DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/New_York"))));
- System.out.println(DateTime.parse("2024-09-30T23:03:00.000Z"));
- System.out.println(DateTime.parse("2024-09-30T23:03:00.000Z", DateTimeFormat.shortDate()));
- }
- }
- """,
- """
- import java.time.ZonedDateTime;
- import java.time.format.DateTimeFormatter;
- import java.time.format.FormatStyle;
- import java.util.TimeZone;
-
- class A {
- public void foo() {
- System.out.println(ZonedDateTime.now());
- System.out.println(ZonedDateTime.now(TimeZone.getTimeZone("America/New_York").toZoneId()));
- System.out.println(ZonedDateTime.parse("2024-09-30T23:03:00.000Z"));
- System.out.println(ZonedDateTime.parse("2024-09-30T23:03:00.000Z", DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT)));
- }
- }
- """
- )
- );
- }
-
- @Test
- void migrateDateTimeInstanceCalls() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
- import org.joda.time.DateTimeZone;
- import org.joda.time.Duration;
- import java.util.TimeZone;
-
- class A {
- public void foo() {
- System.out.println(new DateTime().toDateTime());
- System.out.println(new DateTime().toDateTime(DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/New_York"))));
- System.out.println(new DateTime().withMillis(1234567890L));
- System.out.println(new DateTime().withZone(DateTimeZone.forID("America/New_York")));
- System.out.println(new DateTime().withZoneRetainFields(DateTimeZone.forID("America/New_York")));
- System.out.println(new DateTime().withEarlierOffsetAtOverlap());
- System.out.println(new DateTime().withLaterOffsetAtOverlap());
- System.out.println(new DateTime().withDate(2024, 9, 30));
- System.out.println(new DateTime().withTime(12, 58, 57, 550));
- System.out.println(new DateTime().withDurationAdded(1234567890L, 2));
- System.out.println(new DateTime().plus(1234567890L));
- System.out.println(new DateTime().plus(Duration.standardDays(1)));
- System.out.println(new DateTime().plusYears(1));
- System.out.println(new DateTime().plusMonths(1));
- System.out.println(new DateTime().plusWeeks(1));
- System.out.println(new DateTime().plusDays(1));
- System.out.println(new DateTime().plusHours(1));
- System.out.println(new DateTime().plusMinutes(1));
- System.out.println(new DateTime().plusSeconds(1));
- System.out.println(new DateTime().plusMillis(1));
- System.out.println(new DateTime().minus(1234567890L));
- System.out.println(new DateTime().minus(Duration.standardDays(1)));
- System.out.println(new DateTime().minusYears(1));
- System.out.println(new DateTime().minusMonths(1));
- System.out.println(new DateTime().minusWeeks(1));
- System.out.println(new DateTime().minusDays(1));
- System.out.println(new DateTime().minusHours(1));
- System.out.println(new DateTime().minusMinutes(1));
- System.out.println(new DateTime().minusSeconds(1));
- System.out.println(new DateTime().minusMillis(1));
- System.out.println(new DateTime().toLocalDateTime());
- System.out.println(new DateTime().toLocalDate());
- System.out.println(new DateTime().toLocalTime());
- System.out.println(new DateTime().withYear(2024));
- System.out.println(new DateTime().withWeekyear(2024));
- System.out.println(new DateTime().withMonthOfYear(9));
- System.out.println(new DateTime().withWeekOfWeekyear(39));
- System.out.println(new DateTime().withDayOfYear(273));
- System.out.println(new DateTime().withDayOfMonth(30));
- System.out.println(new DateTime().withDayOfWeek(1));
- System.out.println(new DateTime().withHourOfDay(12));
- System.out.println(new DateTime().withMinuteOfHour(58));
- System.out.println(new DateTime().withSecondOfMinute(57));
- System.out.println(new DateTime().withMillisOfSecond(550));
- System.out.println(new DateTime().withMillisOfDay(123456));
- System.out.println(new DateTime().withTimeAtStartOfDay());
- }
- }
- """,
- """
- import java.time.Duration;
- import java.time.Instant;
- import java.time.ZoneId;
- import java.time.ZonedDateTime;
- import java.time.temporal.ChronoField;
- import java.time.temporal.IsoFields;
- import java.util.TimeZone;
-
- class A {
- public void foo() {
- System.out.println(ZonedDateTime.now());
- System.out.println(ZonedDateTime.now().withZoneSameInstant(TimeZone.getTimeZone("America/New_York").toZoneId()));
- System.out.println(ZonedDateTime.ofInstant(Instant.ofEpochMilli(1234567890L), ZonedDateTime.now().getZone()));
- System.out.println(ZonedDateTime.now().withZoneSameInstant(ZoneId.of("America/New_York")));
- System.out.println(ZonedDateTime.now().withZoneSameLocal(ZoneId.of("America/New_York")));
- System.out.println(ZonedDateTime.now().withEarlierOffsetAtOverlap());
- System.out.println(ZonedDateTime.now().withLaterOffsetAtOverlap());
- System.out.println(ZonedDateTime.now().withYear(2024).withMonth(9).withDayOfMonth(30));
- System.out.println(ZonedDateTime.now().withHour(12).withMinute(58).withSecond(57).withNano(550 * 1_000_000));
- System.out.println(ZonedDateTime.now().plus(Duration.ofMillis(1234567890L).multipliedBy(2)));
- System.out.println(ZonedDateTime.now().plus(Duration.ofMillis(1234567890L)));
- System.out.println(ZonedDateTime.now().plus(Duration.ofDays(1)));
- System.out.println(ZonedDateTime.now().plusYears(1));
- System.out.println(ZonedDateTime.now().plusMonths(1));
- System.out.println(ZonedDateTime.now().plusWeeks(1));
- System.out.println(ZonedDateTime.now().plusDays(1));
- System.out.println(ZonedDateTime.now().plusHours(1));
- System.out.println(ZonedDateTime.now().plusMinutes(1));
- System.out.println(ZonedDateTime.now().plusSeconds(1));
- System.out.println(ZonedDateTime.now().plus(Duration.ofMillis(1)));
- System.out.println(ZonedDateTime.now().minus(Duration.ofMillis(1234567890L)));
- System.out.println(ZonedDateTime.now().minus(Duration.ofDays(1)));
- System.out.println(ZonedDateTime.now().minusYears(1));
- System.out.println(ZonedDateTime.now().minusMonths(1));
- System.out.println(ZonedDateTime.now().minusWeeks(1));
- System.out.println(ZonedDateTime.now().minusDays(1));
- System.out.println(ZonedDateTime.now().minusHours(1));
- System.out.println(ZonedDateTime.now().minusMinutes(1));
- System.out.println(ZonedDateTime.now().minusSeconds(1));
- System.out.println(ZonedDateTime.now().minus(Duration.ofMillis(1)));
- System.out.println(ZonedDateTime.now().toLocalDateTime());
- System.out.println(ZonedDateTime.now().toLocalDate());
- System.out.println(ZonedDateTime.now().toLocalTime());
- System.out.println(ZonedDateTime.now().withYear(2024));
- System.out.println(ZonedDateTime.now().with(IsoFields.WEEK_BASED_YEAR, 2024));
- System.out.println(ZonedDateTime.now().withMonth(9));
- System.out.println(ZonedDateTime.now().with(ChronoField.ALIGNED_WEEK_OF_YEAR, 39));
- System.out.println(ZonedDateTime.now().withDayOfYear(273));
- System.out.println(ZonedDateTime.now().withDayOfMonth(30));
- System.out.println(ZonedDateTime.now().with(ChronoField.DAY_OF_WEEK, 1));
- System.out.println(ZonedDateTime.now().withHour(12));
- System.out.println(ZonedDateTime.now().withMinute(58));
- System.out.println(ZonedDateTime.now().withSecond(57));
- System.out.println(ZonedDateTime.now().withNano(550 * 1_000_000));
- System.out.println(ZonedDateTime.now().with(ChronoField.MILLI_OF_DAY, 123456));
- System.out.println(ZonedDateTime.now().toLocalDate().atStartOfDay(ZonedDateTime.now().getZone()));
- }
- }
- """
- )
- );
- }
-
- @Test
- void migrateDateTimeZone() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTimeZone;
- import java.util.TimeZone;
-
- class A {
- public void foo() {
- System.out.println(DateTimeZone.UTC);
- System.out.println(DateTimeZone.forID("America/New_York"));
- System.out.println(DateTimeZone.forOffsetHours(2));
- System.out.println(DateTimeZone.forOffsetHoursMinutes(5, 30));
- System.out.println(DateTimeZone.forTimeZone(TimeZone.getTimeZone("America/New_York")));
- }
- }
- """,
- """
- import java.time.ZoneId;
- import java.time.ZoneOffset;
- import java.util.TimeZone;
-
- class A {
- public void foo() {
- System.out.println(ZoneOffset.UTC);
- System.out.println(ZoneId.of("America/New_York"));
- System.out.println(ZoneOffset.ofHours(2));
- System.out.println(ZoneOffset.ofHoursMinutes(5, 30));
- System.out.println(TimeZone.getTimeZone("America/New_York").toZoneId());
- }
- }
- """
- )
- );
- }
-
- @Test
- void migrateDateTimeFormat() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.format.DateTimeFormat;
-
- class A {
- public void foo() {
- System.out.println(DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
- // System.out.println(DateTimeFormat.forStyle("SS")); unhandled case
- // System.out.println(DateTimeFormat.patternForStyle("SS", Locale.US)); unhandled case
- System.out.println(DateTimeFormat.shortDate());
- System.out.println(DateTimeFormat.mediumDate());
- System.out.println(DateTimeFormat.longDate());
- System.out.println(DateTimeFormat.fullDate());
- System.out.println(DateTimeFormat.shortTime());
- System.out.println(DateTimeFormat.mediumTime());
- System.out.println(DateTimeFormat.longTime());
- System.out.println(DateTimeFormat.fullTime());
- System.out.println(DateTimeFormat.shortDateTime());
- System.out.println(DateTimeFormat.mediumDateTime());
- System.out.println(DateTimeFormat.longDateTime());
- System.out.println(DateTimeFormat.fullDateTime());
- }
- }
- """,
- """
- import java.time.format.DateTimeFormatter;
- import java.time.format.FormatStyle;
-
- class A {
- public void foo() {
- System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
- // System.out.println(DateTimeFormat.forStyle("SS")); unhandled case
- // System.out.println(DateTimeFormat.patternForStyle("SS", Locale.US)); unhandled case
- System.out.println(DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT));
- System.out.println(DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM));
- System.out.println(DateTimeFormatter.ofLocalizedDate(FormatStyle.LONG));
- System.out.println(DateTimeFormatter.ofLocalizedDate(FormatStyle.FULL));
- System.out.println(DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT));
- System.out.println(DateTimeFormatter.ofLocalizedTime(FormatStyle.MEDIUM));
- System.out.println(DateTimeFormatter.ofLocalizedTime(FormatStyle.LONG));
- System.out.println(DateTimeFormatter.ofLocalizedTime(FormatStyle.FULL));
- System.out.println(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.SHORT, FormatStyle.SHORT));
- System.out.println(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM, FormatStyle.MEDIUM));
- System.out.println(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.LONG, FormatStyle.LONG));
- System.out.println(DateTimeFormatter.ofLocalizedDateTime(FormatStyle.FULL, FormatStyle.FULL));
- }
- }
- """
- )
- );
- }
-
- @Test
- void migrateJodaDuration() {
- // language=java
- rewriteRun(
- java(
- """
- import org.joda.time.Duration;
-
- class A {
- public void foo() {
- System.out.println(Duration.standardDays(1L));
- System.out.println(Duration.standardHours(1L));
- System.out.println(Duration.standardMinutes(1L));
- System.out.println(Duration.standardSeconds(1L));
- System.out.println(Duration.millis(1000L));
- System.out.println(new Duration(1000L));
- System.out.println(new Duration(1000L, 2000L));
- System.out.println(new Duration(1000L).getStandardDays());
- System.out.println(new Duration(1000L).getStandardHours());
- System.out.println(new Duration(1000L).getStandardMinutes());
- System.out.println(new Duration(1000L).getStandardSeconds());
- System.out.println(new Duration(1000L).toDuration());
- System.out.println(new Duration(1000L).withMillis(2000L));
- System.out.println(new Duration(1000L).withDurationAdded(550L, 2));
- System.out.println(new Duration(1000L).withDurationAdded(new Duration(550L), 2));
- System.out.println(new Duration(1000L).plus(550L));
- System.out.println(new Duration(1000L).plus(new Duration(550L)));
- System.out.println(new Duration(1000L).minus(550L));
- System.out.println(new Duration(1000L).minus(new Duration(550L)));
- System.out.println(new Duration(1000L).multipliedBy(2));
- System.out.println(new Duration(1000L).dividedBy(2));
- System.out.println(new Duration(1000L).negated());
- System.out.println(new Duration(1000L).abs());
- }
- }
- """,
- """
- import java.time.Duration;
- import java.time.Instant;
-
- class A {
- public void foo() {
- System.out.println(Duration.ofDays(1L));
- System.out.println(Duration.ofHours(1L));
- System.out.println(Duration.ofMinutes(1L));
- System.out.println(Duration.ofSeconds(1L));
- System.out.println(Duration.ofMillis(1000L));
- System.out.println(Duration.ofMillis(1000L));
- System.out.println(Duration.between(Instant.ofEpochMilli(1000L), Instant.ofEpochMilli(2000L)));
- System.out.println(Duration.ofMillis(1000L).toDays());
- System.out.println(Duration.ofMillis(1000L).toHours());
- System.out.println(Duration.ofMillis(1000L).toMinutes());
- System.out.println(Duration.ofMillis(1000L).getSeconds());
- System.out.println(Duration.ofMillis(1000L));
- System.out.println(Duration.ofMillis(2000L));
- System.out.println(Duration.ofMillis(1000L).plusMillis(550L * 2));
- System.out.println(Duration.ofMillis(1000L).plus(Duration.ofMillis(550L).multipliedBy(2)));
- System.out.println(Duration.ofMillis(1000L).plusMillis(550L));
- System.out.println(Duration.ofMillis(1000L).plus(Duration.ofMillis(550L)));
- System.out.println(Duration.ofMillis(1000L).minusMillis(550L));
- System.out.println(Duration.ofMillis(1000L).minus(Duration.ofMillis(550L)));
- System.out.println(Duration.ofMillis(1000L).multipliedBy(2));
- System.out.println(Duration.ofMillis(1000L).dividedBy(2));
- System.out.println(Duration.ofMillis(1000L).negated());
- System.out.println(Duration.ofMillis(1000L).abs());
- }
- }
- """
- )
- );
- }
-
- @Test
- void migrateAbstractInstant() {
- // language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
- import org.joda.time.Duration;
- import org.joda.time.Instant;
- import org.joda.time.format.DateTimeFormat;
-
- class A {
- public void foo() {
- new DateTime().equals(DateTime.now());
- new DateTime().getZone();
- new DateTime().isAfter(1234567890L);
- new Instant().isAfter(1234567890L);
- new DateTime().isAfter(DateTime.now().minusDays(1));
- new Instant().isAfter(Instant.now().minus(Duration.standardDays(1)));
- new DateTime().isBefore(1234567890L);
- new Instant().isBefore(1234567890L);
- new DateTime().isBefore(DateTime.now().plusDays(1));
- new Instant().isBefore(Instant.now().plus(Duration.standardDays(1)));
- new DateTime().isBeforeNow();
- new DateTime().isEqual(1234567890L);
- new DateTime().isEqual(DateTime.now().plusDays(1));
- new DateTime().toDate();
- new DateTime().toInstant();
- new DateTime().toString();
- new DateTime().toString(DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss"));
- }
- }
- """,
- """
- import java.time.Duration;
- import java.time.Instant;
- import java.time.ZoneId;
- import java.time.ZonedDateTime;
- import java.time.format.DateTimeFormatter;
- import java.util.Date;
-
- class A {
- public void foo() {
- ZonedDateTime.now().equals(ZonedDateTime.now());
- ZonedDateTime.now().getZone();
- ZonedDateTime.now().isAfter(Instant.ofEpochMilli(1234567890L).atZone(ZoneId.systemDefault()));
- Instant.now().isAfter(Instant.ofEpochMilli(1234567890L));
- ZonedDateTime.now().isAfter(ZonedDateTime.now().minusDays(1));
- Instant.now().isAfter(Instant.now().minus(Duration.ofDays(1)));
- ZonedDateTime.now().isBefore(Instant.ofEpochMilli(1234567890L).atZone(ZoneId.systemDefault()));
- Instant.now().isBefore(Instant.ofEpochMilli(1234567890L));
- ZonedDateTime.now().isBefore(ZonedDateTime.now().plusDays(1));
- Instant.now().isBefore(Instant.now().plus(Duration.ofDays(1)));
- ZonedDateTime.now().isBefore(ZonedDateTime.now());
- ZonedDateTime.now().isEqual(Instant.ofEpochMilli(1234567890L).atZone(ZoneId.systemDefault()));
- ZonedDateTime.now().isEqual(ZonedDateTime.now().plusDays(1));
- Date.from(ZonedDateTime.now().toInstant());
- ZonedDateTime.now().toInstant();
- ZonedDateTime.now().toString();
- ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
- }
- }
- """
- )
- );
- }
-
- @Test
- void migrateAbstractDateTime() {
- // language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
-
- class A {
- public void foo() {
- new DateTime().getDayOfMonth();
- new DateTime().getDayOfWeek();
- new DateTime().getHourOfDay();
- new DateTime().getMillisOfSecond();
- new DateTime().getMinuteOfDay();
- new DateTime().getMinuteOfHour();
- new DateTime().getMonthOfYear();
- new DateTime().getSecondOfDay();
- new DateTime().getSecondOfMinute();
- new DateTime().getWeekOfWeekyear();
- new DateTime().toString();
- }
- }
- """,
- """
- import java.time.ZonedDateTime;
- import java.time.temporal.ChronoField;
-
- class A {
- public void foo() {
- ZonedDateTime.now().getDayOfMonth();
- ZonedDateTime.now().getDayOfWeek().getValue();
- ZonedDateTime.now().getHour();
- ZonedDateTime.now().get(ChronoField.MILLI_OF_SECOND);
- ZonedDateTime.now().get(ChronoField.MINUTE_OF_DAY);
- ZonedDateTime.now().getMinute();
- ZonedDateTime.now().getMonthValue();
- ZonedDateTime.now().get(ChronoField.SECOND_OF_DAY);
- ZonedDateTime.now().getSecond();
- ZonedDateTime.now().get(ChronoField.ALIGNED_WEEK_OF_YEAR);
- ZonedDateTime.now().toString();
- }
- }
- """
- )
- );
- }
-
- @Test
- void migrateDateTimeFormatter() {
- // language=java
- rewriteRun(
- java(
- """
- import org.joda.time.format.DateTimeFormat;
- import org.joda.time.DateTime;
- import org.joda.time.DateTimeZone;
-
- class A {
- public void foo() {
- DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").parseDateTime("2024-10-25T15:45:00");
- DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").parseMillis("2024-10-25T15:45:00");
- DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").print(1234567890L);
- DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").print(new DateTime());
- DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").withZone(DateTimeZone.UTC);
- DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss").withZoneUTC();
- }
- }
- """,
- """
- import java.time.Instant;
- import java.time.ZoneId;
- import java.time.ZoneOffset;
- import java.time.ZonedDateTime;
- import java.time.format.DateTimeFormatter;
-
- class A {
- public void foo() {
- ZonedDateTime.parse("2024-10-25T15:45:00", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
- ZonedDateTime.parse("2024-10-25T15:45:00", DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss")).toInstant().toEpochMilli();
- ZonedDateTime.ofInstant(Instant.ofEpochMilli(1234567890L), ZoneId.systemDefault()).format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
- ZonedDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss"));
- DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss").withZone(ZoneOffset.UTC);
- DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss").withZone(ZoneOffset.UTC);
- }
- }
- """
- )
- );
- }
-
- @Test
- void migrateInstant() {
- // language=java
- rewriteRun(
- java(
- """
- import org.joda.time.Instant;
- import org.joda.time.Duration;
-
- class A {
- public void foo() {
- System.out.println(new Instant());
- System.out.println(Instant.now().getMillis());
- System.out.println(Instant.now().minus(Duration.standardDays(1L)));
- System.out.println(Instant.ofEpochMilli(1234567890L));
- System.out.println(Instant.parse("2024-10-25T15:45:00"));
- System.out.println(Instant.now().plus(Duration.standardDays(1L)));
- }
- }
- """,
- """
- import java.time.Duration;
- import java.time.Instant;
-
- class A {
- public void foo() {
- System.out.println(Instant.now());
- System.out.println(Instant.now().toEpochMilli());
- System.out.println(Instant.now().minus(Duration.ofDays(1L)));
- System.out.println(Instant.ofEpochMilli(1234567890L));
- System.out.println(Instant.parse("2024-10-25T15:45:00"));
- System.out.println(Instant.now().plus(Duration.ofDays(1L)));
- }
- }
- """
- )
- );
- }
-
- @Test
- void migrateClassesWithFqn() {
- // language=java
- rewriteRun(
- java(
- """
- class A {
- public void foo() {
- System.out.println(org.joda.time.format.DateTimeFormat.forPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
- }
- }
- """,
- """
- import java.time.format.DateTimeFormatter;
-
- class A {
- public void foo() {
- System.out.println(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSSZ"));
- }
- }
- """
- )
- );
- }
-
- @Test
- void migrateJodaTypeExpressionReferencingNonJodaTypeVar() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
-
- class A {
- public void foo() {
- long millis = DateTime.now().getMillis();
- System.out.println(millis);
- }
- }
- """,
- """
- import java.time.ZonedDateTime;
-
- class A {
- public void foo() {
- long millis = ZonedDateTime.now().toInstant().toEpochMilli();
- System.out.println(millis);
- }
- }
- """
- )
- );
- }
-
- @Test
- void dontChangeMethodsWithUnhandledArguments() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
- import org.joda.time.format.DateTimeFormat;
-
- class A {
- public void foo() {
- // DateTimeFormat.forStyle is unhandled so parent method should not be changed
- System.out.println(DateTime.parse("2024-09-30T23:03:00.000Z", DateTimeFormat.forStyle("SS")));
- }
- }
- """
- )
- );
- }
-
- @Test
- void unhandledCases() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.PeriodType;
-
- class A {
- public void foo() {
- PeriodType.standard();
- }
- }
- """
- )
- );
- }
-
- @Test
- void methodInvocationWithStaticImport() {
- //language=java
- rewriteRun(
- java(
- """
- import static org.joda.time.DateTime.now;
-
- class A {
- public void foo() {
- now();
- }
- }
- """,
- """
- import java.time.ZonedDateTime;
-
- class A {
- public void foo() {
- ZonedDateTime.now();
- }
- }
- """
- )
- );
- }
-
- @Test
- void lambdaVarDeclaration() {
- //language=java
- rewriteRun(
- java(
- """
- import org.joda.time.DateTime;
- import java.util.List;
- import java.util.ArrayList;
-
- class A {
- public void foo() {
- List
- * Licensed under the Moderne Source Available License (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * https://docs.moderne.io/licensing/moderne-source-available-license
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.openrewrite.java.migrate.joda;
-
-import org.junit.jupiter.api.Test;
-import org.openrewrite.DocumentExample;
-import org.openrewrite.java.JavaParser;
-import org.openrewrite.test.RecipeSpec;
-import org.openrewrite.test.RewriteTest;
-
-import static org.openrewrite.java.Assertions.*;
-import static org.openrewrite.maven.Assertions.pomXml;
-
-class NoJodaTimeTest implements RewriteTest {
-
- @Override
- public void defaults(RecipeSpec spec) {
- spec
- .recipeFromResource("/META-INF/rewrite/no-joda-time.yml", "org.openrewrite.java.migrate.joda.NoJodaTime")
- .parser(JavaParser.fromJavaVersion().classpath("joda-time", "threeten-extra"));
- }
-
- @DocumentExample
- @Test
- void migrateJodaTime() {
- rewriteRun(
- mavenProject("foo",
- srcMainJava(
- // language=java
- java(
- """
- import org.joda.time.DateTime;
- import org.joda.time.Interval;
-
- class A {
- void foo() {
- DateTime dt = new DateTime();
- DateTime dt1 = new DateTime().plusDays(1);
- Interval i = new Interval(dt, dt1);
- System.out.println(i.toDuration());
- }
- }
- """,
- """
- import org.threeten.extra.Interval;
-
- import java.time.ZonedDateTime;
-
- class A {
- void foo() {
- ZonedDateTime dt = ZonedDateTime.now();
- ZonedDateTime dt1 = ZonedDateTime.now().plusDays(1);
- Interval i = Interval.of(dt.toInstant(), dt1.toInstant());
- System.out.println(i.toDuration());
- }
- }
- """
- ),
- //language=xml
- pomXml(
- """
-