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.