From 708a5446d7122f22db1dd2cec6be2575e9885a33 Mon Sep 17 00:00:00 2001 From: kak Date: Thu, 28 May 2020 09:29:10 -0700 Subject: [PATCH] Recommend the JavaTimeConversion static conversion methods instead of decomposition-based conversions. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=313601612 --- .../time/PreferJavaTimeOverload.java | 4 --- .../time/PreferJavaTimeOverloadTest.java | 33 +++++++++++++++---- 2 files changed, 26 insertions(+), 11 deletions(-) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverload.java b/core/src/main/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverload.java index 3fb78c61f99..144cf27a7db 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverload.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverload.java @@ -256,8 +256,6 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState return describeMatch(tree, fix.build()); } - // We could suggest using JavaTimeConversions.toJavaDuration(jodaDuration), but that - // requires an additional dependency and isn't open-sourced. fix.replace( arg0, String.format( @@ -293,8 +291,6 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState return describeMatch(tree, fix.build()); } - // We could suggest using JavaTimeConversions.toJavaInstant(jodaInstant), but that - // requires an additional dependency and isn't open-sourced. fix.replace( arg0, String.format( diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverloadTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverloadTest.java index e3755328794..491de9c98f8 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverloadTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/time/PreferJavaTimeOverloadTest.java @@ -186,6 +186,25 @@ public void callingJodaDurationMethodWithDurationOverload_privateMethod() { .doTest(); } + @Test + public void callingJodaInstantMethodWithInstantOverload_privateMethod() { + helper + .addSourceLines( + "TestClass.java", + "import java.time.Instant;", + "public class TestClass {", + " private void bar(org.joda.time.Instant i) {", + " }", + " private void bar(Instant i) {", + " }", + " public void foo(org.joda.time.Instant jodaInstant) {", + " // BUG: Diagnostic contains: bar(Instant.ofEpochMilli(jodaInstant.getMillis()));", + " bar(jodaInstant);", + " }", + "}") + .doTest(); + } + @Test public void callingJodaDurationMethodWithDurationOverload_privateMethod_jodaDurationMillis() { helper @@ -296,19 +315,19 @@ public void callingJodaDurationMethodWithSupertypeJavaDurationOverload() { } @Test - public void callingJodaReadableDurationMethodWithDurationOverload_privateMethod() { + public void callingJodaReadableInstantMethodWithInstantOverload_privateMethod() { helper .addSourceLines( "TestClass.java", - "import java.time.Duration;", + "import java.time.Instant;", "public class TestClass {", - " private void bar(org.joda.time.ReadableDuration d) {", + " private void bar(org.joda.time.ReadableInstant i) {", " }", - " private void bar(Duration d) {", + " private void bar(Instant i) {", " }", - " public void foo(org.joda.time.Duration jodaDuration) {", - " // BUG: Diagnostic contains: bar(Duration.ofMillis(jodaDuration.getMillis()));", - " bar(jodaDuration);", + " public void foo(org.joda.time.Instant jodaInstant) {", + " // BUG: Diagnostic contains: bar(Instant.ofEpochMilli(jodaInstant.getMillis()));", + " bar(jodaInstant);", " }", "}") .doTest();