0

I want to create an api using my own custom annotation that the hidden code should be triggered. I have created my annotation and have created the processor as well. But now the problem is, I don't know how to build it. Let me explain in better way: Its a console applicatioyn, I have to print a text once a method is called.

So, I have created an annotation @PrintText and also created a PrintTextProcessor. But when I try to compile it, it doesn't show the required output. I am annotating a method. But it looks annotation doesn't work. Am I missing anything.

Following is my code Annotation Class:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface PrintText{

}

Annotation Processor Class:

@SupportedAnnotationTypes("com.example.PrintText")
public class PrintTextProcessor extends AbstractProcessor {

   @Override
   public boolean process(Set<? extends TypeElement> annotations, 
                       RoundEnvironment roundEnv) {
     Set<? extends Element> elements = roundEnv.getElementsAnnotatedWith(PrintText.class);
     for(Element e : elements){
        if(!e.getClass().equals(ParticularType.class)){
            processingEnv.getMessager().printMessage(Kind.ERROR,
                 "@PrintText annotated fields must be of type ParticularType");
        }
     }
     return true;
   }

}

Now my main class comes:

public class Main{

   @PrintMe
   public void testMethod(){
      System.out.println("In test method");
   }

   public static void main(String s[]){
      new Main().testMethod();
   }
}

Now when I try to compile this program and run it, it only prints the following text: In test method

I used following commands

javac Main.java

java Main

Did I miss something? I have been gone through several posts on the internet and found that there is apt tool. But I don't know how to build and run it via command line. I am using java6.

Thanks in advance.

A_Di-Matteo
  • 26,902
  • 7
  • 94
  • 128
NewBee
  • 1
  • 2
  • What do you expect like a output? – Abdelhak Jan 10 '16 at 20:59
  • Have you registered your processor in `META-INF/services` ? I think you have to provide a file called `javax.annotation.processing.Processor` there, listing the FQN(s) of your processor(s). – Hendrik Jander Jan 10 '16 at 21:19
  • maybe this can help: (https://stackoverflow.com/questions/11685498/what-is-the-default-annotation-processors-discovery-process) – Hendrik Jander Jan 10 '16 at 21:25

1 Answers1

2

Here is very good example https://github.com/provegard/aptdemo but basically you have to create package META-INF/services and put javax.annotation.processing.Processor file with classpath to your processor (in other words register your processor), then build your app let's say with mvn to get jar file (mvn package, see example) and then compile with javac (javac -cp /path/to/aptdemo-1.0-SNAPSHOT.jar SomeTestClass.java)

  • I can not use any build tool like maven, ant etc. Is there any approach that I can use javac and java commands to make it run. – NewBee Jan 11 '16 at 03:25
  • I mean it will be very kind if you can provide me direct javac and java command to make it run. I tried to make a jar file using java command but that didn't work. – NewBee Jan 11 '16 at 03:32
  • You can build jar file even without mvn and to do the same : 1)jar cfv jarName.jar -C src/main/java/ . 2) javac -cp jarName.jar -processor com.programmaticallyspeaking.aptdemo.AnnotationProcessor src/main/java/com/programmaticallyspeaking/aptdemo/Test.java in this case you even don't need javax.annotation.processing.Processor (not register your proccessor) P.S : t's real commands which I executed with example provided by me – Denys Skalenko Jan 11 '16 at 15:59
  • I hope it what you need – Denys Skalenko Jan 11 '16 at 16:06