Java Code Examples for org.apache.commons.beanutils.PropertyUtils#describe()

The following examples show how to use org.apache.commons.beanutils.PropertyUtils#describe() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractReportModelGenerater.java    From bdf3 with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
private Object calculateBeanPropertyData(String[] columnNames, Map<String, Object> map) throws Exception {
	Object obj = map.get(columnNames[0]);
	if(obj==null){
		return null;
	}
	if (columnNames.length == 1) {
		return obj;
	} else if (columnNames.length == 2) {
		return BeanUtils.getBeanProperty(obj, columnNames[1]);
	} else {
		Map subMap = PropertyUtils.describe(obj);
		List<String> list = new ArrayList<String>();
		int i = 1;
		while (i < columnNames.length) {
			list.add(columnNames[i]);
			i++;
		}
		String[] array = list.toArray(new String[list.size()]);
		return calculateBeanPropertyData(array, subMap);
	}
}
 
Example 2
Source File: ApacheBeanConvertUtil.java    From MicroCommunity with Apache License 2.0 5 votes vote down vote up
/**
 * bean转换为map对象
 *
 * @param orgBean 原始bean
 * @return map对象
 */
public static Map beanCovertMap(Object orgBean) {
    Map newMap = null;

    try {
        newMap = PropertyUtils.describe(orgBean);
    } catch (Exception e) {
        throw new RuntimeException("bean转换Map失败", e);
    }

    return newMap;
}
 
Example 3
Source File: JsonSerializer.java    From jigsaw-payment with Apache License 2.0 5 votes vote down vote up
@Override
public byte[] serialize(ServiceInstance<RpcPayload> instance)
		throws Exception {

	Map<String, Object> map = PropertyUtils.describe(instance);
	map.put("uriSpec", this.serializeUriSpec(instance.getUriSpec()));
	map.remove("class");
	String jsonStr = this.toJson(map);

	return jsonStr.getBytes();
}
 
Example 4
Source File: Formatter.java    From rice with Educational Community License v2.0 5 votes vote down vote up
/**
 * If an element of the Collection isn't a supported type, assume it's a JavaBean, and format each of its properties. Returns a
 * Map containing the formatted properties keyed by property name.
 */
protected Object formatBean(Object bean) {
    Map properties = null;
    try {
    	// begin Kuali Foundation modification
        properties = PropertyUtils.describe(bean);
        // end Kuali Foundation modification
    }
    catch (Exception e) {
        throw new FormatException("Unable to format values for bean " + bean, e);
    }

    Map formattedVals = new HashMap();
    // begin Kuali Foundation modification
    Iterator propIter = properties.entrySet().iterator();

    while (propIter.hasNext()) {
        Map.Entry entry = (Map.Entry) propIter.next();
        Object value = entry.getValue();
        if (value != null && isSupportedType(value.getClass())) {
            Formatter formatter = getFormatter(value.getClass());
            formattedVals.put(entry.getKey(), formatter.formatForPresentation(value));
        }
    }
    // end Kuali Foundation modification
    return formattedVals;
}
 
Example 5
Source File: CSVUtil.java    From scaffold-cloud with MIT License 4 votes vote down vote up
/**
 * 将查出来的列表按照指定的字段 转换为导出需要的数据集合格式
 * @param fieldNames 指定需要导出的列的字段名
 * @param BeanList 从数据库中查出的实体列表
 * @param convertorMap 字段转义map
 * @return 按照字段名生成的数据集合
 */
