public static Map<String, Object> getValueByIgnoreProperty(Object targetObj, String... ignoreProperties) { PropertyDescriptor[] targetPds = getPropertyDescriptors(targetObj.getClass()); List<String> ignoreList = (ignoreProperties != null ? Arrays.asList(ignoreProperties) : null); //非空属性map集合 Map<String, Object> resultMap = new HashMap<>(); for (PropertyDescriptor targetPd : targetPds) { Method readMethod = targetPd.getReadMethod(); if (readMethod != null && (ignoreList == null || !ignoreList.contains(targetPd.getName()))) { try { if (!Modifier.isPublic(readMethod.getDeclaringClass().getModifiers())) { readMethod.setAccessible(true); } Object value = readMethod.invoke(targetObj); if (value == null) { continue; } if (value instanceof String && "".equals(value)){ continue; } if (value instanceof Long || value instanceof Integer || value instanceof String) { resultMap.put(targetPd.getName(), value); } } catch (Throwable ex) { throw new FatalBeanException("Could not check property '" + targetPd.getName() + "'", ex); } } } return resultMap; }
反射获取属性值
最新推荐文章于 2023-04-26 13:23:16 发布