4 Replies Latest reply: Sep 21, 2010 3:04 AM by 843793 RSS

    Overriding inherited annotations

    843793
      Hi,
      I wish to use the “ParameterPriority” annotation to determine the order of the fields appearance when displaying these objects in a GUI.
      Both B and C classes extends class A. If B and C wish to change the appearance order of class A fields, is there a way to override class A annotation values? (or is there another way to obtain this functionality via annotations?)
      Bellow you can find a very stupid example for the situation I've described.
      Thanks!
      // annotation definition:
      
      @Retention(RetentionPolicy.RUNTIME)
      @Target({ElementType.FIELD})
      public @interface ParameterPriority {
            int rank(); 
      }
      
      //class A
      
      Public class A {
      @ ParameterPriority (rank=1)
          private String name;
      @ ParameterPriority (rank=2)
          private int age;
      
      }
      
      //class B
      
      Public class B extends A {
      @ ParameterPriority (rank=3)
          private boolean isFemale;
      }
      
      //class C
      
      Public class C extends A {
      @ ParameterPriority (rank=3)
          private Boolean isStudent;
      }
        • 1. Re: Overriding inherited annotations
          843793
          I don't thik you can achieve that on fields (though you should confirm with someone more knowledgeable) because fields cannot be overriden. A sensible workaround would be to annotate field getters rather than the fields themselves: when you override a method, you have the opportunity override its annotations. Another side advantage is that you annotate public things so they will be visible in the javadoc for instance.

          Btw, if you rename your annotation method from "rank" to "value", you're allowed to omit the method name:
          // annotation definition:
           
          @Retention(RetentionPolicy.RUNTIME)
          @Target({ElementType.METHOD})
          public @interface ParameterPriority {
                int value(); 
          }
           
          //class A
          
          public class A {
              private String name;
              private int age;
          
              @ParameterPriority(5)
              public String getName() { return name; }
          
              @ParameterPriority(10)
              public int getAge() { return age; }
          }
          
          public class B extends A {
              @Override
              @ParameterPriority(2)
              public int getAge() { return super.getAge(); }
          }
          
              // instanceOfA.getMethod("getAge").getAnnotation(ParameterPriority.class).value() == 10
              // instanceOfC.getMethod("getAge").getAnnotation(ParameterPriority.class).value() == 2
          • 2. Re: Overriding inherited annotations
            843793
            Thank you very much, you suggestion is great!
            • 3. Re: Overriding inherited annotations
              843793
              Note that method annotations are not inherited. This means that if you override a method in a subclass, the new method doesn't have any annotations. Because of this there is no such thing as "overriding" method annotations. What you can do, is to re-annotate the method again (which may seem like "overriding" annotations, but is actually re-adding )
              • 4. Re: Overriding inherited annotations
                843793
                Thanks for the clarification!
                In my case, I think re-annotating works for me, but it's good to not the annotation is not inherited, it's sure gives me something to think about :)
                Thanks!