public static List<List<Object>> changeBeanlistToDatalist(String[] fieldNames, List<Object> BeanList, Map convertorMap) throws Exception {
    List<List<Object>> dataList = new ArrayList<>();
    for (int i = 0; i < BeanList.size(); i++) {
        Object bean = BeanList.get(i);
        Map<String ,Object> beanMap = PropertyUtils.describe(bean);
        List<Object> bean2data = new ArrayList<>(fieldNames.length);
        for (int j = 0; j < fieldNames.length; j++) {
            // fileName是字段名字
            String fieldName = fieldNames[j];
            // 这个拿出来的是该字段的值
            Object data = beanMap.get(fieldName);
            boolean isDate = false;
            if(data instanceof Date){
                isDate = true;
                // 将时间类型的转换成 yyyy-MM-dd HH:mm:ss格式的文本
                data = DateUtil.format((Date) data, "yyyy-MM-dd HH:mm:ss");
            }
            if(null != convertorMap && null != data){
                Map nidMap = (Map)convertorMap.get(fieldName);
                if(null != nidMap){
                    // 这里 data如果需要转义 需要用convertorMap进行处理
                    data = nidMap.get(data.toString());
                }
            }
            if(null != data && !"".equals(data)){
                // 如果data值是数字组成 并且长度大于12 加上table制表符 避免流水号订单号等被转成数值显示不完全
                final boolean flag =
                        !isDate && (!NumberUtil.isNumber(data.toString()) || data.toString().length() > 12);
                if(flag){
                    data = data.toString() + "\t";
                }
                // csv格式避免单元格中有英文逗号,用双引号引起来,避免存在双引号,则将原先的双引号转成两个双引号
                data = "\"" + data.toString().replaceAll("\"","\"\"") + "\"";
            }
            bean2data.add(null == data ? "" : data);
        }
        dataList.add(bean2data);
    }
    return dataList;
}
 
Example 6
Source File: SqlBuilder.java    From mybatis.flying with Apache License 2.0 4 votes vote down vote up
/**
 * The insert SQL statement is generated from the incoming object
 * 
 * @param object      Object
 * @param flyingModel FlyingModel
 * @return String
 * @throws NoSuchFieldException      Exception
 * @throws IllegalAccessException    Exception
 * @throws InvocationTargetException Exception
 * @throws NoSuchMethodException     Exception
 */
public static String buildInsertSql(Object object, FlyingModel flyingModel)
		throws NoSuchFieldException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
	String ignoreTag = flyingModel.getIgnoreTag();
	KeyHandler keyHandler = flyingModel.getKeyHandler();
	String whiteListTag = flyingModel.getWhiteListTag();
	boolean useWhiteList = whiteListTag == null ? false : true;
	Map<?, ?> dtoFieldMap = PropertyUtils.describe(object);
	TableMapper tableMapper = buildTableMapper(getTableMappedClass(object.getClass()));

	String tableName = tableMapper.getTableName();
	StringBuilder tableSql = new StringBuilder();
	StringBuilder valueSql = new StringBuilder();

	tableSql.append(INSERT_INTO_BLANK).append(tableName).append(BLANK_OPENPAREN);
	valueSql.append(VALUES_OPENPAREN);

	boolean allFieldNull = true;
	boolean uniqueKeyHandled = false;
	for (FieldMapper fieldMapper : tableMapper.getFieldMapperCache().values()) {
		Object value = dtoFieldMap.get(fieldMapper.getFieldName());

		if (value == null && fieldMapper.isHasDelegate()) {
			value = dtoFieldMap.get(fieldMapper.getDelegate().getFieldName());
			fieldMapper = fieldMapper.getDelegate();
		}

		if (!isAble(fieldMapper.isInsertAble(), value == null, useWhiteList, fieldMapper, whiteListTag,
				ignoreTag)) {
			continue;
		}
		allFieldNull = false;
		tableSql.append(fieldMapper.getDbFieldName()).append(COMMA);
		if (((FieldMapper) fieldMapper).isOpVersionLock()) {
			value = 0;
			valueSql.append("'0',");
		} else {
			valueSql.append(POUND_OPENBRACE);
			if (fieldMapper.isForeignKey()) {
				valueSql.append(fieldMapper.getFieldName()).append(DOT).append(fieldMapper.getForeignFieldName());
			} else {
				valueSql.append(fieldMapper.getFieldName());
			}
			valueSql.append(COMMA).append(JDBCTYPE_EQUAL).append(fieldMapper.getJdbcType().toString());
			if (fieldMapper.getTypeHandlerPath() != null) {
				valueSql.append(COMMA_TYPEHANDLER_EQUAL).append(fieldMapper.getTypeHandlerPath());
			}
			if (fieldMapper.isUniqueKey()) {
				uniqueKeyHandled = true;
				if (keyHandler != null) {
					handleInsertSql(keyHandler, valueSql, fieldMapper, object, uniqueKeyHandled, null);
				}
			}
			valueSql.append(CLOSEBRACE_COMMA);
		}
	}
	if (keyHandler != null && !uniqueKeyHandled) {
		FieldMapper temp = tableMapper.getUniqueKeyNames()[0];
		tableSql.append(temp.getDbFieldName()).append(COMMA);
		handleInsertSql(keyHandler, valueSql, temp, object, uniqueKeyHandled, null);
	}
	if (allFieldNull) {
		throw new BuildSqlException(BuildSqlExceptionEnum.NULL_FIELD);
	}

	tableSql.delete(tableSql.lastIndexOf(COMMA), tableSql.lastIndexOf(COMMA) + 1);
	valueSql.delete(valueSql.lastIndexOf(COMMA), valueSql.lastIndexOf(COMMA) + 1);
	return tableSql.append(CLOSEPAREN_BLANK).append(valueSql).append(CLOSEPAREN).toString();
}
 
