Searching for resources in the classpath

Hi.

Is there an API like PsiManager.findClass() but for finding resources(not classes) in the classpath ?

6 comments

Please, anyone ?

I'm looking for a solution but i'm only hitting dead ends.

0

Hello Hugo,

HP> Is there an API like PsiManager.findClass() but for finding
HP> resources(not classes) in the classpath ?

There is no API that does that directly, however, you can implement this
by yourself. Here's how:

GlobalSearchScope scope = GlobalSearchScope.moduleWithDependenciesAndLibrariesScope(myModule);
final VirtualFile[] files = ProjectRootManager.getInstance(project).getFileIndex().getDirectoriesByPackageName(packageName,
false);
for(VirtualFile file: files) {
final VirtualFile child = file.findChild(fileName);
if (child != null && scope.contains(child)) {
return child;
}
}


--
Dmitry Jemerov
Software Developer
JetBrains, Inc.
http://www.jetbrains.com
"Develop with pleasure!"


0

That code doesn't seem to preserve the classpath search semantics.

I need for the order of the jars in the classpath to work just as searching for classes. I can't quite figure out in what order the directories being put in the array. As far as jar files are concerned the classpath order seems to be working, but i'm a little confused with the source paths. If i have a project with two source paths(src and src2), "src" comes first in the list which is fine, but "src2" comes last after all the jars in the classpath.

I'm going to create an issue request for an API with this functionality but i'd appreciate some help getting a workaround working.

Thanks again.

0

I think i managed to find a workaround.

Thanks again.

0

would you mind sharing your solution? It might come in handy some time!

0

Sure, i just changed the code Dmitry provided to use ModuleRootManager instead of ProjectRootManager. It returns the resources in the correct order.

0

Please sign in to leave a comment.