从Eclipse Plugin的独立Java文件获取ICompilationUnit / ITypeRoot [英] Get ICompilationUnit/ITypeRoot from standalone java file for Eclipse Plugin
问题描述
此插件将每个Java文件作为 ICompilationUnit
。
但是,在我的方法中,我只能得到一个 IFile
的实例。
可以从这个 IFile
对象中创建一个 ICompilationUnit
作为替代, ITypeRoot
也可能正常工作。我看到这是使用以下编辑器直接创建的:
IJavaElement input = JavaUI.getEditorInputJavaElement(editorInput);
if(input instanceof ITypeRoot){
return(ITypeRoot)input;
}
这种方法甚至与非Java项目(来自通用项目/目录的java文件)在编辑器中打开),这使我相信可以直接为文件实现类似的东西。
JavaCore.create(IFile)
将返回一个 ICompilationUnit
,如果这适用于该文件:
IFile file = ...
IJavaElement element = JavaCore.create(file);
if(element instanceof ICompilationUnit){
ICompilationUnit compUnit =(ICompilationUnit)element;
...
}
可能会返回其他对象,例如.class文件的 IClassFile
。
I am hacking an Eclipse Plugin that is working perfectly for Java files in Java projects. I want to make it work for Java files in any kind of project.
This plugin processes each Java file as a ICompilationUnit
.
However, in my approach I can only get an instance of IFile
.
How can I create a ICompilationUnit
from this IFile
object?
As alternative a ITypeRoot
might also work. I've seen this being created directly from the editor using the following:
IJavaElement input= JavaUI.getEditorInputJavaElement(editorInput);
if (input instanceof ITypeRoot) {
return (ITypeRoot) input;
}
This approach works even with non Java projects (java files from generic projects/directories that are opened in the editor), which makes me believe that something similar can be achieved for files directly.
JavaCore.create(IFile)
will return an ICompilationUnit
if that is appropriate for the file:
IFile file = ...
IJavaElement element = JavaCore.create(file);
if (element instanceof ICompilationUnit) {
ICompilationUnit compUnit = (ICompilationUnit)element;
...
}
Other objects may be returned, for example IClassFile
for a .class file.
这篇关于从Eclipse Plugin的独立Java文件获取ICompilationUnit / ITypeRoot的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!