Example 7
Source File: CustomerMaintenableImpl.java    From kfs with GNU Affero General Public License v3.0 4 votes vote down vote up
private boolean oldAndNewObjectIsEqual(String objectName, Object oldObject, Object newObject) {

        //  if both are null, then they're the same
        if (oldObject == null && newObject == null) {
            return true;
        }

        //  if only one is null, then they're different
        if (oldObject == null || newObject == null) {
            return false;
        }

        //  if they're different classes, then they're different
        if (!oldObject.getClass().getName().equals(newObject.getClass().getName())) {
            return false;
        }

        //  get the list of properties and unconverted values for readable props
        Map<String,Object> oldProps;
        Map<String,Object> newProps;
        try {
            oldProps = PropertyUtils.describe(oldObject);
            newProps = PropertyUtils.describe(newObject);
        }
        catch (Exception e) {
            throw new RuntimeException("Exception raised while trying to get a list of properties on OldCustomer.", e);
        }

        //  compare old to new on all readable properties
        Object oldValue, newValue;
        for (String propName : oldProps.keySet()) {
            oldValue = oldProps.get(propName);
            newValue = newProps.get(propName);

            if (!oldAndNewPropertyIsEqual(propName, oldValue, newValue)) {
                return false;
            }
        }

        //  if we didnt find any differences, then they are the same
        return true;
    }
 
Example 8
Source File: ObjectCodeServiceTest.java    From kfs with GNU Affero General Public License v3.0 4 votes vote down vote up
public void testPropertyUtilsDescribe() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
    ObjectCode objectCode = new ObjectCode();
    Map boProps = PropertyUtils.describe(objectCode);
}
 
Example 9
Source File: DroolsEngineAction.java    From Decision with Apache License 2.0 4 votes vote down vote up
private List<Map<String, Object>> formatDroolsResults(Results results) throws Exception{

        List<Map<String, Object>> outputList = new ArrayList<>();

        for (Object singleResult : results.getResults()) {

            Map<String, Object> outputMap = null;

            try {

                Object propertyObject = PropertyUtils.getProperty(singleResult, "result");
                //outputMap = PropertyUtils.describe(singleResult);
                outputMap = PropertyUtils.describe(propertyObject);

            } catch (IllegalAccessException|InvocationTargetException|NoSuchMethodException e) {
                throw new Exception(e);
            }

            outputList.add(outputMap);

        }

        return outputList;

    }
 
Example 10
Source File: MapUtils.java    From frpMgr with MIT License 2 votes vote down vote up
/**
 * 对象转Map
 * @param object 目标对象
 * @return 转换出来的值类型是原类型
 */
public static Map<String, Object> toNavMap(Object object) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
	return PropertyUtils.describe(object);
}