diff --git a/java.source/src/org/netbeans/api/java/source/SourceUtils.java b/java.source/src/org/netbeans/api/java/source/SourceUtils.java --- a/java.source/src/org/netbeans/api/java/source/SourceUtils.java +++ b/java.source/src/org/netbeans/api/java/source/SourceUtils.java @@ -115,6 +115,7 @@ import org.openide.filesystems.FileObject; import org.openide.filesystems.FileUtil; +import org.openide.filesystems.URLMapper; import org.openide.util.Exceptions; import org.openide.util.Lookup; import org.openide.util.Parameters; @@ -600,14 +601,50 @@ */ @org.netbeans.api.annotations.common.SuppressWarnings(value={"DMI_COLLECTION_OF_URLS"}/*,justification="URLs have never host part"*/) //NOI18N public static Set getDependentRoots (final URL root) { - final Map> deps = IndexingController.getDefault().getRootDependencies(); - return getDependentRootsImpl (root, deps); + final Map> sourceDeps = IndexingController.getDefault().getRootDependencies(); + final Map> binaryDeps = IndexingController.getDefault().getBinaryRootDependencies(); + return getDependentRootsImpl (root, sourceDeps, binaryDeps); } @org.netbeans.api.annotations.common.SuppressWarnings(value={"DMI_COLLECTION_OF_URLS"}/*,justification="URLs have never host part"*/) //NOI18N - static Set getDependentRootsImpl (final URL root, final Map> deps) { - //Create inverse dependencies + static Set getDependentRootsImpl (final URL root, final Map> sourceDeps, Map> binaryDeps) { + Set urls; + + if (sourceDeps.containsKey(root)) { + urls = findReverseSourceRoots(root, sourceDeps); + } else { + FileObject rootFO = URLMapper.findFileObject(root); + + if (rootFO != null) { + urls = new HashSet(); + + for (URL binary : findBinaryRootsForSourceRoot(rootFO, binaryDeps)) { + List deps = binaryDeps.get(binary); + + if (deps != null) { + urls.addAll(deps); + } + } + } else { + urls = new HashSet(); + } + } + + //Filter non opened projects + Set cps = GlobalPathRegistry.getDefault().getPaths(ClassPath.SOURCE); + Set toRetain = new HashSet(); + for (ClassPath cp : cps) { + for (ClassPath.Entry e : cp.entries()) { + toRetain.add(e.getURL()); + } + } + urls.retainAll(toRetain); + return urls; + } + + private static Set findReverseSourceRoots(final URL thisSourceRoot, Map> deps) { + //Create inverse dependencies final Map> inverseDeps = new HashMap> (); for (Map.Entry> entry : deps.entrySet()) { final URL u1 = entry.getKey(); @@ -624,7 +661,7 @@ //Collect dependencies final Set result = new HashSet(); final LinkedList todo = new LinkedList (); - todo.add (root); + todo.add (thisSourceRoot); while (!todo.isEmpty()) { final URL u = todo.removeFirst(); if (!result.contains(u)) { @@ -635,19 +672,25 @@ } } } - //Filter non opened projects - Set cps = GlobalPathRegistry.getDefault().getPaths(ClassPath.SOURCE); - Set toRetain = new HashSet(); - for (ClassPath cp : cps) { - for (ClassPath.Entry e : cp.entries()) { - toRetain.add(e.getURL()); + + return result; + } + + private static Set findBinaryRootsForSourceRoot(FileObject sourceRoot, Map> binaryDeps) { + Set result = new HashSet(); + + for (URL bin : binaryDeps.keySet()) { + for (FileObject s : SourceForBinaryQuery.findSourceRoots(bin).getRoots()) { + if (s == sourceRoot) { + result.add(bin); + } } } - result.retainAll(toRetain); + return result; - } - - //Helper methods + } + + //Helper methods /** * Returns classes declared in the given source file which have the main method. diff --git a/java.source/test/unit/src/org/netbeans/api/java/source/SourceUtilsTest.java b/java.source/test/unit/src/org/netbeans/api/java/source/SourceUtilsTest.java --- a/java.source/test/unit/src/org/netbeans/api/java/source/SourceUtilsTest.java +++ b/java.source/test/unit/src/org/netbeans/api/java/source/SourceUtilsTest.java @@ -48,6 +48,7 @@ import java.io.IOException; import java.net.URL; import java.util.Arrays; +import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.List; @@ -293,24 +294,25 @@ final Map> deps = new HashMap> (); deps.put (url1,deps0); + deps.put (url2,Collections.emptyList()); deps.put (url3,deps1); deps.put (url4,deps2); deps.put (url5,deps3); - Set result = SourceUtils.getDependentRootsImpl(url5, deps); + Set result = SourceUtils.getDependentRootsImpl(url5, deps, Collections.>emptyMap()); assertEquals (1, result.size()); assertEquals (url5,result.iterator().next()); - result = SourceUtils.getDependentRootsImpl(url4, deps); + result = SourceUtils.getDependentRootsImpl(url4, deps, Collections.>emptyMap()); assertEquals (new URL[] {url4, url5}, result); - result = SourceUtils.getDependentRootsImpl(url3, deps); + result = SourceUtils.getDependentRootsImpl(url3, deps, Collections.>emptyMap()); assertEquals (new URL[] {url3, url5}, result); - result = SourceUtils.getDependentRootsImpl(url2, deps); + result = SourceUtils.getDependentRootsImpl(url2, deps, Collections.>emptyMap()); assertEquals (new URL[] {url2, url3, url4, url5}, result); - result = SourceUtils.getDependentRootsImpl(url1, deps); + result = SourceUtils.getDependentRootsImpl(url1, deps, Collections.>emptyMap()); assertEquals (new URL[] {url1, url3, url5}, result); } diff --git a/refactoring.java/src/org/netbeans/modules/refactoring/java/RetoucheUtils.java b/refactoring.java/src/org/netbeans/modules/refactoring/java/RetoucheUtils.java --- a/refactoring.java/src/org/netbeans/modules/refactoring/java/RetoucheUtils.java +++ b/refactoring.java/src/org/netbeans/modules/refactoring/java/RetoucheUtils.java @@ -609,32 +609,27 @@ assert files.length >0; Set dependentRoots = new HashSet(); for (FileObject fo: files) { - Project p = null; + ClassPath cp = null; FileObject ownerRoot = null; if (fo != null) { - p = FileOwnerQuery.getOwner(fo); - ClassPath cp = ClassPath.getClassPath(fo, ClassPath.SOURCE); + cp = ClassPath.getClassPath(fo, ClassPath.SOURCE); if (cp!=null) { ownerRoot = cp.findOwnerRoot(fo); } } - if (p != null && ownerRoot != null) { + if (cp != null && ownerRoot != null) { URL sourceRoot = URLMapper.findURL(ownerRoot, URLMapper.INTERNAL); if (dependencies) { dependentRoots.addAll(SourceUtils.getDependentRoots(sourceRoot)); } else { dependentRoots.add(sourceRoot); } - final Set toExclude = new HashSet(Arrays.asList(UnitTestForSourceQuery.findSources(ownerRoot))); - for (SourceGroup root : ProjectUtils.getSources(p).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA)) { - final URL rootURL = URLMapper.findURL(root.getRootFolder(), URLMapper.INTERNAL); - if (!toExclude.contains(rootURL)) { - dependentRoots.add(rootURL); - } + for (FileObject f : cp.getRoots()) { + dependentRoots.add(URLMapper.findURL(f, URLMapper.INTERNAL)); } } else { - for(ClassPath cp: GlobalPathRegistry.getDefault().getPaths(ClassPath.SOURCE)) { - for (FileObject root:cp.getRoots()) { + for(ClassPath scp: GlobalPathRegistry.getDefault().getPaths(ClassPath.SOURCE)) { + for (FileObject root:scp.getRoots()) { dependentRoots.add(URLMapper.findURL(root, URLMapper.INTERNAL)); } }