Search code examples
javaintellij-ideaintellij-plugin

Intellij Idea Plugin get class-file of virtual file


I'm writing an Intellij extension and tehrefore i need to know the location of the class file of a specific virtual file. I'm loading the virtual file the following way:

public void actionPerformed(AnActionEvent e) {
    if (e.getProject() == null) return;
    FileEditorManager manager = FileEditorManager.getInstance(e.getProject());
    VirtualFile files[] = manager.getSelectedFiles();
    //Any files open?
    if (files.length == 0 || !files[0].getPath().toLowerCase().endsWith(".java"))
        return;
    analyse(files[0]);
}

Inside the analyse-function I want to show the content of the class file in a gui.

Is it possible to receive the class file from j given java-file or the psi-tree?


Solution

  • You can use FilenameIndex.getFilesByName() method to get the .class file. Since you know the name of the .java file, you can use that to derive the .class file name.

    But I don't think it will work since .class files are in target directory and IDEA does not index that directory. You can use VfsUtil.collectChildrenRecursively() method to get al the files in the target directory and later filter out the .class file you need.