/
ReferenceBuilder.java
65 lines (58 loc) · 2.29 KB
/
ReferenceBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
* #%L
* org.eclipse.emf.eson
* %%
* Copyright (C) 2009 - 2014 Sebastian Benz, Michael Vorburger and others
* %%
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
* #L%
*/
package org.eclipse.emf.eson.building;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.eson.eFactory.NewObject;
import org.eclipse.emf.eson.eFactory.impl.ReferenceImpl;
import org.eclipse.emf.eson.util.EcoreUtil3;
public class ReferenceBuilder extends FeatureBuilder {
private ReferenceImpl reference;
public ReferenceBuilder(ReferenceImpl reference) {
this.reference = reference;
}
@Override
public void build(boolean preLinkingPhase) throws ModelBuilderException {
if (preLinkingPhase)
return;
getModelBuilder().addDeferredLinkingFeatureBuilder(this);
}
public void link() throws ModelBuilderException {
EObject newValue = getReferencedObject();
final EStructuralFeature eFeature = getFeature().getEFeature();
if (eFeature.eIsProxy())
return;
EcoreUtil3.setOrAddValue(getContainer(), eFeature, newValue);
}
protected EObject getReferencedObject() throws ModelBuilderException {
EObject referencedObject = ((ReferenceImpl)reference).basicGetValue();
if (referencedObject instanceof NewObject) {
throw new IllegalStateException("We shouldn't have any NewObject here anymore now?!");
}
if (referencedObject.eIsProxy()) {
// TODO subclass problem.. :-( @see org.eclipse.xtext.linking.lazy.LazyLinker.findInstantiableCompatible(EClass)
EReference eRef = (EReference) getFeature().getEFeature();
EClass refEClass = eRef.getEReferenceType();
InternalEObject newProxy = (InternalEObject) EcoreUtil.create(refEClass);
URI proxyURI = ((InternalEObject)referencedObject).eProxyURI();
newProxy.eSetProxyURI(proxyURI);
return newProxy;
}
return referencedObject;
}
}