Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Language IDEs » Java Development Tools (JDT) » Help on JDT search engine to find the method reference in a project
Help on JDT search engine to find the method reference in a project [message #639443] Tue, 16 November 2010 15:04 Go to next message
alex  is currently offline alex Friend
Messages: 6
Registered: September 2010
Junior Member
Hi all,

I want to create a Java console program to find the method reference with JDT search engine. The code is like below:

-------------------------------------------------------
import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.core.search.SearchParticipant;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.core.search.SearchRequestor;

public class Test
{

private void testSearchEngine() throws CoreException{
// search pattern
String pattern = "StudentImpl.getName() void";
SearchPattern searchPattern =
SearchPattern.createPattern(pattern,
IJavaSearchConstants.METHOD,
IJavaSearchConstants.REFERENCES,
SearchPattern.R_EXACT_MATCH);

// search scope
IJavaSearchScope scope = SearchEngine.createWorkspaceScope();

// search requestor
final List<IMethod> methodList = new ArrayList<IMethod>();
SearchRequestor requestor = new SearchRequestor() {
public void acceptSearchMatch (SearchMatch match)
{
Object element = match.getElement();
if (match.getElement() instanceof IMethod) {
methodList.add((IMethod)element);
}
}
};

// search engine
SearchEngine searchEngine = new SearchEngine();
searchEngine.search(searchPattern,
new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant() },
scope,
requestor,
new NullProgressMonitor());

System.out.println("-- founded reference: " + methodList.size());
}
/**
* @param args
* @throws CoreException
*/
public static void main (String[] args) throws CoreException
{
Test test = new Test();
test.testSearchEngine();
}

}
--------------------------------------------------

But when I run this Java console program, it throws a NPE as below:
Exception in thread "main" java.lang.NullPointerException
at org.eclipse.jdt.internal.core.JavaModelManager.getInfo(JavaM odelManager.java:1322)
at org.eclipse.jdt.internal.core.JavaElement.getElementInfo(Jav aElement.java:247)
at org.eclipse.jdt.internal.core.JavaElement.getElementInfo(Jav aElement.java:235)
at org.eclipse.jdt.internal.core.JavaElement.getChildren(JavaEl ement.java:190)
at org.eclipse.jdt.internal.core.JavaElement.getChildrenOfType( JavaElement.java:204)
at org.eclipse.jdt.internal.core.JavaModel.getJavaProjects(Java Model.java:215)
at org.eclipse.jdt.internal.core.search.JavaWorkspaceScope.init ialize(JavaWorkspaceScope.java:81)
at org.eclipse.jdt.internal.core.search.JavaSearchScope.<init>(JavaSearchScope.java:61)
at org.eclipse.jdt.internal.core.search.JavaSearchScope.<init>(JavaSearchScope.java:57)
at org.eclipse.jdt.internal.core.search.JavaWorkspaceScope.<init >(JavaWorkspaceScope.java:29)
at org.eclipse.jdt.internal.core.JavaModelManager.getWorkspaceS cope(JavaModelManager.java:1729)
at org.eclipse.jdt.internal.core.search.BasicSearchEngine.creat eWorkspaceScope(BasicSearchEngine.java:155)
at org.eclipse.jdt.core.search.SearchEngine.createWorkspaceScop e(SearchEngine.java:397)
at Test.testSearchEngine(Test.java:30)
at Test.main(Test.java:61)
------------------------------------------------------------ ----------------------

So my question is:
(1) Can we use JDT search engine for writing a java console program?
(2) If we can, how to handle the above issues?

