这是我同时设计的一种快速/肮脏的方法,用于将 DynaBeans 转换为 Map,其中 (String) 键被强制为小写:
private Map asLowerCaseIndexedMap(DynaBean bean) {
Map lowerCaseIndexedMap = new HashMap();
DynaProperty[] dynaProperties = bean.getDynaClass().getDynaProperties();
for (int i=0, n=dynaProperties.length; i<n; i++) {
String propertyName = dynaProperties[i].getName();
lowerCaseIndexedMap.put(propertyName.toLowerCase(), bean.get(propertyName));
}
return lowerCaseIndexedMap;
}
不幸的是,这迫使我跳过其他障碍。而我一直在处理这些豆子:
public void header(List<DynaBean> headerColumns) throws WriteException {
for (DynaBean column : headerColumns) {
int col = (Integer)column.get("columnNumber");
int width = (Integer)column.get("columnWidth");
//etcetera
现在我必须这样做:
public void header(List<DynaBean> headerColumns) throws WriteException {
for (DynaBean column : headerColumns) {
/*
* RowSetDynaClass returns beans with lower case properties by default
* but this is not guaranteed if beans are coming from Hibernate.
*
* Converting from the bean to a map however loses DynaBeans' inherent
* type conversions, which in turn entails the converting back and forth
* from String, to BigDecimal, to int as seen below
*/
Map columnMap = asLowerCaseIndexedMap(column);
int col = (new BigDecimal(columnMap.get("columnnbr").toString())).intValue();
int width = (new BigDecimal(columnMap.get("columnwdth").toString())).intValue();