Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » [SOLVED] Referencing Java methods by annotation
[SOLVED] Referencing Java methods by annotation [message #859865] Fri, 27 April 2012 21:09 Go to next message
Roberto Lo Giacco is currently offline Roberto Lo GiaccoFriend
Messages: 17
Registered: April 2012
Junior Member
Hi, first of all I'm a noob to Xtext and RPC so I will need a lot of details or your contribution Embarrassed

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 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
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 Go to previous messageGo to next message
Roberto Lo Giacco is currently offline Roberto Lo GiaccoFriend
Messages: 17
Registered: April 2012
Junior Member
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 Crying or Very Sad

# 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 #861177 is a reply to message #861133] Sat, 28 April 2012 12:09 Go to previous messageGo to next message
Christian Dietrich is currently offline Christian DietrichFriend
Messages: 14665
Registered: July 2009
Senior Member
Hi

(1) yes the ui project is the right place. create your custom hyperlinkhelper and bind it in the UIModule
(2) if you want to search the whole workspace i guess you dont need the IJavaProjectProvider - if you need it anyway simply let guice inject you and instance
(3) have a look at http://www.eclipse.org/forums/index.php/t/337562/

~Christian


Twitter : @chrdietrich
Blog : https://www.dietrich-it.de
Re: Referencing Java methods by annotation [message #862008 is a reply to message #861177] Sat, 28 April 2012 21:23 Go to previous messageGo to next message
Roberto Lo Giacco is currently offline Roberto Lo GiaccoFriend
Messages: 17
Registered: April 2012
Junior Member
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?
Re: Referencing Java methods by annotation [message #865557 is a reply to message #862008] Mon, 30 April 2012 13:08 Go to previous message
Roberto Lo Giacco is currently offline Roberto Lo GiaccoFriend
Messages: 17
Registered: April 2012
Junior Member
I have it working, thanks a lot Christian! If anyone need it, the implementation is on github, project bdd-eclipse, folder tree/master/org.rlogiacco.eclipse.cucumber.ui

Sorry, I can't post links yet.
Previous Topic:Adding comments using the JvmModelInferrer
Next Topic:semantic change to resource with unresolved references
Goto Forum:
  


Current Time: Fri Apr 19 05:07:20 GMT 2024

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

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

Back to the top