Skip to content

Commit

Permalink
Apply suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 committed Oct 26, 2022
1 parent ee2470c commit 13c862b
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ public CollectorMutability() {}

@Override
public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {
if (!ThirdPartyLibrary.GUAVA.canUse(state) || !COLLECTOR_METHOD.matches(tree, state)) {
if (!ThirdPartyLibrary.GUAVA.isIntroductionAllowed(state)
|| !COLLECTOR_METHOD.matches(tree, state)) {
return Description.NO_MATCH;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public ScheduledTransactionTrace() {}

@Override
public Description matchMethod(MethodTree tree, VisitorState state) {
if (!ThirdPartyLibrary.NEW_RELIC_AGENT_API.canUse(state)
if (!ThirdPartyLibrary.NEW_RELIC_AGENT_API.isIntroductionAllowed(state)
|| !IS_SCHEDULED.matches(tree, state)) {
return Description.NO_MATCH;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ public final class StaticImport extends BugChecker implements MemberSelectTreeMa
"org.springframework.http.MediaType",
"org.testng.Assert",
"reactor.function.TupleUtils",
"tech.picnic.errorprone.bugpatterns.util.MoreTypes");
"tech.picnic.errorprone.bugpatterns.util.MoreTypes",
"tech.picnic.errorprone.bugpatterns.util.ThirdPartyLibrary");

/** Type members that should be statically imported. */
@VisibleForTesting
Expand Down Expand Up @@ -189,7 +190,8 @@ public final class StaticImport extends BugChecker implements MemberSelectTreeMa
"INSTANCE",
"newBuilder",
"of",
"valueOf");
"valueOf",
"values");

/** Instantiates a new {@link StaticImport} instance. */
public StaticImport() {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ public enum ThirdPartyLibrary {
@SuppressWarnings("ImmutableEnumChecker" /* Supplier is deterministic. */)
private final Supplier<Boolean> canUse;

/**
* Instantiates a {@link ThirdPartyLibrary} enum value.
*
* @param witnessFqcn The fully-qualified class name of a type that is expected to be on the
* classpath iff the associated third-party library is on the classpath.
*/
ThirdPartyLibrary(String witnessFqcn) {
this.canUse = VisitorState.memoize(state -> canIntroduceUsage(witnessFqcn, state));
}
Expand All @@ -60,7 +66,7 @@ public enum ThirdPartyLibrary {
* @param state The context under consideration.
* @return {@code true} iff it is okay to assume or create a dependency on this library.
*/
public boolean canUse(VisitorState state) {
public boolean isIntroductionAllowed(VisitorState state) {
return canUse.get(state);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ final class ThirdPartyLibraryTest {
CompilationTestHelper.newInstance(TestChecker.class, getClass());

@Test
void canUse() {
void isIntroductionAllowed() {
compilationTestHelper
.addSourceLines(
"A.java",
Expand All @@ -32,7 +32,7 @@ void canUse() {
}

@Test
void canUseWitnessClassesInSymtab() {
void isIntroductionAllowedWitnessClassesInSymtab() {
compilationTestHelper
.addSourceLines(
"A.java",
Expand All @@ -54,7 +54,7 @@ void canUseWitnessClassesInSymtab() {
}

@Test
void canUseWitnessClassesPartiallyOnClassPath() {
void isIntroductionAllowedWitnessClassesPartiallyOnClassPath() {
compilationTestHelper
.withClasspath(ImmutableList.class, Flux.class)
.addSourceLines(
Expand All @@ -65,7 +65,7 @@ void canUseWitnessClassesPartiallyOnClassPath() {
}

@Test
void canUseWitnessClassesNotOnClassPath() {
void isIntroductionAllowedWitnessClassesNotOnClassPath() {
compilationTestHelper
.withClasspath()
.addSourceLines(
Expand All @@ -78,7 +78,7 @@ void canUseWitnessClassesNotOnClassPath() {

@ParameterizedTest
@ValueSource(booleans = {true, false})
void canUseIgnoreClasspathCompat(boolean ignoreClassPath) {
void isIntroductionAllowedIgnoreClasspathCompat(boolean ignoreClassPath) {
compilationTestHelper
.setArgs("-XepOpt:ErrorProneSupport:IgnoreClasspathCompat=" + ignoreClassPath)
.withClasspath(ImmutableList.class, Flux.class)
Expand All @@ -104,7 +104,10 @@ public Description matchClass(ClassTree tree, VisitorState state) {
return buildDescription(tree)
.setMessage(
Arrays.stream(ThirdPartyLibrary.values())
.map(lib -> String.join(": ", lib.name(), String.valueOf(lib.canUse(state))))
.map(
lib ->
String.join(
": ", lib.name(), String.valueOf(lib.isIntroductionAllowed(state))))
.collect(joining(", ")))
.build();
}
Expand Down

0 comments on commit 13c862b

Please sign in to comment.