final MethodCallExpression methodCall = new MethodCallExpression(new FieldExpression(fieldNode), "clone", ArgumentListExpression.EMPTY_ARGUMENTS);
// return null if field is null
methodCall.setSafe(true);
ExpressionStatement oldVariableAssignment = new ExpressionStatement(
new DeclarationExpression(oldVariable,
Token.newSymbol(Types.ASSIGN, -1, -1),
methodCall));
methodBlockStatement.addStatement(oldVariableAssignment);
oldVariablesMap.addMapEntryExpression(new MapEntryExpression(new ConstantExpression(oldVariable.getName().substring("$old$".length())), oldVariable));
} else if (ClassHelper.isPrimitiveType(fieldType)
|| ClassHelper.isNumberType(fieldType)
|| fieldType.getName().startsWith("java.math")
|| fieldType.getName().equals("groovy.lang.GString")
|| fieldType.getName().equals("java.lang.String")) {
VariableExpression oldVariable = new VariableExpression("$old$" + fieldNode.getName(), fieldNode.getType());
oldVariable.setAccessedVariable(oldVariable);
ExpressionStatement oldVariableAssignment = new ExpressionStatement(
new DeclarationExpression(oldVariable,
Token.newSymbol(Types.ASSIGN, -1, -1),
new FieldExpression(fieldNode)));
methodBlockStatement.addStatement(oldVariableAssignment);
oldVariablesMap.addMapEntryExpression(new MapEntryExpression(new ConstantExpression(oldVariable.getName().substring("$old$".length())), oldVariable));
}
}
}
VariableExpression oldVariable = new VariableExpression("old", new ClassNode(Map.class));
oldVariable.setAccessedVariable(oldVariable);
ExpressionStatement oldVariabeStatement = new ExpressionStatement(
new DeclarationExpression(oldVariable,
Token.newSymbol(Types.ASSIGN, -1, -1),
oldVariablesMap));
methodBlockStatement.addStatement(oldVariabeStatement);
VariableExpression mergedOldVariables = null;
// let's ask the super class for old variables...
if (classNode.getSuperClass() != null && classNode.getSuperClass().getMethod(OLD_VARIABLES_METHOD, Parameter.EMPTY_ARRAY) != null) {
mergedOldVariables = new VariableExpression("mergedOldVariables", new ClassNode(Map.class));
mergedOldVariables.setAccessedVariable(mergedOldVariables);
ExpressionStatement mergedOldVariablesStatement = new ExpressionStatement(
new DeclarationExpression(mergedOldVariables,
Token.newSymbol(Types.ASSIGN, -1, -1),
new MethodCallExpression(oldVariable, "plus", new ArgumentListExpression(new MethodCallExpression(VariableExpression.SUPER_EXPRESSION, OLD_VARIABLES_METHOD, ArgumentListExpression.EMPTY_ARGUMENTS)))));