package 1.config;
import ;
import 1.;
import ;
import ;
import ;
import ;
import ;
import ;
import .method.;
import .method.;
import ;
import ;
import ;
import ;
import .Method;
import ;
import ;
import ;
import ;
import ;
/**
* @author 半卷流年
* @date 2020-8-13 10:28
*/
public class RequestModelArgumentResolver implements HandlerMethodArgumentResolver {
private final Pattern underLinePattern = ("_(\\w)");
@Override
public boolean supportsParameter(MethodParameter parameter) {
return (RequestPropertiesBody.class);
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
Class<?> parameterType = ();
HttpServletRequest request = (HttpServletRequest.class);
CustomHttpServletRequestWrapper requestWrapper = new CustomHttpServletRequestWrapper(request);
String body = ();
//实例化对象
Object result = (parameterType);
if((body)){
return result;
}
JSONObject jsonObject = (body);
//获取字段
Field[] fields = (parameterType);
Map<String, Field> fieldMap = (fields).collect((Field::getName, v -> v));
Object arg = null;
//往对象写入值
for (<String, Object> entry : ()) {
String fieldName = ();
//处理下划线转驼峰命名
if(isExistUnderLine(fieldName)){
fieldName = (fieldName);
}
Field field = (fieldName, null);
if((field)){
continue;
}
arg = ();
if((arg)){
continue;
}
if (isPrimitive(())) {
Class<?> parType = ();
arg = getArg(parType, arg);
}
Method setter = getSetterMethod(parameterType, field,fieldName);
if (setter != null) {
setter.invoke(result, arg);
}
}
return result;
}
private boolean isExistUnderLine(String value){
Matcher m = (value);
return ();
}
private String underLineToCamel(final String value) {
final StringBuffer sb = new StringBuffer();
Matcher m = (value);
while (()){
(sb, m.group(1).toUpperCase());
}
(sb);
return ();
}
private Method getSetterMethod(Class<?> clazz, Field field,String fileName) throws NoSuchMethodException {
return ("set" + toUpperCaseFirstOne(fileName), ());
}
private boolean isPrimitive(Class<?> clazz) {
return Integer.class.equals(clazz) ||
Double.class.equals(clazz) ||
Float.class.equals(clazz) ||
Boolean.class.equals(clazz) ||
Long.class.equals(clazz) ||
Byte.class.equals(clazz) ||
Short.class.equals(clazz) ||
();
}
private Object getArg(Class<?> primitiveClass, Object value) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Object result = null;
if (isPrimitive(primitiveClass)) {
String primitiveName = ();
if ("char".equals(primitiveName)) {
return value;
}
String firstUpperprimitiveName = toUpperCaseFirstOne(primitiveName);
Class<?> encapsulationClass = "int".equals(primitiveName) ? Class.forName("." + firstUpperprimitiveName + "eger") : Class.forName("." + firstUpperprimitiveName);
result = getParseMethod(encapsulationClass, firstUpperprimitiveName).invoke(null, value);
}
return result;
}
private Method getParseMethod(Class<?> encapsulationClass, String firstUpperprimitiveName) throws NoSuchMethodException {
String methodName = "parse" + firstUpperprimitiveName;
return (methodName, String.class);
}
private String toUpperCaseFirstOne(String fieldName) {
if (Character.isUpperCase((0))){
return fieldName;
}
else {
return String.valueOf(Character.toUpperCase((0))) + (1);
}
}
}