-
Notifications
You must be signed in to change notification settings - Fork 3
/
QualifiedNameLookupEngine.java
378 lines (329 loc) · 12.7 KB
/
QualifiedNameLookupEngine.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*******************************************************************************
* Copyright (c) 2020, 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.namespace;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.Deque;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.eclipse.acceleo.query.runtime.IReadOnlyQueryEnvironment;
import org.eclipse.acceleo.query.runtime.IService;
import org.eclipse.acceleo.query.runtime.lookup.basic.CacheLookupEngine;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameLookupEngine;
import org.eclipse.acceleo.query.runtime.namespace.IQualifiedNameResolver;
import org.eclipse.acceleo.query.validation.type.IType;
/**
* Lookup engine for qualified name.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
*/
public class QualifiedNameLookupEngine extends CacheLookupEngine implements IQualifiedNameLookupEngine {
/**
* Mapping from qualifiedName to its {@link CacheLookupEngine}.
*/
private final Map<String, CacheLookupEngine> qualifiedNameServices = new HashMap<String, CacheLookupEngine>();
/**
* The {@link IQualifiedNameResolver}.
*/
private final IQualifiedNameResolver resolver;
/**
* The qualified names context stack.
*/
private final Deque<CallStack> callStacks = new ArrayDeque<CallStack>();
/**
* Constructor.
*
* @param queryEnvironment
* the {@link IReadOnlyQueryEnvironment}
* @param resolver
* the {@link IQualifiedNameResolver}
*/
public QualifiedNameLookupEngine(IReadOnlyQueryEnvironment queryEnvironment,
IQualifiedNameResolver resolver) {
super(queryEnvironment);
this.resolver = resolver;
}
@Override
public IService<?> lookup(String name, IType[] argumentTypes) {
final CallStack currentStack = getCurrentContext();
/* PRIVATE query or template in the same module as our current (last of the stack) */
String last = currentStack.peek();
final IService<?> lastService = getLookupEngine(last).lookup(name, argumentTypes);
IService<?> result = null;
if (lastService != null && lastService.getVisibility() == IService.Visibility.PRIVATE) {
result = lastService;
}
/*
* PUBLIC or PROTECTED template or query in the extends hierarchy of our "lowest" module in that
* hierarchy (first of the stack)
*/
if (result == null) {
String start = currentStack.getStartingQualifiedName();
result = lookupExtendedService(start, name, argumentTypes, IService.Visibility.PROTECTED,
IService.Visibility.PUBLIC);
}
/*
* We couldn't find a template or query matching that in our current extends hierarchy, try the
* imports of our current (last of the stack) module for a PUBLIC matching module element.
*/
if (result == null) {
result = lookupImportedService(last, name, argumentTypes);
}
/* There is no module element matching our target, fall back to regular services. */
if (result == null) {
result = super.lookup(name, argumentTypes);
}
return result;
}
/**
* Looks up in the hierarchy of the given {@code start}ing module qualified name for a query or template
* matching the given name and arguments.
*
* @param startQualifiedName
* The module qualified name we're considering as the "root" of our extends hierarchy.
* @param name
* The name of the service we're looking for.
* @param argumentTypes
* Type of the arguments accepted by the service we're looking for.
* @param candidateVisibilities
* The IService.Visibility to consider for our services.
* @return The service matching the criteria if any, <code>null</code> if none.
*/
private IService<?> lookupExtendedService(String startQualifiedName, String name, IType[] argumentTypes,
IService.Visibility... candidateVisibilities) {
final IService<?> service = getLookupEngine(startQualifiedName).lookup(name, argumentTypes);
IService<?> result = null;
if (service != null && isVisible(service, candidateVisibilities)) {
result = service;
}
if (result == null) {
final String extendedModuleQualifiedName = resolver.getExtend(startQualifiedName);
if (extendedModuleQualifiedName != null) {
result = lookupExtendedService(extendedModuleQualifiedName, name, argumentTypes,
candidateVisibilities);
}
}
return result;
}
/**
* Looks up in the imports of the given {@code start}ing module qualified name for a query or template
* matching the given name and arguments. This will consider public templates and queries from the imports
* and their extends hierarchy.
*
* @param start
* The module qualified name we're considering as the "root" of our imports lookup.
* @param name
* The name of the service we're looking for.
* @param argumentTypes
* Type of the arguments accepted by the service we're looking for.
* @return The service matching the criteria if any, <code>null</code> if none.
*/
private IService<?> lookupImportedService(String start, String name, IType[] argumentTypes) {
IService<?> result = null;
for (String imported : resolver.getImports(start)) {
final IService<?> service = lookupExtendedService(imported, name, argumentTypes,
IService.Visibility.PUBLIC);
if (service != null && service.getVisibility() == IService.Visibility.PUBLIC) {
result = service;
break;
}
}
return result;
}
@Override
public Set<IService<?>> getServices(Set<IType> receiverTypes) {
final Set<IService<?>> result = new LinkedHashSet<IService<?>>();
final Set<IService<?>> storedServices = getRegisteredServices();
for (IType type : receiverTypes) {
if (type != null) {
for (IService<?> service : storedServices) {
if (service.getParameterTypes(queryEnvironment).get(0).isAssignableFrom(type)) {
result.add(service);
}
}
}
}
return result;
}
@Override
public Set<IService<?>> getRegisteredServices() {
final Set<IService<?>> result = new LinkedHashSet<IService<?>>();
final CallStack currentStack = getCurrentContext();
/* Query or Template in the same module as our current (last of the stack) */
final String last = currentStack.peek();
Set<IService<?>> lastServices = getLookupEngine(last).getRegisteredServices();
result.addAll(filterByVisibility(lastServices, IService.Visibility.PRIVATE));
/*
* PUBLIC or PROTECTED template or query in the extends hierarchy of our "lowest" module in that
* hierarchy (first of the stack)
*/
String start = currentStack.getStartingQualifiedName();
result.addAll(getExtendedService(start, IService.Visibility.PROTECTED, IService.Visibility.PUBLIC));
/*
* Imports of our current (last of the stack) module for a PUBLIC matching module element.
*/
result.addAll(getImportedService(last));
result.addAll(super.getRegisteredServices());
return result;
}
/**
* Gets the {@link Set} of imported {@link IService} for the given qualified name.
*
* @param start
* the qualified name
* @return the {@link Set} of imported {@link IService} for the given qualified name and that match the
* given receiver {@link IType}
*/
private Set<IService<?>> getImportedService(String start) {
Set<IService<?>> result = new LinkedHashSet<IService<?>>();
for (String imported : resolver.getImports(start)) {
result.addAll(getExtendedService(imported, IService.Visibility.PUBLIC));
}
return result;
}
/**
* Gets the {@link Set} of {@link IService} form the given qualified name that match the given
* {@link IService.Visibility}.
*
* @param startQualifiedName
* the qualified name
* @param candidateVisibilities
* @return the {@link Set} of {@link IService} form the given qualified name that match the given
* {@link IService.Visibility}
*/
private Set<IService<?>> getExtendedService(String startQualifiedName,
IService.Visibility... candidateVisibilities) {
Set<IService<?>> services = getLookupEngine(startQualifiedName).getRegisteredServices();
Set<IService<?>> result = filterByVisibility(services, candidateVisibilities);
final String extendedModuleQualifiedName = resolver.getExtend(startQualifiedName);
if (extendedModuleQualifiedName != null) {
result.addAll(getExtendedService(extendedModuleQualifiedName, candidateVisibilities));
}
return result;
}
@Override
public boolean isRegisteredService(IService<?> service) {
return getRegisteredServices().contains(service);
}
@Override
public void pushContext(String qualifiedName) {
final CallStack currentStack = callStacks.peekLast();
currentStack.push(qualifiedName);
}
@Override
public void pushImportsContext(String importQualifiedName, String serviceContextQualifiedName) {
final CallStack currentStack = new CallStack(importQualifiedName);
callStacks.addLast(currentStack);
currentStack.push(serviceContextQualifiedName);
}
@Override
public void popContext(String qualifiedName) {
final CallStack currentStack = callStacks.peekLast();
if (currentStack == null || (!currentStack.pop().equals(qualifiedName) && currentStack.isEmpty())) {
throw new IllegalStateException("call stack is out of synchronization");
}
if (currentStack.isEmpty()) {
callStacks.pollLast();
}
}
@Override
public void clearContext(String qualifiedName) {
qualifiedNameServices.remove(qualifiedName);
resolver.cleanContextQualifiedName(qualifiedName);
}
@Override
public CallStack getCurrentContext() {
final CallStack res;
if (!callStacks.isEmpty()) {
res = callStacks.peekLast();
} else {
res = null;
}
return res;
}
/**
* Gets the {@link Set} of filtered {@link IService} with the given {@link IService.Visibility} form the
* given {@link Set} of {@link IService}.
*
* @param services
* the {@link Set} of {@link IService}
* @param candidateVisibilities
* the {@link IService.Visibility}
* @return the {@link Set} of filtered {@link IService} with the given {@link IService.Visibility} form
* the given {@link Set} of {@link IService}
*/
private Set<IService<?>> filterByVisibility(Set<IService<?>> services,
IService.Visibility... candidateVisibilities) {
return services.stream().filter(s -> isVisible(s, candidateVisibilities)).collect(Collectors
.toCollection(LinkedHashSet::new));
}
/**
* Tells if the given {@link IService} is visible according to given {@link IService.Visibility}.
*
* @param service
* the {@link IService}
* @param candidateVisibilities
* The visibilities we're expecting this service to have.
* @return <code>true</code> if the given {@link IService} is visible according to given
* {@link IService.Visibility}, <code>false</code> otherwise
*/
private boolean isVisible(IService<?> service, IService.Visibility... candidateVisibilities) {
final boolean res;
final List<IService.Visibility> visibilityList = Arrays.asList(candidateVisibilities);
res = visibilityList.contains(service.getVisibility());
return res;
}
private CacheLookupEngine getLookupEngine(String qualifiedName) {
if (!qualifiedNameServices.containsKey(qualifiedName)) {
final Object object = resolver.resolve(qualifiedName);
final CacheLookupEngine engine = new CacheLookupEngine(queryEnvironment);
for (IService<?> service : resolver.getServices(this, object, qualifiedName)) {
engine.registerService(service);
}
qualifiedNameServices.put(qualifiedName, engine);
}
return qualifiedNameServices.get(qualifiedName);
}
@Override
public IReadOnlyQueryEnvironment getQueryEnvironment() {
return queryEnvironment;
}
@Override
public String getExtend(String qualifiedName) {
return resolver.getExtend(qualifiedName);
}
@Override
public List<String> getImports(String qualifiedName) {
return resolver.getImports(qualifiedName);
}
@Override
public IQualifiedNameResolver getResolver() {
return resolver;
}
@Override
public IService<?> superServiceLookup(String name, IType[] argumentTypes) {
final IService<?> result;
final CallStack currentStack = getCurrentContext();
final String start = currentStack.getStartingQualifiedName();
final String extendQualifiedName = getExtend(start);
if (extendQualifiedName != null) {
result = lookupExtendedService(extendQualifiedName, name, argumentTypes,
IService.Visibility.PROTECTED, IService.Visibility.PUBLIC);
} else {
result = null;
}
return result;
}
}