public void visitTryCatchFinally(TryCatchStatement statement) { statement.getTryStatement().visit(this); for (CatchStatement catchStatement : statement.getCatchStatements()) { catchStatement.visit(this); } Statement finallyStatement = statement.getFinallyStatement(); if (finallyStatement instanceof EmptyStatement) { // dispatching to EmptyStatement will not call back visitor, // must call our visitEmptyStatement explicitly visitEmptyStatement((EmptyStatement) finallyStatement); } else { finallyStatement.visit(this); } }
@Override public void visitTryCatchFinally(final TryCatchStatement statement) { visitStatement(statement); Map<Variable, VariableState> beforeTryCatch = new HashMap<Variable, VariableState>(getState()); statement.getTryStatement().visit(this); for (CatchStatement catchStatement : statement.getCatchStatements()) { catchStatement.visit(this); } Statement finallyStatement = statement.getFinallyStatement(); // we need to recall which final variables are unassigned so cloning the current state Map<Variable, VariableState> afterTryCatchState = new HashMap<Variable, VariableState>(getState()); if (finallyStatement instanceof EmptyStatement) { // dispatching to EmptyStatement will not call back visitor, // must call our visitEmptyStatement explicitly visitEmptyStatement((EmptyStatement) finallyStatement); } else { finallyStatement.visit(this); } // and now we must reset to uninitialized state variables which were only initialized during // try/catch Map<Variable, VariableState> afterFinally = new HashMap<Variable, VariableState>(getState()); for (Map.Entry<Variable, VariableState> entry : afterFinally.entrySet()) { Variable var = entry.getKey(); VariableState afterFinallyState = entry.getValue(); VariableState beforeTryCatchState = beforeTryCatch.get(var); if (afterFinallyState == VariableState.is_final && beforeTryCatchState != VariableState.is_final && afterTryCatchState.get(var) != beforeTryCatchState) { getState() .put( var, beforeTryCatchState == null ? VariableState.is_uninitialized : beforeTryCatchState); } } }