简体   繁体   English

从独立的Java文件中为Eclipse插件获取ICompilationUnit / ITypeRoot

[英]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.

相关问题 如何从ICompilationUnit获取文件路径信息? - How to get file path information from ICompilationUnit? 开发Eclipse:如何从ICompilationUnit获取所有继承的方法? - Developing a Eclipse: How to get all inherited methods from a ICompilationUnit? 从PackageFragment获取后如何打开ICompilationUnit? 爪哇 - How to get ICompilationUnit open after getting from PackageFragment? Java JDT - 如何从 CompilationUnit 获取 ICompilationUnit - JDT - How to get ICompilationUnit from CompilationUnit Eclipse插件开发找出ICompilationUnit属于哪个workingSet - Eclipse Plugin Developement find out which workingSet an ICompilationUnit belongs to 如何从ICompilationUnit获取IEditorPart - How can I get IEditorPart from ICompilationUnit 当您从插件内的Eclipse工作台调用Java独立应用程序时,这表明进程间通信吗? - When you call a java standalone application from an eclipse workbench inside a plugin, this indicates interprocess communication? 是否有一个Java反编译器(无论是独立的还是Eclipse插件)能够反编译由AspectJ编织的代码? - Is there a Java decompiler (be it standalone or an Eclipse plugin) that is able to decompile code woven by AspectJ? astparser如何在setSource函数中将.java文件设置为ICompilationUnit? - astparser how to set a .java file as an ICompilationUnit in setSource function? 无法从eclipse插件项目捆绑中获取图像文件URL - Unable to get Image file URL from eclipse plugin project Bundle
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM