Skip to content

Commit 708a544

Browse files
kluevercushon
authored andcommitted
Recommend the JavaTimeConversion static conversion methods instead of decomposition-based conversions.
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=313601612
1 parent ab06403 commit 708a544

File tree

2 files changed

+26
-11
lines changed

2 files changed

+26
-11
lines changed

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,8 +256,6 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
256256
return describeMatch(tree, fix.build());
257257
}
258258

259-
// We could suggest using JavaTimeConversions.toJavaDuration(jodaDuration), but that
260-
// requires an additional dependency and isn't open-sourced.
261259
fix.replace(
262260
arg0,
263261
String.format(
@@ -293,8 +291,6 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState
293291
return describeMatch(tree, fix.build());
294292
}
295293

296-
// We could suggest using JavaTimeConversions.toJavaInstant(jodaInstant), but that
297-
// requires an additional dependency and isn't open-sourced.
298294
fix.replace(
299295
arg0,
300296
String.format(

core/src/test/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverloadTest.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,25 @@ public void callingJodaDurationMethodWithDurationOverload_privateMethod() {
186186
.doTest();
187187
}
188188

189+
@Test
190+
public void callingJodaInstantMethodWithInstantOverload_privateMethod() {
191+
helper
192+
.addSourceLines(
193+
"TestClass.java",
194+
"import java.time.Instant;",
195+
"public class TestClass {",
196+
" private void bar(org.joda.time.Instant i) {",
197+
" }",
198+
" private void bar(Instant i) {",
199+
" }",
200+
" public void foo(org.joda.time.Instant jodaInstant) {",
201+
" // BUG: Diagnostic contains: bar(Instant.ofEpochMilli(jodaInstant.getMillis()));",
202+
" bar(jodaInstant);",
203+
" }",
204+
"}")
205+
.doTest();
206+
}
207+
189208
@Test
190209
public void callingJodaDurationMethodWithDurationOverload_privateMethod_jodaDurationMillis() {
191210
helper
@@ -296,19 +315,19 @@ public void callingJodaDurationMethodWithSupertypeJavaDurationOverload() {
296315
}
297316

298317
@Test
299-
public void callingJodaReadableDurationMethodWithDurationOverload_privateMethod() {
318+
public void callingJodaReadableInstantMethodWithInstantOverload_privateMethod() {
300319
helper
301320
.addSourceLines(
302321
"TestClass.java",
303-
"import java.time.Duration;",
322+
"import java.time.Instant;",
304323
"public class TestClass {",
305-
" private void bar(org.joda.time.ReadableDuration d) {",
324+
" private void bar(org.joda.time.ReadableInstant i) {",
306325
" }",
307-
" private void bar(Duration d) {",
326+
" private void bar(Instant i) {",
308327
" }",
309-
" public void foo(org.joda.time.Duration jodaDuration) {",
310-
" // BUG: Diagnostic contains: bar(Duration.ofMillis(jodaDuration.getMillis()));",
311-
" bar(jodaDuration);",
328+
" public void foo(org.joda.time.Instant jodaInstant) {",
329+
" // BUG: Diagnostic contains: bar(Instant.ofEpochMilli(jodaInstant.getMillis()));",
330+
" bar(jodaInstant);",
312331
" }",
313332
"}")
314333
.doTest();

0 commit comments

Comments
 (0)