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!


14 comments
Comment actions Permalink

FileBasedIndex.getInstance()
.getContainingFiles(
      FileTypeIndex.NAME,
      JavaFileType.INSTANCE,
      GlobalSearchScope.allScope(project))

0
Comment actions Permalink

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?

0
Comment actions Permalink

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();

0
Comment actions Permalink

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.

0
Comment actions Permalink

JavaPsiFacade.findClass(qualifiedName, project.getProjectScope).getVirtualFile()

0
Comment actions Permalink

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?

0
Comment actions Permalink

Yes, of course it's possible. For example, you can use classes like JavaClassNameIndex and JavaFieldNameIndex.

0
Comment actions Permalink

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!

0
Comment actions Permalink

> 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?

0
Comment actions Permalink

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


0
Comment actions Permalink

Thank you very much! I was not aware of FileBasedIndex

Cheers Dmitry

0
Comment actions Permalink

I'm finding unit testing FileBasedIndex a real headache.

 

I've got this piece of code:

Collection<VirtualFile> projectJavaFiles = ideaFileIndex.getContainingFiles(
FileTypeIndex.NAME, JavaFileType.INSTANCE, GlobalSearchScope.projectScope(project));
for (VirtualFile virtualFile : projectJavaFiles) {
....
}

I create my project like:

project = PlatformTestCase.createProject("/full/fs/path", "creation_place");

And then try to add files in many different ways:

PlatformTestCase.createChildData(
project.getBaseDir(), "path/to/prj/src/main/com/java/package/Class.java");
project.getBaseDir().createChildData(this, "path/to/prj/src/main/com/java/package/Class.java");
PlatformTestCase.addSurvivingFiles(ImmutableList.of(javaFile1, javaFile2));

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.

0
Comment actions Permalink

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

VirtualFile file = fileSystem.findFileByPath(project.getBasePath() + "/" + path);

In my unit test, I'm adding a test file as

file1 = this.myFixture.addFileToProject(
"path/to/prj/src/main/com/java/package/Class.java", "");

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!

0

Please sign in to leave a comment.