@SuppressWarnings("ConstantConditions")
public void run(final AstNode node) {
run(node, null);
for (final VariableToDeclare v : variablesToDeclare) {
final Variable variable = v.getVariable();
final AssignmentExpression replacedAssignment = v.getReplacedAssignment();
if (replacedAssignment == null) {
final BlockStatement block = (BlockStatement) v.getInsertionPoint().getParent();
final AnalysisResult analysisResult = analyze(v, block);
final VariableDeclarationStatement declaration = new VariableDeclarationStatement(v.getType().clone(), v.getName(), Expression.MYSTERY_OFFSET);
if (variable != null) {
declaration.getVariables().firstOrNullObject().putUserData(Keys.VARIABLE, variable);
}
if (analysisResult.isSingleAssignment) {
declaration.addModifier(Modifier.FINAL);
}
else if (analysisResult.needsInitializer && variable != null) {
declaration.getVariables().firstOrNullObject().setInitializer(
AstBuilder.makeDefaultValue(variable.getType())
);
}
Statement insertionPoint = v.getInsertionPoint();
while (insertionPoint.getPreviousSibling() instanceof LabelStatement) {
insertionPoint = (Statement) insertionPoint.getPreviousSibling();
}
block.getStatements().insertBefore(insertionPoint, declaration);
}
}
//
// Do all the insertions before the replacements. This is necessary because a replacement
// might remove our reference point from the AST.
//
for (final VariableToDeclare v : variablesToDeclare) {
final Variable variable = v.getVariable();
final AssignmentExpression replacedAssignment = v.getReplacedAssignment();
if (replacedAssignment != null) {
final VariableInitializer initializer = new VariableInitializer(v.getName());
final Expression right = replacedAssignment.getRight();