From 195988f44e11dfc18f0b7768229c6d0bcba31bc2 Mon Sep 17 00:00:00 2001 From: ghm Date: Thu, 9 Jan 2025 10:20:08 -0800 Subject: [PATCH] NonApiType: prefer using Module instead of AbstractModule. AbstractModule is an implementation detail; a type that shouldn't really be referred to. PiperOrigin-RevId: 713715344 --- .../errorprone/bugpatterns/NonApiType.java | 6 +++++ .../bugpatterns/NonApiTypeTest.java | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/core/src/main/java/com/google/errorprone/bugpatterns/NonApiType.java b/core/src/main/java/com/google/errorprone/bugpatterns/NonApiType.java index 3433c7cc3d8..2b002417bf5 100644 --- a/core/src/main/java/com/google/errorprone/bugpatterns/NonApiType.java +++ b/core/src/main/java/com/google/errorprone/bugpatterns/NonApiType.java @@ -153,6 +153,12 @@ public final class NonApiType extends BugChecker implements MethodTreeMatcher { "Prefer accepting an Iterable or Collection instead. " + STREAM_LINK, ApiElementType.PARAMETER), + // Guice + withAnyVisibility( + isExactType("com.google.inject.AbstractModule"), + "Prefer using Module instead.", + ApiElementType.ANY), + // ProtoTime withPublicVisibility( isExactType("com.google.protobuf.Duration"), diff --git a/core/src/test/java/com/google/errorprone/bugpatterns/NonApiTypeTest.java b/core/src/test/java/com/google/errorprone/bugpatterns/NonApiTypeTest.java index 7db66ba27bb..f5c6e052ab1 100644 --- a/core/src/test/java/com/google/errorprone/bugpatterns/NonApiTypeTest.java +++ b/core/src/test/java/com/google/errorprone/bugpatterns/NonApiTypeTest.java @@ -365,4 +365,28 @@ public record RecordWithImmutableSet(ImmutableSet ids) { """) .doTest(); } + + @Test + public void guiceModules() { + helper + .addSourceLines( + "Test.java", + """ + import com.google.inject.AbstractModule; + import com.google.inject.Module; + + public class Test extends AbstractModule { + // BUG: Diagnostic contains: NonApiType + public AbstractModule test() { + return new AbstractModule() {}; + } + + // Exact type, no finding. + public Test test2() { + return new Test(); + } + } + """) + .doTest(); + } }