-
Notifications
You must be signed in to change notification settings - Fork 316
/
GuiceModuleAccess.java
442 lines (362 loc) · 12.2 KB
/
GuiceModuleAccess.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
/**
* Copyright (c) 2015, 2020 itemis AG (http://www.itemis.eu) and others.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0.
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.xtext.xtext.generator.model;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import org.apache.log4j.Logger;
import org.eclipse.xtend2.lib.StringConcatenationClient;
import org.eclipse.xtext.xbase.lib.util.ToStringBuilder;
/**
* Configuration object for Guice modules based on
* {@link org.eclipse.xtext.service.AbstractGenericModule}.
*/
public class GuiceModuleAccess {
private static final Logger LOG = Logger.getLogger(GuiceModuleAccess.class);
public static class BindKey {
private final String name;
private final TypeReference type;
private final boolean singleton;
private final boolean eagerSingleton;
@Override
public boolean equals(Object other) {
if (other instanceof GuiceModuleAccess.BindKey) {
return Objects.equals(this.name, ((GuiceModuleAccess.BindKey) other).name)
&& Objects.equals(this.type, ((GuiceModuleAccess.BindKey) other).type);
} else {
return false;
}
}
@Override
public int hashCode() {
int h = 0;
if (name != null) {
h = h + name.hashCode();
}
if (type != null) {
h = h + type.hashCode();
}
return h;
}
public BindKey(String name, TypeReference type, boolean singleton, boolean eagerSingleton) {
this.name = name;
this.type = type;
this.singleton = singleton;
this.eagerSingleton = eagerSingleton;
}
@Override
public String toString() {
ToStringBuilder b = new ToStringBuilder(this);
b.add("name", name);
b.add("type", type);
b.add("singleton", singleton);
b.add("eagerSingleton", eagerSingleton);
return b.toString();
}
public String getName() {
return name;
}
public TypeReference getType() {
return type;
}
public boolean isSingleton() {
return singleton;
}
public boolean isEagerSingleton() {
return eagerSingleton;
}
}
public static class BindValue {
private final Object expression;
private final TypeReference type;
private final boolean provider;
private final List<Object> statements;
public BindValue(Object expression, TypeReference type, boolean provider, List<Object> statements) {
this.expression = expression;
this.type = type;
this.provider = provider;
this.statements = statements;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((this.expression == null) ? 0 : this.expression.hashCode());
result = prime * result + ((this.type == null) ? 0 : this.type.hashCode());
result = prime * result + (this.provider ? 1231 : 1237);
return prime * result + ((this.statements == null) ? 0 : this.statements.hashCode());
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
GuiceModuleAccess.BindValue other = (GuiceModuleAccess.BindValue) obj;
if (this.expression == null) {
if (other.expression != null)
return false;
} else if (!this.expression.equals(other.expression))
return false;
if (this.type == null) {
if (other.type != null)
return false;
} else if (!this.type.equals(other.type))
return false;
if (other.provider != this.provider)
return false;
if (this.statements == null) {
if (other.statements != null)
return false;
} else if (!this.statements.equals(other.statements))
return false;
return true;
}
@Override
public String toString() {
ToStringBuilder b = new ToStringBuilder(this);
b.add("expression", expression);
b.add("type", type);
b.add("provider", provider);
b.add("statements", statements);
return b.toString();
}
public Object getExpression() {
return expression;
}
public TypeReference getType() {
return type;
}
public boolean isProvider() {
return provider;
}
public List<Object> getStatements() {
return statements;
}
}
public static class Binding {
private final GuiceModuleAccess.BindKey key;
private final GuiceModuleAccess.BindValue value;
private final boolean isFinal;
private final String contributedBy;
@Override
public boolean equals(Object other) {
if (other instanceof GuiceModuleAccess.Binding) {
return Objects.equals(this.key, ((GuiceModuleAccess.Binding) other).key);
} else {
return false;
}
}
@Override
public int hashCode() {
return key.hashCode();
}
public Binding(GuiceModuleAccess.BindKey key, GuiceModuleAccess.BindValue value, boolean isFinal,
String contributedBy) {
this.key = key;
this.value = value;
this.isFinal = isFinal;
this.contributedBy = contributedBy;
}
@Override
public String toString() {
ToStringBuilder b = new ToStringBuilder(this);
b.add("key", key);
b.add("value", value);
b.add("isFinal", isFinal);
b.add("contributedBy", contributedBy);
return b.toString();
}
public GuiceModuleAccess.BindKey getKey() {
return key;
}
public GuiceModuleAccess.BindValue getValue() {
return value;
}
public boolean isFinal() {
return isFinal;
}
public String getContributedBy() {
return contributedBy;
}
}
public static class BindingFactory {
private final String contributedBy;
private final Set<GuiceModuleAccess.Binding> bindings = new LinkedHashSet<>();
public BindingFactory() {
this.contributedBy = new Exception().getStackTrace()[1].getClassName();
}
public BindingFactory(String contributedBy) {
this.contributedBy = contributedBy;
}
private void add(GuiceModuleAccess.BindKey type, GuiceModuleAccess.BindValue expr) {
add(type, expr, false);
}
private void add(GuiceModuleAccess.BindKey type, GuiceModuleAccess.BindValue expr, boolean isFinal) {
add(new GuiceModuleAccess.Binding(type, expr, isFinal, contributedBy));
}
private void add(GuiceModuleAccess.Binding binding) {
if (!bindings.add(binding)) {
throw new IllegalArgumentException(
"Duplicate binding for " + binding.key + " in " + this.contributedBy);
}
}
private GuiceModuleAccess.BindKey key(TypeReference type) {
return new GuiceModuleAccess.BindKey(null, type, false, false);
}
private GuiceModuleAccess.BindKey key(String name) {
return new GuiceModuleAccess.BindKey(name, null, false, false);
}
private GuiceModuleAccess.BindKey eagerSingleton(TypeReference type) {
return new GuiceModuleAccess.BindKey(null, type, true, true);
}
private GuiceModuleAccess.BindKey singleton(TypeReference type) {
return new GuiceModuleAccess.BindKey(null, type, true, false);
}
private GuiceModuleAccess.BindValue value(TypeReference type) {
return new GuiceModuleAccess.BindValue(null, type, false, Collections.emptyList());
}
private GuiceModuleAccess.BindValue expr(Object expr) {
return new GuiceModuleAccess.BindValue(expr, null, false, Collections.emptyList());
}
private GuiceModuleAccess.BindValue provider(TypeReference type) {
return new GuiceModuleAccess.BindValue(null, type, true, Collections.emptyList());
}
private GuiceModuleAccess.BindValue providerExpr(Object expr) {
return new GuiceModuleAccess.BindValue(expr, null, true, Collections.emptyList());
}
private GuiceModuleAccess.BindValue statements(Object[] statements) {
return new GuiceModuleAccess.BindValue(null, null, false, Arrays.asList(statements));
}
public GuiceModuleAccess.BindingFactory addTypeToInstance(TypeReference type,
StringConcatenationClient expression) {
add(key(type), expr(expression));
return this;
}
public GuiceModuleAccess.BindingFactory addTypeToProviderInstance(TypeReference type,
StringConcatenationClient expression) {
add(key(type), providerExpr(expression));
return this;
}
public GuiceModuleAccess.BindingFactory addConfiguredBinding(String name, StringConcatenationClient statement) {
add(key(name), statements(new Object[] { statement }));
return this;
}
public GuiceModuleAccess.BindingFactory addTypeToType(TypeReference keyType, TypeReference valueType) {
add(key(keyType), value(valueType));
return this;
}
public GuiceModuleAccess.BindingFactory addTypeToTypeSingleton(TypeReference keyType, TypeReference valueType) {
add(singleton(keyType), value(valueType));
return this;
}
public GuiceModuleAccess.BindingFactory addTypeToTypeEagerSingleton(TypeReference keyType,
TypeReference valueType) {
add(eagerSingleton(keyType), value(valueType));
return this;
}
public GuiceModuleAccess.BindingFactory addTypeToProvider(TypeReference keyType, TypeReference valueType) {
add(key(keyType), provider(valueType));
return this;
}
public GuiceModuleAccess.BindingFactory addTypeToProviderSingleton(TypeReference keyType,
TypeReference valueType) {
this.add(this.singleton(keyType), this.provider(valueType));
return this;
}
public GuiceModuleAccess.BindingFactory addTypeToProviderEagerSingleton(TypeReference keyType,
TypeReference valueType) {
add(eagerSingleton(keyType), provider(valueType));
return this;
}
public GuiceModuleAccess.BindingFactory addfinalTypeToType(TypeReference keyType, TypeReference valueType) {
add(key(keyType), value(valueType), true);
return this;
}
public GuiceModuleAccess.BindingFactory addfinalTypeToTypeSingleton(TypeReference keyType,
TypeReference valueType) {
add(singleton(keyType), value(valueType), true);
return this;
}
public GuiceModuleAccess.BindingFactory addfinalTypeToTypeEagerSingleton(TypeReference keyType,
TypeReference valueType) {
add(eagerSingleton(keyType), value(valueType), true);
return this;
}
public GuiceModuleAccess.BindingFactory addfinalTypeToProvider(TypeReference keyType, TypeReference valueType) {
add(key(keyType), provider(valueType), true);
return this;
}
public GuiceModuleAccess.BindingFactory addfinalTypeToProviderSingleton(TypeReference keyType,
TypeReference valueType) {
add(singleton(keyType), provider(valueType), true);
return this;
}
public GuiceModuleAccess.BindingFactory addfinalTypeToProviderEagerSingleton(TypeReference keyType,
TypeReference valueType) {
add(eagerSingleton(keyType), provider(valueType), true);
return this;
}
public void contributeTo(GuiceModuleAccess module) {
module.addAll(bindings);
}
public String getContributedBy() {
return contributedBy;
}
}
private final Set<GuiceModuleAccess.Binding> bindings = new LinkedHashSet<>();
private TypeReference superClass;
public void add(GuiceModuleAccess.Binding newBinding) {
if (bindings.contains(newBinding)) {
Iterator<GuiceModuleAccess.Binding> iterator = bindings.iterator();
boolean found = false;
while (iterator.hasNext() && !found) {
GuiceModuleAccess.Binding oldBinding = iterator.next();
if (Objects.equals(oldBinding, newBinding)) {
if (oldBinding.isFinal) {
if (newBinding.isFinal) {
throw new IllegalStateException(
"Conflicting final bindings for '" + oldBinding.key.type + "' from fragments "
+ oldBinding.contributedBy + " and " + newBinding.contributedBy);
} else {
GuiceModuleAccess.LOG.warn("Cannot override final binding '" + oldBinding + "'. "
+ "Ignoring binding from fragment '" + newBinding.contributedBy + "'");
}
} else {
GuiceModuleAccess.LOG.debug("replacing binding : " + oldBinding);
GuiceModuleAccess.LOG.debug(" with new binding : " + newBinding);
iterator.remove();
}
found = true;
}
}
}
bindings.add(newBinding);
}
public void addAll(Iterable<GuiceModuleAccess.Binding> bindings) {
for (GuiceModuleAccess.Binding binding : bindings) {
add(binding);
}
}
public Set<GuiceModuleAccess.Binding> getBindings() {
return Collections.unmodifiableSet(bindings);
}
public TypeReference getSuperClass() {
return superClass;
}
public void setSuperClass(TypeReference superClass) {
this.superClass = superClass;
}
}