Skip to main content
summaryrefslogtreecommitdiffstats
blob: 6078cc247e71e12b2d83a645d834f19d9ab0429d (plain) (blame)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/*******************************************************************************
 * Copyright (c) 2005-2009 itemis AG (http://www.itemis.eu) 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
 *
 *******************************************************************************/

package org.eclipse.xtend.typesystem.emf;

import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.impl.DynamicEObjectImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.internal.xtend.type.baseimpl.OperationImpl;
import org.eclipse.internal.xtend.type.baseimpl.PropertyImpl;
import org.eclipse.xtend.expression.TypeSystem;
import org.eclipse.xtend.typesystem.AbstractTypeImpl;
import org.eclipse.xtend.typesystem.Feature;
import org.eclipse.xtend.typesystem.Type;

public class EObjectType extends AbstractTypeImpl {

	public EObjectType(final TypeSystem ts) {
		super(ts, "emf::EObject");
	}

	@Override
	public String getDocumentation() {
		return "base type for all ecore based metamodels (added by oAW4 emftools)";
	}

	@Override
	public Feature[] getContributedFeatures() {
		return new Feature[] {
				new PropertyImpl(EObjectType.this, "eContainer",
						EObjectType.this) {

					public Object get(final Object target) {
						return ((EObject) target).eContainer();
					}

					@Override
					public void set(final Object target, final Object newValue) {
						throw new UnsupportedOperationException();
					}
				},
				new PropertyImpl(EObjectType.this, "eContents", getTypeSystem()
						.getListType(EObjectType.this)) {

					public Object get(final Object target) {
						return ((EObject) target).eContents();
					}

					@Override
					public void set(final Object target, final Object newValue) {
						throw new UnsupportedOperationException();
					}
				},
				new PropertyImpl(EObjectType.this, "eRootContainer",
						EObjectType.this) {

					public Object get(final Object target) {
						return EcoreUtil.getRootContainer((EObject) target);
					}

					@Override
					public void set(final Object target, final Object newValue) {
						throw new UnsupportedOperationException();
					}
				},

				new PropertyImpl(EObjectType.this, "eAllContents",
						getTypeSystem().getSetType(EObjectType.this)) {

					@SuppressWarnings("unchecked")
					public Object get(final Object target) {
						final HashSet allCont = new HashSet();
						final Iterator iter = ((EObject) target).eAllContents();
						while (iter.hasNext()) {
							allCont.add(iter.next());
						}
						return allCont;
					}

					@Override
					public void set(final Object target, final Object newValue) {
						throw new UnsupportedOperationException();
					}
				},

				new OperationImpl(EObjectType.this, "toString", getTypeSystem()
						.getSetType(EObjectType.this)) {
					@Override
					protected Object evaluateInternal(Object target,
							Object[] params) {
						if (target instanceof DynamicEObjectImpl) {
							return EObjectType.toString((EObject) target);
						}
						return target.toString();
					}

				} };
	}

	public boolean isInstance(final Object o) {
		return o instanceof EObject;
	}

	public Object newInstance() {
		throw new UnsupportedOperationException("newInstance on " + getName());
	}

	@Override
	public Set<Type> getSuperTypes() {
		return Collections.singleton(getTypeSystem().getObjectType());
	}

	@SuppressWarnings("unchecked")
	public static StringBuffer toString(EObject x) {
		if (x == null) {
			return new StringBuffer("null");
		}
		StringBuffer buff = new StringBuffer(x.eClass().getEPackage().getName()).append("::").append(x.eClass().getName()).append("(");
		Iterator<EStructuralFeature> iter = x.eClass()
				.getEAllStructuralFeatures().iterator();
		while (iter.hasNext()) {
			EStructuralFeature f = iter.next();
			if (f instanceof EAttribute) {
				buff.append(f.getName()).append("=").append(x.eGet(f));
			}
			if (f instanceof EReference) {
				Object o = x.eGet(f);
				if (((EReference) f).isContainment()) {
					buff.append(f.getName()).append("=").append(toString(o));
				} else {
					buff.append(f.getName()).append("=").append(shortString(o));
				}
			}
			if (iter.hasNext())
				buff.append(",");
		}
		return buff.append(")");
	}

	private static StringBuffer toString(Object o) {
		if (o instanceof Collection) {
			StringBuffer buff = new StringBuffer("{");
			Iterator<?> iter = ((Collection<?>) o).iterator();
			while (iter.hasNext()) {
				Object x = iter.next();
				if (x instanceof EObject) {
					buff.append(toString((EObject) x));
				} else {
					buff.append("nonEMF:").append(x);
				}
				if (iter.hasNext())
					buff.append(",");
			}
			buff.append("}");
			return buff;
		}
		if (o instanceof EObject)
			return toString((EObject) o);
		return new StringBuffer();
	}

	private static String shortString(Object o) {
		if (o instanceof Collection) {
			StringBuffer buff = new StringBuffer("{");
			for (Object x : ((Collection<?>) o)) {
				if (x instanceof EObject)
					buff.append(shortString((EObject) x));
			}
			buff.append("}");
			return buff.toString();
		}
		if (o instanceof EObject)
			return shortString((EObject) o);
		return "";
	}

	public static String shortString(EObject x) {
		if (x == null) {
			return "null";
		}
		if (x.eClass().getEStructuralFeature("name") != null) {
			return x.eGet(x.eClass().getEStructuralFeature("name")) + " : "
					+ x.eClass().getName();
		}
		return "unnamed : " + x.eClass().getName();
	}

}

Back to the top