Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow lambdas and method references referencing subclasses #4438

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import com.sun.source.tree.ClassTree;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.IdentifierTree;
import com.sun.source.tree.LambdaExpressionTree;
import com.sun.source.tree.MemberReferenceTree;
import com.sun.source.tree.MemberSelectTree;
import com.sun.source.tree.MethodTree;
import com.sun.source.tree.Tree;
Expand Down Expand Up @@ -112,6 +114,16 @@ public Void visitMethod(MethodTree node, Void unused) {
return null;
}

@Override
public Void visitMemberReference(MemberReferenceTree node, Void unused) {
return null;
}

@Override
public Void visitLambdaExpression(LambdaExpressionTree node, Void unused) {
return null;
}

@Override
public Void visitMemberSelect(MemberSelectTree tree, Void unused) {
if (ASTHelpers.constValue(tree) != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import com.google.auto.value.processor.AutoValueProcessor;
import com.google.errorprone.CompilationTestHelper;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -309,4 +310,63 @@ public void nonNestedSubclass() {
"class B extends A {}")
.doTest();
}

@Test
public void simpleSubclassMethodReference() {
testHelper
.addSourceLines(
"Foo.java",
"import java.util.function.Supplier;",
"class A {",
" static Supplier<B> supplier = B::new;",
"}",
"class B extends A {}")
.doTest();
}

@Test
public void compoundSubclassMethodReference() {
testHelper
.addSourceLines(
"Foo.java",
"import java.util.Comparator;",
"class A {",
" static Comparator<B> comparator = Comparator.comparing(B::value);",
"}",
"class B extends A {",
" int value;",
" int value() {",
" return value;",
" }",
"}")
.doTest();
}

@Test
public void lambda() {
testHelper
.addSourceLines(
"Foo.java",
"import java.util.function.Supplier;",
"class A {",
" static Supplier<B> supplier = () -> new B();",
"}",
"class B extends A {}")
.doTest();
}

@Test
public void subclassStaticMethod() {
testHelper
.addSourceLines(
"Foo.java",
"class A {",
" // BUG: Diagnostic contains:",
" static int value = B.value(); ",
"}",
"class B extends A {",
" static int value() { return 0; }",
"}")
.doTest();
}
Comment on lines +358 to +371
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The check's treatment of this situation is unchanged with this PR. I just noticed it wasn't tested and wanted to make sure it was still catching this usage while working on the check.

}
Loading