structurizeBean function
Future<StructurizeResult>
structurizeBean(
- DartType type,
- ClassElement classElement,
- SubjectGenContext<
Element> context, - CachedAliasCounter counter,
Implementation
Future<StructurizeResult> structurizeBean(
DartType type,
ClassElement classElement,
SubjectGenContext<Element> context,
CachedAliasCounter counter) async {
List<AliasImport> imports = [];
List<IRStructureField> fields = [];
var element = type.element! as ClassElement;
var serialName = element.name;
var beanFields = classElement.fields.where((element) {
var field = classElement.getField(element.name)!;
if (beanIgnoreChecker.hasAnnotationOf(field)) return false;
return field.getter != null && field.setter != null;
}).toList();
for (var field in beanFields) {
var fieldName = field.name;
var fieldType = field.type;
var serialType = await getSerialType(fieldType, context);
var iterableType = await getIterableType(fieldType, context);
var optional = field.type.nullabilitySuffix == NullabilitySuffix.question;
if (fieldType is DynamicType) optional = true;
if (field.isLate) optional = false;
var propertyName = fieldName;
if (propertyNameChecker.hasAnnotationOf(field)) {
var annotation = propertyNameChecker.annotationsOf(field).first;
propertyName = annotation.getField("name")!.toStringValue()!;
}
var propertySerializer = "null";
if (propertySerializerChecker.hasAnnotationOf(field)) {
var serializerAnnotation =
propertySerializerChecker.annotationsOf(field).first;
propertySerializer =
counter.get(serializerAnnotation.getField("type")!.toTypeValue()!);
}
fields.add(IRStructureField(
fieldName,
counter.get(fieldType),
getTypeTree(fieldType).code(counter),
propertySerializer,
counter.get(serialType),
iterableType,
propertyName,
optional,
!isDogPrimitiveType(serialType),
getRetainedAnnotationSourceArray(field, counter),
mapChecker.isAssignableFrom(field)));
}
// Create proxy arguments
var getters = fields.map((e) => e.accessor).toList();
var activator =
"var obj = ${counter.get(element.thisType)}();${fields.where((element) {
var field = classElement.getField(element.name)!;
return field.getter != null && field.setter != null;
}).mapIndexed((i, e) {
if (e.iterableKind == IterableKind.none) {
return "obj.${e.name} = list[$i];";
} else if (e.optional) {
return "obj.${e.name} = list[$i]?.cast<${e.serialType}>();";
}
return "obj.${e.name} = list[$i];";
}).join("\n")} return obj;";
var structure = IRStructure(counter.get(type), StructureConformity.bean,
serialName, fields, getRetainedAnnotationSourceArray(element, counter));
return StructurizeResult(imports, structure, getters, activator);
}