generateForAnnotatedElement method
FutureOr<String>
generateForAnnotatedElement(
- Element element,
- ConstantReader annotation,
- BuildStep buildStep
inherited
Implement to return source code to generate for element
.
This method is invoked based on finding elements annotated with an
instance of T
. The annotation
is provided as a ConstantReader
.
Supported return values include a single String or multiple String instances within an Iterable or Stream. It is also valid to return a Future of String, Iterable, or Stream.
Implementations should return null
when no content is generated. Empty
or whitespace-only String instances are also ignored.
Implementation
@override
FutureOr<String> generateForAnnotatedElement(
Element element,
ConstantReader annotation,
BuildStep buildStep,
) async {
final matchersObjects = generateFor(annotation);
for (final object in matchersObjects) {
final type = object.dartObject.toTypeValue()!;
assert(
type.element != null && type.element is ClassElement,
'Element should be a class',
);
final classElement = type.element! as ClassElement;
final generic = classElement.thisType.typeGenericParamStr;
final className = classElement.thisType.dartTypeStr;
checkBadTypeByClassElement(classElement, generateFor: generatorName);
final elements = List<Element>.from([])
..addAll(classElement.fields)
..addAll(classElement.methods)
..addAll(classElement.accessors);
if (elements.hasAtleastOneMatchDeclarationAnnotation) {
_buildClassWithDeclarationValidation(classElement, generic, object.id);
} else {
_buildClassWithTypeValidation(
classElement,
className,
generic,
object.id,
);
}
}
_importsStringBuffer
..writeln('\n')
..writeln(_globalVariablesStringBuffer.toString())
..writeln('\n')
..writeln(_classesStringBuffer.toString());
return _importsStringBuffer.toString();
}