4 Replies Latest reply: Nov 11, 2008 2:53 AM by 843793 RSS

    modify Annotation

    843793
      Hi,

      Is it possible to create/modify existing Annotation ?

      Example below : (not exact code ofcourse)

      @Anno(x=5)
      public class A {
      }

      public class B {

      public void main() {
      A a = new A()
      if (SomeCondition) -> set the Annotation for a x=7
      System.out.println("x =" + a.getAnnotation(Anno).getX())

      }
      }
        • 1. Re: modify Annotation
          608410
          At runtime, NO (unless you use aspect oriented programming or other form of bytecode manipulation to change the behaviour of Class.getAnnotation(). - and I wouldn't recommend that)

          At compile time - no again

          At design time there are a number of options depending on your requires, constraints and preferences:

          What you need to think about is what causes the SomeCondition to change and when you can calculate that (compile time, run time?).

          If the condition is a calculation the runtime knows how to perform, then you could just have the annotation have two alternate values and at runtime determine which is applicable.
          @interface Anno {
              int valueIfTrue();
              int valueIfFalse();
          }
          If you want the condition configurable you could pass it as a javascript (or other scripting language) expression and use the JDK scripting tools to execute it at runtime.
          @interface Anno {
              String javascriptConditionalExpression();
              int valueIfTrue();
              int valueIfFalse();
          }
          If you don't like that you could do this
          interface AnnoCondition {
              boolean theCondition();
          }
          
          @interface Anno {
              Class<? extends AnnoCondition();
              int valueIfTrue();
              int valueIfFalse();
          }
          Then do this in your code
          Anno a = A.class.getAnnotation(Anno.class);
          Class<? extends AnnoCondition> condClass = a.annoCondition();
          AnnoCondition condInstance = condClass.newInstance();
          if(condInstance.theCondition()) {
              System.out.println("x=" + a.valueIfTrue());
          } else {
              System.out.println("x=" + a.valueIfFalse());
          }
          Without more detail about what the benefits of changing the annotation value are to you, then it is a bit hard to give concrete advice on more realistic ways of achieving the same (or similar) benefits.

          Bruce
          • 2. Re: modify Annotation
            843793
            Thanks for your answer.

            The target is to be able to build suite file for junit.
            I want to be able to get class name and modify the @SuiteClasses.

            Thanks

            @RunWith(Suite.class)
            @SuiteClasses( {
            Test1.class,
            Test2.class
            })

            public class tmpSuite {
            }
            • 3. Re: modify Annotation
              608410
              but what changes? what is the condition?
              • 4. Re: modify Annotation
                843793
                Hi,

                We have list of classes that can be get from a file, under @SuiteClassses or any other method.
                I want to split to list accotding to some rule for example split for two lists and run each new list to runner.

                Thanks