@@ -108,6 +108,14 @@ public final class PreferJavaTimeOverload extends BugChecker
108
108
private static final Matcher <ExpressionTree > JODA_INSTANT_CONSTRUCTOR_MATCHER =
109
109
constructor ().forClass (JODA_INSTANT ).withParameters ("long" );
110
110
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
+
111
119
private static final Matcher <ExpressionTree > IGNORED_APIS =
112
120
anyOf (
113
121
staticMethod ().onClass ("org.jooq.impl.DSL" ).named ("inline" ),
@@ -240,13 +248,20 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
240
248
}
241
249
}
242
250
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
+
243
259
// We could suggest using JavaTimeConversions.toJavaDuration(jodaDuration), but that
244
260
// requires an additional dependency and isn't open-sourced.
245
261
fix .replace (
246
- arguments . get ( 0 ) ,
262
+ arg0 ,
247
263
String .format (
248
- "%s.ofMillis(%s.getMillis())" ,
249
- qualifiedDuration , state .getSourceForNode (arguments .get (0 ))));
264
+ "%s.ofMillis(%s.getMillis())" , qualifiedDuration , state .getSourceForNode (arg0 )));
250
265
return describeMatch (tree , fix .build ());
251
266
}
252
267
}
@@ -270,13 +285,20 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
270
285
}
271
286
}
272
287
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
+
273
296
// We could suggest using JavaTimeConversions.toJavaInstant(jodaInstant), but that
274
297
// requires an additional dependency and isn't open-sourced.
275
298
fix .replace (
276
- arguments . get ( 0 ) ,
299
+ arg0 ,
277
300
String .format (
278
- "%s.ofEpochMilli(%s.getMillis())" ,
279
- qualifiedInstant , state .getSourceForNode (arguments .get (0 ))));
301
+ "%s.ofEpochMilli(%s.getMillis())" , qualifiedInstant , state .getSourceForNode (arg0 )));
280
302
return describeMatch (tree , fix .build ());
281
303
}
282
304
}
0 commit comments