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

Improve method matcher for AccessPathNullnessPropagation (#866) #867

Closed
Closed
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 @@ -26,9 +26,10 @@

import com.google.common.base.Preconditions;
import com.google.errorprone.VisitorState;
import com.google.errorprone.suppliers.Supplier;
import com.google.errorprone.suppliers.Suppliers;
import com.google.errorprone.matchers.Matcher;
import com.google.errorprone.matchers.method.MethodMatchers;
import com.google.errorprone.util.ASTHelpers;
import com.sun.source.tree.ExpressionTree;
import com.sun.source.tree.MethodInvocationTree;
import com.sun.tools.javac.code.Symbol;
import com.sun.tools.javac.code.Type;
Expand Down Expand Up @@ -141,10 +142,11 @@ public class AccessPathNullnessPropagation

private static final boolean NO_STORE_CHANGE = false;

private static final Supplier<Type> SET_TYPE_SUPPLIER = Suppliers.typeFromString("java.util.Set");
private static final Matcher<ExpressionTree> SET_ITERATOR_METHOD_MATCHER =
MethodMatchers.instanceMethod().onDescendantOf("java.util.Set").named("iterator");

private static final Supplier<Type> ITERATOR_TYPE_SUPPLIER =
Suppliers.typeFromString("java.util.Iterator");
private static final Matcher<ExpressionTree> ITERATOR_NEXT_METHOD_MATCHER =
MethodMatchers.instanceMethod().onDescendantOf("java.util.Iterator").named("next");

private final Nullness defaultAssumption;

Expand Down Expand Up @@ -577,7 +579,7 @@ private void handleEnhancedForOverKeySet(
AccessPath.mapWithIteratorContentsKey(mapNode, lhs, apContext);
if (mapWithIteratorContentsKey != null) {
// put sanity check here to minimize perf impact
if (!isCallToMethod(rhsInv, SET_TYPE_SUPPLIER, "iterator")) {
if (!SET_ITERATOR_METHOD_MATCHER.matches(rhsInv.getTree(), state)) {
throw new RuntimeException(
"expected call to iterator(), instead saw "
+ state.getSourceForNode(rhsInv.getTree()));
Expand All @@ -602,7 +604,7 @@ && isEnhancedForIteratorVariable((LocalVariableNode) receiver)) {
.getMapGetIteratorContentsAccessPath((LocalVariableNode) receiver);
if (mapGetPath != null) {
// put sanity check here to minimize perf impact
if (!isCallToMethod(methodInv, ITERATOR_TYPE_SUPPLIER, "next")) {
if (!ITERATOR_NEXT_METHOD_MATCHER.matches(methodInv.getTree(), state)) {
throw new RuntimeException(
"expected call to next(), instead saw "
+ state.getSourceForNode(methodInv.getTree()));
Expand Down Expand Up @@ -633,16 +635,6 @@ private Node getMapNodeForKeySetIteratorCall(MethodInvocationNode invocationNode
return null;
}

private boolean isCallToMethod(
MethodInvocationNode invocationNode,
Supplier<Type> containingTypeSupplier,
String methodName) {
Symbol.MethodSymbol symbol = ASTHelpers.getSymbol(invocationNode.getTree());
return symbol != null
&& symbol.getSimpleName().contentEquals(methodName)
&& ASTHelpers.isSubtype(symbol.owner.type, containingTypeSupplier.get(state), state);
}

/**
* Is {@code varNode} a temporary variable representing the {@code Iterator} for an enhanced for
* loop? Matched based on the naming scheme used by Checker dataflow.
Expand Down