我试图找到一种特定方法的用法.在这种情况下,我想找到的用法/调用foo.go()
,但不是bar.go()
.下面的代码将找到go()
任何类的所有调用.在下面的代码中,node.target
简单地给出了我x
的var名称,但我正在努力弄清楚方法属于哪个类.
void main() { var x = new Foo(); x.go(); x = new Bar(); x.go(); } class Foo { go() { print('I am foo'); } } class Bar { go() { print('I am bar'); } }
import 'package:analyzer/analyzer.dart'; import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/ast/visitor.dart'; void main() { var targetFile = parseDartFile('test.dart'); var visitor = new UsageVisitor('foo.go'); targetFile.visitChildren(visitor); } class UsageVisitor extends UnifyingAstVisitor { String _class; String _method; UsageVisitor(this._class, this._method); @override visitMethodInvocation(MethodInvocation node) { print({ 'target' : node.target.toString(), // in both cases, gives "x" -- I need "Foo" and "Bar", respectively 'methodName': node.methodName.toString() // gives "go" }); return super.visitNode(node); } }
我如何区分(在分析仪级别),foo.go()
和之间bar.go()
?