Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Get IJavaElement with package and class name
icon4.gif  Get IJavaElement with package and class name [message #901368] Sat, 11 August 2012 11:32 Go to next message
xelon xelon is currently offline xelon xelon
Messages: 1
Registered: August 2012
Junior Member
Hi,

I'm developing a plugin and I'm stuck. I want to open a Java file which is in the workspace with a specific package and class name.

I found JavaUI.openInEditor and IDE.openEditor but they want IJavaElement or IFile as a parameter. But I only have package name and class name. User doesn't know about which project has this class.

Is there a way to find an IJavaElement with this parameters and open it in the editor?

Thanks...
Re: Get IJavaElement with package and class name [message #901548 is a reply to message #901368] Mon, 13 August 2012 06:39 Go to previous messageGo to next message
Dani Megert is currently offline Dani Megert
Messages: 3801
Registered: July 2009
Senior Member
On 11.08.2012 17:33, xelon xelon wrote:
> Hi,
>
> I'm developing a plugin and I'm stuck. I want to open a Java file
> which is in the workspace with a specific package and class name.
>
> I found JavaUI.openInEditor and IDE.openEditor but they want
> IJavaElement or IFile as a parameter. But I only have package name and
> class name. User doesn't know about which project has this class.
> Is there a way to find an IJavaElement with this parameters and open
> it in the editor?
Take a look at org.eclipse.jdt.core.JavaCore.create(*)

Dani
>
> Thanks...
Re: Get IJavaElement with package and class name [message #903466 is a reply to message #901548] Thu, 23 August 2012 13:53 Go to previous message
Benjamin Leipold is currently offline Benjamin Leipold
Messages: 13
Registered: July 2009
Junior Member
You can use JDT's search engine. Here is an example:

  IJavaSearchScope scope = SearchEngine.createWorkspaceScope();
  SearchEngine engine = new SearchEngine();
  SearchPattern pattern = SearchPattern.createPattern("some.Clazz",
            IJavaSearchConstants.TYPE, IJavaSearchConstants.DECLARATIONS,
            SearchPattern.R_EXACT_MATCH);
  SearchRequestor requestor = new SearchRequestor() {
    public void acceptSearchMatch(final SearchMatch match) throws CoreException {
      TypeDeclarationMatch typeMatch = (TypeDeclarationMatch) match;
      IJavaElement type = (IJavaElement) typeMatch.getElement();
      IFile file = (IFile) match.getResource();

      // do something

    }
  };
  try {
    engine.search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, scope, requestor, new NullProgressMonitor());
  } catch (final CoreException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
  }


You only have to replace the string some.Clazz with your package and class name. But keep in mind that there may be more than one match if the class is defined in multiple projects (since workspace scope). If you know the project in advance, you can narrow the search scope. And hopefully only get one match.
Previous Topic:Emacs key bindings, incremental find stopped working
Next Topic:File Locations
Goto Forum:
  


Current Time: Thu Aug 28 11:24:20 EDT 2014

Powered by FUDForum. Page generated in 0.09457 seconds