views:

256

answers:

3

I am creating an eclipse plugin, and I need Class object of selected file, not 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). I suppose they must have access to Class (if X is class in question), because they are running it's functions. So how to create plugin that has "run as " action, and get live object?

+1  A: 

In an eclipse plugin, you will, for instance, get the selected file through an 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.)

From there:

IResource selectedResource = ResourceUtils.getSelectedResource();

IResource The workspace analog of file system files and directories. 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())

VonC
A: 

The classname of an IType "curIType" can be retrieved through

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). So calling Class.forName(classname) won't do any good.

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.

hoess_c
+1  A: 

Eclipse uses an abstract representation of the object being selected, be it a file (IResource) or be it a Java Type (IJavaType). As it is not required for a source file to be compiled (e.g. disabling auto build), there does not necessarily be a .class file or a Class object for the code being edited. Hence, there is no correct way to get a "Class" object from the a selection in the user interface.

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. 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. I tried that and it does work, but it is a very unstable approach and can lead to various hard to trace failures.

mhaller
So for example JUNIT isn't using any elegant way, but instead some complicated approach? (as they certainly must have compiled sources and loaded also)
Sarmun
JUnit is starting a new VM and that VM has the compiled classes on its classpath. I'm not sure how JUnitMax is doing the realtime testing, whether they use dynamic class loading or keep a VM in some sort of pool.
mhaller