[英]Get ICompilationUnit/ITypeRoot from standalone java file for Eclipse Plugin
I am hacking an Eclipse Plugin that is working perfectly for Java files in Java projects. 我正在破解一个Eclipse插件,该插件非常适合Java项目中的Java文件。 I want to make it work for Java files in any kind of project.
我想使它适用于任何类型的项目中的Java文件。
This plugin processes each Java file as a ICompilationUnit
. 该插件将每个Java文件作为
ICompilationUnit
。 However, in my approach I can only get an instance of IFile
. 但是,在我的方法中,我只能获得
IFile
的实例。
How can I create a ICompilationUnit
from this IFile
object? 如何从此
IFile
对象创建ICompilationUnit
?
As alternative a ITypeRoot
might also work. 或者,也可以使用
ITypeRoot
。 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. 这种方法甚至适用于非Java项目(在编辑器中打开的来自通用项目/目录的Java文件),这使我相信可以直接为文件实现类似的功能。
JavaCore.create(IFile)
will return an ICompilationUnit
if that is appropriate for the file: 如果适用于该文件,则
JavaCore.create(IFile)
将返回ICompilationUnit
:
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. 可能会返回其他对象,例如,.class文件的
IClassFile
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.