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

Handle yield statement case in ASTHelpers#targetType #3737

Closed
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
28 changes: 22 additions & 6 deletions check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -1724,13 +1724,15 @@ public static TargetType targetType(VisitorState state) {

Type type = new TargetTypeVisitor(current, state, parent).visit(parent.getLeaf(), null);
if (type == null) {
if (CONSTANT_CASE_LABEL_TREE != null
Tree actualTree = null;
if (YIELD_TREE != null && YIELD_TREE.isAssignableFrom(parent.getLeaf().getClass())) {
actualTree = parent.getParentPath().getParentPath().getParentPath().getLeaf();
} else if (CONSTANT_CASE_LABEL_TREE != null
&& CONSTANT_CASE_LABEL_TREE.isAssignableFrom(parent.getLeaf().getClass())) {
Comment on lines +1727 to 1731
Copy link
Contributor Author

Choose a reason for hiding this comment

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

There should be a better way of handling these. 😬

Copy link
Collaborator

Choose a reason for hiding this comment

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

Agree, but I think reflection is the best option we have right now.

I've been wondering if we should try to use mrjars to avoid some of the reflection, I filed an issue to track that idea: #3756

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's a good idea! Thanks for filing the issue. 🙇

type =
getType(
TargetTypeVisitor.getSwitchExpression(
parent.getParentPath().getParentPath().getLeaf()));
actualTree = parent.getParentPath().getParentPath().getLeaf();
}

type = getType(TargetTypeVisitor.getSwitchExpression(actualTree));
if (type == null) {
return null;
}
Expand All @@ -1739,6 +1741,7 @@ public static TargetType targetType(VisitorState state) {
}

@Nullable private static final Class<?> CONSTANT_CASE_LABEL_TREE = constantCaseLabelTree();
@Nullable private static final Class<?> YIELD_TREE = yieldTree();

@Nullable
private static Class<?> constantCaseLabelTree() {
Expand All @@ -1749,6 +1752,15 @@ private static Class<?> constantCaseLabelTree() {
}
}

@Nullable
private static Class<?> yieldTree() {
try {
return Class.forName("com.sun.source.tree.YieldTree");
} catch (ClassNotFoundException e) {
return null;
}
}

private static boolean canHaveTargetType(Tree tree) {
// Anything that isn't an expression can't have a target type.
if (!(tree instanceof ExpressionTree)) {
Expand Down Expand Up @@ -1831,7 +1843,11 @@ public Type visitCase(CaseTree tree, Void unused) {
}

@Nullable
private static ExpressionTree getSwitchExpression(Tree tree) {
private static ExpressionTree getSwitchExpression(@Nullable Tree tree) {
if (tree == null) {
return null;
}

if (tree instanceof SwitchTree) {
return ((SwitchTree) tree).getExpression();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2993,4 +2993,62 @@ public void switchExpressionsResultingInGenericTypes_doesNotThrow() {
"}")
.doTest();
}

@Test
public void switchExpressionsYieldStatement_doesNotThrow() {
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"import java.util.function.Supplier;",
"class Test {",
" String test(String mode) {",
" return switch (mode) {",
" case \"random\" -> {",
" yield \"foo\";",
" }",
" default -> throw new IllegalArgumentException();",
" };",
" }",
"}")
.doTest();
}

@Test
public void switchExpressionsMethodReference_doesNotThrow() {
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"import java.util.function.Supplier;",
"class Test {",
" Supplier<Double> test(String mode) {",
" return switch (mode) {",
" case \"random\" -> Math::random;",
" default -> throw new IllegalArgumentException();",
" };",
" }",
"}")
.doTest();
}
Comment on lines +2997 to +3033
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These two cases are irrelevant for this change, but I added to make sure NPE is only related to yield AClass::methodReference.


@Test
public void switchExpressionsYieldStatementMethodReference_doesNotThrow() {
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Test.java",
"import java.util.function.Supplier;",
"class Test {",
" Supplier<Double> test(String mode) {",
" return switch (mode) {",
" case \"random\" -> {",
" yield Math::random;",
" }",
" default -> throw new IllegalArgumentException();",
" };",
" }",
"}")
.doTest();
}
}