62
62
import java .util .*;
63
63
import java .util .function .Function ;
64
64
65
+ import static java .lang .String .format ;
65
66
import static java .lang .System .getProperty ;
66
67
import static java .util .Collections .unmodifiableList ;
67
68
import static java .util .Objects .requireNonNull ;
@@ -141,6 +142,10 @@ public class StandardBeanManager implements BeanManager, Instance<Object> {
141
142
private final Set <DeploymentException > deploymentProblems ;
142
143
143
144
145
+ private boolean firedAfterBeanDiscoveryEvent = false ;
146
+
147
+ private boolean firedAfterDeploymentValidationEvent = false ;
148
+
144
149
public StandardBeanManager () {
145
150
this .classLoader = ClassLoaderUtils .getClassLoader (getClass ());
146
151
@@ -170,13 +175,20 @@ public StandardBeanManager() {
170
175
171
176
@ Override
172
177
public Object getReference (Bean <?> bean , Type beanType , CreationalContext <?> ctx ) {
178
+ assertAfterDeploymentValidation ();
179
+ if (!Objects .equals (beanType .getTypeName (), bean .getBeanClass ().getTypeName ())) {
180
+ throw new IllegalArgumentException (format ("The given type[%s] is not a bean type[%s] of the given bean!" ,
181
+ beanType .getTypeName (),
182
+ bean .getBeanClass ()));
183
+ }
173
184
Class <? extends Annotation > scope = bean .getScope ();
174
- // TODO
175
- return null ;
185
+ Context context = getContext ( scope );
186
+ return context . get (( Contextual ) bean , ctx ) ;
176
187
}
177
188
178
189
@ Override
179
190
public Object getInjectableReference (InjectionPoint ij , CreationalContext <?> ctx ) {
191
+ assertAfterDeploymentValidation ();
180
192
// TODO
181
193
return null ;
182
194
}
@@ -189,30 +201,35 @@ public <T> CreationalContext<T> createCreationalContext(Contextual<T> contextual
189
201
190
202
@ Override
191
203
public Set <Bean <?>> getBeans (Type beanType , Annotation ... qualifiers ) {
204
+ assertAfterBeanDiscovery ();
192
205
// TODO
193
206
return null ;
194
207
}
195
208
196
209
@ Override
197
210
public Set <Bean <?>> getBeans (String name ) {
211
+ assertAfterBeanDiscovery ();
198
212
// TODO
199
213
return null ;
200
214
}
201
215
202
216
@ Override
203
217
public Bean <?> getPassivationCapableBean (String id ) {
218
+ assertAfterBeanDiscovery ();
204
219
// TODO
205
220
return null ;
206
221
}
207
222
208
223
@ Override
209
224
public <X > Bean <? extends X > resolve (Set <Bean <? extends X >> beans ) {
225
+ assertAfterBeanDiscovery ();
210
226
// TODO
211
227
return null ;
212
228
}
213
229
214
230
@ Override
215
231
public void validate (InjectionPoint injectionPoint ) {
232
+ assertAfterBeanDiscovery ();
216
233
Annotated annotated = injectionPoint .getAnnotated ();
217
234
if (annotated instanceof AnnotatedField ) { // InjectionPoint on Field
218
235
validateFieldInjectionPoint (injectionPoint );
@@ -226,6 +243,39 @@ public void validate(InjectionPoint injectionPoint) {
226
243
}
227
244
}
228
245
246
+ /**
247
+ * @param injectionPoint {@link InjectionPoint}
248
+ * @throws DefinitionException If an injected field is annotated @Produces, the container automatically detects
249
+ * the problem and treats it as a definition error.
250
+ */
251
+ private void validateFieldInjectionPoint (InjectionPoint injectionPoint ) throws DefinitionException {
252
+ validateForbiddenAnnotation (injectionPoint , Produces .class );
253
+ }
254
+
255
+ /**
256
+ * @param injectionPoint {@link InjectionPoint}
257
+ * @throws DefinitionException If a bean constructor has a parameter annotated @Disposes, @Observes, or @ObservesAsync,
258
+ * the container automatically detects the problem and treats it as a definition error.
259
+ */
260
+ private void validateConstructorParameterInjectionPoint (InjectionPoint injectionPoint ) throws DefinitionException {
261
+ validateForbiddenAnnotation (injectionPoint , Disposes .class );
262
+ validateForbiddenAnnotation (injectionPoint , Observes .class );
263
+ validateForbiddenAnnotation (injectionPoint , ObservesAsync .class );
264
+ }
265
+
266
+ /**
267
+ * @param injectionPoint {@link InjectionPoint}
268
+ * @throws DefinitionException If an initializer method is annotated @Produces, has a parameter annotated @Disposes,
269
+ * has a parameter annotated @Observes, or has a parameter annotated @ObservesAsync,
270
+ * the container automatically detects the problem and treats it as a definition error.
271
+ */
272
+ private void validateMethodParameterInjectionPoint (InjectionPoint injectionPoint ) throws DefinitionException {
273
+ validateForbiddenAnnotation ((Method ) injectionPoint .getMember (), Produces .class );
274
+ validateForbiddenAnnotation (injectionPoint , Disposes .class );
275
+ validateForbiddenAnnotation (injectionPoint , Observes .class );
276
+ validateForbiddenAnnotation (injectionPoint , ObservesAsync .class );
277
+ }
278
+
229
279
/**
230
280
* Is defining annotation type or not.
231
281
* <p>
@@ -272,39 +322,6 @@ public boolean isDefiningAnnotationType(Class<?> type, boolean includedIntercept
272
322
return hasDefiningAnnotation ;
273
323
}
274
324
275
- /**
276
- * @param injectionPoint {@link InjectionPoint}
277
- * @throws DefinitionException If an injected field is annotated @Produces, the container automatically detects
278
- * the problem and treats it as a definition error.
279
- */
280
- private void validateFieldInjectionPoint (InjectionPoint injectionPoint ) throws DefinitionException {
281
- validateForbiddenAnnotation (injectionPoint , Produces .class );
282
- }
283
-
284
- /**
285
- * @param injectionPoint {@link InjectionPoint}
286
- * @throws DefinitionException If a bean constructor has a parameter annotated @Disposes, @Observes, or @ObservesAsync,
287
- * the container automatically detects the problem and treats it as a definition error.
288
- */
289
- private void validateConstructorParameterInjectionPoint (InjectionPoint injectionPoint ) throws DefinitionException {
290
- validateForbiddenAnnotation (injectionPoint , Disposes .class );
291
- validateForbiddenAnnotation (injectionPoint , Observes .class );
292
- validateForbiddenAnnotation (injectionPoint , ObservesAsync .class );
293
- }
294
-
295
- /**
296
- * @param injectionPoint {@link InjectionPoint}
297
- * @throws DefinitionException If an initializer method is annotated @Produces, has a parameter annotated @Disposes,
298
- * has a parameter annotated @Observes, or has a parameter annotated @ObservesAsync,
299
- * the container automatically detects the problem and treats it as a definition error.
300
- */
301
- private void validateMethodParameterInjectionPoint (InjectionPoint injectionPoint ) throws DefinitionException {
302
- validateForbiddenAnnotation ((Method ) injectionPoint .getMember (), Produces .class );
303
- validateForbiddenAnnotation (injectionPoint , Disposes .class );
304
- validateForbiddenAnnotation (injectionPoint , Observes .class );
305
- validateForbiddenAnnotation (injectionPoint , ObservesAsync .class );
306
- }
307
-
308
325
@ Override
309
326
@ Deprecated
310
327
public void fireEvent (Object event , Annotation ... qualifiers ) {
@@ -315,17 +332,20 @@ public void fireEvent(Object event, Annotation... qualifiers) {
315
332
316
333
@ Override
317
334
public <T > Set <ObserverMethod <? super T >> resolveObserverMethods (T event , Annotation ... qualifiers ) {
335
+ assertAfterBeanDiscovery ();
318
336
return (Set ) observerMethodsManager .resolveObserverMethods (event , qualifiers );
319
337
}
320
338
321
339
@ Override
322
340
public List <Decorator <?>> resolveDecorators (Set <Type > types , Annotation ... qualifiers ) {
341
+ assertAfterBeanDiscovery ();
323
342
// TODO
324
343
return null ;
325
344
}
326
345
327
346
@ Override
328
347
public List <Interceptor <?>> resolveInterceptors (InterceptionType type , Annotation ... interceptorBindings ) {
348
+ assertAfterBeanDiscovery ();
329
349
// TODO
330
350
return null ;
331
351
}
@@ -415,6 +435,7 @@ public <T> AnnotatedType<T> createAnnotatedType(Class<T> type) {
415
435
@ Deprecated
416
436
@ Override
417
437
public <T > InjectionTarget <T > createInjectionTarget (AnnotatedType <T > type ) {
438
+
418
439
return null ;
419
440
}
420
441
@@ -491,6 +512,7 @@ public Event<Object> getEvent() {
491
512
492
513
@ Override
493
514
public Instance <Object > createInstance () {
515
+ assertAfterBeanDiscovery ();
494
516
// TODO
495
517
return null ;
496
518
}
@@ -825,11 +847,29 @@ private void registerDecoratorBeans() {
825
847
/**
826
848
* the container must fire an event of type AfterBeanDiscovery, as defined in AfterBeanDiscovery event, and abort
827
849
* initialization of the application if any observer registers a definition error.
850
+ * An exception is thrown if the following operations are called before the AfterBeanDiscovery event is fired:
851
+ * <ul>
852
+ * <li>{@link #getBeans(String)}</li>
853
+ * <li>{@link #getBeans(Type, Annotation...)}</li>
854
+ * <li>{@link #getPassivationCapableBean(String)}</li>
855
+ * <li>{@link #resolve(Set)}</li>
856
+ * <li>{@link #resolveDecorators(Set, Annotation...)}</li>
857
+ * <li>{@link #resolveInterceptors(InterceptionType, Annotation...)}</li>
858
+ * <li>{@link #resolveObserverMethods(Object, Annotation...)}</li>
859
+ * <li>{@link #validate(InjectionPoint)}</li>
860
+ * </ul>
828
861
*/
829
862
private void performAfterBeanDiscovery () {
830
863
fireAfterBeanDiscoveryEvent ();
831
864
}
832
865
866
+ private void assertAfterBeanDiscovery () {
867
+ if (!firedAfterBeanDiscoveryEvent ) {
868
+ throw new UnsupportedOperationException ("Current operation must not be invoked before " +
869
+ "AfterBeanDiscovery event is fired!" );
870
+ }
871
+ }
872
+
833
873
/**
834
874
* the container must detect deployment problems by validating bean dependencies and specialization and abort
835
875
* initialization of the application if any deployment problems exist, as defined in Problems detected automatically
@@ -842,11 +882,24 @@ private void performDeploymentValidation() {
842
882
/**
843
883
* the container must fire an event of type AfterDeploymentValidation, as defined in AfterDeploymentValidation event,
844
884
* and abort initialization of the application if any observer registers a deployment problem.
885
+ * An exception is thrown if the following operations are called before the AfterBeanDiscovery event is fired:
886
+ * <ul>
887
+ * <li>{@link #createInstance()}</li>
888
+ * <li>{@link #getReference(Bean, Type, CreationalContext)}</li>
889
+ * <li>{@link #getInjectableReference(InjectionPoint, CreationalContext)}</li>
890
+ * </ul>
845
891
*/
846
892
private void performAfterDeploymentValidation () {
847
893
fireAfterDeploymentValidationEvent ();
848
894
}
849
895
896
+ private void assertAfterDeploymentValidation () {
897
+ if (!firedAfterDeploymentValidationEvent ) {
898
+ throw new UnsupportedOperationException ("Current operation must not be invoked before " +
899
+ "AfterDeploymentValidation event is fired" );
900
+ }
901
+ }
902
+
850
903
private StandardBeanManager discoverExtensions () {
851
904
load (Extension .class , classLoader ).forEach (this ::addExtension );
852
905
return this ;
@@ -930,6 +983,7 @@ private void fireProcessProducerEvent(AnnotatedMember annotatedMember, Producer
930
983
*/
931
984
private void fireAfterBeanDiscoveryEvent () {
932
985
fireEvent (new AfterBeanDiscoveryEvent (this ));
986
+ firedAfterBeanDiscoveryEvent = true ;
933
987
}
934
988
935
989
@@ -939,6 +993,7 @@ private void fireAfterBeanDiscoveryEvent() {
939
993
*/
940
994
private void fireAfterDeploymentValidationEvent () {
941
995
fireEvent (new AfterDeploymentValidationEvent (this ));
996
+ firedAfterDeploymentValidationEvent = true ;
942
997
}
943
998
944
999
private void fireEvent (Object event ) {
0 commit comments