-
Notifications
You must be signed in to change notification settings - Fork 3
/
ServiceUtils.java
334 lines (301 loc) · 11.6 KB
/
ServiceUtils.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
/*******************************************************************************
* Copyright (c) 2015, 2023 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;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.LinkedHashSet;
import java.util.Set;
import org.eclipse.acceleo.query.runtime.impl.EOperationService;
import org.eclipse.acceleo.query.runtime.impl.JavaMethodReceiverService;
import org.eclipse.acceleo.query.runtime.impl.JavaMethodService;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EOperation;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.EcorePackage;
/**
* Utility for {@link IService}.
*
* @author <a href="mailto:yvan.lussaud@obeo.fr">Yvan Lussaud</a>
* @since 5.0
*/
public final class ServiceUtils {
/**
* Constructor.
*/
private ServiceUtils() {
// nothing to do here
}
/**
* Gets the {@link Set} of {@link IService} for the given {@link Class}. If the class can't be
* instantiated only static {@link Method} will be used to produce {@link IService}.
*
* @param queryEnvironment
* the {@link IReadOnlyQueryEnvironment}
* @param cls
* the {@link Class}
* @param forWorkspace
* tells if the {@link IService} will be used in a workspace
* @return the {@link Set} of {@link IService} for the given {@link Class}
*/
public static Set<IService<?>> getServices(IReadOnlyQueryEnvironment queryEnvironment, Class<?> cls) {
return getServices(queryEnvironment, cls, false);
}
/**
* Gets the {@link Set} of {@link IService} for the given {@link Class}. If the class can't be
* instantiated only static {@link Method} will be used to produce {@link IService}.
*
* @param queryEnvironment
* the {@link IReadOnlyQueryEnvironment}
* @param cls
* the {@link Class}
* @param forWorkspace
* tells if the {@link IService} will be used in a workspace
* @return the {@link Set} of {@link IService} for the given {@link Class}
*/
public static Set<IService<?>> getServices(IReadOnlyQueryEnvironment queryEnvironment, Class<?> cls,
boolean forWorkspace) {
final Set<IService<?>> result = new LinkedHashSet<IService<?>>();
Object instance = null;
try {
Constructor<?> cstr = null;
cstr = cls.getConstructor(new Class[] {});
instance = cstr.newInstance(new Object[] {});
} catch (NoSuchMethodException e) {
// we will go without instance and register only static methods
} catch (SecurityException e) {
// we will go without instance and register only static methods
} catch (InstantiationException e) {
// we will go without instance and register only static methods
} catch (IllegalAccessException e) {
// we will go without instance and register only static methods
} catch (IllegalArgumentException e) {
// we will go without instance and register only static methods
} catch (InvocationTargetException e) {
// we will go without instance and register only static methods
}
result.addAll(getServicesFromInstance(queryEnvironment, cls, instance, forWorkspace));
return result;
}
/**
* Gets the {@link Set} of {@link IService} for the given {@link Class} with receiver as first parameter.
*
* @param queryEnvironment
* the {@link IReadOnlyQueryEnvironment}
* @param cls
* the {@link Class}
* @return the {@link Set} of {@link IService} for the given {@link Class} with receiver as first
* parameter
*/
public static Set<IService<?>> getReceiverServices(IReadOnlyQueryEnvironment queryEnvironment,
Class<?> cls) {
return getReceiverServices(queryEnvironment, cls, false);
}
/**
* Gets the {@link Set} of {@link IService} for the given {@link Class} with receiver as first parameter.
*
* @param queryEnvironment
* the {@link IReadOnlyQueryEnvironment}
* @param cls
* the {@link Class}
* @param forWorkspace
* tells if the {@link IService} will be used in a workspace
* @return the {@link Set} of {@link IService} for the given {@link Class} with receiver as first
* parameter
*/
public static Set<IService<?>> getReceiverServices(IReadOnlyQueryEnvironment queryEnvironment,
Class<?> cls, boolean forWorkspace) {
final Set<IService<?>> result = new LinkedHashSet<IService<?>>();
for (Method method : cls.getMethods()) {
if (isReveiverServiceMethod(method)) {
result.add(new JavaMethodReceiverService(method, forWorkspace));
}
}
return result;
}
/**
* Gets the {@link Set} of {@link IService} for the given {@link Object instance}.
*
* @param queryEnvironment
* the {@link IReadOnlyQueryEnvironment}
* @param instance
* the {@link Object instance}
* @return the {@link Set} of {@link IService} for the given {@link Object instance}
*/
public static Set<IService<?>> getServices(IReadOnlyQueryEnvironment queryEnvironment, Object instance) {
return getServices(queryEnvironment, instance, false);
}
/**
* Gets the {@link Set} of {@link IService} for the given {@link Object instance}.
*
* @param queryEnvironment
* the {@link IReadOnlyQueryEnvironment}
* @param instance
* the {@link Object instance}
* @param forWorkspace
* tells if the {@link IService} will be used in a workspace
* @return the {@link Set} of {@link IService} for the given {@link Object instance}
*/
public static Set<IService<?>> getServices(IReadOnlyQueryEnvironment queryEnvironment, Object instance,
boolean forWorkspace) {
return getServicesFromInstance(queryEnvironment, instance.getClass(), instance, forWorkspace);
}
/**
* Gets {@link IService} from the given instance and {@link Method} array.
*
* @param queryEnvironment
* the {@link IReadOnlyQueryEnvironment}
* @param cls
* the services {@link Class}
* @param instance
* the instance
* @return the {@link ServiceRegistrationResult}
*/
private static Set<IService<?>> getServicesFromInstance(IReadOnlyQueryEnvironment queryEnvironment,
Class<?> cls, Object instance, boolean forWorkspace) {
final Set<IService<?>> result = new LinkedHashSet<IService<?>>();
if (instance instanceof IServiceProvider) {
result.addAll(((IServiceProvider)instance).getServices(queryEnvironment, forWorkspace));
} else {
Method[] methods = cls.getMethods();
for (Method method : methods) {
if (isServiceMethod(instance, method)) {
final IService<?> service = new JavaMethodService(method, instance, forWorkspace);
result.add(service);
}
}
}
return result;
}
/**
* Tells if a given {@link Method} is considered as a {@link IService}. {@link Object} methods are not
* considered and only <code>static</code> {@link Method} are considered if the given instance if
* <code>null</code>.
*
* @param instance
* the instance {@link Object} if any, <code>null</code> otherwise
* @param method
* the {@link Method} to check
* @return <code>true</code> if a given {@link Method} is considered as a {@link IService},
* <code>false</code> otherwise
*/
public static boolean isServiceMethod(Object instance, Method method) {
final boolean result;
if (method.getDeclaringClass() == Object.class) {
// We do not register java.lang.Object method as
// having an expression calling the 'wait' or the notify service
// could yield problems that are difficult to track down.
result = false;
} else if (instance != null || Modifier.isStatic(method.getModifiers())) {
// If we have no instance, only consider static methods.
// Otherwise, any method with at least one parameter can be a service
result = method.getParameterTypes().length > 0;
} else {
result = false;
}
return result;
}
/**
* Tells if a given {@link Method} is considered as a {@link IService} with receiver as first parameter.
* {@link Object} methods are not considered.
*
* @param method
* the {@link Method} to check
* @return <code>true</code> if a given {@link Method} is considered as a {@link IService} with receiver
* as first parameter, <code>false</code> otherwise
*/
public static boolean isReveiverServiceMethod(Method method) {
// We do not register java.lang.Object method as
// having an expression calling the 'wait' or the notify service
// could yield problems that are difficult to track down.
return method.getDeclaringClass() != Object.class;
}
/**
* Registers a {@link Set} of {@link IService} to the given {@link IQueryEnvironment}.
*
* @param queryEnvironment
* the {@link IQueryEnvironment}
* @param services
* the {@link Set} of {@link IService}
* @return the {@link ServiceRegistrationResult}
*/
public static ServiceRegistrationResult registerServices(IQueryEnvironment queryEnvironment,
Set<IService<?>> services) {
final ServiceRegistrationResult result = new ServiceRegistrationResult();
for (IService<?> service : services) {
result.merge(queryEnvironment.registerService(service));
}
return result;
}
/**
* Removes a {@link Set} of {@link IService} from the given {@link IQueryEnvironment}.
*
* @param queryEnvironment
* the {@link IQueryEnvironment}
* @param services
* the {@link Set} of {@link IService}
*/
public static void removeServices(IQueryEnvironment queryEnvironment, Set<IService<?>> services) {
for (IService<?> service : services) {
queryEnvironment.removeService(service);
}
}
/**
* Gets the {@link Set} of {@link IService} for the given {@link EPackage}.
*
* @param ePkg
* the {@link EPackage}
* @return the {@link Set} of {@link IService} for the given {@link EPackage}
*/
public static Set<IService<?>> getServices(EPackage ePkg) {
final Set<IService<?>> result = new LinkedHashSet<IService<?>>();
for (EClassifier eClassifier : ePkg.getEClassifiers()) {
if (eClassifier instanceof EClass) {
result.addAll(getServices((EClass)eClassifier));
}
}
for (EPackage child : ePkg.getESubpackages()) {
result.addAll(getServices(child));
}
return result;
}
/**
* Gets the {@link Set} of {@link IService} for the given {@link EClass}.
*
* @param eCls
* the {@link EClass}
* @return the {@link Set} of {@link IService} for the given {@link EClass}
*/
public static Set<IService<?>> getServices(EClass eCls) {
final Set<IService<?>> result = new LinkedHashSet<IService<?>>();
for (EOperation eOperation : eCls.getEAllOperations()) {
if (isServiceEOperation(eOperation)) {
result.add(new EOperationService(eOperation));
}
}
return result;
}
/**
* Tells if the given {@link EOperation} is considered as a {@link IService}.
* {@link org.eclipse.emf.ecore.EObject EObject} {@link EOperation} are not considered as {@link IService}
* .
*
* @param eOperation
* the {@link EOperation} to test
* @return <code>true</code> if the given {@link EOperation} is considered as a {@link IService},
* <code>false</code> otherwise
*/
public static boolean isServiceEOperation(EOperation eOperation) {
return eOperation.getEContainingClass() != EcorePackage.eINSTANCE.getEObject();
}
}