Search in sources :

Example 1 with IllegalNodeException

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"));
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) Resource(org.eclipse.emf.ecore.resource.Resource) Triple(org.eclipse.xtext.util.Triple) EAnnotation(org.eclipse.emf.ecore.EAnnotation) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) LeafNode(org.eclipse.xtext.nodemodel.impl.LeafNode) IllegalNodeException(org.eclipse.xtext.linking.impl.IllegalNodeException) ILinkingService(org.eclipse.xtext.linking.ILinkingService) LinkingDiagnosticMessageProvider(org.eclipse.xtext.linking.impl.LinkingDiagnosticMessageProvider) List(java.util.List) InternalEList(org.eclipse.emf.ecore.util.InternalEList) LinkingHelper(org.eclipse.xtext.linking.impl.LinkingHelper) EReference(org.eclipse.emf.ecore.EReference) Test(org.junit.Test)

Example 2 with IllegalNodeException

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);
    }
}
Also used : EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) IllegalNodeException(org.eclipse.xtext.linking.impl.IllegalNodeException) EReference(org.eclipse.emf.ecore.EReference)

Example 3 with IllegalNodeException

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());
}
Also used : INode(org.eclipse.xtext.nodemodel.INode) Resource(org.eclipse.emf.ecore.resource.Resource) Triple(org.eclipse.xtext.util.Triple) EAnnotation(org.eclipse.emf.ecore.EAnnotation) DiagnosticMessage(org.eclipse.xtext.diagnostics.DiagnosticMessage) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) LeafNode(org.eclipse.xtext.nodemodel.impl.LeafNode) IllegalNodeException(org.eclipse.xtext.linking.impl.IllegalNodeException) ILinkingService(org.eclipse.xtext.linking.ILinkingService) LinkingDiagnosticMessageProvider(org.eclipse.xtext.linking.impl.LinkingDiagnosticMessageProvider) List(java.util.List) InternalEList(org.eclipse.emf.ecore.util.InternalEList) EReference(org.eclipse.emf.ecore.EReference) Test(org.junit.Test)

Aggregations

EObject (org.eclipse.emf.ecore.EObject)3 EReference (org.eclipse.emf.ecore.EReference)3 InternalEObject (org.eclipse.emf.ecore.InternalEObject)3 IllegalNodeException (org.eclipse.xtext.linking.impl.IllegalNodeException)3 List (java.util.List)2 EAnnotation (org.eclipse.emf.ecore.EAnnotation)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 InternalEList (org.eclipse.emf.ecore.util.InternalEList)2 ILinkingService (org.eclipse.xtext.linking.ILinkingService)2 LinkingDiagnosticMessageProvider (org.eclipse.xtext.linking.impl.LinkingDiagnosticMessageProvider)2 INode (org.eclipse.xtext.nodemodel.INode)2 LeafNode (org.eclipse.xtext.nodemodel.impl.LeafNode)2 Triple (org.eclipse.xtext.util.Triple)2 Test (org.junit.Test)2 DiagnosticMessage (org.eclipse.xtext.diagnostics.DiagnosticMessage)1 LinkingHelper (org.eclipse.xtext.linking.impl.LinkingHelper)1