Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » TMF (Xtext) » Calling IReferenceFinder.findAllReferences in tests
Calling IReferenceFinder.findAllReferences in tests [message #1859831] Thu, 29 June 2023 09:16 Go to next message
Łukasz Grabski is currently offline Łukasz GrabskiFriend
Messages: 10
Registered: June 2023
Junior Member
Hi :)

I'm trying to use IReferenceFinder in one of my unit tests in order to find all the elements that refers to another element defined in the tree. In my test I'm using an injected ParseHelper instance to parse model from string, later on I pick the interested element from the tree and call findAllReferences on IReferenceFinder somehow like this:

ElementDeclarationNode element = result.getElements().get(0);
        var targetURIs = targetURIProvider.get();
        targetURIs.addURI(EcoreUtil.getURI(element));
        referenceFinder.findAllReferences(targetURIs, null, resourceDescriptions, new IReferenceFinder.Acceptor() {
            @Override
            public void accept(EObject source, URI sourceURI, EReference eReference, int index, EObject targetOrProxy, URI targetURI) {
                System.out.println();
            }

            @Override
            public void accept(IReferenceDescription description) {
                System.out.println();
            }
        }, new NullProgressMonitor());


resourceDescriptions is injected as well...
When I run this code the following exception appears:
java.lang.NullPointerException: Cannot invoke "org.eclipse.emf.ecore.resource.ResourceSet.getResources()" because "this.this$1.this$0.resourceSet" is null

	at org.eclipse.xtext.resource.impl.ResourceSetBasedResourceDescriptions$1$1.<init>(ResourceSetBasedResourceDescriptions.java:67)


I have some feeling it's like this due to the fact I dont have any real resource but just a string parsed... What can I do to make it work?

I appreciate any help here, thanks in advance :)
Re: Calling IReferenceFinder.findAllReferences in tests [message #1859832 is a reply to message #1859831] Thu, 29 June 2023 09:22 Go to previous messageGo to next message
Tamas Miklossy is currently offline Tamas MiklossyFriend
Messages: 160
Registered: February 2016
Senior Member
Hello Lukasz!

You can take some inspiration from the DotReferenceFinderTest.xtend test cases.

Hope that helps!
Tamás
Re: Calling IReferenceFinder.findAllReferences in tests [message #1859835 is a reply to message #1859832] Thu, 29 June 2023 09:55 Go to previous messageGo to next message
Łukasz Grabski is currently offline Łukasz GrabskiFriend
Messages: 10
Registered: June 2023
Junior Member
Hi Tamas,

Thank you for your quick reply :) Unfortunately ReferenceQueryExecutor seems to be from "ui" module that is generated for eclipse based DSL project while I'm rather using maven project structure instead - in this setup there is just generic "ide" and core xtext stuff generated :/
Re: Calling IReferenceFinder.findAllReferences in tests [message #1859840 is a reply to message #1859835] Thu, 29 June 2023 12:22 Go to previous message
Łukasz Grabski is currently offline Łukasz GrabskiFriend
Messages: 10
Registered: June 2023
Junior Member
Ha! I made it work, so my full test looks like this now:

 // given
        some dsl code here
                """;
        XtextResourceSet resourceSet = resourceSetProvider.get();

        // when
        Resource resource = loadResource("test.mydsl", script, resourceSet);

        // then
        assertThat(resource.getErrors()).isEmpty();

        // given
        var element = resource.getContents().get(0).eContents().get(0);
        URI uri = EcoreUtil2.getPlatformResourceOrNormalizedURI(element);

        TargetURIs targetURIs = targetURIConverter.fromIterable(List.of(uri));

        // when
        resourceSet.getResources().add(resource);
        var resourceDescriptions = resourceDescriptionsProvider.get();
        resourceDescriptions.setContext(resourceSet);
        var localResourceAccess = new SimpleLocalResourceAccess(resourceSet);
        referenceFinder.findAllReferences(targetURIs, localResourceAccess, resourceDescriptions, new IReferenceFinder.Acceptor() {
            @Override
            public void accept(EObject source, URI sourceURI, EReference eReference, int index, EObject targetOrProxy, URI targetURI) {
                System.out.println();
            }

            @Override
            public void accept(IReferenceDescription description) {
                System.out.println();
            }
        }, new NullProgressMonitor());


I also had to create my own implementation of SimpleLocalResourceAccess (I copied it from here: https://github.com/eclipse/xtext/blob/main/org.eclipse.xtext.ui/src/org/eclipse/xtext/ui/editor/findrefs/SimpleLocalResourceAccess.java

and now everything works as expected :)
Previous Topic:Library in VS Code extension
Next Topic:[SOLVED] A (very stupid?) beginner problem
Goto Forum:
  


Current Time: Sat Jul 27 10:16:50 GMT 2024

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

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

Back to the top