Skip to content

Commit 531270b

Browse files
amishra-utimtebeek
andauthored
Joda-time: Make safe migration optional (#811)
* Joda-time: Make safe migration optional * Skip scan if safe mode is disabled * Handle NPE * Fix flaky test * Remove unused import * Rename option to `safeMigrationOnly` --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 9f029e3 commit 531270b

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

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

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

18+
import lombok.EqualsAndHashCode;
1819
import lombok.Getter;
20+
import lombok.Value;
1921
import org.jspecify.annotations.Nullable;
20-
import org.openrewrite.ExecutionContext;
21-
import org.openrewrite.Preconditions;
22-
import org.openrewrite.ScanningRecipe;
23-
import org.openrewrite.TreeVisitor;
22+
import org.openrewrite.*;
2423
import org.openrewrite.java.search.UsesType;
2524
import org.openrewrite.java.tree.J;
2625
import org.openrewrite.java.tree.J.VariableDeclarations.NamedVariable;
@@ -30,7 +29,23 @@
3029

3130
import static java.util.Collections.emptyList;
3231

32+
@EqualsAndHashCode(callSuper = false)
33+
@Value
3334
public class JodaTimeRecipe extends ScanningRecipe<JodaTimeRecipe.Accumulator> {
35+
36+
/**
37+
* Controls whether additional safety checks are performed during the migration process.
38+
* When enabled, the recipe will verify that expressions are safe to migrate before performing the migration.
39+
* This helps prevent potential issues or bugs that might arise from automatic migration.
40+
*/
41+
@Option(displayName = "Safe migration only",
42+
description = "When enabled, performs additional safety checks to verify that expressions are safe to migrate before converting them. " +
43+
"Safety checks include analyzing method parameters, return values, and variable usages across class boundaries.",
44+
required = false
45+
)
46+
@Nullable
47+
Boolean safeMigrationOnly;
48+
3449
@Override
3550
public String getDisplayName() {
3651
return "Migrate Joda-Time to Java time";
@@ -48,12 +63,12 @@ public Accumulator getInitialValue(ExecutionContext ctx) {
4863

4964
@Override
5065
public TreeVisitor<?, ExecutionContext> getScanner(Accumulator acc) {
51-
return new JodaTimeScanner(acc);
66+
return new JodaTimeScanner(acc, Boolean.TRUE.equals(safeMigrationOnly));
5267
}
5368

5469
@Override
5570
public TreeVisitor<?, ExecutionContext> getVisitor(Accumulator acc) {
56-
JodaTimeVisitor jodaTimeVisitor = new JodaTimeVisitor(acc, true, new LinkedList<>());
71+
JodaTimeVisitor jodaTimeVisitor = new JodaTimeVisitor(acc, Boolean.TRUE.equals(safeMigrationOnly), new LinkedList<>());
5772
return Preconditions.check(new UsesType<>("org.joda.time.*", true), jodaTimeVisitor);
5873
}
5974

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,21 @@ class JodaTimeScanner extends ScopeAwareVisitor {
4141

4242
@Getter
4343
private final JodaTimeRecipe.Accumulator acc;
44+
private final boolean safeMigrationOnly;
4445

4546
private final Map<NamedVariable, Set<NamedVariable>> varDependencies = new HashMap<>();
4647
private final Map<JavaType, Set<String>> unsafeVarsByType = new HashMap<>();
4748
private final Map<JavaType.Method, Set<NamedVariable>> methodReferencedVars = new HashMap<>();
4849
private final Map<JavaType.Method, Set<UnresolvedVar>> methodUnresolvedReferencedVars = new HashMap<>();
4950

5051
public JodaTimeScanner(JodaTimeRecipe.Accumulator acc) {
52+
this(acc, true);
53+
}
54+
55+
public JodaTimeScanner(JodaTimeRecipe.Accumulator acc, boolean safeMigrationOnly) {
5156
super(new LinkedList<>());
5257
this.acc = acc;
58+
this.safeMigrationOnly = safeMigrationOnly;
5359
}
5460

5561
@Override
@@ -67,6 +73,9 @@ public Javadoc visitReference(Javadoc.Reference reference, ExecutionContext ctx)
6773

6874
@Override
6975
public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
76+
if (!safeMigrationOnly) { // skip scan if safe mode is disabled
77+
return cu;
78+
}
7079
super.visitCompilationUnit(cu, ctx);
7180
Set<NamedVariable> allReachable = new HashSet<>();
7281
for (NamedVariable var : acc.getUnsafeVars()) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class JodaTimeRecipeTest implements RewriteTest {
3030
@Override
3131
public void defaults(RecipeSpec spec) {
3232
spec
33-
.recipe(new JodaTimeRecipe())
33+
.recipe(new JodaTimeRecipe(true))
3434
.parser(JavaParser.fromJavaVersion().classpath("joda-time"));
3535
}
3636

0 commit comments

Comments
 (0)