generateForAnnotatedElement method
String
generateForAnnotatedElement(
- Element element,
- ConstantReader annotation,
- BuildStep buildStep
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
String generateForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep) {
if (element is! ClassElement) {
throw InvalidGenerationSourceError(
redError(
'expected ClassElement, found ${element.runtimeType}\n${StackTrace.current}',
),
element: element,
todo: "only classes can be notated with `generateIsolate`",
);
}
final isSharedIsolate = annotation.read("_isSharedIsolate").boolValue;
if (isSharedIsolate) {
final sharedIsolate = annotation.read("sharedIsolate").objectValue;
final Set<DartType> sharedInstanse = HashSet();
for (var field in element.fields)
for (ElementAnnotation meta in field.metadata)
if (meta.element?.displayName == "FromIsolate")
sharedInstanse.add(field.declaration.type);
final String key = sharedIsolate.getField("isolateId")!.toStringValue()!;
final SharedIsolateElement sharedIsolateElement =
SharedIsolateElement.generate(
classElement: element,
sharedIsolate: sharedIsolate,
annotation: annotation,
sharedInstanse: sharedInstanse,
);
final int count = sharedIsolate.getField("classCount")!.toIntValue()!;
sharedIsolateMap.update(
key,
(value) => value..add(sharedIsolateElement),
ifAbsent: () => [sharedIsolateElement],
);
if (sharedIsolateMap[key]!.length == count) {
final classBuffer = StringBuffer();
writeSharedIsolateWarperClasses(
classBuffer,
sharedIsolateMap[key]!,
key.toLowerCase(),
);
writeSharedIsolateEntryPoint(
classBuffer,
sharedIsolateMap[key]!,
key,
);
return classBuffer.toString();
}
return "";
}
final bool isSameType = annotation.read("isSameType").boolValue;
final ClassElement classElement = element;
final classBuffer = StringBuffer();
final String isolateFuncName = "_${classElement.name}Isolate".toLowerCase();
writeIsolateWarperClass(
classBuffer,
classElement,
isolateFuncName,
isSameType,
);
writeIsolateEntryPoint(classBuffer, classElement, isolateFuncName);
return classBuffer.toString();
}