-
1. Re: Automatic Injection based on Stereotype
alesj Mar 29, 2012 4:55 AM (in response to kwintesencja)You could modify the injection point of that Port/Eng bean; adding the right qualifier to it.
Simply grab right annotated type from extension and modify the method or field aka injection point.
-
2. Re: Automatic Injection based on Stereotype
kwintesencja Mar 30, 2012 9:27 PM (in response to alesj)Thanks for the reply Ales but i really dont know what to do in the extension,
public class HelloExtension implements Extension{ void processAnnotatedType(@Observes ProcessAnnotatedType<BaseHelloBean> pat,BeanManager bm) { if(pat.getAnnotatedType().isAnnotationPresent(Portuguese.class)){ AnnotatedType<BaseHelloBean> at = pat.getAnnotatedType(); final InjectionTarget<BaseHelloBean> it = bm.createInjectionTarget(at); //i have no ideia how to inject the PortugueseHello in the annotatedType } } }
Also i dont have(i dont want to have) an injectionPoint in the Port/English beans, the idea is to avoid:
@Inject
public void setHello(@Portuguese Hello hello){ //i want to avoid this method
super.setBaseHello(hello);
}and use:
@Model
@Portuguese
public class PortugueseHelloBean extends BaseHelloBean{
//the hello implementation is automatically injected based on the stereotype
}thus injecting the correct Hello, in the extension, based on the stereotype.
-
3. Re: Automatic Injection based on Stereotype
alesj Mar 30, 2012 4:38 AM (in response to kwintesencja)1 of 1 people found this helpfulSomething like this:
public void processAnnotatedType(@Observes ProcessAnnotatedType<Child> event) {
final AnnotatedType<Child> annotatedType = event.getAnnotatedType();
event.setAnnotatedType(new ForwardingAnnotatedType<Child>() {
@Override
public AnnotatedType<Child> delegate() {
return annotatedType;
}
@Override
public Set<AnnotatedMethod<? super Child>> getMethods() {
return super.getMethods(); // fix injection point here
}
@Override
public Set<AnnotatedField<? super Child>> getFields() {
return super.getFields(); // or, fix injection point here
}
-
4. Re: Automatic Injection based on Stereotype
alesj Mar 30, 2012 4:40 AM (in response to alesj)Child is a test class from my copy/paste example --> in your case you of course need to grab the right HelloBean.
-
5. Re: Automatic Injection based on Stereotype
kwintesencja Mar 30, 2012 12:34 PM (in response to kwintesencja)thanks for the tips, now i just need to know how to add a qualifier to a field
if(pat.getAnnotatedType().isAnnotationPresent(Portuguese.class)){ final AnnotatedType<BaseHelloBean> at = (AnnotatedType<BaseHelloBean>) pat.getAnnotatedType(); ForwardingAnnotatedType<BaseHelloBean> fy = new ForwardingAnnotatedType<BaseHelloBean>() { @Override public AnnotatedType<BaseHelloBean> delegate() { return at; } @Override public Set<AnnotatedField<? super BaseHelloBean>> getFields() { return this.modifyHelloField(super.getFields()); } private Set<AnnotatedField<? super BaseHelloBean>> modifyHelloField(Set<AnnotatedField<? super BaseHelloBean>> fields){ for (AnnotatedField<? super BaseHelloBean> annotatedField : fields) { if(annotatedField.getBaseType().equals(Hello.class)){ //now the question is how to add the qualifier to the field //annotatedField.getAnnotations().add(new PortugueseImpl()); } } } }; pat.setAnnotatedType(fy); }
i've made a fast google search and see that its not simple to add an annotation to a field so i think with CDI its done in a different way, via injection target maybe but i dont know how to do that.
-
6. Re: Automatic Injection based on Stereotype
kwintesencja Mar 30, 2012 1:16 PM (in response to kwintesencja)i think annotatedTypeBuilder should do the trick; http://docs.jboss.org/seam/3/solder/3.0.0.Beta1/api/org/jboss/seam/solder/reflection/annotated/AnnotatedTypeBuilder.html
-
7. Re: Automatic Injection based on Stereotype
kwintesencja Dec 9, 2012 5:31 PM (in response to kwintesencja)Yea that did the trick , thanks for your help Ales.
here is the working extension:
public class HelloExtension implements Extension { private AnnotatedTypeBuilder<BaseHelloBean> builder = null; void processAnnotatedType(@Observes ProcessAnnotatedType<BaseHelloBean> pat) { final AnnotatedType<BaseHelloBean> at = pat.getAnnotatedType(); if (pat.getAnnotatedType().isAnnotationPresent(Portuguese.class)) { injectLanguage(at, new PortugueseImpl()); } else if(pat.getAnnotatedType().isAnnotationPresent(English.class)){ injectLanguage(at, new EnglishImpl()); } if(builder != null){ pat.setAnnotatedType(builder.create()); } } private void injectLanguage(AnnotatedType<BaseHelloBean> at, AnnotationLiteral language){ for (AnnotatedField<? super BaseHelloBean> annotatedField : at.getFields()) { if (annotatedField.getBaseType().equals(Hello.class)) { builder = initializeBuilder(builder, at); builder.addToField(annotatedField, language); builder.addToField(annotatedField, new InjectLiteral()); } } } private <X> AnnotatedTypeBuilder<X> initializeBuilder(final AnnotatedTypeBuilder<X> currentBuilder, final AnnotatedType<X> source) { return new AnnotatedTypeBuilder<X>().readFromType(source); } } public class PortugueseImpl extends AnnotationLiteral<Portuguese> implements Portuguese{ }
and now i can inject the correct Hello based on the stereotype
@Model
@Portuguese
public class PortugueseHelloBean extends BaseHelloBean{
}
edit: blogged here(in pt_BR): http://rpestano.wordpress.com/2012/03/31/hello-world-cdi-extension/
source code here: https://github.com/rmpestano/hello-extension