使用source_gen如何使用扩展注释调用生成器?

时间:2017-01-24 05:29:01

标签: dart

使用source_gen包如何使用扩展注释调用生成器?

假设我有下一个生成器:

class MyAnnotationGenerator extends GeneratorForAnnotation<MyAnnotation> {
const MyAnnotationGenerator();

Future<String> generateForAnnotatedElement(ClassElement element, Serializable annotation, BuildStep buildStep) async {
...
}

我想为MyAnnotationGenerator的每个注释调用MyAnnotation。例如:

class MyAnnotation {
  const MyAnnotation();
}

class MyExtendedAnnotation extends MyAnnotation {
  const MyExtendedAnnotation();
}

因此,每次使用注释MyExtendedAnnotation时,我都会致电MyAnnotationGenerator

2 个答案:

答案 0 :(得分:1)

我不相信你可以GeneratorForAnnotation

您可以实现Generator界面并自行进行检查。

答案 1 :(得分:1)

GeneratorForAnnotation不检查扩展注释。

您可以编写自己的发电机并进行检查, 如果您不想,则必须按注释类设置生成器。

class MyAnnotationGenerator extends GeneratorForAnnotation<MyAnnotation>
class MyExtendAnnotationGenerator extends GeneratorForAnnotation<MyExtendedAnnotation>

new GeneratorBuilder(const [
     const MyAnnotationGenerator(),
     const MyExtendAnnotationGenerator()
   ])