findClassElementWithLibraryUriAndClassName function

Future<ClassElement?> findClassElementWithLibraryUriAndClassName({
  1. required String codePath,
  2. required String targetLibraryUri,
  3. required String targetClassName,
})

Implementation

Future<ClassElement?> findClassElementWithLibraryUriAndClassName({
  required String codePath,
  required String targetLibraryUri,
  required String targetClassName,
}) async {
  final contexts = getProjectContextList(codePath);
  if (contexts == null || contexts.isEmpty) {
    printError("no project contexts found");
    exit(0);
  }
  // targetLibraryUri ??= 'package:http/http.dart';
  // targetClassName ??= 'Response';
  final lib = await contexts.first.currentSession.getLibraryByUri(
    targetLibraryUri,
  );

  if (lib is LibraryElementResult) {
    final targetElement = lib.element.exportNamespace.get2(targetClassName);
    if (targetElement is ClassElement) {
      return targetElement;
    }
    return null;
  }
  return null;
}