diff --git a/java/spi.java.hints/src/org/netbeans/modules/java/hints/spiimpl/pm/CopyFinderBasedBulkSearch.java b/java/spi.java.hints/src/org/netbeans/modules/java/hints/spiimpl/pm/CopyFinderBasedBulkSearch.java index 87a77110fcdc..d77f1428c576 100644 --- a/java/spi.java.hints/src/org/netbeans/modules/java/hints/spiimpl/pm/CopyFinderBasedBulkSearch.java +++ b/java/spi.java.hints/src/org/netbeans/modules/java/hints/spiimpl/pm/CopyFinderBasedBulkSearch.java @@ -23,14 +23,12 @@ import com.sun.source.util.TreePath; import java.io.InputStream; import java.util.Collection; -import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.Map; import java.util.Map.Entry; import java.util.concurrent.atomic.AtomicBoolean; -import javax.lang.model.type.TypeMirror; import org.netbeans.api.java.source.CompilationInfo; import org.netbeans.modules.java.hints.providers.spi.HintDescription.AdditionalQueryConstraints; import org.netbeans.api.java.source.matching.Matcher; @@ -51,18 +49,17 @@ public CopyFinderBasedBulkSearch() { @Override public Map> match(CompilationInfo info, AtomicBoolean cancel, TreePath toSearch, BulkPattern pattern, Map timeLog) { Parameters.notNull("info", info); + Map> result = new HashMap<>(); TreePath topLevel = new TreePath(info.getCompilationUnit()); - - for (Entry e : ((BulkPatternImpl) pattern).pattern2Code.entrySet()) { - for (Occurrence od : Matcher.create(info).setCancel(new AtomicBoolean()).setUntypedMatching().setCancel(cancel).match(Pattern.createPatternWithFreeVariables(new TreePath(topLevel, e.getKey()), Collections.emptyMap()))) { - Collection c = result.get(e.getValue()); + Matcher matcher = Matcher.create(info) + .setUntypedMatching() + .setCancel(cancel); - if (c == null) { - result.put(e.getValue(), c = new LinkedList<>()); - } - - c.add(od.getOccurrenceRoot()); + for (Entry e : ((BulkPatternImpl) pattern).pattern2Code.entrySet()) { + for (Occurrence od : matcher.match(Pattern.createPatternWithFreeVariables(new TreePath(topLevel, e.getKey()), Map.of()))) { + result.computeIfAbsent(e.getValue(), k -> new LinkedList<>()) + .add(od.getOccurrenceRoot()); } } @@ -71,8 +68,20 @@ public Map> match(CompilationInfo info, AtomicBoole @Override public boolean matches(CompilationInfo info, AtomicBoolean cancel, TreePath toSearch, BulkPattern pattern) { - //XXX: performance - return !match(info, cancel, toSearch, pattern).isEmpty(); + Parameters.notNull("info", info); + + TreePath topLevel = new TreePath(info.getCompilationUnit()); + Matcher matcher = Matcher.create(info) + .setUntypedMatching() + .setCancel(cancel); + + for (Tree tree : ((BulkPatternImpl) pattern).pattern2Code.keySet()) { + if (!matcher.match(Pattern.createPatternWithFreeVariables(new TreePath(topLevel, tree), Map.of())).isEmpty()) { + return true; + } + } + + return false; } @Override @@ -109,7 +118,7 @@ private static final class BulkPatternImpl extends BulkPattern { private final Map pattern2Code; public BulkPatternImpl(Collection additionalConstraints, Map pattern2Code) { - super(new LinkedList(pattern2Code.values()), null, null, new LinkedList(additionalConstraints)); + super(new LinkedList<>(pattern2Code.values()), null, null, new LinkedList<>(additionalConstraints)); this.pattern2Code = pattern2Code; } diff --git a/java/spi.java.hints/src/org/netbeans/spi/java/hints/JavaFixUtilities.java b/java/spi.java.hints/src/org/netbeans/spi/java/hints/JavaFixUtilities.java index 600e62a2a471..abf89699734a 100644 --- a/java/spi.java.hints/src/org/netbeans/spi/java/hints/JavaFixUtilities.java +++ b/java/spi.java.hints/src/org/netbeans/spi/java/hints/JavaFixUtilities.java @@ -1846,9 +1846,11 @@ public Void scan(Tree tree, Void p) { if (tree == assignmentTree.getVariable()) { if (!info.getTreeUtilities().isExpressionStatement(assignmentTree.getExpression()) && canHaveSideEffects(assignmentTree.getExpression())) { ret.set(false); + return null; } } else { ret.set(false); + return null; } } case AND_ASSIGNMENT, DIVIDE_ASSIGNMENT, LEFT_SHIFT_ASSIGNMENT, MINUS_ASSIGNMENT, @@ -1858,13 +1860,16 @@ public Void scan(Tree tree, Void p) { if (tree == compoundAssignmentTree.getVariable()) { if (!info.getTreeUtilities().isExpressionStatement(compoundAssignmentTree.getExpression()) && canHaveSideEffects(compoundAssignmentTree.getExpression())) { ret.set(false); + return null; } } else { ret.set(false); + return null; } } default -> { ret.set(false); + return null; } } } @@ -1886,6 +1891,7 @@ public Void scan(Tree tree, Void p) { case METHOD_INVOCATION, NEW_CLASS, POSTFIX_DECREMENT, POSTFIX_INCREMENT, PREFIX_DECREMENT, PREFIX_INCREMENT -> { ret.set(true); + return null; } } }