Skip to content

Commit

Permalink
Add support for toSeconds in PreferJavaTimeOverload
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 676398528
  • Loading branch information
java-team-github-bot authored and Error Prone Team committed Sep 19, 2024
1 parent 43ed6f3 commit fa3c23d
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,8 @@ public final class PreferJavaTimeOverload extends BugChecker
private static final Matcher<ExpressionTree> JAVA_DURATION_DECOMPOSITION_MATCHER =
instanceMethod()
.onExactClass(JAVA_DURATION)
.namedAnyOf("toNanos", "toMillis", "getSeconds", "toMinutes", "toHours", "toDays");
.namedAnyOf(
"toNanos", "toMillis", "toSeconds", "toMinutes", "toHours", "toDays", "getSeconds");

// TODO(kak): Add support for constructors that accept a <long, TimeUnit> or JodaTime Duration

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,27 @@ public CacheBuilder foo(CacheBuilder builder) {

@Test
public void callingLongTimeUnitMethodWithDurationOverload_durationDecompose() {
helper
.addSourceLines(
"TestClass.java",
"""
import com.google.common.cache.CacheBuilder;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
public class TestClass {
public CacheBuilder foo(CacheBuilder builder) {
Duration duration = Duration.ofMillis(12345);
// BUG: Diagnostic contains: builder.expireAfterAccess(duration);
return builder.expireAfterAccess(duration.toSeconds(), TimeUnit.SECONDS);
}
}
""")
.doTest();
}

@Test
public void callingLongTimeUnitMethodWithDurationOverload_durationDecompose_getSeconds() {
helper
.addSourceLines(
"TestClass.java",
Expand Down

0 comments on commit fa3c23d

Please sign in to comment.