12 Replies Latest reply on Dec 3, 2009 1:06 AM by swd847

    Use of AnnotationLiteral

    swd847

      I have seen a few posts on the forum where people are using AnnotationLiteral without making it implement the appropriate interface, e.g.


      AnnotationLiteral<Role> role = new AnnotationListeral<Role>(){};
      



      rather than:


      static class RoleLit extends  AnnotationListeral<Role> implements Role {}
      
      RoleLit role = new RoleLit();
      
      



      When is it ok to use the first form, and when it the second form required? The spec only has examples of the second form.

        • 1. Re: Use of AnnotationLiteral
          nickarls

          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

            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

              Sure it is! It compiles(tm) ;-)

              • 4. Re: Use of AnnotationLiteral
                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

                  You need the long form when you have annotation members.

                  • 6. Re: Use of AnnotationLiteral
                    asookazian

                    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

                      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

                        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

                          It's now called AnnotationInstanceProvider and is availbile in the weld-extensions repo:


                          http://anonsvn.jboss.org/repos/weld/extensions/trunk

                          • 10. Re: Use of AnnotationLiteral
                            gavin.king

                            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

                              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

                                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.