📄 beansutils.java
字号:
/* * Distributed as part of c3p0 v.0.9.1-pre6 * * Copyright (C) 2005 Machinery For Change, Inc. * * Author: Steve Waldman <swaldman@mchange.com> * * This library is free software; you can redistribute it and/or modify * it under the terms of the GNU Lesser General Public License version 2.1, as * published by the Free Software Foundation. * * This software is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this software; see the file LICENSE. If not, write to the * Free Software Foundation, Inc., 59 Temple Place, Suite 330, * Boston, MA 02111-1307, USA. */package com.mchange.v2.beans;import java.beans.*;import java.lang.reflect.*;import java.util.*;import com.mchange.v2.log.*;import com.mchange.v2.lang.Coerce;public final class BeansUtils{ final static MLogger logger = MLog.getLogger( BeansUtils.class ); final static Object[] EMPTY_ARGS = new Object[0]; public static PropertyEditor findPropertyEditor( PropertyDescriptor pd ) { PropertyEditor out = null; Class editorClass = null; try { editorClass = pd.getPropertyEditorClass(); if (editorClass != null) out = (PropertyEditor) editorClass.newInstance(); } catch (Exception e) {// e.printStackTrace();// System.err.println("WARNING: Bad property editor class " + editorClass.getName() + // " registered for property " + pd.getName()); if (logger.isLoggable( MLevel.WARNING ) ) logger.log(MLevel.WARNING, "Bad property editor class " + editorClass.getName() + " registered for property " + pd.getName(), e); } if ( out == null ) out = PropertyEditorManager.findEditor( pd.getPropertyType() ); return out; } public static boolean equalsByAccessibleProperties( Object bean0, Object bean1 ) throws IntrospectionException { return equalsByAccessibleProperties( bean0, bean1, Collections.EMPTY_SET ); } public static boolean equalsByAccessibleProperties( Object bean0, Object bean1, Collection ignoreProps ) throws IntrospectionException { Map m0 = new HashMap(); Map m1 = new HashMap(); extractAccessiblePropertiesToMap( m0, bean0, ignoreProps ); extractAccessiblePropertiesToMap( m1, bean1, ignoreProps ); //System.err.println("Map0 -> " + m0); //System.err.println("Map1 -> " + m1); return m0.equals(m1); } public static void overwriteAccessibleProperties( Object sourceBean, Object destBean ) throws IntrospectionException { overwriteAccessibleProperties( sourceBean, destBean, Collections.EMPTY_SET ); } public static void overwriteAccessibleProperties( Object sourceBean, Object destBean, Collection ignoreProps ) throws IntrospectionException { try { BeanInfo beanInfo = Introspector.getBeanInfo( sourceBean.getClass(), Object.class ); //so we don't see message about getClass() PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); for( int i = 0, len = pds.length; i < len; ++i) { PropertyDescriptor pd = pds[i]; if ( ignoreProps.contains( pd.getName() ) ) continue; Method getter = pd.getReadMethod(); Method setter = pd.getWriteMethod(); if ( getter == null || setter == null ) { if ( pd instanceof IndexedPropertyDescriptor ) {// System.err.println("WARNING: BeansUtils.overwriteAccessibleProperties() does not");// System.err.println("support indexed properties that do not provide single-valued");// System.err.println("array getters and setters! [The indexed methods provide no means");// System.err.println("of modifying the size of the array in the destination bean if");// System.err.println("it does not match the source.]"); if ( logger.isLoggable( MLevel.WARNING ) ) logger.warning("BeansUtils.overwriteAccessibleProperties() does not" + " support indexed properties that do not provide single-valued" + " array getters and setters! [The indexed methods provide no means" + " of modifying the size of the array in the destination bean if" + " it does not match the source.]"); } //System.err.println("Property inaccessible for overwriting: " + pd.getName()); if (logger.isLoggable( MLevel.INFO )) logger.info("Property inaccessible for overwriting: " + pd.getName()); } else { Object value = getter.invoke( sourceBean, EMPTY_ARGS ); setter.invoke( destBean, new Object[] { value } ); } } } catch ( IntrospectionException e ) { throw e; } catch ( Exception e ) { //e.printStackTrace(); if (Debug.DEBUG && Debug.TRACE >= Debug.TRACE_MED && logger.isLoggable( MLevel.FINE )) logger.log( MLevel.FINE, "Converting exception to throwable IntrospectionException" ); throw new IntrospectionException( e.getMessage() ); } } public static void overwriteAccessiblePropertiesFromMap( Map sourceMap, Object destBean, boolean skip_nulls ) throws IntrospectionException { overwriteAccessiblePropertiesFromMap( sourceMap, destBean, skip_nulls, Collections.EMPTY_SET ); } public static void overwriteAccessiblePropertiesFromMap( Map sourceMap, Object destBean, boolean skip_nulls, Collection ignoreProps ) throws IntrospectionException { overwriteAccessiblePropertiesFromMap( sourceMap, destBean, skip_nulls, ignoreProps, false, MLevel.WARNING, MLevel.WARNING, true); } public static void overwriteAccessiblePropertiesFromMap( Map sourceMap, Object destBean, boolean skip_nulls, Collection ignoreProps, boolean coerce_strings, MLevel cantWriteLevel, MLevel cantCoerceLevel, boolean die_on_one_prop_failure) throws IntrospectionException { if (cantWriteLevel == null) cantWriteLevel = MLevel.WARNING; if (cantCoerceLevel == null) cantCoerceLevel = MLevel.WARNING; Set sourceMapProps = sourceMap.keySet(); String propName = null; BeanInfo beanInfo = Introspector.getBeanInfo( destBean.getClass(), Object.class ); //so we don't see message about getClass() PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); //System.err.println("ignoreProps: " + ignoreProps ); for( int i = 0, len = pds.length; i < len; ++i) { PropertyDescriptor pd = pds[i]; propName = pd.getName(); if (! sourceMapProps.contains( propName )) continue; if ( ignoreProps != null && ignoreProps.contains( propName ) ) { //System.err.println("ignoring: " + propName); continue; } //else // System.err.println("not ignoring: " + propName); Object propVal = sourceMap.get( propName ); if (propVal == null) { if (skip_nulls) continue; //do we need to worry about primitives here? } Method setter = pd.getWriteMethod(); boolean rethrow = false; Class propType = pd.getPropertyType();;// try// { if ( setter == null ) { if ( pd instanceof IndexedPropertyDescriptor ) { if ( logger.isLoggable( MLevel.FINER ) ) logger.finer("BeansUtils.overwriteAccessiblePropertiesFromMap() does not" + " support indexed properties that do not provide single-valued" + " array getters and setters! [The indexed methods provide no means" + " of modifying the size of the array in the destination bean if" + " it does not match the source.]"); } if ( logger.isLoggable( cantWriteLevel )) { String msg = "Property inaccessible for overwriting: " + propName; logger.log( cantWriteLevel, msg ); if (die_on_one_prop_failure) { rethrow = true; throw new IntrospectionException( msg ); } } } else { if (coerce_strings && propVal != null && propVal.getClass() == String.class && (propType = pd.getPropertyType()) != String.class && Coerce.canCoerce( propType )) { Object coercedPropVal; try { coercedPropVal = Coerce.toObject( (String) propVal, propType ); System.err.println(propName + "-> coercedPropVal: " + coercedPropVal); setter.invoke( destBean, new Object[] { coercedPropVal } );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -