Skip to content

Commit

Permalink
Recognize format string conversions for java.time
Browse files Browse the repository at this point in the history
MOE_MIGRATED_REVID=143992911
  • Loading branch information
cushon committed Jan 10, 2017
1 parent cea92db commit 275d07a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import edu.umd.cs.findbugs.formatStringChecker.Formatter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.TemporalAccessor;
import java.util.ArrayDeque;
import java.util.Calendar;
import java.util.Collection;
Expand Down Expand Up @@ -173,6 +177,9 @@ private static Object getInstance(Tree tree, VisitorState state) {
if (types.isSubtype(type, state.getTypeFromString(Calendar.class.getName()))) {
return new GregorianCalendar();
}
if (types.isSubtype(type, state.getTypeFromString(TemporalAccessor.class.getName()))) {
return LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault());
}
return new Object();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,21 @@ public void nullArgument() throws Exception {
"}")
.doTest();
}

@Test
public void javaUtilTime() throws Exception {
compilationHelper
.addSourceLines(
"Test.java",
"import java.time.Instant;",
"import java.time.LocalDateTime;",
"import java.time.ZoneId;",
"class Test {",
" void f() {",
" System.err.printf(\"%tY\",",
" LocalDateTime.ofInstant(Instant.now(), ZoneId.systemDefault()));",
" }",
"}")
.doTest();
}
}

0 comments on commit 275d07a

Please sign in to comment.