title: How to write annotation processor to raise a warning message if a java source is calling an annotated method
body: <p>Here is my requirement in Java 6: I am using Eclipse JUNO.</p>
  
  <ol>
  <li>Annotate a method with a custom annotation.</li>
  <li>During compilation, raise warning message if a method is calling the
  annotated method.</li>
  </ol>
  
  <p>I am looking for something like @Deprecated annotation.</p>
  
  <p>This is what I have done:</p>
  
  <ol>
  <li>Wrote a custom annotation.</li>
  <li>Wrote an annotation processor to read and process the methods with
  the annotation.</li>
  </ol>
  
  <p>Created a jar and added it in annotation processor path. My sample code (see below) raises the warning message in the annotated method. But it is not my requirement.</p>
  
  <p>What I couldn’t do:</p>
  
  <ol>
  <li>I could not get the calling methods. I want to raise the warning
  message in those calling methods.</li>
  </ol>
  
  <p>My sample code:</p>
  
  <p>Custom annotation:</p>
  
  <pre><code>package tool.apichecks;
  
  import java.lang.annotation.ElementType;
  import java.lang.annotation.Retention;
  import java.lang.annotation.RetentionPolicy;
  import java.lang.annotation.Target;
  
  @Retention(RetentionPolicy.SOURCE)
  @Target({ ElementType.METHOD })
  public @interface HighCostMethod {
      String altMethod();
  }
  </code></pre>
  
  <p>Annotation Processor:</p>
  
  <pre><code>    package tool.apichecks;
  
  import java.util.Set;
  
  import javax.annotation.processing.AbstractProcessor;
  import javax.annotation.processing.ProcessingEnvironment;
  import javax.annotation.processing.RoundEnvironment;
  import javax.annotation.processing.SupportedAnnotationTypes;
  import javax.lang.model.element.Element;
  import javax.lang.model.element.TypeElement;
  import javax.tools.Diagnostic.Kind;
  
  @SupportedAnnotationTypes({ "tool.apichecks.HighCostMethod" })
  public class MethodProcessor extends AbstractProcessor {
  
      private enum MethodType {
          HIGH_COST(HighCostMethod.class.getName());
  
          private String name;
  
          private MethodType(String name) {
              this.name = name;
          }
  
          private static MethodType getMethodType(String name) {
              MethodType methodType = null;
              for (MethodType methodType2 : MethodType.values()) {
                  if (methodType2.name.equals(name)) {
                      methodType = methodType2;
                      break;
                  }
              }
              return methodType;
          }
      }
  
      private ProcessingEnvironment processingEnvironment;
  
      @Override
      public synchronized void init(ProcessingEnvironment processingEnvironment) {
          this.processingEnvironment = processingEnvironment;
      }
  
      @Override
      public boolean process(Set&lt;? extends TypeElement&gt; annotations,
              RoundEnvironment roundEnvironment) {
          if (!roundEnvironment.processingOver()) {
              for (TypeElement annotation : annotations) {
                  final Set&lt;? extends Element&gt; elements = roundEnvironment
                          .getElementsAnnotatedWith(annotation);
                  MethodType methodType = MethodType.getMethodType(annotation
                          .toString());
  
                  for (Element element : elements) {
                      switch (methodType) {
                      case HIGH_COST: {
                          processHighCostMethod(element);
                          break;
                      }
                      }
                  }
              }
          }
          return true;
      }
  
      protected void processHighCostMethod(Element element) {
          HighCostMethod highCostMethod = element
                  .getAnnotation(HighCostMethod.class);
          /* TODO This warns the annotated method itself. I don't want this. I want to warn the methods that calls this method */
          processingEnvironment
                  .getMessager()
                  .printMessage(
                          Kind.WARNING,
                          String.format(
                                  "Do not use high cost method %s. Instead use %s method.",
                                  element, highCostMethod.altMethod()), element);
      }
  
  }
  </code></pre>