* @param actualArguments
* @return
*/
@SuppressWarnings("unchecked")
public Object[] resolveArguments(Method method, Object[] actualArguments) {
Context context = Context.getCurrentContext();
Map<String, Object> data = (Map<String, Object>) context.getParams().get("data");
Class<?>[] parameterTypes = method.getParameterTypes();
MethodParameter[] parameters = new MethodParameter[parameterTypes.length];
for (int i = 0; i < parameterTypes.length; i++) {
parameters[i] = new MethodParameter(method, i);
parameters[i].initParameterNameDiscovery(parameterNameDiscoverer);
}
Object[] args = new Object[parameters.length];
for (int i = 0; i < parameters.length; i++) {
String name = parameters[i].getParameterName();
if (data != null && data.containsKey(name)) {
Object val = data.get(name);
if (conversionService.canConvert(val.getClass(), parameters[i].getParameterType())) {
args[i] = conversionService.convert(val, parameters[i].getParameterType());
} else {
args[i] = val;
}
}
if (args[i] == null) {
for (Annotation paramAnn : parameters[i].getParameterAnnotations()) {
if (data != null && RequestParam.class.isInstance(paramAnn)) {
RequestParam requestParam = (RequestParam) paramAnn;
String paramName = requestParam.value();
boolean required = requestParam.required();
String defaultValue = requestParam.defaultValue();
Object val = data.get(paramName);
if (val == null) {
if (defaultValue != null) {
val = defaultValue;
} else if (required) {
throw new JibeRuntimeException(paramName
+ " parameter has to be present in the request");
}
}
if (conversionService.canConvert(val.getClass(), parameters[i].getParameterType())) {
args[i] = conversionService.convert(val, parameters[i].getParameterType());
} else {
args[i] = val;
}
break;
} else if (ConversationAttribute.class.isInstance(paramAnn)) {
ConversationAttribute convAttrAnn = (ConversationAttribute) paramAnn;
if (StringUtils.isEmpty(convAttrAnn.value())) {
for (Object attr : context.getConversation().getAttributes().values()) {
if (attr != null && parameters[i].getParameterType().isAssignableFrom(attr.getClass())) {
args[i] = attr;
}
}
} else {
args[i] = context.getConversation().getAttribute(convAttrAnn.value());
}
}
}
}
Class<?> parameterType = parameters[i].getParameterType();