thanks in advance.
-Alexander
Re: Help on JDT search engine to find the method reference in a project [message #639540 is a reply to message #639443] Tue, 16 November 2010 20:17 Go to previous messageGo to next message
Olivier Thomann is currently offline Olivier ThomannFriend
Messages: 518
Registered: July 2009
Senior Member
You need to run in a headless Eclipse where JDT is properly initialized. You can create an Eclipse application and run your code inside this Eclipse application.
--
Olivier
Re: Help on JDT search engine to find the method reference in a project [message #639831 is a reply to message #639540] Thu, 18 November 2010 02:07 Go to previous messageGo to next message
alex  is currently offline alex Friend
Messages: 6
Registered: September 2010
Junior Member
Hi Olivier,

Thank you for your response. But when I try to run the program in Eclipse, it still throws such an error. I wonder whether I need to do some configuration in Eclipse? If possible, may you run the program in Eclipse and see whether this error happens in your environment?

thanks in advance.
Alexander
Re: Help on JDT search engine to find the method reference in a project [message #639835 is a reply to message #639831] Thu, 18 November 2010 03:11 Go to previous messageGo to next message
Olivier Thomann is currently offline Olivier ThomannFriend
Messages: 518
Registered: July 2009
Senior Member
You would need to run inside a headless eclipse. Not just run the program from within Eclipse.
__
Olivier
Re: Help on JDT search engine to find the method reference in a project [message #640428 is a reply to message #639835] Mon, 22 November 2010 03:31 Go to previous messageGo to next message
alex  is currently offline alex Friend
Messages: 6
Registered: September 2010
Junior Member
Thank you Olivier. As you know, I'm a totally newer to JDT, so may you give me some hint how to create the Headless Eclipse program? Any url help is welcomed.

thanks
-Alexander
Re: Help on JDT search engine to find the method reference in a project [message #641344 is a reply to message #640428] Thu, 25 November 2010 06:57 Go to previous messageGo to next message
Satyam Kandula is currently offline Satyam KandulaFriend
Messages: 444
Registered: July 2009
Senior Member
The search API can be called only through a plugin and so it essentially means you have to create a plugin. Googling on Eclipse plugin development should give you many sites which could help you how to develop plugins.
Re: Help on JDT search engine to find the method reference in a project [message #642009 is a reply to message #641344] Mon, 29 November 2010 14:27 Go to previous messageGo to next message
alex  is currently offline alex Friend
Messages: 6
Registered: September 2010
Junior Member
I just created one Eclipse plugin, and created one HelloWorld test program, it does work well.

But when I run the below code, the result is empty.
package com.ppm.helloworld;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.search.IJavaSearchConstants;
import org.eclipse.jdt.core.search.IJavaSearchScope;
import org.eclipse.jdt.core.search.SearchEngine;
import org.eclipse.jdt.core.search.SearchMatch;
import org.eclipse.jdt.core.search.SearchParticipant;
import org.eclipse.jdt.core.search.SearchPattern;
import org.eclipse.jdt.core.search.SearchRequestor;

public class TestSearchEngine {
public void testSearchEngine() throws CoreException{
// search pattern
String pattern = "com.ppm.helloworld.sample.StudentImpl.getName() void";
SearchPattern searchPattern =
SearchPattern.createPattern(pattern,
IJavaSearchConstants.METHOD,
IJavaSearchConstants.REFERENCES,
SearchPattern.R_EXACT_MATCH);

// search scope
IJavaSearchScope scope = SearchEngine.createWorkspaceScope();

// search requestor
final List<IMethod> methodList = new ArrayList<IMethod>();
SearchRequestor requestor = new SearchRequestor() {
public void acceptSearchMatch (SearchMatch match)
{
Object element = match.getElement();
if (match.getElement() instanceof IMethod) {
methodList.add((IMethod)element);
}
}
};

// search engine
SearchEngine searchEngine = new SearchEngine();
searchEngine.search(searchPattern,
new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant() },
scope,
requestor,
new NullProgressMonitor());

System.out.println("-- founded reference: " + methodList.size());
}
}
--------------------------------------------------

By the way, class com.ppm.helloworld.sample.StudentImpl is in the same project with the above search class.

Any hints?

thanks
Alexander
Re: Help on JDT search engine to find the method reference in a project [message #642378 is a reply to message #642009] Wed, 01 December 2010 06:43 Go to previous message
Satyam Kandula is currently offline Satyam KandulaFriend
Messages: 444
Registered: July 2009
Senior Member
The file should be in a project of the workspace that your eclipse app uses.
Previous Topic:Preferences keep resetting
Next Topic:Re: Debug JDK source code
Goto Forum:
  


Current Time: Tue Apr 23 10:38:09 GMT 2024

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

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

Back to the top