0

I have created a custom annotation and annotation processor using Abstract Processor. Which means I want to do annotation processing a before compile time. I exported my custom annotation and processor as a Jar and trying to use it with a simple java test program. I am sure that at compile time it finds my java processor class but somehow I am not getting o/p that I am expecting on console. Below is the code of the process method in my processor.

Also, I am purposely using @SupportedSourceVersion(SourceVersion.RELEASE_6) so that I get a version warning which confirms that when I execute a test class it finds my annotation processor.

Here is the GitHub link to my full project which also has the packaged Jar that I have created.

public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv) {
    // TODO Auto-generated method stub

    processingEnv.getMessager().printMessage(Kind.NOTE, "Hello compile time message");

    for (Element elem : roundEnv.getElementsAnnotatedWith(Contract.class)) {
        if(elem.getKind() != ElementKind.METHOD)
        {
            processingEnv.getMessager().printMessage(Kind.NOTE, "Error");
            return true;
        }

        System.out.println("Hello ... Inside for ");
        Contract contract = elem.getAnnotation(Contract.class);
        String message = "annotation found in " + elem.getSimpleName()
                       + " with complexity " + contract.pre_cond();
        processingEnv.getMessager().printMessage(Kind.NOTE, message);
    }
    return true; // no further processing of this annotation type
}  

Below is the test class that I am using,

import annotations.Contract;

public class AnnotationTest {


public AnnotationTest()
{
    super();
}

@Contract(pre_cond = { "var > 0" })
public static void testMethod(int var)
{
    System.out.println("hello1");
}

public static void main(String args[])
{
    testMethod(1);
    System.out.println("hello");
}

I am compiling the above test class using javac -cp newcontractlib.jar AnnotationTest.java command and I just get the version warning and no messages from my process method.

Yogesh D
  • 1,663
  • 2
  • 23
  • 38

1 Answers1

1

How are you invoking javac, is it through Maven? I think you need to enable debug logging (append -X to your Maven invocation) in order to see diagnostics of kind NOTE.

You also can use the compiler options -XprintProcessorInfo and -XprintRounds to see whether your processor kicks in.

Gunnar
  • 18,095
  • 1
  • 53
  • 73