title: How to supply value to an annotation dynamically?
body: <p>I have created a cutom annotation with a <code>boolean</code> value attribute.</p>
  <pre><code>@Target(ElementType.METHOD)
  @Retention(RetentionPolicy.RUNTIME)
  public @interface CustomAnnotation {
      boolean value() default false;
  }
  </code></pre>
  <p>The annotation should be set dynamically based on property read from a configuration file.</p>
  <p>I've tried to inject the value directly from the configuration:</p>
  <pre><code>@CustomAnnotation(&quot;${xxxxx.xxxxx.enableFeature:false}&quot;)
  public void apply(){}
  </code></pre>
  <p>And:</p>
  <pre><code>@CustomAnnotation(&quot;&quot;#{new Boolean('${xxxxx.xxxxx.enableFeature:false}&quot;)
  public void apply(){}
  </code></pre>
  <p>I'm getting the same compilation problem for both solutions:</p>
  <blockquote>
  <p>Type mismatch: cannot convert from String to boolean</p>
  </blockquote>
  <hr />
  <p>I've tried a third solution by injecting the value as variable and then using it:</p>
  <pre><code>@Component
  public class ApplyClass {
  
  @Value(&quot;${xxxxx.xxxxx.enableFeature:false}&quot;)
  private boolean enableFeature;
  
  @CustomAnnotation(enableFeature)
  public void apply(){}
  
  }
  </code></pre>
  <p>I've got another compilation issue:</p>
  <blockquote>
  <p>The value for annotation attribute must be a constant expression</p>
  </blockquote>
  <hr />
  <p>Even by switching the variable to constant, this is not working.</p>
  <pre><code>@Component
  public class ApplyClass {
  
  @Value(&quot;${xxxxx.xxxxx.enableFeature:false}&quot;)
  private static final boolean enableFeature; // ---&gt; Leads to compilation problem 
  
  @CustomAnnotation(enableFeature&quot;)
  public void apply(){}
  
  }
  </code></pre>
  <hr />
  <p>So, a part from doing code smells like:</p>
  <pre><code>@Component
  public class ApplyClass {
  
  @Value(&quot;${xxxxx.xxxxx.enableFeature:false}&quot;)
  private  boolean enableFeature; 
  
  private static final ENABLE_FEATURE_CONSTANT = enableFeature
  
  @CustomAnnotation(ENABLE_FEATURE_CONSTANT)
  public void apply(){}
  
  }
  </code></pre>
  <ol>
  <li>Is that a known limitation of <code>annotations</code> ?</li>
  <li>Is there any recommendation or solution a part from using <code>Strings</code>
  instead of other types ?</li>
  </ol>