Merge pull request #3540 from mbien/referencedIn-fix
[jackpot] DefaultRuleUtilities::referencedIn fix for single variable matching
diff --git a/java/java.hints.declarative/src/org/netbeans/modules/java/hints/declarative/conditionapi/Matcher.java b/java/java.hints.declarative/src/org/netbeans/modules/java/hints/declarative/conditionapi/Matcher.java
index 3e2f58a..d87e171 100644
--- a/java/java.hints.declarative/src/org/netbeans/modules/java/hints/declarative/conditionapi/Matcher.java
+++ b/java/java.hints.declarative/src/org/netbeans/modules/java/hints/declarative/conditionapi/Matcher.java
@@ -21,6 +21,7 @@
import com.sun.source.tree.Tree;
import com.sun.source.util.TreePath;
+import com.sun.source.util.Trees;
import org.netbeans.api.java.source.support.ErrorAwareTreePathScanner;
import java.util.Collection;
import java.util.LinkedList;
@@ -90,26 +91,39 @@
return result[0];
}
+ @SuppressWarnings("BoxedValueEquality")
public boolean referencedIn(@NonNull Variable variable, @NonNull Variable in) {
- final Element e = ctx.ctx.getInfo().getTrees().getElement(ctx.getSingleVariable(variable));
+ final Trees trees = ctx.ctx.getInfo().getTrees();
+ final Element e = trees.getElement(ctx.getSingleVariable(variable));
if (e == null) { //TODO: check also error
return false;
}
for (TreePath tp : ctx.getVariable(in)) {
+
+ if (e.equals(trees.getElement(tp))) {
+ return true;
+ }
+
boolean occurs = new ErrorAwareTreePathScanner<Boolean, Void>() {
+ private boolean found = false;
@Override
public Boolean scan(Tree tree, Void p) {
+ if (found) {
+ return true; // fast path
+ }
+
if (tree == null) {
return false;
}
TreePath currentPath = new TreePath(getCurrentPath(), tree);
- Element currentElement = ctx.ctx.getInfo().getTrees().getElement(currentPath);
+ Element currentElement = trees.getElement(currentPath);
if (e.equals(currentElement)) {
- return true; //TODO: throwing an exception might be faster...
+ found = true;
+ return true;
}
return super.scan(tree, p);
diff --git a/java/java.hints.declarative/test/unit/src/org/netbeans/modules/java/hints/declarative/conditionapi/isReferencedIn.hint b/java/java.hints.declarative/test/unit/src/org/netbeans/modules/java/hints/declarative/conditionapi/isReferencedIn.hint
new file mode 100644
index 0000000..41e917e
--- /dev/null
+++ b/java/java.hints.declarative/test/unit/src/org/netbeans/modules/java/hints/declarative/conditionapi/isReferencedIn.hint
@@ -0,0 +1,30 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+int $a = 0;
+int $b = $c; :: !referencedIn($a, $c)
+=>
+int $b = $c;
+;;
+
+int $a = 1;
+java.util.Arrays.asList($args$); :: !referencedIn($a, $args$)
+=>
+java.util.Arrays.asList($args$);
+;;
diff --git a/java/java.hints.declarative/test/unit/src/org/netbeans/modules/java/hints/declarative/conditionapi/isReferencedIn.test b/java/java.hints.declarative/test/unit/src/org/netbeans/modules/java/hints/declarative/conditionapi/isReferencedIn.test
new file mode 100644
index 0000000..112b544
--- /dev/null
+++ b/java/java.hints.declarative/test/unit/src/org/netbeans/modules/java/hints/declarative/conditionapi/isReferencedIn.test
@@ -0,0 +1,75 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+%%TestCase is-referenced-1
+package test;
+public class Test {
+ private void t() {
+ int a = 0;
+ int b = 2 + 1;
+ }
+}
+%%=>
+package test;
+public class Test {
+ private void t() {
+ int b = 2 + 1;
+ }
+}
+%%TestCase is-referenced-2
+package test;
+public class Test {
+ private void t() {
+ int a = 0;
+ int b = a;
+ }
+}
+%%TestCase is-referenced-3
+package test;
+public class Test {
+ private void t() {
+ int a = 0;
+ int b = a + 1;
+ }
+}
+%%TestCase is-referenced-4
+package test;
+import java.util.Arrays;
+public class Test {
+ private void t() {
+ int a = 1;
+ Arrays.asList(0, 5, a);
+ }
+}
+%%TestCase is-referenced-5
+package test;
+import java.util.Arrays;
+public class Test {
+ private void t() {
+ int a = 1;
+ Arrays.asList(0, 5, 4);
+ }
+}
+%%=>
+package test;
+import java.util.Arrays;
+public class Test {
+ private void t() {
+ Arrays.asList(0, 5, 4);
+ }
+}