2 Replies Latest reply: Jun 2, 2009 10:34 AM by 843793 RSS

    Good Tutorial on Annotations

    800429
      Hi guys,

      Well I am advancing too slow with annotations. Do your guys know a good and complete tutorial?

      As I said in the other post, I am not just creating annotations, this is easy, I am trying to load the annotations by using reflections and generate dynamic panels during runtime. I was doing good up until get an odd problem.

      I have four classes:
      public class LoggerAction {
           
          @ActionAnnotation(
              paramType = {String.class, String.class},
              paramName = {"xxx1", "xxx2"},
              paramToolTip = {"tipx1", "tipx2"}
          )
          public void xxxAction(String xxx1, String xxx2){
              
          }
          
          @ActionAnnotation(
              paramType = {String.class},
              paramName = {"yyy1"},
              paramToolTip = {"tipy"}
          )
          public void yyyAction(String yyy1){
              
          }
          
          @ActionAnnotation(
              paramType = {String.class},
              paramName = {"zzz1"},
              paramToolTip = {"tipz"}
          )
          public void zzzAction(String zzz1){
              
          }
         
      }
      
      public class TCPAction {
          
           @ActionAnnotation(
              paramType = {String.class, String.class},
              paramName = {"aaa1", "aaa2"},
              paramToolTip = {"tipa1", "tipa2"}
          )
          public void aaaAction(String aaa1, TestEntity aaa2){
              
          }
          
          @ActionAnnotation(
              paramType = {String.class, TestEntity.class},
              paramName = {"bbb1", "bbb2"},
              paramToolTip = {"tipb1", "tipb2"}
          )
          public void bbbAction(String bbb1, TestEntity bbb2){
              
          }
          
          @ActionAnnotation(
              paramType = {String.class, TestEntity.class},
              paramName = {"ccc1", "ccc2"},
              paramToolTip = {"tipc1", "tipc2"}
          )
          public void cccAction(String ccc1, TestEntity ccc2){
              
          }
      
      }
      
      public class TimerAction {
          
          @ActionAnnotation(
              paramType = {Integer.class},
              paramName = {"nnn1"},
              paramToolTip = {"tipn1"}
          )
          public void nnnAction(int nnn1){
              
          }
          
          @ActionAnnotation(
              paramType = {String.class, Integer.class},
              paramName = {"mmm1", "mmm2"},
              paramToolTip = {"tipm1", "tipm2"}
          )
          public void mmmAction(String mmm1, int mmm2){
              
          }
      
      }
      
      public class UDPAction {
          
          @ActionAnnotation(
              paramType = {String.class, String.class},
              paramName = {"ppp1", "ppp2"},
              paramToolTip = {"tipp1", "tipp2"}
          )
          public void pppAction(String ppp1, String ppp2){
              
          }
          
          @ActionAnnotation(
              paramType = {String.class, String.class},
              paramName = {"qqq1", "qqq2"},
              paramToolTip = {"tipq1", "tipq2"}
          )
          public void qqqAction(String qqq1, String qqq2){
              
          }
      
      }
      My annotation declaration is:
      @Retention(RetentionPolicy.RUNTIME)
      @Target(ElementType.METHOD)
      public @interface ActionAnnotation {
          Class[]  paramType();
          String[] paramName();    
          String[] paramToolTip();
      }//End ActionAnnotation
      However when I run the following simple source code
      String[] classes = {"TCPAction", "LoggerAction", "TimerAction", "UDPAction"};
              try{
                  for(int i = 0; i < classes.length; i++){
                      Class aClass = Class.forName(ACTION_PACK + classes);
      System.out.println("Loading class: " + aClass.getCanonicalName());
      Method[] aMethods = aClass.getMethods();
      for(int j = 0; j < aMethods.length; j++){
      if(aMethods[j].getName().endsWith(ACTION_PREFIX)){
      System.out.println("\t" + aMethods[j].getName());
      ActionAnnotation annot = aMethods[j].getAnnotation(ActionAnnotation.class);
      if(annot != null){
      for(int k = 0; k < annot.paramType().length; k++){
      System.out.println("\t\t" + annot.paramType()[k] + " " + annot.paramName()[k]);
      }//end for(annotations)
      }//end if
      }//end if
      }//end for(methods)
      }//end for(classes)

      }//end try
      catch(Exception cnfe){
      ;//TODO print some error here
      }//end catch
      I am getting the output:
      Loading class: entity.statechart.action.TCPAction
              aaaAction
              bbbAction
              cccAction
      Loading class: entity.statechart.action.LoggerAction
              xxxAction
                      class java.lang.String xxx1
                      class java.lang.String xxx2
              yyyAction
                      class java.lang.String yyy1
              zzzAction
                      class java.lang.String zzz1
      Loading class: entity.statechart.action.TimerAction
              nnnAction
              mmmAction
      Loading class: entity.statechart.action.UDPAction
              pppAction
              qqqAction
      
      Well, for some reason the Runtime is taking only the LoggerAction as having valid annotations. However I did declare it for all classes methods.
      
      Any idea?                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
        • 1. Re: Good Tutorial on Annotations
          800429
          Hi all,

          Well, The code below just started to work today. I don't know what is the reason, its is probably a Netbeans/Ant or Java Compiler bug where some .classes are kept in a cache or something like that. I just did a clean and rebuild the project again and so annottions started to work properly.

          Regards
          • 2. Re: Good Tutorial on Annotations
            843793
            One of your problems is definitely this code:
            catch(Exception cnfe){
            ;//TODO print some error here
            }//end catch
            Never do that.

            Even in quick-and-dirty testing code, you must do at least a "cnfe.printStackTrace()".

            Otherwise you have no chance whatsoever to find your problems.