Getting all .java files within a project
1) Currently, I'm resorting to an auxiliary map to get all .java files in a project:
final Map<String, VirtualFile> locationMap =
Maps.newHashMap(); // The inner does not have to be concurrent.
ProjectFileIndex.SERVICE.getInstance(project).iterateContent(new ContentIterator() {
@Override
public boolean processFile(VirtualFile fileOrDir) {
String cloudPathIndex = getCloudPathFromFile(project, fileOrDir);
if (!Strings.isNullOrEmpty(cloudPathIndex)) {
locationMap.put(cloudPathIndex, fileOrDir);
}
return true;
}
});
---
Ideally, such a map would already be built and I wouldn't need to perform this traversal myself. Is there an alternative to using ProjectFileIndex in this generic way?
2) Is there any class that gives me VirtualFiles indexed in a similar way to <package_name>[/.]<file_name>?
Thanks!
Please sign in to leave a comment.
FileBasedIndex.getInstance()
.getContainingFiles(
FileTypeIndex.NAME,
JavaFileType.INSTANCE,
GlobalSearchScope.allScope(project))
Thanks for your response.
I modified your code to give me just the files in my project:
FileBasedIndex.getInstance().getContainingFiles(FileTypeIndex.NAME, JavaFileType.INSTANCE, GlobalSearchScope.projectScope(project))
But the files are still indexed by their absolute path. e.g., file:///Users/joaomartins/IdeaProjects/JoaoDiscoProject3/jamjavatest/guestbook/src/test/java/ninja/joao/guestbook/GuestbookServletTest.java
I'd like files to be indexed by ninja/joao/guestbook/GuestbookServletTest.java (package/file.java), instead.
I haven't found any class that does this. Am I to understand no class offers this?
if you are looking for path relative to src, you could replace path up to src with "".
here is how to get src:
ProjectRootManager.getInstance(project).getContentSourceRoots();
What are you trying to accomplish? Collecting all .java files in a project in a single collection is unlikely to be an efficient way to solve your problem.
JavaPsiFacade.findClass(qualifiedName, project.getProjectScope).getVirtualFile()
Hello Dmitry,
let's say we'd like to build cache of Psi elements.
if getting a collection of all virtual files is not good, what is the recommended way of accessing all Psi elements in a project?
is this possible to access cache built by Idea?
Yes, of course it's possible. For example, you can use classes like JavaClassNameIndex and JavaFieldNameIndex.
I want to register a breakpoint on a file, but the breakpoint location can vary.
If I were to standardize breakpoint locations on something like com/package/name/Class.java (which is what you'd get from an agent that only know the class it's on right now), I would want a ServerToIDEFileResolver that, given a key like the full class name before, gives me the VirtualFile. That resolver would only need to resort to that collection once, and then whenever files are added/deleted.
Using the IDEA platform, I've been able to get all .java files for a project but indexed by their absolute path in the file system.
I will have a go at the JavaPsiFacade you mentioned. Thanks!
> you can use classes like JavaClassNameIndex and JavaFieldNameIndex.
.. and for custom languages, does Idea build cache for those too? Or are only built-in / supported languages being cached?
For a custom language, you can build your own indices with IDEA's help. http://www.jetbrains.org/intellij/sdk/docs/basics/indexing_and_psi_stubs.html
Thank you very much! I was not aware of FileBasedIndex
Cheers Dmitry
I'm finding unit testing FileBasedIndex a real headache.
I've got this piece of code:
I create my project like:
And then try to add files in many different ways:
However, project.getBaseDir() returns null.
What is the recommended way to add files to a test project?
Also, is it required that files are actually written to the file system? Reading the PlatformTestCase code, it seems that way, although I shouldn't need to write anything to disk to do unit testing in this case...
Thanks for your help.
Please see http://www.jetbrains.org/intellij/sdk/docs/basics/testing_plugins/test_project_and_testdata_directories.html which describes the proper way to do this.
Thanks, that shed some light into unit testing in IDEA.
There is one thing that's puzzling me: in my code, one of the ways I'm fetching files is
In my unit test, I'm adding a test file as
The path this file is added to is
private/var/folders/00/162_8000h01000cxqpysvccm004r99/T/unitTest_getFileFromPath_fullPath_525/testGetFileFromPath_fullPath0/path/to/prj/src/main/com/java/package/Class.java
whereas project.getBaseDir() returns
/private/var/folders/00/162_8000h01000cxqpysvccm004r99/T/unitTest_getFileFromPath_fullPath_525/unitTest0.tmp/path/to/prj/src/main/com/java/package/Class.java
... and the fileSystem fetch fails. This should work out of the box, without me having to hack anything for the files to have the same path. It is working when I run the plugin normally.
Do you have any suggestions for how to resolve this?
Thanks!