Search in sources :

Example 76 with EReference

use of org.eclipse.emf.ecore.EReference in project xtext-core by eclipse.

the class CrossReferenceSerializer method serializeCrossRef.

@Override
public String serializeCrossRef(EObject context, CrossReference grammarElement, final EObject target, INode node) {
    final EReference ref = GrammarUtil.getReference(grammarElement, context.eClass());
    String text = null;
    if (node != null) {
        List<EObject> objects = linkingService.getLinkedObjects(context, ref, node);
        if (Iterables.any(objects, new Predicate<EObject>() {

            private final URI targetURI = EcoreUtil.getURI(target);

            @Override
            public boolean apply(EObject input) {
                return input == target || EcoreUtil.getURI(input).equals(targetURI);
            }
        }))
            return ITokenSerializer.KEEP_VALUE_FROM_NODE_MODEL;
    }
    text = getUnconvertedLinkText(target, ref, context);
    if (text != null)
        return getConvertedValue(text, grammarElement);
    if (node != null) {
        return linkingHelper.getCrossRefNodeAsString(node, false);
    }
    return null;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) URI(org.eclipse.emf.common.util.URI) EReference(org.eclipse.emf.ecore.EReference)

Example 77 with EReference

use of org.eclipse.emf.ecore.EReference in project xtext-core by eclipse.

the class Linker method ensureIsLinked.

