如何检查访问的节点是否在 Eclipse JDT 解析中的 if 部分或 IfStatement 节点的部分?
How to check if a visited node is in if part or else part of IfStatement node in Eclipse JDT parsing?
当我在 AST 遍历访问 MethodInvocation 节点时,我想知道它是在 IfStatement then 部分还是 else 部分或者在 expression 部分。 then 部分可以是一个完整的代码块,但我认为我的代码只处理一个 then 语句。
这是访问方法调用的代码片段
@Override
public boolean visit(MethodInvocation node)
{
StructuralPropertyDescriptor location = node.getLocationInParent();
setNodeRegion(location);
下面是我想为 IfStatement 的每个区域设置标志的方式
private void setNodeRegion(StructuralPropertyDescriptor location) {
if(location == IfStatement.EXPRESSION_PROPERTY ||
location == IfStatement.THEN_STATEMENT_PROPERTY)
{
ParseContextAction.ifBlockRegion = true;
}
else
{
if(location == IfStatement.ELSE_STATEMENT_PROPERTY)
{
ParseContextAction.elseBlockRegion = true;
}
else
{
if(location == CatchClause.BODY_PROPERTY)
{
ParseContextAction.catchBlockRegion = true;
}
else
{
ParseContextAction.basicBlockRegion = true;
}
}
}
}
如果您使用 visit(IfStatement node)
而不是 visit(MethodInvocation node)
,您可以同时访问 then (getThenStatement()
) 和 else (getElseStatement()
) 有单独访问者的分支:
@Override
public boolean visit(IfStatement node) {
Statement thenBranch = node.getThenStatement();
if (thenBranch != null) {
thenBranch.accept(new ASTVisitor(false) {
@Override
public boolean visit(MethodInvocation node) {
// handle method invocation in the then branch
return true; // false, if nested method invocations should be ignored
}
}
}
Statement elseBranch = node.getElseStatement();
if (elseBranch != null) {
elseBranch.accept(new ASTVisitor(false) {
@Override
public boolean visit(MethodInvocation node) {
// handle method invocation in the else branch
return true; // false, if nested method invocations should be ignored
}
}
}
return true; // false, if nested if statements should be ignored
}
当我在 AST 遍历访问 MethodInvocation 节点时,我想知道它是在 IfStatement then 部分还是 else 部分或者在 expression 部分。 then 部分可以是一个完整的代码块,但我认为我的代码只处理一个 then 语句。
这是访问方法调用的代码片段
@Override
public boolean visit(MethodInvocation node)
{
StructuralPropertyDescriptor location = node.getLocationInParent();
setNodeRegion(location);
下面是我想为 IfStatement 的每个区域设置标志的方式
private void setNodeRegion(StructuralPropertyDescriptor location) {
if(location == IfStatement.EXPRESSION_PROPERTY ||
location == IfStatement.THEN_STATEMENT_PROPERTY)
{
ParseContextAction.ifBlockRegion = true;
}
else
{
if(location == IfStatement.ELSE_STATEMENT_PROPERTY)
{
ParseContextAction.elseBlockRegion = true;
}
else
{
if(location == CatchClause.BODY_PROPERTY)
{
ParseContextAction.catchBlockRegion = true;
}
else
{
ParseContextAction.basicBlockRegion = true;
}
}
}
}
如果您使用 visit(IfStatement node)
而不是 visit(MethodInvocation node)
,您可以同时访问 then (getThenStatement()
) 和 else (getElseStatement()
) 有单独访问者的分支:
@Override
public boolean visit(IfStatement node) {
Statement thenBranch = node.getThenStatement();
if (thenBranch != null) {
thenBranch.accept(new ASTVisitor(false) {
@Override
public boolean visit(MethodInvocation node) {
// handle method invocation in the then branch
return true; // false, if nested method invocations should be ignored
}
}
}
Statement elseBranch = node.getElseStatement();
if (elseBranch != null) {
elseBranch.accept(new ASTVisitor(false) {
@Override
public boolean visit(MethodInvocation node) {
// handle method invocation in the else branch
return true; // false, if nested method invocations should be ignored
}
}
}
return true; // false, if nested if statements should be ignored
}