Calling IReferenceFinder.findAllReferences in tests [message #1859831] |
Thu, 29 June 2023 09:16 |
Łukasz Grabski 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 #1859840 is a reply to message #1859835] |
Thu, 29 June 2023 12:22 |
Łukasz Grabski 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 :)
|
|
|
Powered by
FUDForum. Page generated in 0.03365 seconds