[SOLVED] Referencing Java methods by annotation [message #859865] |
Fri, 27 April 2012 21:09 |
|
Hi, first of all I'm a noob to Xtext and RPC so I will need a lot of details or your contribution
I'm trying to create an open source plugin to support Cucumber's feature file editing. I've managed so far to create the grammar (it has some warnings but it works!) and customize the outline, now I wish to link my DSL to Java.
Now, for those who are not familiar with Cucumber, in the custom grammar you write something like
Given the user inputs Roberto as firstname
then you can write a Java class method and annotate it with
@Given("^the user inputs (.*) as firstname$")
public void inputFirstname(String arg) {...}
I wish to:
1. CTRL-click on the former and get redirected to the latter
2. generate a warning if no match can be found
Is this possible? If it is, can someone contribute it to the project (eclipse-bdd on github) or help me code it?
Thank you very much,
Roberto
[Updated on: Mon, 30 April 2012 13:08] Report message to a moderator
|
|
|
Re: Referencing Java methods by annotation [message #859915 is a reply to message #859865] |
Fri, 27 April 2012 21:42 |
|
Hi,
from my point of view this is rather a JDT question and not an Xtext Question.
Xtext lets you cutomize Hyperlinks through IHyperlinkHelper
and it offers a JdtHyperlink to a IJavaElement
so the real problem is to get this JavaElement
in this case a IMethod.
=> How to get a matching one from jdt is the basic problem
to solve.
as access point to jdt you can use IJavaProjectProvider.getJavaProject
the rest is jdt specific
~Christian
Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
|
|
|
Re: Referencing Java methods by annotation [message #861133 is a reply to message #859915] |
Sat, 28 April 2012 11:42 |
|
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 11:43] Report message to a moderator
|
|
|
|
Re: Referencing Java methods by annotation [message #862008 is a reply to message #861177] |
Sat, 28 April 2012 21:23 |
|
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.02265 seconds