/
NewObjectExtensions.java
71 lines (62 loc) · 2.57 KB
/
NewObjectExtensions.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
66
67
68
69
70
71
/*
* #%L
* org.eclipse.emf.eson
* %%
* Copyright (C) 2015 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.apache.log4j.Logger;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.emf.eson.eFactory.Feature;
import org.eclipse.emf.eson.eFactory.NewObject;
import org.eclipse.xtext.EcoreUtil2;
import com.google.common.base.Optional;
public class NewObjectExtensions {
private static Logger logger = Logger.getLogger(NewObjectExtensions.class);
// see also org.eclipse.emf.eson.serialization.ContainmentBuilder.nullifyEClassIfDefault(NewObject)
public Optional<EClass> getDeclaredOrInferredEClass(NewObject newObject) {
EClass eClass = newObject.getEClass();
if (eClass == null) {
Feature newObjectFeature = EcoreUtil2.getContainerOfType(newObject, Feature.class);
if (newObjectFeature != null) {
EStructuralFeature eFeature = newObjectFeature.getEFeature();
if (!(eFeature instanceof EReference))
return Optional.absent();
EReference eReference = (EReference) eFeature;
eClass = eReference.getEReferenceType();
} else {
// Do NOT logger.error here!
return Optional.absent();
}
}
if (eClass.getEPackage() == null) {
EcoreUtil.resolve(newObject.eClass(), newObject);
}
if (eClass.eIsProxy()) {
if (logger.isDebugEnabled())
logger.debug("The EClass for NewObject " + getNewObjectDescriptionForErrorMessage(newObject) + " is still an unresolved EMF Proxy, something isn't working in your cross-Resource reference resolution");
return Optional.absent();
}
EPackage ePackage = eClass.getEPackage();
if (ePackage == null) {
logger.error("No EPackage registered for EClass '" + eClass.getName() + "' defined in NewObject " + getNewObjectDescriptionForErrorMessage(newObject));
return Optional.absent();
}
return Optional.of(eClass);
}
protected String getNewObjectDescriptionForErrorMessage(NewObject newObject) {
return "name '" + newObject.getName()
+ "' at URI " + newObject.eResource().getURI()
+ "#" + newObject.eResource().getURIFragment(newObject);
}
}