com.mycomm.itool.WebAppModule.utils.WebInstanceBuilder Maven / Gradle / Ivy
/*
* Copyright 2020 jw362j.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.mycomm.itool.WebAppModule.utils;
import com.mycomm.IProtocol.beans.JDataTypes;
import com.mycomm.IProtocol.log.UniversalLogHolder;
import com.mycomm.itool.SystemUtil;
import java.lang.reflect.Field;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import javax.servlet.ServletRequest;
/**
*
* @author jw362j
*/
public class WebInstanceBuilder {
private static final SimpleDateFormat sdf_yyyy_MM_ddHH_mm_ss = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void buildInstance(Object instance, ServletRequest request) {
if (instance == null || request == null) {
return;
}
Enumeration parameterNames = request.getParameterNames();
Field[] fields = instance.getClass().getDeclaredFields();
if (parameterNames == null || fields == null || fields.length <= 0) {
return;
}
for (Field field : fields) {
if (field == null) {
continue;
}
JDataTypes fType = JDataTypes.getDataType(field);
if (fType == null) {
continue;
}
String fieldName = field.getName();
String fieldValue = request.getParameter(fieldName);
if (SystemUtil.isTxtEmpty(fieldValue)) {
continue;
}
field.setAccessible(true);
try {
switch (fType) {
case JByte:
//must be a string with length 1
if (fieldValue.length() == 1) {
byte byteValue = (byte) fieldValue.charAt(0);
field.setByte(instance, byteValue);
}
break;
case JShort:
//must be a string in number format,16-bit signed two's complement integer. It has a minimum value of -32768 and a maximum value of 32767
if (fieldValue.length() <= 5 && SystemUtil.isNumberString(fieldValue)) {
field.setShort(instance, Short.valueOf(fieldValue));
}
break;
case JFloat:
if (SystemUtil.isNumeric(fieldValue)) {
field.setFloat(instance, Float.valueOf(fieldValue));
}
break;
case JDouble:
if (SystemUtil.isNumeric(fieldValue)) {
field.setDouble(instance, Float.valueOf(fieldValue));
}
break;
case JInt:
if (SystemUtil.isNumberString(fieldValue)) {
field.setInt(instance, Integer.valueOf(fieldValue));
}
break;
case JLong:
if (SystemUtil.isNumberString(fieldValue)) {
field.setLong(instance, Long.valueOf(fieldValue));
}
break;
case JBoolean:
if ("1".equals(fieldValue.toLowerCase()) || "yes".equals(fieldValue.toLowerCase())
|| "true".equals(fieldValue.toLowerCase()) || "y".equals(fieldValue.toLowerCase())
|| "ok".equals(fieldValue.toLowerCase())) {
field.setBoolean(instance, true);
}
break;
case JDate:
if (SystemUtil.isNumberString(fieldValue)) {
field.set(instance, new Date(Long.valueOf(fieldValue)));
} else {
field.set(instance, sdf_yyyy_MM_ddHH_mm_ss.parse(fieldValue));
}
break;
case JChar1:
case JChar8:
case JChar16:
case JChar32:
case JChar64:
case JChar128:
case JChar255:
case JString:
field.set(instance, fieldValue);
break;
default:
break;
}
} catch (IllegalArgumentException e) {
UniversalLogHolder.e(WebInstanceBuilder.class.getSimpleName(), "IllegalAccessException:" + e.getMessage());
} catch (IllegalAccessException ex) {
UniversalLogHolder.e(WebInstanceBuilder.class.getSimpleName(), "IllegalAccessException:" + ex.getMessage());
} catch (ParseException ex) {
UniversalLogHolder.e(WebInstanceBuilder.class.getSimpleName(), "ParseException:" + ex.getMessage());
}
}
}
}