Skip to content

Commit 0bd6e06

Browse files
amishra-utimtebeek
andauthored
Fix bugs in JodaTime to JavaTime migration recipe (#622)
* Fix bugs in JodaTime to JavaTime migration recipe * Add TODO comment for dataFlowBug test * Enable ChainedLambdaExpressions test --------- Co-authored-by: Tim te Beek <[email protected]>
1 parent 39507e5 commit 0bd6e06

File tree

4 files changed

+109
-1
lines changed

4 files changed

+109
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public J visitCompilationUnit(J.CompilationUnit cu, ExecutionContext ctx) {
6363
@Override
6464
public NamedVariable visitVariable(NamedVariable variable, ExecutionContext ctx) {
6565
if (!variable.getType().isAssignableFrom(JODA_CLASS_PATTERN)) {
66-
return variable;
66+
return (NamedVariable) super.visitVariable(variable, ctx);
6767
}
6868
// TODO: handle class variables
6969
if (isClassVar(variable)) {

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ private boolean isJodaVarRef(@Nullable Expression expr) {
249249
if (expr instanceof J.Identifier) {
250250
return ((J.Identifier) expr).getFieldType() != null;
251251
}
252+
if (expr instanceof MethodCall) {
253+
return expr.getType().isAssignableFrom(JODA_CLASS_PATTERN);
254+
}
252255
return false;
253256
}
254257

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,30 @@ public void bar(DateTime dt) {
285285
)
286286
);
287287
}
288+
289+
@Test
290+
void dontMigrateMethodInvocationIfSelectExprIsNotMigrated() {
291+
//language=java
292+
rewriteRun(
293+
java(
294+
"""
295+
import org.joda.time.Interval;
296+
297+
class A {
298+
private Query query = new Query();
299+
public void foo() {
300+
query.interval().getEndMillis();
301+
}
302+
static class Query {
303+
private Interval interval;
304+
305+
public Interval interval() {
306+
return interval;
307+
}
308+
}
309+
}
310+
"""
311+
)
312+
);
313+
}
288314
}

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

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.openrewrite.test.RecipeSpec;
2222
import org.openrewrite.test.RewriteTest;
2323

24+
import static org.assertj.core.api.Assertions.assertThat;
2425
import static org.junit.jupiter.api.Assertions.assertEquals;
2526
import static org.junit.jupiter.api.Assertions.assertTrue;
2627
import static org.openrewrite.java.Assertions.java;
@@ -203,4 +204,82 @@ public void bar(DateTime dt) {
203204
assertEquals("dt", var.getSimpleName());
204205
}
205206
}
207+
208+
@Test
209+
void detectUnsafeVarsInInitializer() {
210+
JodaTimeScanner scanner = new JodaTimeScanner(new JodaTimeRecipe.Accumulator());
211+
// language=java
212+
rewriteRun(
213+
spec -> spec.recipe(toRecipe(() -> scanner)),
214+
java(
215+
"""
216+
import org.joda.time.Interval;
217+
import java.util.stream.Collectors;
218+
import java.util.stream.Stream;
219+
import java.util.List;
220+
221+
class A {
222+
public Interval interval() {
223+
return new Interval(10, 20);
224+
}
225+
226+
public void foo() {
227+
List<Integer> list = Stream.of(1, 2, 3).peek(i -> {
228+
Interval i1 = interval();
229+
Interval i2 = new Interval(i, 100);
230+
if (i1 != null && !i1.contains(i2)) {
231+
System.out.println("i1 does not contain i2");
232+
}
233+
}).toList();
234+
}
235+
}
236+
"""
237+
)
238+
);
239+
assertThat(scanner.getAcc().getUnsafeVars().stream().map(J.VariableDeclarations.NamedVariable::getSimpleName))
240+
.hasSize(2)
241+
.containsExactlyInAnyOrder("i1", "i2");
242+
}
243+
244+
@Test
245+
void detectUnsafeVarsInChainedLambdaExpressions() {
246+
JodaTimeScanner scanner = new JodaTimeScanner(new JodaTimeRecipe.Accumulator());
247+
// language=java
248+
rewriteRun(
249+
spec -> spec.recipe(toRecipe(() -> scanner)),
250+
java(
251+
"""
252+
import org.joda.time.Interval;
253+
import java.util.stream.Collectors;
254+
import java.util.stream.Stream;
255+
import java.util.List;
256+
257+
class A {
258+
public Interval interval() {
259+
return new Interval(10, 20);
260+
}
261+
262+
public void foo() {
263+
List<Integer> list = Stream.of(1, 2, 3).peek(i -> {
264+
Interval i1 = interval();
265+
Interval i2 = new Interval(i, 100);
266+
if (i1 != null && !i1.contains(i2)) {
267+
System.out.println("i1 does not contain i2");
268+
}
269+
}).peek(i -> {
270+
Interval i3 = interval();
271+
Interval i4 = new Interval(i, 100);
272+
if (i3 != null && !i3.contains(i4)) {
273+
System.out.println("i3 does not contain i4");
274+
}
275+
}).toList();
276+
}
277+
}
278+
"""
279+
)
280+
);
281+
assertThat(scanner.getAcc().getUnsafeVars().stream().map(J.VariableDeclarations.NamedVariable::getSimpleName))
282+
.hasSize(4)
283+
.containsExactlyInAnyOrder("i1", "i2", "i3", "i4");
284+
}
206285
}

0 commit comments

Comments
 (0)