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);
}
}
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);
}
}
}
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);
}
}
}
}
}
}
}
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);
}
}
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;
}
Aggregations