title: How can I define my Custom Annotation to use a subset of target elements?
body: <p><strong>Question:</strong></p>
  
  <p>Is it possible to target multiple element types? (More than one, less than all)</p>
  
  <p><strong>Details:</strong></p>
  
  <p>I'm trying create an annotation that is only acceptable on Methods and Fields. </p>
  
  <p>I know if I don't specify the <code>@Target</code> annotation I can use my custom annotation on all elements. However, I want compile time safety on element types that conflict with my logic.</p>
  
  <p>In <strong>C#/.NET</strong></p>
  
  <pre><code>[AttributeUsage(AttributeTargets.Field | AttributeTargets.Method)]
  public class MyAttribute : Attribute
  {
      ...
  </code></pre>
  
  <p><strong>Java Attempts</strong></p>
  
  <p><strong>Multiple Target Annotations:</strong></p>
  
  <pre><code>@Target(ElementType.METHOD)
  @Target(ElementType.FIELD)
  public @interface MyAnnotation
  {
  </code></pre>
  
  <p><em>Compiler Error:</em></p>
  
  <blockquote>
    <p>MyAnnotation.java:8: error: duplicate annotation</p>
  </blockquote>
  
  <p><strong>OR'd Values:</strong></p>
  
  <pre><code>@Target(ElementType.METHOD | ElementType.FIELD)
  public @interface MyAnnotation
  {
  </code></pre>
  
  <p><em>Compiler Error:</em></p>
  
  <blockquote>
    <p>MyAnnotation.java:7: error: bad operand types for binary operator '|'</p>
  </blockquote>
  
  <p><strong>Annotation Array</strong></p>
  
  <pre><code>@Targets({@Target(ElementType.METHOD),@Target(ElementType.FIELD)})
  public @interface MyAnnotation
  {
  </code></pre>
  
  <p><em>Compiler Error:</em></p>
  
  <blockquote>
    <p>MyAnnotation.java:7: error: cannot find symbol
    @Targets({@Target(ElementType.METHOD),@Target(ElementType.FIELD)})<br>
      ^symbol: class Targets</p>
  </blockquote>