generateTopLevelMethod method

Future<MethodDeclaration> generateTopLevelMethod(
  1. MethodMirror methodMirror,
  2. Package package,
  3. String libraryUri,
  4. Uri sourceUri,
)

Generate top-level method with analyzer support

Implementation

Future<MethodDeclaration> generateTopLevelMethod(
  mirrors.MethodMirror methodMirror,
  Package package,
  String libraryUri,
  Uri sourceUri,
) async {
  final methodName = mirrors.MirrorSystem.getName(methodMirror.simpleName);

  final libraryElement = await _getLibraryElement(Uri.parse(libraryUri));

  // Get top-level function element
  ExecutableElement? functionElement;
  if (libraryElement != null) {
    functionElement = libraryElement.topLevelFunctions.firstWhereOrNull((f) => f.name == methodName);
  }

  final dartType = functionElement?.type;
  final mirrorType = methodMirror.returnType;
  Type runtimeType = mirrorType.hasReflectedType ? mirrorType.reflectedType : mirrorType.runtimeType;

  // Extract annotations and resolve type
  if(GenericTypeParser.shouldCheckGeneric(runtimeType)) {
    final annotations = await _extractAnnotations(mirrorType.metadata, package);
    final resolvedType = await _resolveTypeFromGenericAnnotation(annotations, methodName);
    if (resolvedType != null) {
      runtimeType = resolvedType;
    }
  }

  final result = StandardMethodDeclaration(
    name: methodName,
    element: functionElement,
    dartType: dartType,
    type: runtimeType,
    libraryDeclaration: _libraryCache[libraryUri]!,
    returnType: await _getLinkDeclaration(methodMirror.returnType, package, libraryUri, dartType),
    annotations: await _extractAnnotations(methodMirror.metadata, package),
    sourceLocation: sourceUri,
    isStatic: true,
    isAbstract: false,
    isGetter: methodMirror.isGetter,
    isSetter: methodMirror.isSetter,
    isFactory: false,
    isPublic: !_isInternal(methodName),
    isSynthetic: _isSynthetic(methodName),
    isConst: false,
  );

  result.parameters = await _extractParameters(
    methodMirror.parameters,
    functionElement?.typeParameters,
    package,
    libraryUri,
    result
  );

  return result;
}