使用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
。
答案 0 :(得分:1)
我不相信你可以GeneratorForAnnotation
。
您可以实现Generator
界面并自行进行检查。
答案 1 :(得分:1)
GeneratorForAnnotation
不检查扩展注释。
您可以编写自己的发电机并进行检查, 如果您不想,则必须按注释类设置生成器。
class MyAnnotationGenerator extends GeneratorForAnnotation<MyAnnotation>
class MyExtendAnnotationGenerator extends GeneratorForAnnotation<MyExtendedAnnotation>
new GeneratorBuilder(const [
const MyAnnotationGenerator(),
const MyExtendAnnotationGenerator()
])