Commits

Jan Lahoda committed d14379c

Correctly pass constraints to the CopyFinder.computeDuplicates (to prevent false occurrence reports).

  • Participants
  • Parent commits fbb6692

Comments (0)

Files changed (5)

File api/src/org/netbeans/modules/jackpot30/impl/hints/HintsInvoker.java

                 TreePath toplevel = new TreePath(info.getCompilationUnit());
                 TreePath patt = new TreePath(toplevel, p.getPattern());
 
-                for (Entry<TreePath, Pair<Map<String, TreePath>, Map<String, String>>> e : CopyFinder.computeDuplicates(info, patt, startAt, cancel).entrySet()) {
+                for (Entry<TreePath, Pair<Map<String, TreePath>, Map<String, String>>> e : CopyFinder.computeDuplicates(info, patt, startAt, cancel, p.getConstraints()).entrySet()) {
                     HintContext c = new HintContext(info, AbstractHint.HintSeverity.WARNING, e.getKey(), e.getValue().getA(), e.getValue().getB());
                     
                     for (HintDescription hd : patternHints.get(d)) {

File api/src/org/netbeans/modules/jackpot30/impl/pm/CopyFinder.java

     }
 
     //XXX: should probably also include designedTypeHack:
-    public static Map<TreePath, Pair<Map<String, TreePath>, Map<String, String>>> computeDuplicates(CompilationInfo info, TreePath searchingFor, TreePath scope, AtomicBoolean cancel) {
+    public static Map<TreePath, Pair<Map<String, TreePath>, Map<String, String>>> computeDuplicates(CompilationInfo info, TreePath searchingFor, TreePath scope, AtomicBoolean cancel, Map<String, TypeMirror> designedTypeHack) {
         CopyFinder f = new CopyFinder(searchingFor, info, cancel);
         
+        f.designedTypeHack = designedTypeHack;
+        
         f.scan(scope, null);
         
         return f.result;

File api/test/unit/src/org/netbeans/modules/jackpot30/impl/hints/HintsInvokerTest.java

                             "4:9-6:10:verifier:HINT");
     }
     
+    public void testPatternFalseOccurrence() throws Exception {
+        performAnalysisTest("test/Test.java",
+                            "|package test;\n" +
+                            "\n" +
+                            "public class Test {\n" +
+                            "     private int test(java.io.File f) {\n" +
+                            "         f.toURI().toURL();\n" +
+                            "     }\n" +
+                            "}\n");
+    }
+
     private static final Map<String, HintDescription> test2Hint;
 
     static {
 
         test2Hint.put("testPatternAssert1", HintDescription.create(HintDescription.PatternDescription.create("assert $1 : $2;", constraints), new WorkerImpl()));
         test2Hint.put("testPatternStatementAndSingleStatementBlockAreSame", HintDescription.create(HintDescription.PatternDescription.create("if ($1) return $2;", Collections.<String, String>emptyMap()), new WorkerImpl()));
+        test2Hint.put("testPatternFalseOccurrence", HintDescription.create(HintDescription.PatternDescription.create("$1.toURL()", Collections.singletonMap("$1", "java.io.File")), new WorkerImpl()));
     }
 
     @Override

File api/test/unit/src/org/netbeans/modules/jackpot30/impl/pm/CopyFinderTest.java

         prepareTest(code, -1);
 
         Tree patternTree = Pattern.parseAndAttribute(info, pattern, Collections.<String, TypeMirror>emptyMap(), new Scope[1]);
-        Map<TreePath, Pair<Map<String, TreePath>, Map<String, String>>> result = CopyFinder.computeDuplicates(info, new TreePath(new TreePath(info.getCompilationUnit()), patternTree), new TreePath(info.getCompilationUnit()), new AtomicBoolean());
+        Map<TreePath, Pair<Map<String, TreePath>, Map<String, String>>> result = CopyFinder.computeDuplicates(info, new TreePath(new TreePath(info.getCompilationUnit()), patternTree), new TreePath(info.getCompilationUnit()), new AtomicBoolean(), Collections.<String, TypeMirror>emptyMap());
 
         assertSame(1, result.size());
 
 
 //    @Override
     protected Collection<TreePath> computeDuplicates(TreePath path) {
-        return CopyFinder.computeDuplicates(info, path, new TreePath(info.getCompilationUnit()), new AtomicBoolean()).keySet();
+        return CopyFinder.computeDuplicates(info, path, new TreePath(info.getCompilationUnit()), new AtomicBoolean(), Collections.<String, TypeMirror>emptyMap()).keySet();
     }
 
     private void performTest(String code) throws Exception {

File api/test/unit/src/org/netbeans/modules/jackpot30/impl/pm/OrigCopyFinderTest.java

 
 import com.sun.source.util.TreePath;
 import java.util.Collection;
+import java.util.Collections;
 import java.util.concurrent.atomic.AtomicBoolean;
+import javax.lang.model.type.TypeMirror;
 
 /**
  *
 
     @Override
     protected Collection<TreePath> computeDuplicates(TreePath path) {
-        return CopyFinder.computeDuplicates(info, path, new TreePath(info.getCompilationUnit()), new AtomicBoolean()).keySet();
+        return CopyFinder.computeDuplicates(info, path, new TreePath(info.getCompilationUnit()), new AtomicBoolean(), Collections.<String, TypeMirror>emptyMap()).keySet();
     }
 
 }