Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Eclipse Platform » Find fields with SearchEngine
Find fields with SearchEngine [message #336149] Tue, 26 May 2009 20:25 Go to next message
michel Labarre is currently offline michel LabarreFriend
Messages: 7
Registered: July 2009
Junior Member
Hello
My problem concerns the results of the SearchEngine used in a plugin
implementing CompletionAssist in javaEditor. My goal is obtain all fields
in a method. The following code permits to get all member class fields but
not the fields declared in the method. Is-it possible to do? What's failed
in my code ?

------------------------------------------------------------ ----------------
IWorkbenchPage page=PlatformUI.getWorkbench().getActiveWorkbenchWindow()
.getActivePage();
IJavaElement element= JavaUI.getEditorInputJavaElement(
page.getActiveEditor().getEditorInput());
IJavaSearchScope scope = SearchEngine.createJavaSearchScope(
new IJavaElement[] { element } ,true);
IEditorPart editor = page.getActiveEditor();
IFile file = ((IFileEditorInput) editor.getEditorInput()).getFile();
IJavaProject project = JavaCore.create(file.getProject());
SearchPattern searchPattern = SearchPattern.createPattern(
"*",
IJavaSearchConstants.FIELD,
IJavaSearchConstants.DECLARATIONS,
SearchPattern.R_PATTERN_MATCH);

final List<Object> list = new ArrayList<Object>();
SearchRequestor requestor = new SearchRequestor() {
public void acceptSearchMatch(SearchMatch match) {
Object element = match.getElement();
list.add((Object)element);
if (element instanceof IJavaElement) {
logger.debug("element is IJavaElement");
IJavaElement e = (IJavaElement)element;
logger.debug(e.getAdapter(IJavaElement.class));
logger.debug(e.getClass());
}
}
};
SearchEngine searchEngine = new SearchEngine();
try {
searchEngine.search(searchPattern,
new SearchParticipant[] {

SearchEngine.getDefaultSearchParticipant()},
scope,
requestor,
new NullProgressMonitor());
} catch (CoreException e) {
logger.debug(e.getMessage());
}
------------------------------------------------------------ ---------------
So, editing the following sample class :

------------------------------------------------------------ ---------------
public class cc {
private Object1 obj1= null;
private Object2 obj2= null;

/**
* Main
*/
public static void main(String[] args) {
new cc();
}
/**
* Constructor
*/
public cc() {
try {
....
Object3 obj3 = new Object3();
// Fills input datas
obj3.get <---------------- completion
}
}
}
------------------------------------------------------------ --------------
... when I ask completion, my code is executed but only obj1 and obj2 are
found... not obj3 in constructor...

Thanks for your help
Re: Find fields with SearchEngine [message #336188 is a reply to message #336149] Thu, 28 May 2009 12:51 Go to previous messageGo to next message
Dani Megert is currently offline Dani MegertFriend
Messages: 3802
Registered: July 2009
Senior Member
michel wrote:
> Hello
> My problem concerns the results of the SearchEngine used in a plugin
> implementing CompletionAssist in javaEditor. My goal is obtain all
> fields in a method. The following code permits to get all member class
> fields but not the fields declared in the method.
Fields cannot be declared inside a method.

Dani
> Is-it possible to do? What's failed in my code ?
>
> ------------------------------------------------------------ ----------------
>
> IWorkbenchPage
> page=PlatformUI.getWorkbench().getActiveWorkbenchWindow()
> .getActivePage();
> IJavaElement element= JavaUI.getEditorInputJavaElement(
> page.getActiveEditor().getEditorInput());
> IJavaSearchScope scope = SearchEngine.createJavaSearchScope(
> new IJavaElement[] { element } ,true);
> IEditorPart editor = page.getActiveEditor();
> IFile file = ((IFileEditorInput) editor.getEditorInput()).getFile();
> IJavaProject project = JavaCore.create(file.getProject());
> SearchPattern searchPattern = SearchPattern.createPattern(
> "*",
> IJavaSearchConstants.FIELD,
> IJavaSearchConstants.DECLARATIONS,
> SearchPattern.R_PATTERN_MATCH);
> final List<Object> list = new ArrayList<Object>();
> SearchRequestor requestor = new SearchRequestor() {
> public void acceptSearchMatch(SearchMatch match) {
> Object element = match.getElement();
> list.add((Object)element);
> if (element instanceof IJavaElement) {
> logger.debug("element is IJavaElement");
> IJavaElement e = (IJavaElement)element;
> logger.debug(e.getAdapter(IJavaElement.class));
> logger.debug(e.getClass());
> }
> }
> };
> SearchEngine searchEngine = new SearchEngine();
> try {
> searchEngine.search(searchPattern, new
> SearchParticipant[] {
> SearchEngine.getDefaultSearchParticipant()},
> scope, requestor,
> new NullProgressMonitor());
> } catch (CoreException e) {
> logger.debug(e.getMessage());
> }
> ------------------------------------------------------------ ---------------
>
> So, editing the following sample class :
>
> ------------------------------------------------------------ ---------------
>
> public class cc {
> private Object1 obj1= null;
> private Object2 obj2= null;
>
> /**
> * Main
> */
> public static void main(String[] args) {
> new cc();
> }
> /**
> * Constructor
> */
> public cc() {
> try {
> ....
> Object3 obj3 = new Object3();
> // Fills input datas
> obj3.get <---------------- completion
> }
> }
> }
> ------------------------------------------------------------ --------------
>
> .. when I ask completion, my code is executed but only obj1 and obj2
> are found... not obj3 in constructor...
>
> Thanks for your help
>
>
Re: Find fields with SearchEngine [message #336211 is a reply to message #336188] Fri, 29 May 2009 14:47 Go to previous messageGo to next message
michel Labarre is currently offline michel LabarreFriend
Messages: 7
Registered: July 2009
Junior Member
Thank you
Concerning the method to find the class of the object declared in the
method (obj3) what is the best method to to that ?
I have tried the codeSelect(offset, length) method on the compilation unit
(offset points to the 'o' of obj3, for a length of 4) but have no result.
But perhaps I have not understood any thing in the mecanism ...
Re: Find fields with SearchEngine [message #336212 is a reply to message #336211] Fri, 29 May 2009 17:34 Go to previous message
michel Labarre is currently offline michel LabarreFriend
Messages: 7
Registered: July 2009
Junior Member
Using AST (ASTVisitor) I have get a list of all components in the source.
I have always a problem.
It seems that ASTNodes relating all variables (or block, or try) in a
method A that contains an error doesn't pass in ASTVisitor methods. It is
very problematic during completion cause the target line is always in
error.
If I do completion in another method B, then the previous method A shows
all ASTNodes.

Is a solution exists ?
Thank you very much
Previous Topic:Setting up Bundle-RequiredExecutionEnvironment causes access restriction errors
Next Topic:Syntax Coloring for a Non-File based Editor
Goto Forum:
  


Current Time: Tue Apr 23 08:23:09 GMT 2024

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

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

Back to the top