-
1. Re: Use of AnnotationLiteral
nickarls Nov 23, 2009 10:36 PM (in response to swd847)I'm not sure when it's allowed and when not but I think it was mostly designed for anonymous usage in order to
fake
qualifiers when working with InstanceBean or the SPI that takes vararg qualifiers -
2. Re: Use of AnnotationLiteral
swd847 Nov 23, 2009 10:45 PM (in response to swd847)Yes, but the first form is not assignable to the Annotation type, which seems like it should cause problems.
-
3. Re: Use of AnnotationLiteral
nickarls Nov 23, 2009 10:53 PM (in response to swd847)Sure it is! It compiles(tm) ;-)
-
4. Re: Use of AnnotationLiteral
swd847 Nov 23, 2009 11:04 PM (in response to swd847)Sorry, not the actual type Annotation, but to the actual subtype of the annotation type , e.g.
Annotation.class.isAssignableFrom(role.getClass());
will be true for both forms, however
Role.class.isAssignableFrom(role.getClass());
will only work for the second form.
-
5. Re: Use of AnnotationLiteral
gavin.king Nov 23, 2009 11:14 PM (in response to swd847)You need the long form when you have annotation members.
-
6. Re: Use of AnnotationLiteral
asookazian Dec 2, 2009 7:49 PM (in response to swd847)is AnnotationLiteral or equivalent going to be supported in JDK 7?
context:
@Stateless public class ProductManager { @PersistenceContext EntityManager em; @Inject @Any Event<Product> productEvent; public void delete(Product product) { em.delete(product); productEvent.select(new AnnotationLiteral<Deleted>(){}).fire(product); } public void persist(Product product) { em.persist(product); productEvent.select(new AnnotationLiteral<Created>(){}).fire(product); } ... }
please explain how this line of code works or what it does exactly:
productEvent.select(new AnnotationLiteral<Deleted>(){}).fire(product);
Supports inline instantiation of annotation type instances.http://docs.jboss.org/cdi/api/1.0/javax/enterprise/util/AnnotationLiteral.html
So basically the container fires a deleted event for product instance to be observed later?
are any other frmwks dynamically instantiating annotations like this?
it would be nice if we could simply write:
productEvent.fire(@Deleted product);
-
7. Re: Use of AnnotationLiteral
nickarls Dec 2, 2009 7:57 PM (in response to swd847)You create a @Deleted qualifier (in the closest way doable currently in java) to narrow down the recievers and fire the product as a payload
Stuart has done some work on an AnnotationCache component that can be injected and used as a factory for prettier annotation instantiation...
-
8. Re: Use of AnnotationLiteral
asookazian Dec 2, 2009 7:59 PM (in response to swd847)
Nicklas Karlsson wrote on Dec 02, 2009 19:57:
Stuart has done some work on an AnnotationCache component that can be injected and used as a factory for prettier annotation instantiation...cool, i'd like to see that...
-
9. Re: Use of AnnotationLiteral
swd847 Dec 3, 2009 12:39 AM (in response to swd847)It's now called AnnotationInstanceProvider and is availbile in the weld-extensions repo:
-
10. Re: Use of AnnotationLiteral
gavin.king Dec 3, 2009 12:56 AM (in response to swd847)Folks, note that, while Stuart's work is extremely useful for portable extension code, I don't recommend the use of AnnotationInstanceProvider in application code, since it's non-typesafe (you specify member names using strings).
-
11. Re: Use of AnnotationLiteral
asookazian Dec 3, 2009 1:02 AM (in response to swd847)
Gavin King wrote on Dec 03, 2009 00:56:
Folks, note that, while Stuart's work is extremely useful for portable extension code, I don't recommend the use of AnnotationInstanceProvider in application code, since it's non-typesafe (you specify member names using strings).You're referring to this:
/** * Returns an instance of the given annotation type with member values * specified in the map. */ public <T extends Annotation> T get(Class<T> annotation, Map<String, Object> values)
i.e. the Map<String, Object>?
so how do we make this typesafe? use an annotation I suppose?
-
12. Re: Use of AnnotationLiteral
swd847 Dec 3, 2009 1:06 AM (in response to swd847)If you are in a position where the annotation is already on the classpath just subclass AnnotationLiteral. AnnotationInstanceProvider should only be used in situations where you do not have access to the annotation at compile time. e.g. in my XML extension you can do this:
<test:OtherQualifier> <Qualifier/> </test:OtherQualifier> <test:QualifiedBean1> <test:OtherQualifier value1="AA" value2="1">A</test:OtherQualifier> </test:QualifiedBean1> <test:QualifiedBean2> <test:OtherQualifier value1="BB" value2="2" value="B" /> </test:QualifiedBean2> <test:QualifierTestBean> <test:bean1> <test:OtherQualifier value1="AA" value2="1" value="A" /> <Inject/> </test:bean1> <test:bean2> <test:OtherQualifier value1="BB" value2="2">B</test:OtherQualifier> <Inject/> </test:bean2> </test:QualifierTestBean>
In this situation I need to use the creator to create instances of OtherQualifier because OtherQualifier is not available to subclass when I am compiling my extension.