diff --git a/check_api/src/main/java/com/google/errorprone/scanner/ErrorProneInjector.java b/check_api/src/main/java/com/google/errorprone/scanner/ErrorProneInjector.java index 2183023183a..b30280ea191 100644 --- a/check_api/src/main/java/com/google/errorprone/scanner/ErrorProneInjector.java +++ b/check_api/src/main/java/com/google/errorprone/scanner/ErrorProneInjector.java @@ -106,8 +106,10 @@ public static Optional> findConstructor(Class clazz) { findConstructorMatching( clazz, c -> - stream(c.getParameters()) - .allMatch(p -> p.getType().equals(ErrorProneFlags.class)))); + c.getParameters().length != 0 + && stream(c.getParameters()) + .allMatch(p -> p.getType().equals(ErrorProneFlags.class)))) + .or(() -> findConstructorMatching(clazz, c -> c.getParameters().length == 0)); } @SuppressWarnings("unchecked") diff --git a/check_api/src/test/java/com/google/errorprone/scanner/ErrorProneInjectorTest.java b/check_api/src/test/java/com/google/errorprone/scanner/ErrorProneInjectorTest.java index 3d6dc3d02df..3c17130d1a6 100644 --- a/check_api/src/test/java/com/google/errorprone/scanner/ErrorProneInjectorTest.java +++ b/check_api/src/test/java/com/google/errorprone/scanner/ErrorProneInjectorTest.java @@ -19,6 +19,7 @@ import static com.google.common.truth.Truth.assertThat; import static org.junit.Assert.assertThrows; +import com.google.errorprone.ErrorProneFlags; import com.google.errorprone.scanner.ErrorProneInjector.ProvisionException; import javax.inject.Inject; import org.junit.Test; @@ -58,6 +59,16 @@ public void bothConstructors_injectable() { assertThat(obj.x).isEqualTo(2); } + @Test + public void errorProneFlags_favouredOverZeroArg() { + var injector = + ErrorProneInjector.create().addBinding(ErrorProneFlags.class, ErrorProneFlags.empty()); + + var obj = injector.getInstance(ErrorProneFlagsAndZeroArgsConstructor.class); + + assertThat(obj.x).isEqualTo(1); + } + @Test public void pathInError() { var injector = ErrorProneInjector.create(); @@ -89,4 +100,16 @@ public static final class InjectConstructorAndZeroArgConstructor { this.x = 0; } } + + public static final class ErrorProneFlagsAndZeroArgsConstructor { + final int x; + + ErrorProneFlagsAndZeroArgsConstructor() { + this.x = 0; + } + + ErrorProneFlagsAndZeroArgsConstructor(ErrorProneFlags flags) { + this.x = 1; + } + } }