0

Is there a way that I can give an annotation an method as an input. Then use the value returned from it to pass to the annotation ?

public String fnc(String value){

   //do something
   return ret;
}

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@interface MAnon{
    String cond = fnc(cond);
}

@MAnon(fnc("Something")) //does not work
@MAnon("Something")
public void foo(){ ... }

public void bar(){
    //reflection call
   MAnon a = ...getDeclaredAnnotations()[0]
   String foo = a; //may not be the same as what is typed
}
Android_Dev
  • 41
  • 1
  • 7
  • No, annotations can only reference compile-time constants. – shmosel Aug 21 '17 at 01:35
  • @shmosel what is the point of `RetentionPolicy.RUNTIME` if it can only take compile time constant it would be no different, from using methods. – Android_Dev Aug 21 '17 at 01:42
  • That just means the metadata is accessible at runtime, not that it's dynamic. – shmosel Aug 21 '17 at 01:43
  • A common pattern is to reference some class that can be instantiated and used for dynamic functionality. – shmosel Aug 21 '17 at 01:44
  • This is completely off topic but: When you create a string in Java as a local variable does that value still exist in memory, and does string = "" override the old value or creates a new pointer so the other value still exists in memory – Android_Dev Aug 23 '17 at 01:50

0 Answers0