I am using the build package to take a .dart file and look for definitions of a certain class or its subclasses. Can ClassElement be used for subclasses too?
I am only expecting a single definition of a specific class to be within one file of the project, but there's no reason clients couldn't subclass and go wild.
There is not a way to directly grab all subclasses, but you can find all classes and check if each one is a subclass of the one you're interested in.
Start by getting the
LibraryElementfor the file you're generating code for withBuildStep.inputLibrary. From there find all the classes in the library withvar classes = libraryElement.units.expand((cu) => cu.types);. Then check if each one is a subclass of the class you are interested in by checking whether theClassElementfor the type you are interested in is inClassElement.allSupertypesfor the type you are checking.var subtypes = classes.where((c) => c.allSupertypes.contains(lookingFor));.You may find the
LibraryReaderandTypeCheckerutilities fromsource_genuseful.