/
NameAccessor.java
91 lines (80 loc) · 3.18 KB
/
NameAccessor.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* #%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.apache.log4j.Logger;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.eson.eFactory.CustomNameMapping;
import org.eclipse.emf.eson.eFactory.Factory;
import org.eclipse.emf.eson.eFactory.NewObject;
import org.eclipse.jdt.annotation.NonNullByDefault;
import org.eclipse.jdt.annotation.Nullable;
import org.eclipse.xtext.EcoreUtil2;
import com.google.common.collect.Iterables;
@NonNullByDefault
public class NameAccessor {
@SuppressWarnings("null")
private static Logger logger = Logger.getLogger(ModelBuilder.class);
protected static final String DEFAULT_NAME_FEATURE = "name";
public void setName(NewObject newObject, EObject eObject, String name) {
EAttribute nameAttribute = getNameAttribute(newObject);
if (nameAttribute != null)
setName(eObject, name, nameAttribute);
}
// public String getName(EObject context, EObject eObject) throws NoNameFeatureMappingException {
// Check.hasEPackage(context, EFactoryPackage.eINSTANCE);
// EAttribute nameAttribute = getNameAttribute(context, eObject);
// Object name = eObject.eGet(nameAttribute);
// if (name == null) {
// name = "";
// }
// return name.toString();
// }
protected void setName(EObject eObject, String name, EAttribute nameAttribute) {
try {
eObject.eSet(nameAttribute, name);
} catch (IllegalArgumentException e) {
logger.error("setName() failed to eSet, on EObject " + eObject.toString() + ", nameAttribute: " + nameAttribute + ", name: " + name, e);
}
}
public @Nullable EAttribute getNameAttribute(NewObject newObject) {
Factory context = EcoreUtil2.getContainerOfType(newObject, Factory.class);
return getNameAttribute(newObject, context);
}
public @Nullable EAttribute getNameAttribute(NewObject newObject, @Nullable Factory context) {
if (newObject.getEClass() == null)
return null;
if (context != null) {
for (CustomNameMapping mapping : getCustomNameMappings(context)) {
if (EcoreUtil2.isAssignableFrom(mapping.getEClass(), newObject.getEClass())) {
EAttribute attribute = mapping.getNameFeature();
if (attribute != null)
return attribute;
else
return null;
}
}
}
// If no explicit mapping found, fall back to default:
EStructuralFeature defaultNameFeature = newObject.getEClass().getEStructuralFeature(DEFAULT_NAME_FEATURE);
if (defaultNameFeature != null && defaultNameFeature instanceof EAttribute)
return (EAttribute) defaultNameFeature;
else
return null;
}
@SuppressWarnings("null")
protected Iterable<CustomNameMapping> getCustomNameMappings(Factory factory) {
return Iterables.filter(factory.getAnnotations(), CustomNameMapping.class);
}
}