Skip to main content
summaryrefslogtreecommitdiffstats
blob: e818b9894e964b20c55db93fb55283fece8a9b35 (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
/*
Copyright (c) 2008 Arno Haase.
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

Contributors:
    Arno Haase - initial API and implementation
 */
package org.eclipse.xtend.backend.types.uml2.internal;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;

import org.apache.commons.logging.LogFactory;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.EStructuralFeature;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.uml2.uml.Classifier;
import org.eclipse.uml2.uml.Element;
import org.eclipse.uml2.uml.Generalization;
import org.eclipse.uml2.uml.Property;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.Type;
import org.eclipse.xtend.backend.common.BackendType;
import org.eclipse.xtend.backend.common.ExecutionContext;
import org.eclipse.xtend.backend.types.AbstractProperty;
import org.eclipse.xtend.backend.types.AbstractType;
import org.eclipse.xtend.backend.types.builtin.CollectionType;
import org.eclipse.xtend.backend.types.uml2.UmlTypesystem;


/**
 * 
 * @author Arno Haase (http://www.haase-consulting.com)
 */
public final class StereotypeType extends AbstractType {
    private final Stereotype _stereoType;
    
    public StereotypeType (String name, Stereotype stereoType, UmlTypesystem umlTs) {
        super (name, name, superTypes (umlTs, stereoType).toArray (new BackendType[0])); //TODO uniqueRepresentation
        _stereoType = stereoType;
        
        for (StereotypeProperty stp: getProperties(this, stereoType, umlTs))
            register (stp);
    }

    
    private Collection<StereotypeProperty> getProperties (BackendType owningType, Stereotype stereoType, UmlTypesystem umlTs) {
        final Collection<StereotypeProperty> result = new HashSet<StereotypeProperty> ();
        
        for (Property attrib: stereoType.getAttributes()) {
            if (attrib.getName() == null)
                continue;

            UmlTypesystem.fixName (attrib);
            
            final Type umlType = getTypeResolveProxy (attrib);
            if (umlType.getQualifiedName() == null) {
                LogFactory.getLog (owningType.getClass ()).error ("qualified name is null for element " + attrib.getQualifiedName());
                continue;
            }
            
            final BackendType backendType = (attrib.isMultivalued()) ? CollectionType.INSTANCE : umlTs.findType (umlType);
            result.add (new StereotypeProperty (backendType, attrib.getName ()));
        }
        
        return result;
    }

    
    private static Collection<BackendType> superTypes (UmlTypesystem umlTs, Stereotype stereoType) {
        final List<Classifier> all = new ArrayList<Classifier> (stereoType.getExtendedMetaclasses());
        all.addAll (stereoType.getSuperClasses());

        final List<BackendType> result = new ArrayList<BackendType>();
        
        for (Classifier classifier : all) 
            result.add (umlTs.getTypeForEClassifier (classifier.eClass()));

        return result;
    }
    
    
    private Type getTypeResolveProxy (Property p) {
        Type result = p.getType();
        
        if (result.eIsProxy()) {
            final InternalEObject proxy = (InternalEObject) result;
            final URI uri = proxy.eProxyURI();

            result = (Type) EcoreUtil.resolve (proxy, p);

            if (result.eIsProxy()) 
                throw new IllegalStateException ("Couldn't resolve proxy under " + uri);
        }
        
        return result;
    }

    
    @Override
    public int hashCode () {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((_stereoType == null) ? 0 : _stereoType.hashCode());
        return result;
    }

    @Override
    public boolean equals (Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        final StereotypeType other = (StereotypeType) obj;
        if (_stereoType == null) {
            if (other._stereoType != null)
                return false;
        } else if (!_stereoType.equals(other._stereoType))
            return false;
        return true;
    }
    
    
    private final class StereotypeProperty extends AbstractProperty {
        public StereotypeProperty (BackendType type, String name) {
            super (StereotypeType.this, type, Object.class, name, false);
        }
        
        @Override
        public Object getRaw (ExecutionContext ctx, Object target) {
            if (target instanceof Element) {
                final Element ele = (Element) target;
                
                for (Stereotype st : ele.getAppliedStereotypes()) {
                    if (isStereoTypeAssignable (st, _stereoType)) {
                        final Object value = ele.getValue (st, getName());

                        // custom datatypes
                        // see Bug#185033
                        if (value instanceof EList) {
                            final EList<?> eList = (EList<?>) value;
                            final Collection<Object> values = new ArrayList<Object>();
                            for (Object dynObject: eList) {
                                final Object dynValue = getDynamicValue(dynObject);
                                if (dynValue != null)
                                    values.add(dynValue);
                            }
                            if (!values.isEmpty ())
                                return values;
                        }
                        else if (value instanceof EObject) {
                            final Object dynValue = getDynamicValue(value);
                            if (dynValue != null)
                                return dynValue;
                        }

                        return value;
                    }
                }
            }
            throw new IllegalArgumentException("uml2 Element expected but was " + target.getClass().getName());
        }
        
        private Object getDynamicValue(final Object value) {
            if (value instanceof EObject) {
                final EObject dynObject = (EObject) value;
                final EClass dynClass = dynObject.eClass();
                final EStructuralFeature baseClassFeature = dynClass.getEStructuralFeature("base_Class");
                
                if(baseClassFeature != null){
                    return dynObject.eGet(baseClassFeature,true);
                }
            }
            return null;
        }
        
        private boolean isStereoTypeAssignable(Stereotype st1, Stereotype st2) {
            if (st1.getQualifiedName().equals(st2.getQualifiedName())) {
                return true;
            }
            List<Generalization> gs = st1.getGeneralizations();
            for (Generalization g : gs) {
                if (g.getGeneral() instanceof Stereotype && isStereoTypeAssignable((Stereotype) g.getGeneral(), st2))
                    return true;
            }
            return false;
        }
    }

}

Back to the top