简体   繁体   English

Eclipse 插件 - Class object

[英]Eclipse plugin - Class object

I am creating an eclipse plugin, and I need Class object of selected file, not IType.我正在创建一个 eclipse 插件,我需要 Class object 的选定文件,而不是 IType。 Is it possible, and how is the best way to do it?有可能吗,最好的方法是什么?

Edit: when I think about it, the best way is to add it like run as (like junit, profiler, or other plugins are doing).编辑:当我想到它时,最好的方法是像运行一样添加它(就像 junit、分析器或其他插件正在做的那样)。 I suppose they must have access to Class (if X is class in question), because they are running it's functions.我想他们必须有权访问 Class (如果 X 是 class 有问题),因为他们正在运行它的功能。 So how to create plugin that has "run as " action, and get live object?那么如何创建具有“运行方式”操作的插件,并获得实时 object?

In an eclipse plugin, you will, for instance, get the selected file through an IAction .例如,在 eclipse 插件中,您将通过IAction获取所选文件。
(it represents the non-UI side of a command which can be triggered by the end user. Actions are typically associated with buttons, menu items, and items in tool bars.) (它代表可由最终用户触发的命令的非 UI 端。操作通常与按钮、菜单项和工具栏中的项相关联。)

From there:从那里:

IResource selectedResource = ResourceUtils.getSelectedResource();

IResource The workspace analog of file system files and directories. IResource文件系统文件和目录的工作区模拟。 There are exactly four types of resource: files, folders, projects and the workspace root.有四种类型的资源:文件、文件夹、项目和工作区根。

From its type, you can cast it into an IFile , which gives you acces to its full path ( getFullPath() )根据它的类型,您可以将其转换为IFile ,这使您可以访问其完整路径( getFullPath()

Eclipse uses an abstract representation of the object being selected, be it a file (IResource) or be it a Java Type (IJavaType). Eclipse 使用选择的 object 的抽象表示,无论是文件(IResource)还是 Java 类型(IJavaType)。 As it is not required for a source file to be compiled (eg disabling auto build), there does not necessarily be a.class file or a Class object for the code being edited.由于不需要编译源文件(例如禁用自动构建),因此正在编辑的代码不一定有 .class 文件或 Class object 文件。 Hence, there is no correct way to get a "Class" object from the a selection in the user interface.因此,没有正确的方法可以从用户界面中的选择中获取“类”object。

However, as yesterday mentions, you could rely on the fact that the Eclipse builder mechanism will always compile the source files immediately and thus a.class file exists.但是,正如昨天提到的,您可以依靠 Eclipse 构建器机制将始终立即编译源文件,因此存在 .class 文件这一事实。 To reach to this.class file at runtime, you would need to create a dynamic class loader for the project or start a runtime VM.要在运行时访问 this.class 文件,您需要为项目创建动态 class 加载程序或启动运行时 VM。 I tried that and it does work, but it is a very unstable approach and can lead to various hard to trace failures.我试过了,它确实有效,但这是一种非常不稳定的方法,可能会导致各种难以追踪的故障。

The classname of an IType "curIType" can be retrieved through IType "curIType" 的类名可以通过

curIType.getFullyQualifiedName()

That's the simple part.这是简单的部分。 But then you have the problem, that this class does not have to be in the classloader of your plugin (if it's a class of one of the userprojects, it's seldom part of your classloader).但是你有一个问题,这个 class 不必在你的插件的类加载器中(如果它是一个用户项目的 class,它很少是你的类加载器的一部分)。 So calling Class.forName(classname) won't do any good.所以调用 Class.forName(classname) 不会有任何好处。

I had a similar case and did (in a first attempt) solve it by creating an own thread with an own classloader, which included all libraries of the current classloader and all libraries of the type's project.我有一个类似的案例,并且(在第一次尝试中)通过使用自己的类加载器创建一个自己的线程来解决它,其中包括当前类加载器的所有库和该类型项目的所有库。 That's neither a short code nor a simple one and I've already refactored it.这既不是一个简短的代码也不是一个简单的代码,我已经对其进行了重构。 It's much simpler to get all information out of the IType and not using the classes anywhere in the plugincode.从 IType 中获取所有信息并且不在插件代码中的任何地方使用类要简单得多。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM