reflectStart function

List<GenClassBean> reflectStart(
  1. List<Type> types,
  2. String path
)

reflect class and parse it

Implementation

List<GenClassBean> reflectStart(List<Type> types, String path) {
  var genClassList = <GenClassBean>[];
  types.forEach((element) {
    var genClass = GenClassBean();
    genClassList.add(genClass);

    /// ready
    var classMirror = reflectClass(element);

    if (path.isNotEmpty && path != tmpPath) {
      //read dart file content
      var file = File(path);
      fileContent = file.readAsLinesSync();
      tmpPath = path;
    }
    MethodMirror? constructorMethodMirror;
    constructorMethodMirror = classMirror.declarations.values.firstWhere(
            (element) => element is MethodMirror && element.isConstructor)
        as MethodMirror?;
    InstanceMirror? instanceMirror;
    if (!classMirror.isAbstract && !classMirror.isEnum) {
      try {
        instanceMirror = classMirror
            .newInstance(constructorMethodMirror!.constructorName, []);
      } catch (e) {
        print(e);
        throw "can't found default constructor method, please remove other constructor method!";
      }
    }
    var declarations = classMirror.declarations;

    ///set path
    genClass.path = classMirror.location.toString();

    ///set class info
    var classInfo = ClassInfo();
    if (classMirror.isAbstract) {
      classInfo.type = ClassType.abstract;
    } else if (classMirror.isEnum) {
      classInfo.type = ClassType.enumType;
    } else {
      classInfo.type = ClassType.normal;
    }
    classInfo.name = element.toString();
    genClass.classInfo = classInfo;

    ///set property
    var allProperty = <Property>[];
    var propertyList = declarations.values
        .where((element) => element is VariableMirror)
        .map((e) => e as VariableMirror)
        .toList();
    var propertyLocations = propertyList
        .map((e) => e.location)
        .where((value) => value != null)
        .map((e) => e!)
        .toList();
    allProperty.addAll(_parsePropertyTypes(
        classMirror, instanceMirror, propertyList, propertyLocations));
    genClass.properties = allProperty;

    ///set method
    var allMethod = <MethodInfo>[];
    if (classInfo.type != ClassType.enumType) {
      var methodList = declarations.values
          .where((element) => element is MethodMirror && !element.isConstructor)
          .map((e) => e as MethodMirror)
          .toList();
      var lastMethodLocation = classMirror.location!.line;
      methodList.asMap().forEach((index, element) {
        String methodLineStr = fileContent[element.location!.line - 1];
        var method = MethodInfo();
        method.name = MirrorSystem.getName(element.simpleName);
        method.isAbstract = element.isAbstract;
        method.originDeclaration = methodLineStr;
        method.comment = _parseComment(
            fileContent, element.location!.line - 1, lastMethodLocation);
        lastMethodLocation = element.location!.line - 1;
        int argParamsStartIndex =
            methodLineStr.indexOf(method.name) + method.name.length + 1;
        element.parameters.forEach((param) {
          // param.isOptional
          var property = Property();
          method.args.add(property);
          var type = param.type;
          property.type = MirrorSystem.getName(type.qualifiedName);
          property.name = MirrorSystem.getName(param.simpleName);
          String startStr = methodLineStr.substring(argParamsStartIndex);
          int start = startStr.indexOf(property.type.split(".").last);
          int end = startStr.indexOf(" " + property.name, start) + 1;
          argParamsStartIndex += end + property.name.length;
          startStr = startStr
              .substring(start + property.type.split(".").last.length, end)
              .trim();
          if (startStr.endsWith("?")) {
            property.canBeNull = true;
            startStr = startStr.substring(0, startStr.length - 1);
          }
          if (startStr.isEmpty) {
            startStr = "<>";
          }
          startStr = startStr.replaceAll("<", "{").replaceAll(">", "}");
          String newLineContent = _formatTypeStr2JsonStr(startStr, 0, "");
          Map<dynamic, dynamic> typeJson = jsonDecode(newLineContent);
          _findMethodArgumentsType(
              property, param.type.typeArguments, typeJson);
        });

        var returnProperty = Property();
        String targetLineStr =
            methodLineStr.split(method.name).first.replaceAll(" ", "");
        if (targetLineStr.endsWith("?")) {
          returnProperty.canBeNull = true;
        }
        if (targetLineStr.isNotEmpty) {
          targetLineStr = "<$targetLineStr>";
          String jsonStr =
              targetLineStr.replaceAll("<", "{").replaceAll(">", "}");
          String newLineContent = _formatTypeStr2JsonStr(jsonStr, 0, "");
          Map<dynamic, dynamic> typeJson = jsonDecode(newLineContent);
          _findMethodArgumentsType(
              returnProperty, [element.returnType], typeJson);
          method.returnType = returnProperty.subType[0];
          method.returnType.canBeNull = returnProperty.canBeNull;
          allMethod.add(method);
        }
      });
    }
    genClass.methods = allMethod;
  });

  // print(genClassList);
  // print('reflectEnd! $types');
  return genClassList;
}