Package: EAllContentsService

EAllContentsService

nameinstructionbranchcomplexitylinemethod
EAllContentsService(Method, Object)
M: 5 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 2 C: 0
0%
M: 1 C: 0
0%
getType(Call, ValidationServices, IValidationResult, IReadOnlyQueryEnvironment, List)
M: 211 C: 0
0%
M: 30 C: 0
0%
M: 16 C: 0
0%
M: 36 C: 0
0%
M: 1 C: 0
0%
getTypeForSpecificType(ValidationServices, IReadOnlyQueryEnvironment, List, EClass)
M: 210 C: 0
0%
M: 24 C: 0
0%
M: 13 C: 0
0%
M: 34 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2015 Obeo.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * Obeo - initial API and implementation
10: *******************************************************************************/
11: package org.eclipse.acceleo.query.services;
12:
13: import com.google.common.collect.Sets;
14:
15: import java.lang.reflect.Method;
16: import java.util.Collection;
17: import java.util.LinkedHashSet;
18: import java.util.List;
19: import java.util.Set;
20:
21: import org.eclipse.acceleo.query.ast.Call;
22: import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
23: import org.eclipse.acceleo.query.runtime.IValidationResult;
24: import org.eclipse.acceleo.query.runtime.impl.ValidationServices;
25: import org.eclipse.acceleo.query.validation.type.EClassifierLiteralType;
26: import org.eclipse.acceleo.query.validation.type.EClassifierSetLiteralType;
27: import org.eclipse.acceleo.query.validation.type.EClassifierType;
28: import org.eclipse.acceleo.query.validation.type.IType;
29: import org.eclipse.acceleo.query.validation.type.SequenceType;
30: import org.eclipse.emf.ecore.EClass;
31: import org.eclipse.emf.ecore.EClassifier;
32: import org.eclipse.emf.ecore.EcorePackage;
33:
34: /**
35: * EAllContents {@link org.eclipse.acceleo.query.runtime.IService IService}.
36: *
37: * @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
38: */
39: class EAllContentsService extends FilterService {
40:
41:         /**
42:          * Creates a new service instance given a method and an instance.
43:          *
44:          * @param serviceMethod
45:          * the method that realizes the service
46:          * @param serviceInstance
47:          * the instance on which the service must be called
48:          */
49:         EAllContentsService(Method serviceMethod, Object serviceInstance) {
50:                 super(serviceMethod, serviceInstance);
51:         }
52:
53:         @Override
54:         public Set<IType> getType(Call call, ValidationServices services, IValidationResult validationResult,
55:                         IReadOnlyQueryEnvironment queryEnvironment, List<IType> argTypes) {
56:                 final Set<IType> result = new LinkedHashSet<IType>();
57:
58:                 final IType receiverType = argTypes.get(0);
59:                 final Set<EClass> receiverEClasses = new LinkedHashSet<EClass>();
60:•                if (receiverType.getType() instanceof EClass) {
61:                         receiverEClasses.add((EClass)receiverType.getType());
62:•                } else if (receiverType.getType() instanceof Class) {
63:                         final Set<EClassifier> eClassifiers = queryEnvironment.getEPackageProvider().getEClassifiers(
64:                                         (Class<?>)receiverType.getType());
65:•                        if (eClassifiers != null) {
66:•                                for (EClassifier eCls : eClassifiers) {
67:•                                        if (eCls instanceof EClass) {
68:                                                 receiverEClasses.add((EClass)eCls);
69:                                         }
70:                                 }
71:                         }
72:                 } else {
73:                         throw new IllegalStateException("don't know what to do with " + receiverType.getType());
74:                 }
75:
76:•                if (receiverEClasses.isEmpty()) {
77:                         result.add(new SequenceType(queryEnvironment, services.nothing(
78:                                         "Only EClass can contain other EClasses not %s", argTypes.get(0))));
79:                 } else {
80:•                        for (EClass eCls : receiverEClasses) {
81:•                                if (eCls == EcorePackage.eINSTANCE.getEObject()) {
82:•                                        if (argTypes.size() == 1) {
83:                                                 result.add(new SequenceType(queryEnvironment, argTypes.get(0)));
84:•                                        } else if (argTypes.size() == 2 && argTypes.get(1) instanceof EClassifierLiteralType) {
85:                                                 result.add(new SequenceType(queryEnvironment, new EClassifierType(queryEnvironment,
86:                                                                 ((EClassifierLiteralType)argTypes.get(1)).getType())));
87:•                                        } else if (argTypes.size() == 2 && argTypes.get(1) instanceof EClassifierSetLiteralType) {
88:•                                                for (EClassifier eClsFilter : ((EClassifierSetLiteralType)argTypes.get(1))
89:                                                                 .getEClassifiers()) {
90:                                                         result.add(new SequenceType(queryEnvironment, new EClassifierType(
91:                                                                         queryEnvironment, eClsFilter)));
92:                                                 }
93:•                                        } else if (argTypes.size() == 2) {
94:                                                 result.addAll(super.getType(call, services, validationResult, queryEnvironment,
95:                                                                 argTypes));
96:                                         }
97:                                 } else {
98:                                         result.addAll(getTypeForSpecificType(services, queryEnvironment, argTypes, eCls));
99:                                 }
100:                         }
101:                 }
102:
103:                 return result;
104:         }
105:
106:         /**
107:          * Gets the {@link IType} of elements returned by the service when the receiver type is not the
108:          * {@link org.eclipse.emf.ecore.EObject EObject} {@link EClass}.
109:          *
110:          * @param services
111:          * the {@link ValidationServices}
112:          * @param queryEnvironment
113:          * the {@link IReadOnlyQueryEnvironment}
114:          * @param argTypes
115:          * arguments {@link IType}
116:          * @param receiverEClass
117:          * the receiver type can't be {@link org.eclipse.emf.ecore.EObject EObject} {@link EClass}
118:          * @return the {@link IType} of elements returned by the service when the receiver type is not the
119:          * {@link org.eclipse.emf.ecore.EObject EObject} {@link EClass}
120:          */
121:         private Set<IType> getTypeForSpecificType(ValidationServices services,
122:                         IReadOnlyQueryEnvironment queryEnvironment, List<IType> argTypes, final EClass receiverEClass) {
123:                 final Set<IType> result = new LinkedHashSet<IType>();
124:
125:•                if (argTypes.size() == 1) {
126:                         final Set<IType> containedTypes = new LinkedHashSet<IType>();
127:•                        for (EClass contained : queryEnvironment.getEPackageProvider().getAllContainedEClasses(
128:                                         receiverEClass)) {
129:                                 containedTypes.add(new SequenceType(queryEnvironment, new EClassifierType(queryEnvironment,
130:                                                 contained)));
131:                         }
132:                         result.addAll(containedTypes);
133:•                        if (result.isEmpty()) {
134:                                 result.add(new SequenceType(queryEnvironment, services.nothing(
135:                                                 "%s doesn't contain any other EClass", argTypes.get(0))));
136:                         }
137:•                } else if (argTypes.size() == 2) {
138:                         final Set<IType> filterTypes = Sets.newLinkedHashSet();
139:•                        if (argTypes.get(1) instanceof EClassifierSetLiteralType) {
140:•                                for (EClassifier eClassifier : ((EClassifierSetLiteralType)argTypes.get(1)).getEClassifiers()) {
141:                                         filterTypes.add(new EClassifierType(queryEnvironment, eClassifier));
142:                                 }
143:•                        } else if (argTypes.get(1) instanceof EClassifierLiteralType) {
144:                                 filterTypes.add(argTypes.get(1));
145:                         } else {
146:                                 final Collection<EClassifier> eObjectEClasses = queryEnvironment.getEPackageProvider()
147:                                                 .getTypes("ecore", "EObject");
148:•                                for (EClassifier eObjectEClass : eObjectEClasses) {
149:                                         filterTypes.add(new EClassifierType(queryEnvironment, eObjectEClass));
150:                                 }
151:                         }
152:•                        for (IType filterType : filterTypes) {
153:•                                for (EClass containedEClass : queryEnvironment.getEPackageProvider().getAllContainedEClasses(
154:                                                 receiverEClass)) {
155:                                         final IType lowerType = services.lower(new EClassifierType(queryEnvironment,
156:                                                         containedEClass), filterType);
157:•                                        if (lowerType != null) {
158:                                                 result.add(new SequenceType(queryEnvironment, lowerType));
159:                                         }
160:                                 }
161:                         }
162:•                        if (result.isEmpty()) {
163:                                 result.add(new SequenceType(queryEnvironment, services.nothing(
164:                                                 "%s can't contain %s direclty or indirectly", argTypes.get(0), argTypes.get(1))));
165:                         }
166:                 }
167:
168:                 return result;
169:         }
170: }