4

I want to identify the type (java/c/...) of the currently selected project from an Eclipse plug-in; If possible some other information also (used libraries and what not), basically all the information surrounding the project.

I have been searching on google and here for a couple of hours now, to no avail. Can i even access the information i want without selecting some bogus Extension Point (none of the ones listed seem to fit what i want to do)? I mean, i don't want to actually add anything to Eclipse right now, i just want to print out that stuff into the console. (start small and stuff)

Sorry for the long rant, but i don't know how to better express my problem right now.

wlfbck
  • 554
  • 8
  • 22

1 Answers1

8

A lot of this information is stored in the .project file in the project folder. The correct way to access the information is via the IProject object for the project.

The IProject.getDescription method returns you a IProjectDescription containing information about the project.

To determine the type of project you need to look at the natures that are defined in the project description. The IProjectDescription.getNatureIds() method returns an array of the nature ids. A Java project will have the org.eclipse.jdt.core.javanature and a plug-in will have org.eclipse.pde.PluginNature (and a Java plug-in will have both these natures).

To find the IProject. If you have a selection in something like Package or Project Explorer try:

IResource resource = (IResource)Platform.getAdapterManager().getAdapter(obj, IResource.class);

or

IFile file = (IFile)Platform.getAdapterManager().getAdapter(obj, IFile.class);

the getProject() method of these interfaces gives you the IProject. You may also just be able to do:

IProject project = (IProject)Platform.getAdapterManager().getAdapter(obj, IProject.class);

Platform in the above is org.eclipse.core.runtime.Platform (there are other Platform classes in Eclipse so be sure to get the correct one).

If you just have a project name then use:

IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(name);
greg-449
  • 109,219
  • 232
  • 102
  • 145
  • 1
    First of all, thanks for your answer. But how do i even get the selected IProject? I got the activeworkbench and the selection service, problem is if i click my improvised button, the button is ofcourse the selected element. – wlfbck Nov 16 '13 at 22:20
  • 2
    Added info on finding `IProject` to answer – greg-449 Nov 17 '13 at 09:18
  • 1
    Thank you very much! Found what i needed thanks to your answer :) – wlfbck Nov 17 '13 at 14:22
  • Sorry, someone less competent here: you say "if you have a selection in something like Package or Project Explorer try:" + [Java line of code]... could you explain what I actually have to do to get such a line to run? Is it part of a `main` method in a .java file?? I'm lost! – mike rodent Jan 10 '18 at 21:01
  • @mikerodent This code has to be part of an Eclipse plugin installed in Eclipse. plugins don't use a `main` method. Code in plugins usually runs in response to some event, like a menu item being selected. – greg-449 Jan 10 '18 at 21:07