¶Ô±ÈÐÂÎļþ |
| | |
| | | package com.zhitan.common.utils; |
| | | |
| | | import com.alibaba.fastjson.JSONObject; |
| | | import com.zhitan.common.constant.CommonConst; |
| | | |
| | | import java.lang.reflect.Field; |
| | | import java.math.BigDecimal; |
| | | import java.util.ArrayList; |
| | | import java.util.List; |
| | | |
| | | /** |
| | | * @Description: 屿§å¼æä½å·¥å
·ç±» |
| | | * @author: yxw |
| | | * @date: 2022å¹´03æ07æ¥ 9:57 |
| | | */ |
| | | public class PropUtils { |
| | | /** |
| | | * 为å®ä½èµå¼ |
| | | * |
| | | * @param obj |
| | | * @param propName |
| | | * @param value |
| | | * @return |
| | | */ |
| | | public static Object setValue(Object obj, String propName, Object value) { |
| | | try { |
| | | Field field = obj.getClass().getDeclaredField(propName); |
| | | field.setAccessible(true); |
| | | String nameTypeName = field.getType().getTypeName(); |
| | | nameTypeName = StringUtil.ifEmptyOrNullReturnValue(nameTypeName).toLowerCase(); |
| | | if (nameTypeName.contains("float")) { |
| | | float num = (float) DoubleUtil.toDouble(value); |
| | | field.set(obj, num); |
| | | } else if (nameTypeName.contains("double")) { |
| | | field.set(obj, DoubleUtil.toDouble(value)); |
| | | } else if (nameTypeName.contains("bigdecimal")) { |
| | | BigDecimal valueOf = BigDecimal.valueOf(Double.parseDouble(String.valueOf(value))) |
| | | .setScale(CommonConst.DIGIT_2, BigDecimal.ROUND_HALF_UP); |
| | | |
| | | field.set(obj, valueOf); |
| | | } else { |
| | | field.set(obj, value); |
| | | } |
| | | } catch (Exception e) { |
| | | return null; |
| | | } |
| | | return obj; |
| | | } |
| | | |
| | | /** |
| | | * è·å对象çæå®å段çå¼ |
| | | * |
| | | * @param obj |
| | | * @param propName |
| | | * @return |
| | | */ |
| | | public static String getPropValue(Object obj, String propName) { |
| | | String propValue = ""; |
| | | try { |
| | | if (null != obj) { |
| | | JSONObject jsonObject = StringUtil.toJsonObject(obj); |
| | | if (!StringUtil.isEmptyOrNull(propName)) { |
| | | propValue = jsonObject.getString(propName); |
| | | } |
| | | } |
| | | } catch (Exception e) { |
| | | } |
| | | return propValue; |
| | | } |
| | | |
| | | /** |
| | | * è·å对象çæå®å段çç±»å |
| | | * è¿å屿§çç±»åå符串ï¼å°åï¼ä¾å¦:string/integer/double/dateï¼ |
| | | * |
| | | * @param obj å®ä½ |
| | | * @param propName 屿§å |
| | | * @return ï¼å°åï¼ä¾å¦:string/integer/double/dateï¼ |
| | | */ |
| | | public static String getPropType(Object obj, String propName) { |
| | | String propType = ""; |
| | | try { |
| | | if (null != obj && !StringUtil.isEmptyOrNull(propName)) { |
| | | Field[] fields = obj.getClass().getDeclaredFields(); |
| | | for (Field field : fields) { |
| | | String name = field.getName().toLowerCase(); |
| | | if (name.equals(propName.toLowerCase())) { |
| | | propType = field.getType().getSimpleName().toLowerCase(); |
| | | } |
| | | } |
| | | } |
| | | |
| | | } catch (Exception e) { |
| | | |
| | | } |
| | | return propType; |
| | | } |
| | | |
| | | /** |
| | | * è¡è½¬å |
| | | * @param clazz ç»æç±»ï¼å¦ UserInfo.class |
| | | * @param list æ°æ®é |
| | | * @return |
| | | * @param <T> |
| | | */ |
| | | public static <T> List rowToColumn(Class<T> clazz, List<T> list){ |
| | | List<List> result; |
| | | Field[] fields = clazz.getDeclaredFields(); |
| | | result = new ArrayList<>(fields.length); |
| | | for (int i = 0; i < fields.length; i++) { |
| | | result.add(new ArrayList()); |
| | | } |
| | | for (T t : list) { |
| | | for (int i = 0; i < fields.length; i++) { |
| | | List l = result.get(i); |
| | | Field field = fields[i]; |
| | | field.setAccessible(true); |
| | | try { |
| | | l.add(field.get(t)); |
| | | } catch (IllegalAccessException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | } |
| | | } |
| | | return result; |
| | | } |
| | | } |