|
|
Re: Referencing Java methods by annotation [message #861133 is a reply to message #859915] |
Sat, 28 April 2012 07:42   |
Eclipse User |
|
|
|
Hi Christian, I understand your answer but I don't know how to proceed:
# where should I start to implement such search? In my DSLUiModel class? And which method should I use? As I said, I'm a complete noob with Xtext
# the IJavaProjectProvider interface doesn't have such getJavaProject static method, should I get an instance injected?
# I've found on the JDT forum a thread that should provide a partial answer to `how to find the method with declared annotation` and it should be something like
SearchPattern pattern = SearchPattern.createPattern("Given", IJavaSearchConstants.annotation_TYPE, IJavaSearchConstants.REFERENCES, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
SearchRequestor requestor = new SearchRequestor() {
public void acceptSearchMatch(SearchMatch match) throws CoreException {
// do something each time a match is got...
}
};
new SearchEngine().search(pattern, new SearchParticipant[] {SearchEngine.getDefaultSearchParticipant()}, SearchEngine.createWorkspaceScope(), requestor, null);
# I don't know how to proceed implementing the IHyperlinkHelper, I've read the Hyperlinking documentation but with poor results. I thought that the first step would be to have the Given the user inputs Roberto as firstname part selectable by CTRL-click, then associate that action to right target. I'm still struggling with the first part while I think the second part can be JDT related as you suggested.
I know, I know, I'm annoying...
[Updated on: Sat, 28 April 2012 07:43] by Moderator
|
|
|
|
Re: Referencing Java methods by annotation [message #862008 is a reply to message #861177] |
Sat, 28 April 2012 17:23   |
Eclipse User |
|
|
|
Ok, thanks to your directions I now have the link highlighted and following the other thread I now have a SearchMatch for the method mapping!
I created a new implementation of IHyperlink to open the exact Java method, please check if I did something wrong performance wise:
public static Collection<IHyperlink> findLinkTargets(final String annotationValue, final Region region, String[] annotationNames) {
final List<IHyperlink> results = new ArrayList<IHyperlink>();
for (String annotationName : annotationNames) {
SearchPattern pattern = SearchPattern.createPattern(annotationName, IJavaSearchConstants.ANNOTATION_TYPE,
IJavaSearchConstants.ANNOTATION_TYPE_REFERENCE, SearchPattern.R_EXACT_MATCH | SearchPattern.R_CASE_SENSITIVE);
SearchRequestor requestor = new SearchRequestor() {
public void acceptSearchMatch(SearchMatch match) throws CoreException {
// TODO verify pattern
results.add(new JavaHyperlink("Open mapping", (IJavaElement) match.getElement(), region));
}
};
try {
new SearchEngine().search(pattern, new SearchParticipant[] { SearchEngine.getDefaultSearchParticipant() }, SearchEngine.createWorkspaceScope(),
requestor, null);
} catch (CoreException e) {
e.printStackTrace();
}
}
return results;
}
As you can see I now need to get the annotation value and verify the pattern in the annotation matches the string used in my DSL. I guess this can be quite resource intensive tho. Should I be worried by this?
|
|
|
|
Powered by
FUDForum. Page generated in 0.42634 seconds