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

ImmutableChecker handles null types #3267

Closed
wants to merge 4 commits into from
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
32 changes: 24 additions & 8 deletions check_api/src/main/java/com/google/errorprone/util/ASTHelpers.java
Expand Up @@ -1754,14 +1754,30 @@ public Type visitAnnotation(AnnotationTree tree, Void unused) {

@Override
public Type visitCase(CaseTree tree, Void unused) {
Tree t = parent.getParentPath().getLeaf();
// JDK 12+, t can be SwitchExpressionTree
if (t instanceof SwitchTree) {
SwitchTree switchTree = (SwitchTree) t;
return getType(switchTree.getExpression());
}
// TODO(b/176098078): When the ErrorProne project switches to JDK 12, we should check
// for SwitchExpressionTree.
Tree switchTree = parent.getParentPath().getLeaf();
return getType(getSwitchExpression(switchTree));
}

@Nullable
private static ExpressionTree getSwitchExpression(Tree tree) {
if (tree instanceof SwitchTree) {
return ((SwitchTree) tree).getExpression();
}
// Reflection is required for JDK < 12
try {
Class<?> switchExpression = Class.forName("com.sun.source.tree.SwitchExpressionTree");
Class<?> clazz = tree.getClass();
if (switchExpression.isAssignableFrom(clazz)) {
try {
Method method = clazz.getMethod("getExpression");
return (ExpressionTree) method.invoke(tree);
} catch (ReflectiveOperationException e) {
throw new LinkageError(e.getMessage(), e);
}
}
} catch (ClassNotFoundException e) {
// continue below
}
return null;
}

Expand Down
Expand Up @@ -16,11 +16,14 @@

package com.google.errorprone.bugpatterns.threadsafety;

import static org.junit.Assume.assumeTrue;

import com.google.common.collect.ImmutableList;
import com.google.errorprone.CompilationTestHelper;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import com.google.errorprone.annotations.Immutable;
import com.google.errorprone.annotations.concurrent.LazyInit;
import com.google.errorprone.util.RuntimeVersion;
import java.util.Arrays;
import org.junit.Ignore;
import org.junit.Test;
Expand Down Expand Up @@ -2969,4 +2972,27 @@ public void anonymousClass_canCallSuperMethodOnNonImmutableSuperClass() {
"}")
.doTest();
}

@Test
public void switchExpressionsResultingInGenericTypes_doesNotThrow() {
assumeTrue(RuntimeVersion.isAtLeast14());
compilationHelper
.addSourceLines(
"Kind.java",
"import com.google.errorprone.annotations.Immutable;",
"@Immutable enum Kind { A, B; }")
.addSourceLines(
"Test.java",
"import java.util.Optional;",
"import java.util.function.Supplier;",
"class Test {",
" Supplier<Optional<String>> test(Kind kind) {",
" return switch (kind) {",
" case A -> Optional::empty;",
" case B -> () -> Optional.of(\"\");",
" };",
" }",
"}")
.doTest();
}
}