use of org.eclipse.xtext.linking.impl.IllegalNodeException in project xtext-core by eclipse.
the class LazyLinkingResourceTest method testEObjectReference.
@Test
public void testEObjectReference() throws Exception {
final EAnnotation source = EcoreFactory.eINSTANCE.createEAnnotation();
final EObject referencedObject = XtextFactory.eINSTANCE.createGrammar();
LazyLinkingResource res = new LazyLinkingResource();
res.setLinkingHelper(new LinkingHelper() {
@Override
public String getCrossRefNodeAsString(INode node, boolean convert) {
return node.getText();
}
});
res.setEncoder(new LazyURIEncoder() {
@Override
public boolean isCrossLinkFragment(Resource res, String s) {
return "foo".equals(s);
}
@Override
public Triple<EObject, EReference, INode> decode(Resource res, String uriFragment) {
return Tuples.create((EObject) source, EcorePackage.Literals.EANNOTATION__REFERENCES, (INode) new LeafNode());
}
});
res.setLinkingService(new ILinkingService() {
@Override
public List<EObject> getLinkedObjects(EObject context, EReference reference, INode node) throws IllegalNodeException {
return Lists.newArrayList(referencedObject);
}
});
res.setDiagnosticMessageProvider(new LinkingDiagnosticMessageProvider());
assertEquals(referencedObject, res.getEObject("foo"));
}
use of org.eclipse.xtext.linking.impl.IllegalNodeException in project xtext-core by eclipse.
the class LazyLinkingResource method getEObject.
/**
* @since 2.4
*/
protected EObject getEObject(String uriFragment, Triple<EObject, EReference, INode> triple) throws AssertionError {
if (!resolving.add(triple))
return handleCyclicResolution(triple);
try {
Set<String> unresolveableProxies = getUnresolvableURIFragments();
if (unresolveableProxies.contains(uriFragment))
return null;
EReference reference = triple.getSecond();
try {
List<EObject> linkedObjects = getLinkingService().getLinkedObjects(triple.getFirst(), reference, triple.getThird());
if (linkedObjects.isEmpty()) {
if (isUnresolveableProxyCacheable(triple))
unresolveableProxies.add(uriFragment);
createAndAddDiagnostic(triple);
return null;
}
if (linkedObjects.size() > 1)
throw new IllegalStateException("linkingService returned more than one object for fragment " + uriFragment);
EObject result = linkedObjects.get(0);
if (!EcoreUtil2.isAssignableFrom(reference.getEReferenceType(), result.eClass())) {
log.error("An element of type " + result.getClass().getName() + " is not assignable to the reference " + reference.getEContainingClass().getName() + "." + reference.getName());
if (isUnresolveableProxyCacheable(triple))
unresolveableProxies.add(uriFragment);
createAndAddDiagnostic(triple);
return null;
}
// remove previously added error markers, since everything should be fine now
unresolveableProxies.remove(uriFragment);
removeDiagnostic(triple);
return result;
} catch (CyclicLinkingException e) {
if (e.triple.equals(triple)) {
log.error(e.getMessage(), e);
if (isUnresolveableProxyCacheable(triple))
unresolveableProxies.add(uriFragment);
createAndAddDiagnostic(triple);
return null;
} else {
throw e;
}
}
} catch (IllegalNodeException ex) {
createAndAddDiagnostic(triple, ex);
return null;
} finally {
resolving.remove(triple);
}
}
use of org.eclipse.xtext.linking.impl.IllegalNodeException in project xtext-core by eclipse.
the class LazyLinkingResourceTest method testWarningInsteadOfError.
@Test
public void testWarningInsteadOfError() throws Exception {
final EAnnotation source = EcoreFactory.eINSTANCE.createEAnnotation();
LazyLinkingResource res = new LazyLinkingResource();
res.setEncoder(new LazyURIEncoder() {
@Override
public boolean isCrossLinkFragment(Resource res, String s) {
return "foo".equals(s);
}
@Override
public Triple<EObject, EReference, INode> decode(Resource res, String uriFragment) {
return Tuples.create((EObject) source, EcorePackage.Literals.EANNOTATION__REFERENCES, (INode) new LeafNode());
}
});
res.setLinkingService(new ILinkingService() {
@Override
public List<EObject> getLinkedObjects(EObject context, EReference reference, INode node) throws IllegalNodeException {
return Collections.emptyList();
}
});
res.setDiagnosticMessageProvider(new LinkingDiagnosticMessageProvider() {
@Override
public DiagnosticMessage getUnresolvedProxyMessage(ILinkingDiagnosticContext context) {
return new DiagnosticMessage("myMessage", Severity.WARNING, null);
}
});
assertTrue(res.getWarnings().isEmpty());
assertNull(res.getEObject("foo"));
assertEquals(1, res.getWarnings().size());
assertTrue(res.getErrors().isEmpty());
assertEquals("myMessage", res.getWarnings().get(0).getMessage());
}
Aggregations