1 2 Previous Next 16 Replies Latest reply on Dec 18, 2006 6:15 AM by kabirkhan

    Can't read parameter annotations through reflection

    dwl8

      I'm currently thinking about using JBoss AOP but i can't seem to get by this issue. I cannot read the parameter annotations of methods w/ a pointcut annotation. Here's the code i'm using:

      jboss-aop.xml:

      <aop>
       <aspect class="test.aop.aspects.LockAspect"/>
      
       <bind pointcut="all(@test.aop.annotations.WriteLock)">
       <advice name="writeLock" aspect="test.aop.aspects.LockAspect"/>
       </bind>
      
       <bind pointcut="all(@test.aop.annotations.ReadLock)">
       <advice name="readLock" aspect="test.aop.aspects.LockAspect"/>
       </bind>
      </aop>
      


      java code:
      @ReadLock
      public static People getPerson(@FirmId Integer firmId, Integer personId) {
       return peopleMap.get(firmId).get(personId);
      }
      


      When i try to read the parameter annotation @FirmId through method.getParameterAnnotations() i get 0 length arrays for each param. When i change @ReadLock to, say, @Deprecated, I can read the parameter annotation correctly. I'm wondering if the loadtime byte code weaving(?) is doing something to the annotation. Any help is greatly appreciated. I'm pretty new to this stuff so i may be missing something obvious...

        • 1. Re: Can't read parameter annotations through reflection
          stalep

          hm, even though i havent tested it myself but i thought it should work. the code you are calling is CtMethod/CtBehavior which is javassist, but thats not your problem :)
          have you tested .getAvailableParameterAnnotations() too?
          if that doesnt work either, could i bother you to make a small test to reproduce it (even though your example was nice a working test makes it much easier for me :)

          • 2. Re: Can't read parameter annotations through reflection
            flavia.rainone.old

            What is the retention policy of your annotation @ReadLock?
            Maybe you forgot to set it as RetentionPolicy.RUNTIME...

            • 3. Re: Can't read parameter annotations through reflection
              dwl8

              Thank you both for your replies.

              flavia, all my annotations are set to RetentionPolicy.RUNTIME. If only that was the issue... :)

              stalep, I created a unit test and it read the annotation correctly. It seems that when I deploy it to my app server (Tomcat) the annotation can't be read. Here's a simple class I was using to recreate the issue.

              public class Person {
               private static HashMap<Integer,Person> peopleMap = new HashMap<Integer,Person>();
              
               @ReadLock
               public static Person getPerson(@FirmId Integer firmId) {
               try {
               Method m = Person.class.getMethod("getPerson",Integer.class);
               Annotation[][] paramAnnot = m.getParameterAnnotations();
               for(Annotation[] a : paramAnnot) {
               System.out.println("# of Annotations Found: " + a.length);
               //log.debug("# of Annotations Found: " + a.length);
               }
               } catch(NoSuchMethodException e) {
               System.out.println("Method not found");
               //log.debug("No Such Method");
               }
               return peopleMap.get(firmId);
               }
              
               public static void main(String[] args) {
               try {
               Method m = Person.class.getMethod("getPerson",Integer.class);
               Annotation[][] paramAnnot = m.getParameterAnnotations();
               for(Annotation[] a : paramAnnot) {
               System.out.println("# of Annotations Found: " + a.length);
               //log.debug("# of Annotations Found: " + a.length);
               }
               } catch(NoSuchMethodException e) {
               System.out.println("Method not found");
               //log.debug("No Such Method");
               }
               }
              }
              


              when main is run, i get "# of Annotations Found: 1". However, when I make a call to getPerson after it's been deployed to Tomcat, I get "# of Annotations Found: 0". Strange.

              Could anyone give this code a whirl and keep me from losing my sanity?

              • 4. Re: Can't read parameter annotations through reflection
                stalep

                hm, i might be far off, but if i remember correctly tomcat doesnt work well with annotations (dont sue me if im wrong, its early in the morning).
                could you try to deploy the code in jboss and not tomcat and see how it works? since it works when you test is outside tomcat i believe that its no jbossaop bug, but rather a container/deploy bug... (but again, its early :)

                • 5. Re: Can't read parameter annotations through reflection
                  dwl8

                  I did a bit more testing and when I get rid of the @ReadLock bind in my jboss-aop.xml, parameter annotations can be read correctly even when deployed in Tomcat.

                  I'll try it on JBoss and see if I have more luck.

                  • 6. Re: Can't read parameter annotations through reflection
                    flavia.rainone.old

                    Hi! I think I've found the problem. Could you please just confirm to me that the problem stays the same even if you run it as a simple java application, without any server involved?
                    Besides, I need to know which version of JBoss AOP you are using, so I can fix your version first.

                    • 7. Re: Can't read parameter annotations through reflection
                      dwl8

                      I ran the Person class as a simple java app with the -javaagent vm parameter and the problem still occurs. The main method prints out "# of Annotations Found: 0". If I run main w/o JBoss AOP there is no issue. I get # of Annotations Found: 1"

                      • 8. Re: Can't read parameter annotations through reflection
                        dwl8

                        also forgot to add that i'm using version 1.5.2. Thanks for your help

                        • 9. Re: Can't read parameter annotations through reflection
                          flavia.rainone.old

                          You're welcome!
                          This is in fact a bug. As soon as I fix it, I'll post a message in this forum.

                          • 10. Re: Can't read parameter annotations through reflection
                            flavia.rainone.old

                            Sorry for the delay.
                            The bug seems simple of solving, but I had a hard time trying to find out a way of doing what I need to by using Javassist, until I realized that, currently, it does not support the operation I need.
                            Maybe I'll add this operation to Javassist, but, in the mean time, there is a workaround I can use. I'll see if I implement this in the JBoss AOP 1.5.3 repository for you.
                            Could you please tell me which instrumentor are you using? You can run JBoss AOP in one of this following ways:
                            - either in the optimized (default) or in the unoptimized way
                            - with gen advisor (default) instrumentation or with the classic one

                            • 11. Re: Can't read parameter annotations through reflection
                              dwl8

                              no problem for the delay, thanks a lot for your help! I really appreciate it! Our project isn't going live for at least 4-6 months, still in the early stages of development.

                              currently I'm using default settings for everything. i haven't had the time to delve too deep into JBoss AOP (I plan to though). I have a few methods annotated w/ pointcuts and am using the loadtime option through the -javaagent vm param. So at this point our application is relatively simple in terms of JBoss AOP - only a jboss-aop.xml file, one aspect, and a few pointcuts. Please let me know if you need more info or if there's any way I can help.

                              • 12. Re: Can't read parameter annotations through reflection
                                flavia.rainone.old

                                The bug has been solved on 1.5 branch (using that workaround I told you of) only for the default weaving configuration(which is, actually, the optimized one with classic instrumentation, and not gen advisor instrumentation, differently from what I told you before).
                                Please, follow these instructions:
                                http://wiki.jboss.org/wiki/Wiki.jsp?page=BuildingJBossAOPFromSubversion
                                But use this repository address, with anonymous svn:
                                http://anonsvn.jboss.org/repos/jbossas/branches/Branch_AOP_1_5/

                                This is a temporary fix, and it's been made only on this branch.

                                As soon as the problem is completely addressed, on all versions, I'll post a new message here.

                                If you have problems with making your example work with this version, let me know.

                                Thanks!
                                Flavia

                                • 13. Re: Can't read parameter annotations through reflection
                                  kabirkhan
                                  • 14. Re: Can't read parameter annotations through reflection
                                    kabirkhan

                                    BTW Generated Advisors are in beta/unreleased for the 1.5.x versions

                                    1 2 Previous Next