Skip to content

Commit

Permalink
Address PR feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Dec 16, 2023
1 parent 532dfe6 commit 2695c4d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import static com.google.errorprone.BugPattern.LinkType.CUSTOM;
import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
import static com.google.errorprone.BugPattern.StandardTags.FRAGILE_CODE;
import static com.google.errorprone.matchers.Matchers.allOf;
import static com.google.errorprone.matchers.Matchers.anything;
import static com.google.errorprone.matchers.Matchers.classLiteral;
import static com.google.errorprone.matchers.Matchers.instanceMethod;
import static com.google.errorprone.matchers.Matchers.receiverOfInvocation;
import static com.google.errorprone.matchers.Matchers.toType;
import static tech.picnic.errorprone.bugpatterns.util.Documentation.BUG_PATTERNS_BASE_URL;

import com.google.auto.service.AutoService;
Expand All @@ -29,8 +34,11 @@
*
* <p>For top-level types these two methods generally return the same result, but for nested types
* the former separates identifiers using a dollar sign ({@code $}) rather than a dot ({@code .}).
*
* @implNote This check currently only flags {@link Class#getName()} invocations on class literals,
* and doesn't flag method references. This avoids false positives, such as suggesting use of
* {@link Class#getCanonicalName()} in contexts where the canonical name is {@code null}.
*/
// XXX: This check currently doesn't flag `Class::getName` method references.
@AutoService(BugChecker.class)
@BugPattern(
summary = "This code should likely use the type's canonical name",
Expand All @@ -42,7 +50,11 @@ public final class CanonicalClassNameUsage extends BugChecker
implements MethodInvocationTreeMatcher {
private static final long serialVersionUID = 1L;
private static final Matcher<ExpressionTree> GET_NAME_INVOCATION =
instanceMethod().onExactClass(Class.class.getCanonicalName()).named("getName");
toType(
MethodInvocationTree.class,
allOf(
receiverOfInvocation(classLiteral(anything())),
instanceMethod().onExactClass(Class.class.getCanonicalName()).named("getName")));
private static final Pattern CANONICAL_NAME_USING_TYPES =
Pattern.compile("(com\\.google\\.errorprone|tech\\.picnic\\.errorprone)\\..*");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ void identification() {
" instanceMethod().onExactClass(A.class.getCanonicalName());",
" MoreTypes.type(A.class.getCanonicalName());",
" MoreTypes.type(A.class.getCanonicalName() + \".SubType\");",
" instanceMethod().onExactClass(new Object() {}.getClass().getName());",
" instanceMethod().onExactClass(methodInUnnamedPackage(A.class.getName()));",
" // BUG: Diagnostic contains:",
" instanceMethod().onExactClass(A.class.getName());",
Expand Down

0 comments on commit 2695c4d

Please sign in to comment.