Skip to content

Commit

Permalink
Favour the ErrorProneFlags constructor over the zero-args one.
Browse files Browse the repository at this point in the history
I was a bit lazy and figured this edge case wouldn't matter too much, but it turns out to annoy people.

Fixes external #3931

PiperOrigin-RevId: 535569770
  • Loading branch information
graememorgan authored and Error Prone Team committed May 26, 2023
1 parent 1b1494a commit bafa50f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@ public static <T> Optional<Constructor<T>> findConstructor(Class<T> 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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;
}
}
}

0 comments on commit bafa50f

Please sign in to comment.