I want to create my own annotation processor. I'm as fas as that I registered my processor but am stuck now at the point of replacing the annotated function with the real functions code.
I want generate something simple like following:
public void onTest(Test data)
{
}
With an annotation like following:
// Annotation
@HandleEvent(Test.class)
My AbstractProcessor
looks like following so far:
@SupportedAnnotationTypes("HandleEvent")
public class AnnotationProcessor extends AbstractProcessor
{
@Override
public synchronized void init(ProcessingEnvironment env)
{
}
@Override
public boolean process(Set<? extends TypeElement> annotations, RoundEnvironment roundEnv)
{
for (Element element : roundEnv.getElementsAnnotatedWith(HandleEvent.class))
{
HandleEvent annotation = element.getAnnotation(HandleEvent.class);
Class clazz = annotation.value();
String functionName = clazz.getName().replaceAll("\\.", "");
StringBuilder sb = new StringBuilder();
sb.append("public void ").append(functionName).append("(").append(clazz.getName()).append(" data)").append("\n");
sb.append("{");
sb.append("}");
// How can I create a function in code with the above created string???
// What do I have to do know and where should I write the function code into???
}
return true;
}
}
What do I have to do next to really generate the class with the generated functions in it?