📄 beansutils.java
字号:
} catch (IllegalArgumentException e) { // thrown by Coerce.toObject() // recall that NumberFormatException inherits from IllegalArgumentException String msg = "Failed to coerce property: " + propName + " [propVal: " + propVal + "; propType: " + propType + "]"; if ( logger.isLoggable( cantCoerceLevel ) ) logger.log( cantCoerceLevel, msg, e ); if (die_on_one_prop_failure) { rethrow = true; throw new IntrospectionException( msg ); } } catch (Exception e) { String msg = "Failed to set property: " + propName + " [propVal: " + propVal + "; propType: " + propType + "]"; if ( logger.isLoggable( cantWriteLevel ) ) logger.log( cantWriteLevel, msg, e ); if (die_on_one_prop_failure) { rethrow = true; throw new IntrospectionException( msg ); } } } else { try { //System.err.println("invoking method: " + setter); setter.invoke( destBean, new Object[] { propVal } ); } catch (Exception e) { String msg = "Failed to set property: " + propName + " [propVal: " + propVal + "; propType: " + propType + "]"; if ( logger.isLoggable( cantWriteLevel ) ) logger.log( cantWriteLevel, msg, e ); if (die_on_one_prop_failure) { rethrow = true; throw new IntrospectionException( msg ); } } } }// }// catch (Exception e)// {// if (e instanceof IntrospectionException && rethrow)// throw (IntrospectionException) e;// else// {// String msg = // "An exception occurred while trying to set property '" + propName +// "' to value '" + propVal + "'. ";// logger.log(MLevel.WARNING, msg, e);// if (die_on_one_prop_failure)// {// rethrow = true;// throw new IntrospectionException( msg + e.toString());// }// }// } } } public static void appendPropNamesAndValues(StringBuffer appendIntoMe, Object bean, Collection ignoreProps) throws IntrospectionException { Map tmp = new TreeMap( String.CASE_INSENSITIVE_ORDER ); extractAccessiblePropertiesToMap( tmp, bean, ignoreProps ); boolean first = true; for (Iterator ii = tmp.keySet().iterator(); ii.hasNext(); ) { String key = (String) ii.next(); Object val = tmp.get( key ); if (first) first = false; else appendIntoMe.append( ", " ); appendIntoMe.append( key ); appendIntoMe.append( " -> "); appendIntoMe.append( val ); } } public static void extractAccessiblePropertiesToMap( Map fillMe, Object bean ) throws IntrospectionException { extractAccessiblePropertiesToMap( fillMe, bean, Collections.EMPTY_SET ); } public static void extractAccessiblePropertiesToMap( Map fillMe, Object bean, Collection ignoreProps ) throws IntrospectionException { String propName = null; try { BeanInfo bi = Introspector.getBeanInfo( bean.getClass(), Object.class ); PropertyDescriptor[] pds = bi.getPropertyDescriptors(); for (int i = 0, len = pds.length; i < len; ++i) { PropertyDescriptor pd = pds[i]; propName = pd.getName(); if (ignoreProps.contains( propName )) continue; Method readMethod = pd.getReadMethod(); Object propVal = readMethod.invoke( bean, EMPTY_ARGS ); fillMe.put( propName, propVal ); } } catch ( IntrospectionException e ) {// if (propName != null)// System.err.println("Problem occurred while overwriting property: " + propName); if ( logger.isLoggable( MLevel.WARNING ) ) logger.warning("Problem occurred while overwriting property: " + propName); if (Debug.DEBUG && Debug.TRACE >= Debug.TRACE_MED && logger.isLoggable( MLevel.FINE )) logger.logp( MLevel.FINE, BeansUtils.class.getName(), "extractAccessiblePropertiesToMap( Map fillMe, Object bean, Collection ignoreProps )", (propName != null ? "Problem occurred while overwriting property: " + propName : "") + " throwing...", e ); throw e; } catch ( Exception e ) { //e.printStackTrace(); if (Debug.DEBUG && Debug.TRACE >= Debug.TRACE_MED && logger.isLoggable( MLevel.FINE )) logger.logp( MLevel.FINE, BeansUtils.class.getName(), "extractAccessiblePropertiesToMap( Map fillMe, Object bean, Collection ignoreProps )", "Caught unexpected Exception; Converting to IntrospectionException.", e ); throw new IntrospectionException( e.toString() + (propName == null ? "" : " [" + propName + ']') ); } } private static void overwriteProperty( String propName, Object value, Method putativeSetter, Object target ) throws Exception { if ( putativeSetter.getDeclaringClass().isAssignableFrom( target.getClass() ) ) putativeSetter.invoke( target, new Object[] { value } ); else { BeanInfo beanInfo = Introspector.getBeanInfo( target.getClass(), Object.class ); PropertyDescriptor pd = null; PropertyDescriptor[] pds = beanInfo.getPropertyDescriptors(); for( int i = 0, len = pds.length; i < len; ++i) if (propName.equals( pds[i].getName() )) { pd = pds[i]; break; } Method targetSetter = pd.getWriteMethod(); targetSetter.invoke( target, new Object[] { value } ); } } public static void overwriteSpecificAccessibleProperties( Object sourceBean, Object destBean, Collection props ) throws IntrospectionException { try { Set _props = new HashSet(props); 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]; String name = pd.getName(); if (! _props.remove( name ) ) 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.]"); } if ( logger.isLoggable( MLevel.INFO ) ) logger.info("Property inaccessible for overwriting: " + pd.getName()); } else { Object value = getter.invoke( sourceBean, EMPTY_ARGS ); overwriteProperty( name, value, setter, destBean ); //setter.invoke( destBean, new Object[] { value } ); } } if ( logger.isLoggable( MLevel.WARNING ) ) { for (Iterator ii = _props.iterator(); ii.hasNext(); ) logger.warning("failed to find expected property: " + ii.next()); //System.err.println("failed to find expected property: " + ii.next()); } } catch ( IntrospectionException e ) { throw e; } catch ( Exception e ) { //e.printStackTrace(); if (Debug.DEBUG && Debug.TRACE >= Debug.TRACE_MED && logger.isLoggable( MLevel.FINE )) logger.logp( MLevel.FINE, BeansUtils.class.getName(), "overwriteSpecificAccessibleProperties( Object sourceBean, Object destBean, Collection props )", "Caught unexpected Exception; Converting to IntrospectionException.", e ); throw new IntrospectionException( e.getMessage() ); } } public static void debugShowPropertyChange( PropertyChangeEvent evt ) { System.err.println("PropertyChangeEvent: [ propertyName -> " + evt.getPropertyName() + ", oldValue -> " + evt.getOldValue() + ", newValue -> " + evt.getNewValue() + " ]"); } private BeansUtils() {}}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -