我想创建自己的注释处理器。我就像我注册了我的处理器一样迷人,但现在我已经陷入了用实际功能代码替换带注释功能的程度。
我希望生成如下的简单内容:
public void onTest(Test data)
{
}
使用如下注释:
// Annotation
@HandleEvent(Test.class)
到目前为止,我的AbstractProcessor
似乎如下:
@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;
}
}
接下来要做什么才能真正生成包含生成函数的类?