Java 코드에서 모든 catch 블록을 추출하고 싶습니다. 정상적인 try-catch 블록을 추출 할 수 있지만 catch 블록이 "if"와 같은 다른 유형의 블록에 중첩되어 있으면 내 코드가이를 탐지 할 수 있습니다. 다음Java 코드에서 중첩 된 catch 블록 추출
는내가 AST 파서를 사용하여 작성한 코드입니다 :
public static void methodVisitor(String content)
{
ASTParser metparse = ASTParser.newParser(AST.JLS3);
metparse.setSource(content.toCharArray());
metparse.setKind(ASTParser.K_STATEMENTS);
Block block = (Block) metparse.createAST(null);
block.accept(new ASTVisitor()
{
public boolean visit(VariableDeclarationFragment var)
{
return false;
}
public boolean visit(SimpleName node)
{
return false;
}
public boolean visit(IfStatement myif)
{
System.out.println("myif=" + myif.toString());
return false;
}
public boolean visit(TryStatement mytry)
{
System.out.println("mytry=" + mytry.toString());
List catchClauses = mytry.catchClauses();
CatchClause clause = (CatchClause) catchClauses.get(0);
SingleVariableDeclaration exception = clause.getException();
Type type = exception.getType();
System.out.println("catch=" + catchClauses.toString());
return false;
}
public boolean visit(CatchClause mycatch)
{
System.out.println("mycatch=" + mycatch.toString());
return false;
}
});
}
이 코드는 다음과 같은 조건에서 캐치 절을 추출 할 수 없습니다 :
if (base != null && base.getClass().isArray())
{
context.setPropertyResolved(base, property);
try
{
int idx = coerce(property);
checkBounds(base, idx);
}
catch (IllegalArgumentException e)
{
}
}
누군가가 중첩 캐치를 추출하는 방법을 알고 있습니까 블록. 감사합니다. 미리 !!
"AST 파서"도구 모음의 일부입니까? 내 생각 엔 (귀하의 도구 집합을 모르겠다.) "방문객"이 나무를 걷기 위해 집단적으로 의도되었으며, "방문 (IfStmt ..)"이 "false"를 반환하면 트리 탐색이 중단 될 때 if 성명서가 발급됩니다. 이렇게하면 중첩 된 "try"절이 숨어있는 "if"의 하위 부분으로 재귀를 막을 수 있습니다. "vist (IfStmt ...)"를 "true"로 돌려 봅니다 (자녀를 방문해야 할 수도 있습니다). –
감사합니다. 일했다 :-) – Sangeeta