final ClassNode paramTypeClassNode = param.getType();
final String methodParamName = param.getName();
final Expression defaultValueExpression;
if (paramTypeClassNode.equals(ClassHelper.Boolean_TYPE)) {
defaultValueExpression = new ConstantExpression(false);
} else if (PRIMITIVE_CLASS_NODES.contains(paramTypeClassNode)) {
defaultValueExpression = new ConstantExpression(0);
} else {
defaultValueExpression = new ConstantExpression(null);
}
final ConstantExpression paramConstantExpression = new ConstantExpression(requestParameterName);
final Expression paramsTypeConversionMethodArguments = new ArgumentListExpression(
paramConstantExpression/*, defaultValueExpression*/, new ConstantExpression(null));
final String conversionMethodName;
if (TYPE_WRAPPER_CLASS_TO_CONVERSION_METHOD_NAME.containsKey(paramTypeClassNode)) {
conversionMethodName = TYPE_WRAPPER_CLASS_TO_CONVERSION_METHOD_NAME.get(paramTypeClassNode);
} else {
conversionMethodName = paramTypeClassNode.getName();
}
Expression getParamsExpression = buildGetPropertyExpression(new VariableExpression("this"), "params", classNode);
final MethodCallExpression retrieveConvertedValueExpression = new MethodCallExpression(
getParamsExpression, conversionMethodName, paramsTypeConversionMethodArguments);
Class<?> defaultValueClass = null; // choose any
if("char".equals(conversionMethodName)) {
// TypeConvertingMap.'char' method has 2 different signatures, choose the one with "Character 'char'(String name, Integer defaultValue)" signature
defaultValueClass = Integer.class;
}
applyMethodTarget(retrieveConvertedValueExpression, TypeConvertingMap.class, null, defaultValueClass);
final Expression paramsContainsKeyMethodArguments = new ArgumentListExpression(paramConstantExpression);
final BooleanExpression containsKeyExpression = new BooleanExpression(
applyDefaultMethodTarget(new MethodCallExpression(getParamsExpression, "containsKey", paramsContainsKeyMethodArguments), Map.class));
final Token equalsToken = Token.newSymbol(Types.EQUALS, 0, 0);
final VariableExpression convertedValueExpression = new VariableExpression(
"___converted_" + methodParamName, new ClassNode(Object.class));
final DeclarationExpression declareConvertedValueExpression = new DeclarationExpression(
convertedValueExpression, equalsToken, new EmptyExpression());
Statement declareVariableStatement = new ExpressionStatement(declareConvertedValueExpression);
wrapper.addStatement(declareVariableStatement);
final VariableExpression methodParamExpression = new VariableExpression(
methodParamName, paramTypeClassNode);
final DeclarationExpression declareParameterVariableStatement = new DeclarationExpression(
methodParamExpression, equalsToken, new EmptyExpression());
declareVariableStatement = new ExpressionStatement(declareParameterVariableStatement);
wrapper.addStatement(declareVariableStatement);
final Expression assignmentExpression = new BinaryExpression(
convertedValueExpression, equalsToken,
new TernaryExpression(containsKeyExpression, retrieveConvertedValueExpression, defaultValueExpression));
wrapper.addStatement(new ExpressionStatement(assignmentExpression));
Expression rejectValueMethodCallExpression = getRejectValueExpression(classNode, methodParamName);
BlockStatement ifConvertedValueIsNullBlockStatement = new BlockStatement();
ifConvertedValueIsNullBlockStatement.addStatement(
new ExpressionStatement(rejectValueMethodCallExpression));
ifConvertedValueIsNullBlockStatement.addStatement(
new ExpressionStatement(new BinaryExpression(
methodParamExpression, equalsToken, defaultValueExpression)));
final BooleanExpression isConvertedValueNullExpression = new BooleanExpression(new BinaryExpression(
convertedValueExpression, Token.newSymbol(Types.COMPARE_EQUAL, 0, 0),
new ConstantExpression(null)));
final ExpressionStatement assignConvertedValueToParamStatement = new ExpressionStatement(
new BinaryExpression(methodParamExpression, equalsToken, convertedValueExpression));
final Statement ifStatement = new IfStatement(isConvertedValueNullExpression,
ifConvertedValueIsNullBlockStatement,
assignConvertedValueToParamStatement);