Search in sources :

Example 11 with InternalEList

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

the class DefaultEcoreElementFactory method add.

@Override
@SuppressWarnings("unchecked")
public void add(EObject object, String feature, Object value, String ruleName, INode node) throws ValueConverterException {
    if (value == null)
        return;
    final EStructuralFeature structuralFeature = object.eClass().getEStructuralFeature(feature);
    if (structuralFeature == null)
        throw new IllegalArgumentException(object.eClass().getName() + "." + feature + " does not exist");
    try {
        if (value instanceof EObject) {
            // containment lists are unique per-se and the tokenValue was created just a sec ago
            ((InternalEList<EObject>) object.eGet(structuralFeature)).addUnique((EObject) value);
        } else {
            final Object tokenValue = getTokenValue(value, ruleName, node);
            checkNullForPrimitiveFeatures(structuralFeature, value, node);
            ((Collection<Object>) object.eGet(structuralFeature)).add(tokenValue);
        }
    } catch (ValueConverterWithValueException e) {
        final Object tokenValue = e.getValue();
        checkNullForPrimitiveFeatures(structuralFeature, value, node);
        ((Collection<Object>) object.eGet(structuralFeature)).add(tokenValue);
        throw e;
    } catch (ValueConverterException e) {
        throw e;
    } catch (NullPointerException e) {
        log.error(e.getMessage(), e);
        throw new ValueConverterException("A NullPointerException occured. This indicates a missing value converter or a bug in its implementation.", node, e);
    } catch (Exception e) {
        throw new ValueConverterException(null, node, e);
    }
}
Also used : ValueConverterWithValueException(org.eclipse.xtext.conversion.ValueConverterWithValueException) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) InternalEList(org.eclipse.emf.ecore.util.InternalEList) Collection(java.util.Collection) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException) ValueConverterWithValueException(org.eclipse.xtext.conversion.ValueConverterWithValueException) ValueConverterException(org.eclipse.xtext.conversion.ValueConverterException)

Example 12 with InternalEList

use of org.eclipse.emf.ecore.util.InternalEList 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 13 with InternalEList

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

the class LazyLinkingResource method doResolveLazyCrossReference.

/**
 * Ensures that all the lazy proxy values that are referenced by {@code crossRef} are replaced
 * by non-lazy proxies or resolved instances.
 *
 * @since 2.4
 */
protected void doResolveLazyCrossReference(InternalEObject source, EStructuralFeature crossRef) {
    if (crossRef.isMany()) {
        @SuppressWarnings("unchecked") InternalEList<EObject> list = (InternalEList<EObject>) source.eGet(crossRef);
        for (int i = 0; i < list.size(); i++) {
            EObject proxy = list.basicGet(i);
            if (proxy.eIsProxy()) {
                URI proxyURI = ((InternalEObject) proxy).eProxyURI();
                if (getURI().equals(proxyURI.trimFragment())) {
                    final String fragment = proxyURI.fragment();
                    if (getEncoder().isCrossLinkFragment(this, fragment)) {
                        EObject target = getEObject(fragment);
                        if (target != null) {
                            try {
                                source.eSetDeliver(false);
                                list.setUnique(i, target);
                            } finally {
                                source.eSetDeliver(true);
                            }
                        }
                    }
                }
            }
        }
    } else {
        EObject proxy = (EObject) source.eGet(crossRef, false);
        if (proxy != null && proxy.eIsProxy()) {
            URI proxyURI = ((InternalEObject) proxy).eProxyURI();
            if (getURI().equals(proxyURI.trimFragment())) {
                final String fragment = proxyURI.fragment();
                if (getEncoder().isCrossLinkFragment(this, fragment)) {
                    EObject target = getEObject(fragment);
                    if (target != null) {
                        try {
                            source.eSetDeliver(false);
                            source.eSet(crossRef, target);
                        } finally {
                            source.eSetDeliver(true);
                        }
                    }
                }
            }
        }
    }
}
Also used : EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) InternalEList(org.eclipse.emf.ecore.util.InternalEList) URI(org.eclipse.emf.common.util.URI) InternalEObject(org.eclipse.emf.ecore.InternalEObject)

Example 14 with InternalEList

use of org.eclipse.emf.ecore.util.InternalEList in project n4js by eclipse.

the class N4JSLinker method createAndSetProxy.

/**
 * Creates a proxy instance that will later on allow to lazily resolve the semantically referenced instance for the
 * given {@link CrossReference xref}.
 */
