我source_gen
用来通过 Dart 解析一些 Dart 文件analyzer
。
我正在扩展GeneratorForAnnotation<>
和覆盖方法
FutureOr<String> generateForAnnotatedElement(Element element, ConstantReader annotation, BuildStep buildStep)
。这个方法给了我一个基本元素,我导航它直到找到其他元素,比如 a MethodElement
。
一旦我掌握了这个MethodElement
,我想详细分析方法的块。我想要这样的东西:
AstNode node = methodElement.computeNode();
更详细地说:
class MyGenerator extends GeneratorForAnnotation<MyAnnotation> {
@override
FutureOr<String> generateForAnnotatedElement(
Element element, ConstantReader annotation, BuildStep buildStep,
) {
if (element is ClassElement)
for (MethodElement methodElement in element.methods) {
AstNode node = methodElement.computeNode();
processMyNode(node);
}
else
throw AssertionError();
}
}
不幸的是,computeNode
已弃用。如果我尝试调用它,我会得到:
NoSuchMethodError: Class 'RestrictedAnalysisContext' has no instance method 'resolveCompilationUnit' with matching arguments.
Receiver: Instance of 'RestrictedAnalysisContext'
Tried calling: resolveCompilationUnit(Instance of 'FileSource', Instance of 'LibraryElementImpl')
Found: resolveCompilationUnit(Source, LibraryElement) => CompilationUnit
我的问题:我怎样才能得到AstNode
?