Skip to content

Commit 67a923c

Browse files
kluevercushon
authored andcommitted
Unwrap JavaTimeConversion calls when converting.
Before: bar(Duration.ofMillis(toJodaDuration(duration).getMillis())); After: bar(duration); ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=313478073
1 parent 9803086 commit 67a923c

File tree

1 file changed

+28
-6
lines changed

1 file changed

+28
-6
lines changed

core/src/main/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverload.java

+28-6
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ public final class PreferJavaTimeOverload extends BugChecker
108108
private static final Matcher<ExpressionTree> JODA_INSTANT_CONSTRUCTOR_MATCHER =
109109
constructor().forClass(JODA_INSTANT).withParameters("long");
110110

111+
private static final String JAVA_TIME_CONVERSIONS =
112+
"com.google.thirdparty.jodatime.JavaTimeConversions";
113+
114+
private static final Matcher<ExpressionTree> TO_JODA_DURATION =
115+
staticMethod().onClass(JAVA_TIME_CONVERSIONS).named("toJodaDuration");
116+
private static final Matcher<ExpressionTree> TO_JODA_INSTANT =
117+
staticMethod().onClass(JAVA_TIME_CONVERSIONS).named("toJodaInstant");
118+
111119
private static final Matcher<ExpressionTree> IGNORED_APIS =
112120
anyOf(
113121
staticMethod().onClass("org.jooq.impl.DSL").named("inline"),
@@ -240,13 +248,20 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
240248
}
241249
}
242250

251+
// If we're converting to a JodaTime Duration (from a java.time Duration) to call the
252+
// JodaTime overload, just unwrap it!
253+
if (TO_JODA_DURATION.matches(arg0, state)) {
254+
fix.replace(
255+
arg0, state.getSourceForNode(((MethodInvocationTree) arg0).getArguments().get(0)));
256+
return describeMatch(tree, fix.build());
257+
}
258+
243259
// We could suggest using JavaTimeConversions.toJavaDuration(jodaDuration), but that
244260
// requires an additional dependency and isn't open-sourced.
245261
fix.replace(
246-
arguments.get(0),
262+
arg0,
247263
String.format(
248-
"%s.ofMillis(%s.getMillis())",
249-
qualifiedDuration, state.getSourceForNode(arguments.get(0))));
264+
"%s.ofMillis(%s.getMillis())", qualifiedDuration, state.getSourceForNode(arg0)));
250265
return describeMatch(tree, fix.build());
251266
}
252267
}
@@ -270,13 +285,20 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
270285
}
271286
}
272287

288+
// If we're converting to a JodaTime Instant (from a java.time Instant) to call the JodaTime
289+
// overload, just unwrap it!
290+
if (TO_JODA_INSTANT.matches(arg0, state)) {
291+
fix.replace(
292+
arg0, state.getSourceForNode(((MethodInvocationTree) arg0).getArguments().get(0)));
293+
return describeMatch(tree, fix.build());
294+
}
295+
273296
// We could suggest using JavaTimeConversions.toJavaInstant(jodaInstant), but that
274297
// requires an additional dependency and isn't open-sourced.
275298
fix.replace(
276-
arguments.get(0),
299+
arg0,
277300
String.format(
278-
"%s.ofEpochMilli(%s.getMillis())",
279-
qualifiedInstant, state.getSourceForNode(arguments.get(0))));
301+
"%s.ofEpochMilli(%s.getMillis())", qualifiedInstant, state.getSourceForNode(arg0)));
280302
return describeMatch(tree, fix.build());
281303
}
282304
}

0 commit comments

Comments
 (0)