prepareAnnotatedMethods method

List<AnnotatedMethodEntry> prepareAnnotatedMethods(
  1. List<ClassDeclaration> components
)
inherited

Prepares annotated method entries by scanning declarations.

Implementation

List<AnnotatedMethodEntry> prepareAnnotatedMethods(
  List<ClassDeclaration> components,
) {
  final entries = <AnnotatedMethodEntry>[];
  final classByName = <String, ClassDeclaration>{
    for (final c in components) c.getName(): c,
  };

  for (final decl in components) {
    for (final method in decl.getMethods()) {
      for (final annotation in method.getAnnotations()) {
        final annName = annotation.getName();
        entries.add(
          AnnotatedMethodEntry(
            annotationName: annName,
            className: decl.getName(),
            methodName: method.getName(),
            uri: decl.getLibrary().getUri(),
          ),
        );

        // Recursively resolve extended/meta-annotated ancestors
        final visited = <String>{annName};
        void addAnnotationHierarchy(String currentName) {
          final annDecl = classByName[currentName];
          if (annDecl == null) return;

          final sc = annDecl.getSuperClass();
          if (sc != null) {
            final scName = sc.getName();
            if (scName.isNotEmpty && scName != 'Object' && scName != 'ReflectableAnnotation') {
              if (visited.add(scName)) {
                entries.add(
                  AnnotatedMethodEntry(
                    annotationName: scName,
                    className: decl.getName(),
                    methodName: method.getName(),
                    uri: decl.getLibrary().getUri(),
                  ),
                );
                addAnnotationHierarchy(scName);
              }
            }
          }

          for (final iface in annDecl.getInterfaces()) {
            final ifaceName = iface.getName();
            if (ifaceName.isNotEmpty && ifaceName != 'ReflectableAnnotation') {
              if (visited.add(ifaceName)) {
                entries.add(
                  AnnotatedMethodEntry(
                    annotationName: ifaceName,
                    className: decl.getName(),
                    methodName: method.getName(),
                    uri: decl.getLibrary().getUri(),
                  ),
                );
                addAnnotationHierarchy(ifaceName);
              }
            }
          }

          for (final metaAnn in annDecl.getAnnotations()) {
            final metaName = metaAnn.getName();
            if (metaName.isNotEmpty && metaName != 'Target') {
              if (visited.add(metaName)) {
                entries.add(
                  AnnotatedMethodEntry(
                    annotationName: metaName,
                    className: decl.getName(),
                    methodName: method.getName(),
                    uri: decl.getLibrary().getUri(),
                  ),
                );
                addAnnotationHierarchy(metaName);
              }
            }
          }
        }

        addAnnotationHierarchy(annName);
      }
    }
  }

  return entries;
}