我试图获取Assignment,它是MethodInvocation的父级 测试代码:
package test;
public class Test
{
public void method()
{
String a = "123".substring(1);
}
}
我的代码:
public static Assignment getAssignmentBlock(MethodInvocation inv)
{
ASTNode node = inv.getParent();
while(node != null)
{
if(node instanceof Assignment)
return (Assignment) node;
node = node.getParent();
}
return null;
}
和访客
cu.accept(new ASTVisitor()
{
public boolean visit(MethodInvocation inv)
{
System.out.println(inv.getName().getFullyQualifiedName());
Assignment a = MethodCall.getAssignmentBlock(inv);
System.out.println(a != null);
return true;
}
});
但是我一直看到false
意味着Assignment为null
问题是什么?或者如果这是错误的方式,请以正确的方式提出建议。感谢