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

UnnecessaryBoxedAssignment: avoid CCE on method reference #4157

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
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ public Description matchVariable(VariableTree tree, VisitorState state) {
}

private Description matchCommon(ExpressionTree expression, VisitorState state) {
if (expression == null || !VALUE_OF_MATCHER.matches(expression, state)) {
if (!(expression instanceof MethodInvocationTree)
|| !VALUE_OF_MATCHER.matches(expression, state)) {
return Description.NO_MATCH;
}
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) expression;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.errorprone.bugpatterns.testdata;

import java.util.function.Function;

/**
* @author [email protected] (Andy Turner)
*/
Expand Down Expand Up @@ -76,4 +78,8 @@ Integer positive_wrappedAgain(int aInteger) {
a = Integer.valueOf(aInteger);
return Integer.valueOf(a);
}

void negative_methodReference() {
Function<String, Boolean> toBoolean = Boolean::valueOf;
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Without the fix this triggers:

java.lang.ClassCastException: class com.sun.tools.javac.tree.JCTree$JCMemberReference cannot be cast to class com.sun.source.tree.MethodInvocationTree (com.sun.tools.javac.tree.JCTree$JCMemberReference and com.sun.source.tree.MethodInvocationTree are in module jdk.compiler of loader 'app')
	at com.google.errorprone.bugpatterns.UnnecessaryBoxedAssignment.matchCommon(UnnecessaryBoxedAssignment.java:72)
	at com.google.errorprone.bugpatterns.UnnecessaryBoxedAssignment.matchVariable(UnnecessaryBoxedAssignment.java:64)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.google.errorprone.bugpatterns.testdata;

import java.util.function.Function;

/**
* @author [email protected] (Andy Turner)
*/
Expand Down Expand Up @@ -76,4 +78,8 @@ Integer positive_wrappedAgain(int aInteger) {
a = aInteger;
return a;
}

void negative_methodReference() {
Function<String, Boolean> toBoolean = Boolean::valueOf;
}
}
Loading