findClassesWithIDField static method

List<ClassElement> findClassesWithIDField(
  1. List<ClassElement> classes
)

Finds all classes that have an ID field of type String from a list of classes. Checks both the class itself and its superclasses for an ID field. Returns a list of ClassElements that meet the criteria.

Implementation

static List<ClassElement> findClassesWithIDField(final List<ClassElement> classes) {
  List<ClassElement> resultClasses = [];
  for (final aClass in classes) {
    //Check if the class or any of its superclasses has an ID field that is a String:
    bool hasIDField = false;
    for (final field in aClass.fields) {
      if (field.name == 'id' && field.type.element?.displayName == 'String') {
        hasIDField = true;
        break;
      }
    }
    for (final parent in aClass.allSupertypes) {
      for (final parentField in parent.element.fields) {
        if (parentField.name == 'id' && parentField.type.element?.displayName == 'String') {
          hasIDField = true;
          break;
        }
      }
    }

    if (!hasIDField) {
      print(ColorfulText.paint("Annotated class ${aClass.name} ignored. It (or its superclasses) does not have an ID field of type String.", ColorfulText.red));
    }
    else {
      resultClasses.add(aClass);
    }
  }
  return resultClasses;
}