From 89d43717aec9e64093c0fa136cad4733663ce7f3 Mon Sep 17 00:00:00 2001 From: Patrick Koenig Date: Thu, 25 Jul 2019 10:37:32 -0700 Subject: [PATCH 1/3] Allow method references in Optional orElse --- .../OptionalOrElseMethodInvocation.java | 25 ++++++++++++++----- .../OptionalOrElseMethodInvocationTests.java | 15 +++++++++++ 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/OptionalOrElseMethodInvocation.java b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/OptionalOrElseMethodInvocation.java index 65bb3002a..8b6562baa 100644 --- a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/OptionalOrElseMethodInvocation.java +++ b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/OptionalOrElseMethodInvocation.java @@ -45,13 +45,9 @@ public final class OptionalOrElseMethodInvocation extends BugChecker implements .onExactClass("java.util.Optional") .named("orElse"); - private static final Matcher METHOD_OR_CONSTRUCTOR = Matchers.anyOf( - MethodMatchers.anyMethod(), - MethodMatchers.constructor()); - private static final Matcher METHOD_INVOCATIONS = Matchers.anyOf( - METHOD_OR_CONSTRUCTOR, - Matchers.contains(ExpressionTree.class, METHOD_OR_CONSTRUCTOR)); + MethodInvocationMatcher.INSTANCE, + Matchers.contains(ExpressionTree.class, MethodInvocationMatcher.INSTANCE)); @Override public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) { @@ -74,4 +70,21 @@ public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState .build(); } + private enum MethodInvocationMatcher implements Matcher { + + INSTANCE; + + @Override + public boolean matches(ExpressionTree tree, VisitorState state) { + switch (tree.getKind()) { + case NEW_CLASS: + case METHOD_INVOCATION: + return true; + default: + break; + } + + return false; + } + } } diff --git a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/OptionalOrElseMethodInvocationTests.java b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/OptionalOrElseMethodInvocationTests.java index a9f82c7e4..131626dcd 100644 --- a/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/OptionalOrElseMethodInvocationTests.java +++ b/baseline-error-prone/src/test/java/com/palantir/baseline/errorprone/OptionalOrElseMethodInvocationTests.java @@ -128,6 +128,21 @@ public void testVariable() { .doTest(); } + @Test + public void testMethodReference() { + compilationHelper + .addSourceLines( + "Test.java", + "import java.util.Optional;", + "import java.util.function.Supplier;", + "class Test {", + " String f() { return \"hello\"; }", + " private final Optional> optionalSupplier = Optional.of(() -> \"hello\");", + " private final Supplier supplier = optionalSupplier.orElse(this::f);", + "}") + .doTest(); + } + @Test public void testOrElseGet() { compilationHelper From a7707769819361c70197ff0f47050bf4dcd7260e Mon Sep 17 00:00:00 2001 From: Patrick Koenig Date: Thu, 25 Jul 2019 10:41:39 -0700 Subject: [PATCH 2/3] Changelog --- changelog/@unreleased/pr-709.v2.yml | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 changelog/@unreleased/pr-709.v2.yml diff --git a/changelog/@unreleased/pr-709.v2.yml b/changelog/@unreleased/pr-709.v2.yml new file mode 100644 index 000000000..4a5bdacb3 --- /dev/null +++ b/changelog/@unreleased/pr-709.v2.yml @@ -0,0 +1,5 @@ +type: fix +fix: + description: OptionalOrElseMethodInvocation now allows method references in orElse. + links: + - https://github.com/palantir/gradle-baseline/pull/709 From 2fd278fc1a091090bb383876799711fcf1773b49 Mon Sep 17 00:00:00 2001 From: Patrick Koenig Date: Thu, 25 Jul 2019 10:43:22 -0700 Subject: [PATCH 3/3] Comments --- .../errorprone/OptionalOrElseMethodInvocation.java | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/OptionalOrElseMethodInvocation.java b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/OptionalOrElseMethodInvocation.java index 8b6562baa..b718fe85e 100644 --- a/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/OptionalOrElseMethodInvocation.java +++ b/baseline-error-prone/src/main/java/com/palantir/baseline/errorprone/OptionalOrElseMethodInvocation.java @@ -29,6 +29,7 @@ import com.google.errorprone.matchers.method.MethodMatchers; import com.sun.source.tree.ExpressionTree; import com.sun.source.tree.MethodInvocationTree; +import com.sun.source.tree.Tree.Kind; @AutoService(BugChecker.class) @BugPattern( @@ -76,15 +77,9 @@ private enum MethodInvocationMatcher implements Matcher { @Override public boolean matches(ExpressionTree tree, VisitorState state) { - switch (tree.getKind()) { - case NEW_CLASS: - case METHOD_INVOCATION: - return true; - default: - break; - } - - return false; + Kind kind = tree.getKind(); + return kind == Kind.NEW_CLASS + || kind == Kind.METHOD_INVOCATION; } } }