Skip to main content



      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
Eclipse UserFriend
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
Eclipse UserFriend
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
Eclipse UserFriend
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: Wed Jul 16 21:45:30 EDT 2025

Powered by FUDForum. Page generated in 0.08646 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top