@SuppressWarnings({ "rawtypes", "unchecked" })
protected void ensureIsLinked(EObject obj, INode node, CrossReference ref, Set<EReference> handledReferences, IDiagnosticProducer producer) {
    final EReference eRef = GrammarUtil.getReference(ref, obj.eClass());
    if (eRef == null) {
        ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext context = createDiagnosticContext(obj, eRef, node);
        DiagnosticMessage message = diagnosticMessageProvider.getIllegalCrossReferenceMessage(context, ref);
        producer.addDiagnostic(message);
        return;
    }
    handledReferences.add(eRef);
    beforeEnsureIsLinked(obj, eRef, producer);
    producer.setTarget(obj, eRef);
    try {
        final List<EObject> links = getLinkedObject(obj, eRef, node);
        if (links == null || links.isEmpty()) {
            if (!isNullValidResult(obj, eRef, node)) {
                ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext context = createDiagnosticContext(obj, eRef, node);
                DiagnosticMessage message = diagnosticMessageProvider.getUnresolvedProxyMessage(context);
                producer.addDiagnostic(message);
            }
            return;
        }
        if (eRef.getUpperBound() >= 0 && links.size() > eRef.getUpperBound()) {
            ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext context = createDiagnosticContext(obj, eRef, node);
            DiagnosticMessage message = diagnosticMessageProvider.getViolatedBoundsConstraintMessage(context, links.size());
            producer.addDiagnostic(message);
            return;
        }
        if (eRef.getUpperBound() == 1) {
            obj.eSet(eRef, links.get(0));
        } else {
            // eRef.getUpperBound() == -1 ||
            // eRef.getUpperBound() < links.size
            // TODO extract and check weather equals or identity is used by list
            final List<EObject> list = (List<EObject>) obj.eGet(eRef);
            if (links.size() > 1 && eRef.isUnique() && (list instanceof InternalEList)) {
                final Set<EObject> addUs = new LinkedHashSet<EObject>(links);
                // addUs.removeAll(list); // removeAll calls most likely list.contains() which is rather slow
                for (int i = 0; i < list.size(); i++) addUs.remove(list.get(i));
                if (!((InternalEList) list).addAllUnique(addUs)) {
                    ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext context = createDiagnosticContext(obj, eRef, node);
                    DiagnosticMessage message = diagnosticMessageProvider.getViolatedBoundsConstraintMessage(context, links.size());
                    producer.addDiagnostic(message);
                }
            } else if (!list.addAll(links)) {
                ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext context = createDiagnosticContext(obj, eRef, node);
                DiagnosticMessage message = diagnosticMessageProvider.getViolatedBoundsConstraintMessage(context, links.size());
                producer.addDiagnostic(message);
            }
        }
    } catch (IllegalNodeException e) {
        ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext context = createDiagnosticContext(obj, eRef, node);
        DiagnosticMessage message = diagnosticMessageProvider.getIllegalNodeMessage(context, e);
        producer.addDiagnostic(message);
        if (log.isDebugEnabled()) {
            log.debug(e.getMessage(), e);
        }
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ILinkingDiagnosticMessageProvider(org.eclipse.xtext.linking.ILinkingDiagnosticMessageProvider) ILinkingDiagnosticContext(org.eclipse.xtext.linking.ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext) DiagnosticMessage(org.eclipse.xtext.diagnostics.DiagnosticMessage) EObject(org.eclipse.emf.ecore.EObject) InternalEList(org.eclipse.emf.ecore.util.InternalEList) List(java.util.List) InternalEList(org.eclipse.emf.ecore.util.InternalEList) ILinkingDiagnosticContext(org.eclipse.xtext.linking.ILinkingDiagnosticMessageProvider.ILinkingDiagnosticContext) EReference(org.eclipse.emf.ecore.EReference)

Example 78 with EReference

use of org.eclipse.emf.ecore.EReference in project xtext-core by eclipse.

the class Linker method ensureLinked.

public void ensureLinked(EObject obj, IDiagnosticProducer producer) {
    ICompositeNode node = NodeModelUtils.getNode(obj);
    if (node == null)
        return;
    Set<EReference> handledReferences = new HashSet<EReference>();
    ensureLinked(obj, producer, node, handledReferences, false);
    producer.setNode(node);
    setDefaultValues(obj, handledReferences, producer);
}
Also used : ICompositeNode(org.eclipse.xtext.nodemodel.ICompositeNode) EReference(org.eclipse.emf.ecore.EReference) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 79 with EReference

use of org.eclipse.emf.ecore.EReference in project xtext-core by eclipse.

the class LazyURIEncoder method resolveShortFragment.

public EObject resolveShortFragment(Resource res, String shortFragment) {
    List<String> split = Strings.split(shortFragment, '.');
    int contentsIdx = Integer.parseInt(split.get(0));
    EObject result = res.getContents().get(contentsIdx);
    int splitIdx = 1;
    while (splitIdx < split.size()) {
        int featureId = Integer.parseInt(split.get(splitIdx++));
        EReference reference = (EReference) result.eClass().getEStructuralFeature(featureId);
        if (reference.isMany()) {
            List<?> list = (List<?>) result.eGet(reference);
            int listIdx = Integer.parseInt(split.get(splitIdx++));
            result = (EObject) list.get(listIdx);
        } else {
            result = (EObject) result.eGet(reference);
        }
    }
    return result;
}
Also used : EObject(org.eclipse.emf.ecore.EObject) List(java.util.List) EReference(org.eclipse.emf.ecore.EReference)

Example 80 with EReference

use of org.eclipse.emf.ecore.EReference in project xtext-core by eclipse.

the class LazyURIEncoder method appendShortFragment.

public void appendShortFragment(EObject obj, StringBuilder target) {
    EReference containmentFeature = obj.eContainmentFeature();
    if (containmentFeature == null) {
        target.append(obj.eResource().getContents().indexOf(obj));
    } else {
        EObject container = obj.eContainer();
        appendShortFragment(container, target);
        target.append('.').append(container.eClass().getFeatureID(containmentFeature));
        if (containmentFeature.isMany()) {
            List<?> list = (List<?>) container.eGet(containmentFeature);
            target.append('.').append(list.indexOf(obj));
        }
    }
}
Also used : EObject(org.eclipse.emf.ecore.EObject) List(java.util.List) EReference(org.eclipse.emf.ecore.EReference)

Aggregations

EReference (org.eclipse.emf.ecore.EReference)229 EObject (org.eclipse.emf.ecore.EObject)118 EClass (org.eclipse.emf.ecore.EClass)58 List (java.util.List)52 ArrayList (java.util.ArrayList)48 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)37 Test (org.junit.Test)31 EAttribute (org.eclipse.emf.ecore.EAttribute)29 PalladioSelectEObjectDialog (org.palladiosimulator.editors.commons.dialogs.selection.PalladioSelectEObjectDialog)28 EClassifier (org.eclipse.emf.ecore.EClassifier)21 Resource (org.eclipse.emf.ecore.resource.Resource)21 IdEObject (org.bimserver.emf.IdEObject)18 EList (org.eclipse.emf.common.util.EList)17 IScope (org.eclipse.xtext.scoping.IScope)15 HashMapVirtualObject (org.bimserver.shared.HashMapVirtualObject)14 InternalEObject (org.eclipse.emf.ecore.InternalEObject)14 URI (org.eclipse.emf.common.util.URI)13 EPackage (org.eclipse.emf.ecore.EPackage)12 AbstractEList (org.eclipse.emf.common.util.AbstractEList)10 CrossReference (org.eclipse.xtext.CrossReference)10