@SuppressWarnings("unchecked")
private void createAndSetProxy(N4JSResource resource, EObject obj, INode node, EReference eRef, CrossReference xref, IDiagnosticProducer diagnosticProducer) {
    final EObject proxy = createProxy(resource, obj, node, eRef, xref, diagnosticProducer);
    proxy.eSetDeliver(false);
    if (eRef.isMany()) {
        ((InternalEList<EObject>) obj.eGet(eRef, false)).addUnique(proxy);
    } else {
        obj.eSet(eRef, proxy);
    }
}
Also used : EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject) InternalEList(org.eclipse.emf.ecore.util.InternalEList)

Example 15 with InternalEList

use of org.eclipse.emf.ecore.util.InternalEList in project statecharts by Yakindu.

the class DerivedEObjectEList method isEmpty.

@Override
public boolean isEmpty() {
    if (sourceFeatureIDs != null) {
        for (int i = 0; i < sourceFeatureIDs.length; i++) {
            int sourceFeatureID = sourceFeatureIDs[i];
            if (owner.eIsSet(sourceFeatureID)) {
                EStructuralFeature sourceFeature = getEStructuralFeature(sourceFeatureID);
                Object value = owner.eGet(sourceFeatureID, false, true);
                if (FeatureMapUtil.isFeatureMap(sourceFeature)) {
                    FeatureMap featureMap = (FeatureMap) value;
                    for (int j = 0, size = featureMap.size(); j < size; j++) {
                        if (isIncluded(featureMap.getEStructuralFeature(j)) ? featureMap.getValue(j) != null : isIncluded(featureMap.getValue(j))) {
                            return false;
                        }
                    }
                } else if (isIncluded(sourceFeature)) {
                    if (sourceFeature.isMany() ? ((List<?>) value).size() > 0 : value != null) {
                        return false;
                    }
                } else {
                    if (sourceFeature.isMany()) {
                        InternalEList<?> valuesList = (InternalEList<?>) value;
                        if (valuesList instanceof RandomAccess) {
                            for (int j = 0, size = valuesList.size(); j < size; j++) {
                                if (isIncluded(valuesList.basicGet(j))) {
                                    return false;
                                }
                            }
                        } else {
                            for (Iterator<?> v = valuesList.basicIterator(); v.hasNext(); ) {
                                if (isIncluded(v.next())) {
                                    return false;
                                }
                            }
                        }
                    } else if (isIncluded(value)) {
                        return false;
                    }
                }
            }
        }
    }
    return true;
}
Also used : FeatureMap(org.eclipse.emf.ecore.util.FeatureMap) EStructuralFeature(org.eclipse.emf.ecore.EStructuralFeature) Iterator(java.util.Iterator) ListIterator(java.util.ListIterator) AbstractSequentialInternalEList(org.eclipse.emf.ecore.util.AbstractSequentialInternalEList) InternalEList(org.eclipse.emf.ecore.util.InternalEList) RandomAccess(java.util.RandomAccess) EObject(org.eclipse.emf.ecore.EObject) InternalEObject(org.eclipse.emf.ecore.InternalEObject)

Aggregations

InternalEList (org.eclipse.emf.ecore.util.InternalEList)24 EObject (org.eclipse.emf.ecore.EObject)9 ITypeBinding (org.eclipse.jdt.core.dom.ITypeBinding)9 JvmTypeConstraint (org.eclipse.xtext.common.types.JvmTypeConstraint)9 InternalEObject (org.eclipse.emf.ecore.InternalEObject)7 EStructuralFeature (org.eclipse.emf.ecore.EStructuralFeature)4 JvmTypeReference (org.eclipse.xtext.common.types.JvmTypeReference)4 Iterator (java.util.Iterator)3 ListIterator (java.util.ListIterator)3 RandomAccess (java.util.RandomAccess)3 AbstractSequentialInternalEList (org.eclipse.emf.ecore.util.AbstractSequentialInternalEList)3 FeatureMap (org.eclipse.emf.ecore.util.FeatureMap)3 IAnnotationBinding (org.eclipse.jdt.core.dom.IAnnotationBinding)3 JvmAnnotationReference (org.eclipse.xtext.common.types.JvmAnnotationReference)3 JvmMember (org.eclipse.xtext.common.types.JvmMember)3 List (java.util.List)2 URI (org.eclipse.emf.common.util.URI)2 EReference (org.eclipse.emf.ecore.EReference)2 IMethodBinding (org.eclipse.jdt.core.dom.IMethodBinding)2 AbortCompilation (org.eclipse.jdt.internal.compiler.problem.AbortCompilation)2