package com.peyed.util;import java.beans.PropertyDescriptor;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.peyed.model.BaseObject;
import com.peyed.model.LabelValue;
import org.springframework.aop.support.AopUtils;
/**
* Utility class to convert one object to another.
*
* <p>
* <a href="ConvertUtil.java.html"><i>View Source</i></a>
* </p>
*
* @author <a href="mailto:[email protected]">Matt Raible</a>
*/
public final class ConvertUtil {
//~ Static fields/initializers ============================================= private static Log log = LogFactory.getLog(ConvertUtil.class); //~ Methods ================================================================ /**
* Method to convert a ResourceBundle to a Map object.
* @param rb a given resource bundle
* @return Map a populated map
*/
public static Map convertBundleToMap(ResourceBundle rb) {
Map map = new HashMap(); for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
String key = (String) keys.nextElement();
map.put(key, rb.getString(key));
} return map;
}
public static Map convertListToMap(List list) {
Map map = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
LabelValue option = (LabelValue) it.next();
map.put(option.getLabel(), option.getValue());
}
return map;
} /**
* Method to convert a ResourceBundle to a Properties object.
* @param rb a given resource bundle
* @return Properties a populated properties object
*/
public static Properties convertBundleToProperties(ResourceBundle rb) {
Properties props = new Properties(); for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
String key = (String) keys.nextElement();
props.put(key, rb.getString(key));
} return props;
} /**
* Convenience method used by tests to populate an object from a
* ResourceBundle
* @param obj an initialized object
* @param rb a resource bundle
* @return a populated object
*/
public static Object populateObject(Object obj, ResourceBundle rb) {
try {
Map map = convertBundleToMap(rb); BeanUtils.copyProperties(obj, map);
} catch (Exception e) {
e.printStackTrace();
log.error("Exception occured populating object: " + e.getMessage());
} return obj;
} /**
* This method inspects a POJO or Form and figures out its pojo/form
* equivalent.
*
* @param o the object to inspect
* @return the Class of the persistable object
* @throws ClassNotFoundException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public static Object getOpposingObject(Object o) throws ClassNotFoundException,
InstantiationException,
IllegalAccessException {
String name = o.getClass().getName();
log.debug("o.getClass().getName():"+name); if (name.lastIndexOf("Form")==-1) {
if (log.isDebugEnabled()) {
log.debug("getting form equivalent of pojo...");
} name = StringUtils.replace(name, ".model.", ".form.");
if (AopUtils.isCglibProxy(o)) {
name = name.substring(0, name.indexOf("$$"));
}
name += "Form";
} else {
if (log.isDebugEnabled()) {
log.debug("getting pojo equivalent of form...");
}
name = StringUtils.replace(name, ".form.", ".model.");
name = name.substring(0, name.lastIndexOf("Form"));
}
Class obj = Class.forName(name); if (log.isDebugEnabled()) {
log.debug("returning className: " + obj.getName());
} return obj.newInstance();
} /**
* Convenience method to convert a form to a POJO and back again
*
* @param o the object to tranfer properties from
* @return converted object
*/
public static Object convert(Object o) throws Exception {
if (o == null) {
return null;
}
Object target = getOpposingObject(o);
log.debug("target:"+target);
BeanUtils.copyProperties(target, o);
return target;
} /**
* Convenience method to convert Lists (in a Form) from POJOs to Forms.
* Also checks for and formats dates.
*
* @param o
* @return Object with converted lists
* @throws Exception
*/
public static Object convertLists(Object o) throws Exception {
if (o == null) {
return null;
} Object target = null; PropertyDescriptor[] origDescriptors =
PropertyUtils.getPropertyDescriptors(o); for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName(); if (origDescriptors[i].getPropertyType().equals(List.class)) {
List list = (List) PropertyUtils.getProperty(o, name);
for (int j=0; j < list.size(); j++) {
Object origin = list.get(j);
target = convert(origin);
list.set(j, target);
}
PropertyUtils.setProperty(o, name, list);
}
}
return o;
}
}
那个大哥能给我讲讲 public static Object getOpposingObject(Object o) throws ClassNotFoundException,
InstantiationException,
IllegalAccessException这个方法啊,兄弟有些看不懂
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.ResourceBundle;import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import com.peyed.model.BaseObject;
import com.peyed.model.LabelValue;
import org.springframework.aop.support.AopUtils;
/**
* Utility class to convert one object to another.
*
* <p>
* <a href="ConvertUtil.java.html"><i>View Source</i></a>
* </p>
*
* @author <a href="mailto:[email protected]">Matt Raible</a>
*/
public final class ConvertUtil {
//~ Static fields/initializers ============================================= private static Log log = LogFactory.getLog(ConvertUtil.class); //~ Methods ================================================================ /**
* Method to convert a ResourceBundle to a Map object.
* @param rb a given resource bundle
* @return Map a populated map
*/
public static Map convertBundleToMap(ResourceBundle rb) {
Map map = new HashMap(); for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
String key = (String) keys.nextElement();
map.put(key, rb.getString(key));
} return map;
}
public static Map convertListToMap(List list) {
Map map = new LinkedHashMap();
for (Iterator it = list.iterator(); it.hasNext();) {
LabelValue option = (LabelValue) it.next();
map.put(option.getLabel(), option.getValue());
}
return map;
} /**
* Method to convert a ResourceBundle to a Properties object.
* @param rb a given resource bundle
* @return Properties a populated properties object
*/
public static Properties convertBundleToProperties(ResourceBundle rb) {
Properties props = new Properties(); for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) {
String key = (String) keys.nextElement();
props.put(key, rb.getString(key));
} return props;
} /**
* Convenience method used by tests to populate an object from a
* ResourceBundle
* @param obj an initialized object
* @param rb a resource bundle
* @return a populated object
*/
public static Object populateObject(Object obj, ResourceBundle rb) {
try {
Map map = convertBundleToMap(rb); BeanUtils.copyProperties(obj, map);
} catch (Exception e) {
e.printStackTrace();
log.error("Exception occured populating object: " + e.getMessage());
} return obj;
} /**
* This method inspects a POJO or Form and figures out its pojo/form
* equivalent.
*
* @param o the object to inspect
* @return the Class of the persistable object
* @throws ClassNotFoundException
* @throws InstantiationException
* @throws IllegalAccessException
*/
public static Object getOpposingObject(Object o) throws ClassNotFoundException,
InstantiationException,
IllegalAccessException {
String name = o.getClass().getName();
log.debug("o.getClass().getName():"+name); if (name.lastIndexOf("Form")==-1) {
if (log.isDebugEnabled()) {
log.debug("getting form equivalent of pojo...");
} name = StringUtils.replace(name, ".model.", ".form.");
if (AopUtils.isCglibProxy(o)) {
name = name.substring(0, name.indexOf("$$"));
}
name += "Form";
} else {
if (log.isDebugEnabled()) {
log.debug("getting pojo equivalent of form...");
}
name = StringUtils.replace(name, ".form.", ".model.");
name = name.substring(0, name.lastIndexOf("Form"));
}
Class obj = Class.forName(name); if (log.isDebugEnabled()) {
log.debug("returning className: " + obj.getName());
} return obj.newInstance();
} /**
* Convenience method to convert a form to a POJO and back again
*
* @param o the object to tranfer properties from
* @return converted object
*/
public static Object convert(Object o) throws Exception {
if (o == null) {
return null;
}
Object target = getOpposingObject(o);
log.debug("target:"+target);
BeanUtils.copyProperties(target, o);
return target;
} /**
* Convenience method to convert Lists (in a Form) from POJOs to Forms.
* Also checks for and formats dates.
*
* @param o
* @return Object with converted lists
* @throws Exception
*/
public static Object convertLists(Object o) throws Exception {
if (o == null) {
return null;
} Object target = null; PropertyDescriptor[] origDescriptors =
PropertyUtils.getPropertyDescriptors(o); for (int i = 0; i < origDescriptors.length; i++) {
String name = origDescriptors[i].getName(); if (origDescriptors[i].getPropertyType().equals(List.class)) {
List list = (List) PropertyUtils.getProperty(o, name);
for (int j=0; j < list.size(); j++) {
Object origin = list.get(j);
target = convert(origin);
list.set(j, target);
}
PropertyUtils.setProperty(o, name, list);
}
}
return o;
}
}
那个大哥能给我讲讲 public static Object getOpposingObject(Object o) throws ClassNotFoundException,
InstantiationException,
IllegalAccessException这个方法啊,兄弟有些看不懂
解决方案 »
- 项目导出到Project中,乱码问题
- WEB系统的Filter
- org.apache.commons.httpclient.HttpMethodDirector executeWithRetry的问题
- 不知道struts怎么想的,还是关于传多个参数的问题
- 要尽快达到这个标准,哪位能给指个路?
- 我很想问大家, 程序员的路在哪里?
- Servlet实现单个文件上传实现
- 在Jb7+Jboss下建了一个CMP实体Bean,对应的表中ID字段是自动增量,插记录。。。。
- 请熟悉者谈谈VisualAge开发EJB与Weblogic的配合使用的情况
- 查询列表的数据 有个打印按钮 如何点击一下打印按钮 就能打印数据 程序代码如何弄呢 大家以前碰到过打印吗
- hibernate如何實現一個對象映射數據表的多行
- (急)求教:用webservice建一个工程,然后从MySQL数据库的一个表test中取出所有的数据,在jsp页面上显示
当然这些都是根据传进去的对象名字来说的