CommonClassModel<FieldModel> constructor

CommonClassModel<FieldModel>(
  1. ClassElement clazz,
  2. MkField<FieldModel> mkField,
  3. ConstantReader reader
)

Implementation

factory CommonClassModel(
  ClassElement clazz,
  MkField<FieldModel> mkField,
  ConstantReader reader,
) {
  try {
    // build a map of the qualified imports, mapping module identifiers to import prefixes
    final lib = clazz.library;
    final imports = ImportModel();
    lib.imports.forEach((imp) {
      imports.addImportElement(imp);
    });

    // The fields are from the SumDataType class.
    final genToString = reader.objectValue.getField('genToString')!.toBoolValue();
    final genEqHashCode = reader.objectValue.getField('genEqHashCode')!.toBoolValue();
    final annotation = CodgenConfig(
      toString: genToString,
      eqHashCode: genEqHashCode,
      nnbd: lib.featureSet.isEnabled(Feature.non_nullable),
    );

    final mixinName = clazz.name;
    final List<String> typeArgs = clazz.typeParameters.map((param) => param.name).toList();
    final className = '_' + mixinName;
    final baseName = className + 'Base';
    final fields = <FieldModel>[];

    for (var field in clazz.fields) {
      if (field.name != 'hashCode' && !field.isStatic) {
        final msgPrefix = "Invalid getter '${field.name}' for data class '$mixinName'";
        if (field.getter == null) {
          throw Exception('$msgPrefix: field must have a getter');
        } else if (field.setter != null) {
          throw Exception('$msgPrefix: field must not have a setter');
        } else {
          if (field.getter!.isAbstract) {
            fields.add(mkField(field, imports, annotation));
          }
        }
      }
    }

    return CommonClassModel.mk(
      mixinName: mixinName,
      baseClassName: baseName,
      className: className,
      fields: fields,
      typeArgs: typeArgs,
      config: annotation,
    );
  } on CodegenException catch (e) {
    e.className = clazz.name;
    rethrow;
  }
}