Find fields with SearchEngine [message #336149] |
Tue, 26 May 2009 20:25 ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
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 message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
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 message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
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 Go to previous message](theme/Solstice/images/up.png) |
Eclipse User![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=) |
|
|
|
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
|
|
|
Powered by
FUDForum. Page generated in 0.03262 seconds