-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add DateTimeExampleStringCheck ErrorProne plugin
- Loading branch information
1 parent
1033c4b
commit e870756
Showing
5 changed files
with
84 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
mug-errorprone/src/main/java/com/google/mu/errorprone/DateTimeExampleStringCheck.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package com.google.mu.errorprone; | ||
|
||
import static com.google.errorprone.BugPattern.SeverityLevel.ERROR; | ||
import static com.google.errorprone.matchers.Matchers.staticMethod; | ||
|
||
import com.google.auto.service.AutoService; | ||
import com.google.mu.time.DateTimeFormats; | ||
import com.google.errorprone.BugPattern; | ||
import com.google.errorprone.BugPattern.LinkType; | ||
import com.google.errorprone.VisitorState; | ||
import com.google.errorprone.bugpatterns.BugChecker; | ||
import com.google.errorprone.matchers.Matcher; | ||
import com.google.errorprone.util.ASTHelpers; | ||
import com.sun.source.tree.ExpressionTree; | ||
import com.sun.source.tree.MethodInvocationTree; | ||
|
||
/** Validate the example datetime string passed to {@code DateTimeFormats.formatOf()}. */ | ||
@BugPattern( | ||
summary = "Checks that the format string passed to DateTimeFormats.formatOf() is supported.", | ||
link = "go/java-tips/024#safer-string-format-reuse", | ||
linkType = LinkType.CUSTOM, | ||
severity = ERROR) | ||
@AutoService(BugChecker.class) | ||
public final class DateTimeExampleStringCheck extends AbstractBugChecker | ||
implements AbstractBugChecker.MethodInvocationCheck { | ||
private static final Matcher<ExpressionTree> MATCHER = | ||
staticMethod().onClass("com.google.mu.time.DateTimeFormats").named("formatOf"); | ||
|
||
@Override | ||
public void checkMethodInvocation(MethodInvocationTree tree, VisitorState state) | ||
throws ErrorReport { | ||
if (!MATCHER.matches(tree, state) || tree.getArguments().isEmpty()) { | ||
return; | ||
} | ||
ExpressionTree exampleArg = tree.getArguments().get(0); | ||
String exampleString = ASTHelpers.constValue(exampleArg, String.class); | ||
checkingOn(exampleArg).require(exampleString != null, "compile-time string constant expected"); | ||
try { | ||
Object verified = DateTimeFormats.formatOf(exampleString); | ||
} catch (IllegalArgumentException e) { | ||
throw checkingOn(exampleArg).report(e.getMessage()); | ||
} | ||
} | ||
} |
27 changes: 27 additions & 0 deletions
27
mug-errorprone/src/test/java/com/google/mu/errorprone/DateTimeExampleStringCheckTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package com.google.mu.errorprone; | ||
|
||
import com.google.errorprone.CompilationTestHelper; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.junit.runners.JUnit4; | ||
|
||
@RunWith(JUnit4.class) | ||
public final class DateTimeExampleStringCheckTest { | ||
private final CompilationTestHelper helper = | ||
CompilationTestHelper.newInstance(DateTimeExampleStringCheck.class, getClass()); | ||
|
||
@Test | ||
public void mmddyyNotSupported() { | ||
helper | ||
.addSourceLines( | ||
"Test.java", | ||
"import static com.google.mu.time.DateTimeFormats.formatOf;", | ||
"import java.time.format.DateTimeFormatter;", | ||
"class Test {", | ||
" private static final DateTimeFormatter FORMAT = formatOf(", | ||
" // BUG: Diagnostic contains: unsupported date time example: 10/20/2023", | ||
" \"10/20/2023 10:10:10\");", | ||
"}") | ||
.doTest(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters