2

Here is my requirement in Java 6: I am using Eclipse JUNO.

  1. Annotate a method with a custom annotation.
  2. During compilation, raise warning message if a method is calling the annotated method.

I am looking for something like @Deprecated annotation.

This is what I have done:

  1. Wrote a custom annotation.
  2. Wrote an annotation processor to read and process the methods with the annotation.

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.

What I couldn’t do:

  1. I could not get the calling methods. I want to raise the warning message in those calling methods.

My sample code:

Custom annotation:

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();
}

Annotation Processor:

    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<? extends TypeElement> annotations,
            RoundEnvironment roundEnvironment) {
        if (!roundEnvironment.processingOver()) {
            for (TypeElement annotation : annotations) {
                final Set<? extends Element> 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);
    }

}

1 Answers1

0

Using an AnnotationProcessor will only work on the files containing the annotations or overriding methods, but not calling methods. Maybe there's a way around this, but then you will probably be limited by projects, because the processor only looks at one project at a time.

I guess you need to write an Eclipse plugin with a builder, that analyses code in all files and checks called methods for annotations. That a lot more work than an annotation processor, but you also have more options. E.g. you could implement a quick fix for the error markers.

Stroboskop
  • 4,327
  • 5
  • 35
  • 52