-
Notifications
You must be signed in to change notification settings - Fork 3
/
AbstractService.java
355 lines (311 loc) · 9.59 KB
/
AbstractService.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
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/*******************************************************************************
* Copyright (c) 2015, 2024 Obeo.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Obeo - initial API and implementation
*******************************************************************************/
package org.eclipse.acceleo.query.runtime.impl;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.StringJoiner;
import org.eclipse.acceleo.query.ast.Call;
import org.eclipse.acceleo.query.runtime.AcceleoQueryEvaluationException;
import org.eclipse.acceleo.query.runtime.ICompletionProposal;
import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
import org.eclipse.acceleo.query.runtime.IService;
import org.eclipse.acceleo.query.runtime.IValidationResult;
import org.eclipse.acceleo.query.validation.type.IType;
import org.eclipse.emf.ecore.EClass;
/**
* Abstract implementation of {@link IService}.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public abstract class AbstractService<O> implements IService<O> {
/**
* The {@link O origin} of this service.
*/
private final O origin;
/**
* Known {@link IReadOnlyQueryEnvironment} to invalidate cached {@link #returnTypes} and
* {@link #parameterTypes}.
*/
private IReadOnlyQueryEnvironment knwonEnvironment;
/**
* Return {@link IType} cache.
*/
private Set<IType> returnTypes;
/**
* Parameters {@link IType} cache.
*/
private List<Set<IType>> parameterTypes;
/**
* Constructor with an {@link Object origin}.
*
* @param serviceOrigin
* the (maybe {@code null}) {@link Object origin} of this service.
*/
protected AbstractService(O serviceOrigin) {
this.origin = serviceOrigin;
}
/**
* {@inheritDoc}
*
* @see org.eclipse.acceleo.query.runtime.IService#getOrigin()
*/
@Override
public O getOrigin() {
return this.origin;
}
@Override
public List<Set<IType>> getParameterTypes(IReadOnlyQueryEnvironment queryEnvironment) {
if (knwonEnvironment != queryEnvironment || returnTypes == null) {
knwonEnvironment = queryEnvironment;
parameterTypes = computeParameterTypes(queryEnvironment);
}
return parameterTypes;
}
/**
* Computes the {@link #getParameterTypes(IReadOnlyQueryEnvironment)}.
*
* @param queryEnvironment
* the {@link IReadOnlyQueryEnvironment}
* @return the {@link #getParameterTypes(IReadOnlyQueryEnvironment)}
*/
protected abstract List<Set<IType>> computeParameterTypes(IReadOnlyQueryEnvironment queryEnvironment);
@Override
public Set<IType> getType(IReadOnlyQueryEnvironment queryEnvironment) {
if (knwonEnvironment != queryEnvironment || returnTypes == null) {
knwonEnvironment = queryEnvironment;
returnTypes = computeType(queryEnvironment);
}
return new LinkedHashSet<>(returnTypes);
}
/**
* Computes the {@link #getType(IReadOnlyQueryEnvironment)}.
*
* @param queryEnvironment
* the {@link IReadOnlyQueryEnvironment}
* @return the {@link #getType(IReadOnlyQueryEnvironment)}
*/
protected abstract Set<IType> computeType(IReadOnlyQueryEnvironment queryEnvironment);
@Override
public boolean isEqualParameterTypes(IReadOnlyQueryEnvironment queryEnvironment, IService<?> service) {
final List<Set<IType>> paramTypes1 = getParameterTypes(queryEnvironment);
final List<Set<IType>> paramTypes2 = service.getParameterTypes(queryEnvironment);
boolean result;
if (paramTypes1.size() == paramTypes2.size()) {
final Iterator<Set<IType>> it1 = paramTypes1.iterator();
final Iterator<Set<IType>> it2 = paramTypes2.iterator();
result = true;
while (it1.hasNext()) {
final Set<IType> types1 = it1.next();
final Set<IType> types2 = it2.next();
for (IType type1 : types1) {
boolean isEquals = false;
for (IType type2 : types2) {
if (type2.equals(type1)) {
isEquals = true;
break;
}
}
if (!isEquals) {
result = false;
break;
}
}
}
} else {
result = false;
}
return result;
}
public boolean isLowerOrEqualParameterTypes(IReadOnlyQueryEnvironment queryEnvironment,
IService<?> service) {
final List<Set<IType>> paramTypes1 = getParameterTypes(queryEnvironment);
final List<Set<IType>> paramTypes2 = service.getParameterTypes(queryEnvironment);
boolean result;
if (paramTypes1.size() == paramTypes2.size()) {
final Iterator<Set<IType>> it1 = paramTypes1.iterator();
final Iterator<Set<IType>> it2 = paramTypes2.iterator();
result = true;
while (it1.hasNext()) {
final Set<IType> types1 = it1.next();
final Set<IType> types2 = it2.next();
for (IType paramType1 : types1) {
boolean isAssignable = false;
for (IType paramType2 : types2) {
if (paramType2.isAssignableFrom(paramType1)) {
isAssignable = true;
break;
}
}
if (!isAssignable) {
result = false;
break;
}
}
}
} else {
result = false;
}
return result;
}
public boolean matches(IReadOnlyQueryEnvironment queryEnvironment, IType[] argumentTypes) {
assert getNumberOfParameters() == argumentTypes.length;
boolean result = true;
final List<Set<IType>> parameterTypes = getParameterTypes(queryEnvironment);
for (int i = 0; i < parameterTypes.size() && result; i++) {
if (argumentTypes[i].getType() != null) {
boolean oneAssignable = false;
for (IType parameterType : parameterTypes.get(i)) {
if (parameterType.isAssignableFrom(argumentTypes[i])) {
oneAssignable = true;
break;
}
}
if (!oneAssignable) {
result = false;
break;
}
}
}
return result;
}
@Override
public Set<IType> validateAllType(ValidationServices services, IReadOnlyQueryEnvironment queryEnvironment,
Map<List<IType>, Set<IType>> allTypes) {
final Set<IType> result = new LinkedHashSet<IType>();
for (Entry<List<IType>, Set<IType>> entry : allTypes.entrySet()) {
// FIXME check for "empty" and put a message instead.
result.addAll(entry.getValue());
}
return result;
}
@Override
public Object invoke(Object... arguments) throws AcceleoQueryEvaluationException {
final Object result;
try {
result = internalInvoke(arguments);
// CHECKSTYLE:OFF
} catch (Exception e) {
// CHECKSTYLE:ON
final Throwable cause;
final String message;
if (e.getCause() != null) {
cause = e.getCause();
} else {
cause = e;
}
message = cause.getMessage();
throw new AcceleoQueryEvaluationException(getShortSignature() + " with arguments " + Arrays
.deepToString(arguments) + " failed:\n\t" + message, cause);
}
return result;
}
/**
* Does the internal invocation.
*
* @param arguments
* arguments
* @return the result of the invocation
* @throws Exception
* when the invocation goes wrong
*/
protected abstract Object internalInvoke(Object[] arguments) throws Exception;
/**
* Build up the specified service's signature for reporting.
*
* @param argumentTypes
* the service's call argument types
* @return the specified service's signature
*/
protected String serviceShortSignature(Object[] argumentTypes) {
StringBuilder builder = new StringBuilder();
builder.append(getName()).append('(');
boolean first = true;
for (Object argType : argumentTypes) {
if (!first) {
builder.append(',');
} else {
first = false;
}
builder.append(getTypeString(argType));
}
return builder.append(')').toString();
}
/**
* Gets the {@link String} representation of the given type {@link Object}.
*
* @param argType
* the type {@link Object}
* @return the {@link String} representation of the given type {@link Object}
*/
private String getTypeString(Object argType) {
final String res;
if (argType instanceof Set<?>) {
if (((Set<?>)argType).size() > 1) {
final StringJoiner joiner = new StringJoiner(" | ");
for (Object type : (Set<?>)argType) {
joiner.add(getTypeString(type));
}
res = joiner.toString();
} else {
res = getTypeString(((Set<?>)argType).iterator().next());
}
} else if (argType instanceof Class<?>) {
res = ((Class<?>)argType).getCanonicalName();
} else if (argType instanceof EClass) {
res = "EClass=" + ((EClass)argType).getName();
} else if (argType == null) {
res = "Object=null";
} else {
// should not happen
res = "Object=" + argType.toString();
}
return res;
}
@Override
public String toString() {
return getLongSignature();
}
@Override
public List<ICompletionProposal> getProposals(IReadOnlyQueryEnvironment queryEnvironment,
Set<IType> receiverTypes) {
return Collections.emptyList();
}
@Override
public Visibility getVisibility() {
return Visibility.PUBLIC;
}
@Override
public Set<IType> getType(Call call, ValidationServices services, IValidationResult validationResult,
IReadOnlyQueryEnvironment queryEnvironment, List<IType> argTypes) {
return getType(queryEnvironment);
}
@Override
public boolean equals(Object obj) {
return obj instanceof IService<?> && (getOrigin() == null && ((IService<?>)obj).getOrigin() == null
|| getOrigin().equals(((IService<?>)obj).getOrigin()));
}
@Override
public int hashCode() {
final int res;
if (getOrigin() != null) {
res = getOrigin().hashCode();
} else {
res = super.hashCode();
}
return res;
}
}