generateMethod method

Future<MethodDeclaration> generateMethod(
  1. MethodMirror methodMirror,
  2. Element? parentElement,
  3. Package package,
  4. String libraryUri,
  5. Uri sourceUri,
  6. String className,
  7. ClassDeclaration? parentClass,
)

Generate method declaration with analyzer support

Implementation

Future<MethodDeclaration> generateMethod(
  mirrors.MethodMirror methodMirror,
  Element? parentElement,
  Package package,
  String libraryUri,
  Uri sourceUri,
  String className,
  ClassDeclaration? parentClass,
) async {
  final methodName = mirrors.MirrorSystem.getName(methodMirror.simpleName);

  // Get appropriate analyzer element
  Element? methodElement;
  if (parentElement is InterfaceElement) {
    if (methodMirror.isGetter) {
      methodElement = parentElement.getGetter(methodName);
    } else if (methodMirror.isSetter) {
      methodElement = parentElement.getSetter(methodName);
    } else {
      methodElement = parentElement.getMethod(methodName);
    }
  }

  final dartType = (methodElement as ExecutableElement?)?.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: methodElement,
    dartType: dartType,
    type: runtimeType,
    libraryDeclaration: _libraryCache[libraryUri]!,
    returnType: await _getLinkDeclaration(methodMirror.returnType, package, libraryUri, dartType),
    annotations: await _extractAnnotations(methodMirror.metadata, package),
    isPublic: !_isInternal(methodName),
    isSynthetic: _isSynthetic(methodName),
    sourceLocation: sourceUri,
    isStatic: methodMirror.isStatic,
    isAbstract: methodMirror.isAbstract,
    isGetter: methodMirror.isGetter,
    isSetter: methodMirror.isSetter,
    parentClass: parentClass != null ? StandardLinkDeclaration(
      name: parentClass.getName(),
      type: parentClass.getType(),
      pointerType: parentClass.getType(),
      qualifiedName: parentClass.getQualifiedName(),
      isPublic: parentClass.getIsPublic(),
      canonicalUri: Uri.parse(parentClass.getPackageUri()),
      referenceUri: Uri.parse(parentClass.getPackageUri()),
      isSynthetic: parentClass.getIsSynthetic(),
    ) : null,
    isFactory: methodMirror.isFactoryConstructor,
    isConst: methodMirror.isConstConstructor,
  );

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

  return result;
}