4 Replies Latest reply on Oct 5, 2010 5:24 PM by swd847

    Is It Possible To Get Method Parameter using Weld?

    xsalefter.xsalefter.yahoo.com

      Hi.. I'm try to play and create an Weld Extension. I want to register an annotated method parameter.


      My extension code is look like this:



      public class SweldScanner implements Extension {
        <X> void processAnnotatedType(@Observes ProcessAnnotatedType<X> pat) {
          for (AnnotatedMethod<? super X> method : pat.getAnnotatedType().getMethods()) {
            if (method.getJavaMember().isAnnotationPresent(Action.class)) {
              for (AnnotatedParameter<? super X> parameter : method.getParameters()) {
                // How to get the parameter name here..??
              }
            }
          }
        }
      }
      



      The client code might be like this:


      @Controller
      public class PersonController {
      
        @Action(in=PersonDisplayUI.class)
        public void onClickButtonCreate
        (@OnClick JButton buttonCreate, @UI PersonInsertUI personInsertUI) {
          personInsertUI.setVisible(true);
        }
      }
      



      My plan is trying to get available buttonCreate instance in class


      @Action(in=PersonDisplayUI.class)

      .

        • 1. Re: Is It Possible To Get Method Parameter using Weld?
          cjalmeida

          This is usually not possible in a portable way, since parameter names are usually stripped when compiling into bytecode. It's possible if you use some JavaAssist + set debug flags when compiling.


          To get where you want, maybe you could use a convention where you could use the method name for the auto-binding. Such as buttonCreate_OnClick like good ol' VB6?

          • 2. Re: Is It Possible To Get Method Parameter using Weld?
            swd847

            The usual solution to this particular problem is to put the name into a parameter annotation, in this case you could add a field to the OnClick annotation or create a new one specially:


            public void onClickButtonCreate(@OnClick("buttonCreate") JButton buttonCreate, @UI PersonInsertUI personInsertUI) 
            {
                personInsertUI.setVisible(true);
            }
            


            • 3. Re: Is It Possible To Get Method Parameter using Weld?
              xsalefter.xsalefter.yahoo.com

              Hi Stuart. I have proposed your way on this post , but Gavin tell me that it is untypesafe. So I choose the second approach (my forum post). Hope it's more typesafe.


              To get where you want, maybe you could use a convention where you could use the method name for the auto-binding. Such as buttonCreate_OnClick like good ol' VB6?
              



              Hi Almeida.. I personally will choose


              @OnClick("buttonCreate")



              than


              buttonCreate_OnClick



              It's limit developer freedom, i think.


              However Stuart, can Weld provide this feature, so developer don't need to use javaassist / asm manually?

              • 4. Re: Is It Possible To Get Method Parameter using Weld?
                swd847

                xsa lefter wrote on Oct 05, 2010 09:32:


                However Stuart, can Weld provide this feature, so developer don't need to use javaassist / asm manually?


                No. There are a few problems with it:


                - There is no guarantee that the compiler actually generated the appropriate attribute in the bytecode (can't remember what it is called of the top of my head).


                - In some environments with funny classloading it is not necessarily possible to read the bytecode of loaded classes directly (I know we have had problems using javassist in google app engine in the past).