use of org.eclipse.emf.ecore.EReference in project xtext-core by eclipse.
the class PartialSerializer method findRegion.
protected IAstRegion findRegion(IEObjectRegion owner, FeatureChange change) {
EStructuralFeature feature = change.getFeature();
if (feature instanceof EReference && ((EReference) feature).isContainment()) {
ITextRegionAccess access = owner.getTextRegionAccess();
EObject oldValue = change.getReferenceValue();
if (oldValue != null) {
return access.regionForEObject(oldValue);
}
EObject value = (EObject) owner.getSemanticElement().eGet(feature);
if (value != null) {
return access.regionForEObject(value);
}
return null;
} else {
return owner.getRegionFor().feature(feature);
}
}
use of org.eclipse.emf.ecore.EReference in project xtext-core by eclipse.
the class ReferenceUpdater method createUpdatableReference.
protected IUpdatableReference createUpdatableReference(ISemanticRegion current) {
EReference ref = (EReference) current.getContainingFeature();
CrossReference crossRef = GrammarUtil.containingCrossReference(current.getGrammarElement());
EObject owner = current.getContainingRegion().getSemanticElement();
Object value = owner.eGet(ref);
if (value instanceof List<?>) {
List<?> targets = (List<?>) value;
int i = current.getIndexInContainingFeature();
EObject t = (EObject) targets.get(i);
if (t != null && !t.eIsProxy()) {
return new UpdatableReference(owner, ref, i, t, crossRef, current);
}
} else if (value instanceof EObject) {
EObject t = (EObject) value;
if (!t.eIsProxy()) {
return new UpdatableReference(owner, ref, -1, t, crossRef, current);
}
} else {
throw new IllegalStateException();
}
return null;
}
use of org.eclipse.emf.ecore.EReference in project xtext-core by eclipse.
the class ResourceLifecycleManager method openAndApplyReferences.
public Resource openAndApplyReferences(ResourceSet resourceSet, RelatedResource toLoad) {
Resource resource = resourceSet.getResource(toLoad.getUri(), true);
for (IReferenceSnapshot desc : toLoad.outgoingReferences) {
EObject source = resource.getEObject(desc.getSourceEObjectUri().fragment());
EObject target = desc.getTarget().getObject();
EReference reference = desc.getEReference();
if (reference.isMany()) {
@SuppressWarnings("unchecked") List<Object> list = (EList<Object>) source.eGet(reference, false);
list.set(desc.getIndexInList(), target);
} else {
source.eSet(reference, target);
}
}
return resource;
}
use of org.eclipse.emf.ecore.EReference in project xtext-core by eclipse.
the class EcoreUtil2Test method testGetAllReferencedObjects.
@Test
public void testGetAllReferencedObjects() {
EClass a = createEClass("a");
EClass b = createEClass("b");
EPackage pack = EcoreFactory.eINSTANCE.createEPackage();
pack.setName("empty");
pack.setNsPrefix("empty");
pack.setNsURI("empty");
pack.getEClassifiers().add(a);
pack.getEClassifiers().add(b);
EReference ref = EcoreFactory.eINSTANCE.createEReference();
a.getEStructuralFeatures().add(ref);
ref.setUpperBound(1);
ref.setEType(b);
EObject objA = pack.getEFactoryInstance().create(a);
EObject objB = pack.getEFactoryInstance().create(b);
List<EObject> res = EcoreUtil2.getAllReferencedObjects(objA, ref);
assertNotNull(res);
assertTrue(res.isEmpty());
res = EcoreUtil2.getAllReferencedObjects(objA, ref);
assertNotNull(res);
objA.eSet(ref, objB);
}
use of org.eclipse.emf.ecore.EReference in project xtext-core by eclipse.
the class EcoreUtil2Test method testExternalFormOfEReferenceNoNPE.
/**
* This test assumes that an EPackage with indexed references is no longer available.
*/
@Test
public void testExternalFormOfEReferenceNoNPE() throws Exception {
EReference reference = EcorePackage.Literals.EATTRIBUTE__EATTRIBUTE_TYPE;
URI uri = EcoreUtil.getURI(reference);
String externalForm = uri.toString();
EReference foundReference = EcoreUtil2.getEReferenceFromExternalForm(EPackage.Registry.INSTANCE, externalForm);
assertSame(reference, foundReference);
String brokenExternalFrom = makeInvalid(externalForm);
assertNull(EcoreUtil2.getEReferenceFromExternalForm(EPackage.Registry.INSTANCE, brokenExternalFrom));
String shortExternalForm = EcoreUtil2.toExternalForm(reference);
foundReference = EcoreUtil2.getEReferenceFromExternalForm(EPackage.Registry.INSTANCE, shortExternalForm);
assertSame(reference, foundReference);
String brokenShortExternalFrom = makeInvalid(shortExternalForm);
assertNull(EcoreUtil2.getEReferenceFromExternalForm(EPackage.Registry.INSTANCE, brokenShortExternalFrom));
brokenShortExternalFrom = shortExternalForm.replace('A', 'a');
assertNull(EcoreUtil2.getEReferenceFromExternalForm(EPackage.Registry.INSTANCE, brokenShortExternalFrom));
}
Aggregations