/**
*
* Liferay sql List<Object>转 Java Bean方法
*
* JavaBean 必须属性实现set方法
*
* @param liferayObj
* List<Object>
* @param obj
* JavaBean
* @return
*/
public static <T> ArrayList<T> LiferayDataObject2JavaBean(List<Object> liferayObj, Class<T> obj) {
if (null == liferayObj || liferayObj.size() == 0) {
return null;
}
if (null == liferayObj || liferayObj.size() == 0) {
return null;
}
ArrayList<T> arrayList = new ArrayList<T>();
try {
Field fiedls[] = obj.getDeclaredFields();
for (int i = 0; liferayObj != null && i < liferayObj.size(); i++) {
Object[] o = (Object[]) liferayObj.get(i);
// 创建对象实例
T newInstance = obj.newInstance();
for (int j = 0; j < fiedls.length; j++) {
String name = fiedls[j].getName();
String substring = name.substring(0, 1);
String replace = name.replaceFirst(substring, substring.toUpperCase());
// 获取字段类型
Class<?> type = obj.getDeclaredField(name).getType();
Method method = obj.getMethod("set" + replace, type);
// 判断读取数据的类型
if (type.isAssignableFrom(String.class)) {
method.invoke(newInstance, (String) (o[j] == null ? "" : o[j]));
} else if (type.isAssignableFrom(int.class) || type.isAssignableFrom(Integer.class)) {
method.invoke(newInstance, (Integer) (o[j] == null ? 0 : o[j]));
} else if (type.isAssignableFrom(Boolean.class) || type.isAssignableFrom(boolean.class)) {
method.invoke(newInstance, (Boolean) (o[j] == null ? false : o[j]));
} else if (type.isAssignableFrom(Long.class) || type.isAssignableFrom(long.class)) {
method.invoke(newInstance, (Long) (o[j] == null ? 0 : o[j]));
} else if (type.isAssignableFrom(Double.class) || type.isAssignableFrom(double.class)) {
method.invoke(newInstance, (Double) (o[j] == null ? 0 : o[j]));
}
arrayList.add(newInstance);
}
return arrayList;
} catch (Exception e) {
logger.error(e.getMessage());
}
return arrayList;
}