generateStreamItemForAnnotatedElement method
Definition
generateStreamItemForAnnotatedElement(
- Element element,
- ConstantReader annotation,
- BuildStep buildStep
Returns an object of type T
that will be added to the Stream
emitted by generateStream.
- Override this method in classes extending
MergingGenerator
. - This method is a generalization of generateForAnnotatedElement.
Implementation
@override
Definition generateStreamItemForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep) {
if (element.kind != ElementKind.CLASS) {
throw "ERROR: @Managed can only be used on a class, found on $element";
}
var constructor = (element as ClassElement).unnamedConstructor;
if (constructor == null) {
throw "ERROR: No unnamed constructor found for $element";
}
var constructorParams = constructor.parameters;
var typeName = element.name;
return Definition(
registration: """
fabric.registerFactory((Fabric fabric) {
return $typeName(
${constructorParams.map(_generateParam).join(",\n")}
);
});
""",
imports: {
_pathOf(element.enclosingElement),
...constructorParams.map((param) =>
_pathOf(param.type.element!.library!.definingCompilationUnit)),
},
);
}