/
AbstractBean.java
565 lines (517 loc) · 15.2 KB
/
AbstractBean.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
/*
* JBoss, Home of Professional Open Source
* Copyright 2008, Red Hat Middleware LLC, and individual contributors
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.webbeans.bean;
import java.lang.annotation.Annotation;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.webbeans.BindingType;
import javax.webbeans.DefinitionException;
import javax.webbeans.Dependent;
import javax.webbeans.DeploymentType;
import javax.webbeans.Event;
import javax.webbeans.InjectionPoint;
import javax.webbeans.Named;
import javax.webbeans.Specializes;
import javax.webbeans.Standard;
import javax.webbeans.Stereotype;
import javax.webbeans.manager.Bean;
import org.jboss.webbeans.ManagerImpl;
import org.jboss.webbeans.injection.InjectionPointImpl;
import org.jboss.webbeans.introspector.AnnotatedItem;
import org.jboss.webbeans.introspector.jlr.AbstractAnnotatedItem.AnnotationMap;
import org.jboss.webbeans.literal.CurrentLiteral;
import org.jboss.webbeans.log.LogProvider;
import org.jboss.webbeans.log.Logging;
import org.jboss.webbeans.model.MergedStereotypes;
import org.jboss.webbeans.util.Proxies;
import org.jboss.webbeans.util.Reflections;
/**
* An abstract bean representation common for all beans
*
* @author Pete Muir
*
* @param <T>
* @param <E>
*/
public abstract class AbstractBean<T, E> extends Bean<T>
{
@SuppressWarnings("unchecked")
private static Set<Class<?>> STANDARD_WEB_BEAN_CLASSES = new HashSet<Class<?>>(Arrays.asList(Event.class, ManagerImpl.class));
private boolean proxyable;
/**
* Helper class for getting deployment type
*
* Loops through the enabled deployment types (backwards) and returns the
* first one present in the possible deployments type, resulting in the
* deployment type of highest priority
*
* @param enabledDeploymentTypes The currently enabled deployment types
* @param possibleDeploymentTypes The possible deployment types
* @return The deployment type
*/
public static Class<? extends Annotation> getDeploymentType(List<Class<? extends Annotation>> enabledDeploymentTypes, AnnotationMap possibleDeploymentTypes)
{
for (int i = (enabledDeploymentTypes.size() - 1); i > 0; i--)
{
if (possibleDeploymentTypes.containsKey((enabledDeploymentTypes.get(i))))
{
return enabledDeploymentTypes.get(i);
}
}
return null;
}
// Logger
private LogProvider log = Logging.getLogProvider(AbstractBean.class);
// The binding types
private Set<Annotation> bindingTypes;
// The name
protected String name;
// The scope type
protected Class<? extends Annotation> scopeType;
// The merged stereotypes
private MergedStereotypes<T, E> mergedStereotypes;
// The deployment type
protected Class<? extends Annotation> deploymentType;
// The type
protected Class<T> type;
// The API types
protected Set<Type> types;
// The injection points
protected Set<AnnotatedItem<?, ?>> annotatedInjectionPoints;
// If the type a primitive?
private boolean primitive;
// The Web Beans manager
protected ManagerImpl manager;
// Cached values
private Type declaredBeanType;
/**
* Constructor
*
* @param manager The Web Beans manager
*/
public AbstractBean(ManagerImpl manager)
{
super(manager);
this.manager = manager;
annotatedInjectionPoints = new HashSet<AnnotatedItem<?, ?>>();
}
/**
* Initializes the bean and its metadata
*/
protected void init()
{
mergedStereotypes = new MergedStereotypes<T, E>(getAnnotatedItem().getMetaAnnotations(Stereotype.class));
initType();
initPrimitive();
log.debug("Building Web Bean bean metadata for " + getType());
initBindingTypes();
initName();
initDeploymentType();
checkDeploymentType();
initScopeType();
initTypes();
initProxyable();
checkRequiredTypesImplemented();
}
/**
* Initializes the API types
*/
protected void initTypes()
{
types = new HashSet<Type>();
Reflections.getTypeHierachy(getType(), types);
}
/**
* Initializes the binding types
*/
protected void initBindingTypes()
{
this.bindingTypes = new HashSet<Annotation>();
boolean specialization = getAnnotatedItem().isAnnotationPresent(Specializes.class);
this.bindingTypes.addAll(getAnnotatedItem().getMetaAnnotations(BindingType.class));
if (specialization)
{
this.bindingTypes.addAll(getSpecializedType().getBindings());
log.trace("Using binding types " + bindingTypes + " specified by annotations and specialized supertype");
}
else if (bindingTypes.size() == 0)
{
log.trace("Adding default @Current binding type");
this.bindingTypes.add(new CurrentLiteral());
}
else
{
log.trace("Using binding types " + bindingTypes + " specified by annotations");
}
return;
}
/**
* Initializes the deployment types
*/
protected void initDeploymentType()
{
Set<Annotation> deploymentTypes = getAnnotatedItem().getMetaAnnotations(DeploymentType.class);
if (deploymentTypes.size() > 1)
{
throw new DefinitionException("At most one deployment type may be specified (" + deploymentTypes + " are specified) on " + getAnnotatedItem().toString());
}
if (deploymentTypes.size() == 1)
{
this.deploymentType = deploymentTypes.iterator().next().annotationType();
log.trace("Deployment type " + deploymentType + " specified by annotation");
return;
}
AnnotationMap possibleDeploymentTypes = getMergedStereotypes().getPossibleDeploymentTypes();
if (possibleDeploymentTypes.size() > 0)
{
this.deploymentType = getDeploymentType(manager.getEnabledDeploymentTypes(), possibleDeploymentTypes);
log.trace("Deployment type " + deploymentType + " specified by stereotype");
return;
}
this.deploymentType = getDefaultDeploymentType();
log.trace("Using default @Production deployment type");
return;
}
/**
* Gets the default deployment type
*
* @return The default deployment type
*/
protected abstract Class<? extends Annotation> getDefaultDeploymentType();
/**
* Initializes the name
*/
protected void initName()
{
boolean beanNameDefaulted = false;
boolean specialization = getAnnotatedItem().isAnnotationPresent(Specializes.class);
if (getAnnotatedItem().isAnnotationPresent(Named.class))
{
if (specialization)
{
throw new DefinitionException("Name specified for specialized bean");
}
String javaName = getAnnotatedItem().getAnnotation(Named.class).value();
if ("".equals(javaName))
{
log.trace("Using default name (specified by annotations)");
beanNameDefaulted = true;
}
else
{
log.trace("Using name " + javaName + " specified by annotations");
this.name = javaName;
return;
}
}
else if (specialization)
{
this.name = getSpecializedType().getName();
log.trace("Using supertype name");
return;
}
if (beanNameDefaulted || getMergedStereotypes().isBeanNameDefaulted())
{
this.name = getDefaultName();
return;
}
}
protected void initProxyable()
{
proxyable = Proxies.apiTypesAreProxyable(getTypes());
}
/**
* Initializes the primitive flag
*/
protected void initPrimitive()
{
this.primitive = Reflections.isPrimitive(getType());
}
protected boolean injectionPointsAreSerializable()
{
// TODO: a bit crude, don't check *all* injectionpoints, only those listed
// in the spec for passivation checks
for (AnnotatedItem<?, ?> injectionPoint : getAnnotatedInjectionPoints())
{
Annotation[] bindings = injectionPoint.getMetaAnnotationsAsArray(BindingType.class);
Bean<?> resolvedBean = manager.resolveByType(injectionPoint.getType(), bindings).iterator().next();
if (Dependent.class.equals(resolvedBean.getScopeType()) && !resolvedBean.isSerializable())
{
return false;
}
}
return true;
}
/**
* Initializes the scope type
*/
protected abstract void initScopeType();
protected boolean initScopeFromStereotype()
{
Set<Annotation> possibleScopeTypes = getMergedStereotypes().getPossibleScopeTypes();
if (possibleScopeTypes.size() == 1)
{
this.scopeType = possibleScopeTypes.iterator().next().annotationType();
log.trace("Scope " + scopeType + " specified by stereotype");
return true;
}
else if (possibleScopeTypes.size() > 1)
{
throw new DefinitionException("All stereotypes must specify the same scope OR a scope must be specified on the bean");
}
else
{
return false;
}
}
/**
* Initializes the type of the bean
*/
protected abstract void initType();
/**
* Validates the deployment type
*/
protected void checkDeploymentType()
{
if (deploymentType == null)
{
throw new DefinitionException("type: " + getType() + " must specify a deployment type");
}
else if (deploymentType.equals(Standard.class) && !STANDARD_WEB_BEAN_CLASSES.contains(getAnnotatedItem().getType()))
{
throw new DefinitionException(getAnnotatedItem().getName() + " cannot have deployment type @Standard");
}
}
/**
* Validates that the required types are implemented
*/
protected void checkRequiredTypesImplemented()
{
for (Class<?> requiredType : getMergedStereotypes().getRequiredTypes())
{
log.trace("Checking if required type " + requiredType + " is implemented");
if (!requiredType.isAssignableFrom(type))
{
throw new DefinitionException("Required type " + requiredType + " isn't implemented on " + type);
}
}
}
/**
* Binds the decorators to the proxy
*/
protected void bindDecorators()
{
// TODO Implement decorators
}
/**
* Binds the interceptors to the proxy
*/
protected void bindInterceptors()
{
// TODO Implement interceptors
}
/**
* Returns the annotated time the bean represents
*
* @return The annotated item
*/
protected abstract AnnotatedItem<T, E> getAnnotatedItem();
/**
* Gets the binding types
*
* @return The set of binding types
*
* @see javax.webbeans.manager.Bean#getBindings()
*/
public Set<Annotation> getBindings()
{
return bindingTypes;
}
/**
* Gets the declared bean type
*
* @return The bean type
*/
protected Type getDeclaredBeanType()
{
if (declaredBeanType == null)
{
Type type = getClass();
if (type instanceof ParameterizedType)
{
ParameterizedType parameterizedType = (ParameterizedType) type;
if (parameterizedType.getActualTypeArguments().length == 1)
{
declaredBeanType = parameterizedType.getActualTypeArguments()[0];
}
}
}
return declaredBeanType;
}
/**
* Gets the default name of the bean
*
* @return The default name
*/
protected abstract String getDefaultName();
/**
* Gets the deployment type of the bean
*
* @return The deployment type
*
* @see javax.webbeans.manager.Bean#getDeploymentType()
*/
public Class<? extends Annotation> getDeploymentType()
{
return deploymentType;
}
/**
* Gets the injection points of the bean
*
* @return The set of injection points
*/
public Set<AnnotatedItem<?, ?>> getAnnotatedInjectionPoints()
{
return annotatedInjectionPoints;
}
public Set<InjectionPoint> getInjectionPoints()
{
Set<InjectionPoint> injectionsPoints = new HashSet<InjectionPoint>();
for (AnnotatedItem<?, ?> annotatedInjectionPoint : annotatedInjectionPoints)
{
injectionsPoints.add(InjectionPointImpl.of(annotatedInjectionPoint, this));
}
return injectionsPoints;
}
/**
* Gets the merged stereotypes of the bean
*
* @return The set of merged stereotypes
*/
public MergedStereotypes<T, E> getMergedStereotypes()
{
return mergedStereotypes;
}
/**
* Gets the name of the bean
*
* @return The name
*
* @see javax.webbeans.manager.Bean#getName()
*/
public String getName()
{
return name;
}
/**
* Gets the scope type of the bean
*
* @return The scope type
*
* @see javax.webbeans.manager.Bean#getScopeType()
*/
public Class<? extends Annotation> getScopeType()
{
return scopeType;
}
/**
* Gets the specializes type of the bean
*
* @return The specialized type
*/
protected AbstractBean<? extends T, E> getSpecializedType()
{
throw new UnsupportedOperationException();
}
/**
* Gets the type of the bean
*
* @return The type
*/
public Class<T> getType()
{
return type;
}
/**
* Gets the API types of the bean
*
* @return The set of API types
*
* @see javax.webbeans.manager.Bean#getTypes()
*/
@Override
public Set<Type> getTypes()
{
return types;
}
/**
* Checks if this beans annotated item is assignable from another annotated
* item
*
* @param annotatedItem The other annotation to check
* @return True if assignable, otherwise false
*/
public boolean isAssignableFrom(AnnotatedItem<?, ?> annotatedItem)
{
return this.getAnnotatedItem().isAssignableFrom(annotatedItem);
}
/**
* Indicates if bean is nullable
*
* @return True if nullable, false otherwise
*
* @see javax.webbeans.manager.Bean#isNullable()
*/
@Override
public boolean isNullable()
{
return !isPrimitive();
}
/**
* Indicates if bean type is a primitive
*
* @return True if primitive, false otherwise
*/
public boolean isPrimitive()
{
return primitive;
}
@Override
public boolean isSerializable()
{
// TODO: OK?
return true;
}
/**
* Gets a string representation
*
* @return The string representation
*/
@Override
public String toString()
{
return "AbstractBean " + getName();
}
public boolean isProxyable()
{
return proxyable;
}
}