⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 propertyutil.java

📁 自己写的一个struts+spring+hibernate测试程序
💻 JAVA
字号:
/*
 * Created on 2006-3-10
 */
package com.common.utils;

import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.util.Iterator;
import java.util.Map;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.ConvertUtils;
import org.apache.commons.beanutils.Converter;
import org.apache.commons.beanutils.DynaBean;
import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.base.UpperCaseDynaBean;


/**
 * <p>Title: </p>
 * <p>Description: property util , It is a wrapper of BeanUtils to some extent </p>
 * <p>Copyright: Copyright (c) 2006</p>
 * <p>Company: </p>
 * @author tommy.zeng
 * @version 1.0
 */
public class PropertyUtil {
	 protected final static Log log = LogFactory.getLog(PropertyUtil.class);
    public static void copyProperty(Object target,String property,final DynaBean bean) {
    	copyProperty(target,property,bean,false);
    }
    
    public static void copyProperty(Object target,String property,final DynaBean bean,boolean upperCase) {
        try {
            BeanUtils.copyProperty(target,property,bean.get(upperCase?property.toUpperCase():property));
        }
        catch(Exception e){
            
        }
    }
    /**
     *copy Property
     * @param target
     * @param orig
     */
    public static void copyProperties(Object dest,Object orig)    
    throws IllegalAccessException, InvocationTargetException 
    {
    	 
    		 //把orig 里面如果是null 则应该转成"";

    	        // Validate existence of the specified beans
    	        if (dest == null) {
    	            throw new IllegalArgumentException
    	                    ("No destination bean specified");
    	        }
    	        if (orig == null) {
    	            throw new IllegalArgumentException("No origin bean specified");
    	        }
    	       

    	        // Copy the properties, converting as necessary
    	        if (orig instanceof DynaBean) {
    	            DynaProperty origDescriptors[] =
    	                ((DynaBean) orig).getDynaClass().getDynaProperties();
    	            for (int i = 0; i < origDescriptors.length; i++) {
    	                String name = origDescriptors[i].getName();
    	                Object value = ((DynaBean) orig).get(name);
    	                copyProperty(dest, name, value);
    	            }
    	        } else if (orig instanceof Map) {
    	            Iterator names = ((Map) orig).keySet().iterator();
    	            while (names.hasNext()) {
    	                String name = (String) names.next();
    	                Object value = ((Map) orig).get(name);
    	                copyProperty(dest, name, value);
    	            }
    	        } else /* if (not a DynaBean or a Map) */ {
    	            PropertyDescriptor origDescriptors[] =
    	                PropertyUtils.getPropertyDescriptors(orig);
    	            for (int i = 0; i < origDescriptors.length; i++) {
    	                if (origDescriptors[i].getReadMethod() == null) {
    	                    if (log.isTraceEnabled()) {
    	                        log.trace("-->No getter on JavaBean for " +
    	                                  origDescriptors[i].getName() + ", skipping");
    	                    }
    	                    continue;
    	                }
    	                String name = origDescriptors[i].getName();
    	                if ("class".equals(name)) {
    	                    continue; // No point in trying to set an object's class
    	                }
    	                Object value = null;
    	                try {
    	                    value = PropertyUtils.getSimpleProperty(orig, name);
    	                } catch (NoSuchMethodException e) {
    	                    log.error("-->Should not have happened", e);
    	                    value = null; // Can not happen
    	                }
    	                log.info(name+"="+value);
    	                if(value!=null)
    	                  copyProperty(dest, name, value);
    	            } 
    	        }

              
       }
        
  
    /**
     * <p>Copy the specified property value to the specified destination bean,
     * performing any type conversion that is required.  If the specified
     * bean does not have a property of the specified name, return without
     * doing anything.  If you have custom destination property types, register
     * {@link Converter}s for them by calling the <code>register()</code>
     * method of {@link ConvertUtils}.</p>
     *
     * <p><strong>FIXME</strong> - Indexed and mapped properties that do not
     * have getter and setter methods for the underlying array or Map are not
     * copied by this method.</p>
     *
     * @param bean Bean on which setting is to be performed
     * @param name Simple property name of the property to be set
     * @param value Property value to be set
     *
     * @exception IllegalAccessException if the caller does not have
     *  access to the property accessor method
     * @exception InvocationTargetException if the property accessor method
     *  throws an exception
     */
    public static void copyProperty(Object bean, String name, Object value)
        throws IllegalAccessException, InvocationTargetException {

        if (log.isTraceEnabled()) {
            StringBuffer sb = new StringBuffer("  copyProperty(");
            sb.append(bean);
            sb.append(", ");
            sb.append(name);
            sb.append(", ");
            if (value == null) {
                sb.append("<NULL>");
            } else if (value instanceof String) {
                sb.append((String) value);
            } else if (value instanceof String[]) {
                String values[] = (String[]) value;
                sb.append('[');
                for (int i = 0; i < values.length; i++) {
                    if (i > 0) {
                        sb.append(',');
                    }
                    sb.append(values[i]);
                }
                sb.append(']');
            } else {
                sb.append(value.toString());
            }
            sb.append(')');
            log.trace(sb.toString());
        }

        if (bean instanceof DynaBean) {
            DynaProperty propDescriptor =
                ((DynaBean) bean).getDynaClass().getDynaProperty(name);
            if (propDescriptor != null) {
                Converter converter =
                    ConvertUtils.lookup(propDescriptor.getType());
                if (converter != null) {
                    value = converter.convert(propDescriptor.getType(), value);
                }
                try {
                    PropertyUtils.setSimpleProperty(bean, name, value);
                } catch (NoSuchMethodException e) {
                    log.error("-->Should not have happened", e);
                    ; // Silently ignored
                }
            } else {
                if (log.isTraceEnabled()) {
                    log.trace("-->No setter on 'to' DynaBean, skipping");
                }
            }
        } else /* if (!(bean instanceof DynaBean)) */ {
            PropertyDescriptor propDescriptor = null;
            try {
                propDescriptor =
                    PropertyUtils.getPropertyDescriptor(bean, name);
            } catch (NoSuchMethodException e) {
                propDescriptor = null;
            }
            if ((propDescriptor != null) &&
                (propDescriptor.getWriteMethod() == null)) {
                propDescriptor = null;
            }
            if (propDescriptor != null) {
                Converter converter =
                    ConvertUtils.lookup(propDescriptor.getPropertyType());
                if (converter != null) {
                    value = converter.convert
                        (propDescriptor.getPropertyType(), value);
                }
                try {
                    PropertyUtils.setSimpleProperty(bean, name, value);
                } catch (NoSuchMethodException e) {
                    log.error("-->Should not have happened", e);
                    ; // Silently ignored
                }
            } else {
                if (log.isTraceEnabled()) {
                    log.trace("-->No setter on JavaBean, skipping");
                }
            }
        }

    }


	/**
     * set all property-keys of a bean to upper case and return a new bean
     * @param bean
     * @return
     */
    public static DynaBean upperCaseProperty(final DynaBean bean) {
        DynaBean newBean = new UpperCaseDynaBean();
        try {
            BeanUtils.copyProperties(newBean,bean);
        }
        catch(Exception e){
            
        }
        return newBean;
    }
    
    
    public static void resetProperty(DynaBean bean) {
        DynaBean newBean = new UpperCaseDynaBean();
        try {
            BeanUtils.copyProperties(newBean,bean);
            BeanUtils.copyProperties(bean,newBean);
        }
        catch(Exception e){
            
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -