📄 xmlintrospectorhelper.java
字号:
* @param updateMethodName the name of the custom updater method to user.
* If null, then then
* @param beanClass the <code>Class</code> from which the update method should be found.
* This may be null only when <code>updateMethodName</code> is also null.
* @since 0.5
* @deprecated 0.6 moved into ElementRule
*/
public static void configureProperty(
ElementDescriptor elementDescriptor,
PropertyDescriptor propertyDescriptor,
String updateMethodName,
Class beanClass ) {
Class type = propertyDescriptor.getPropertyType();
Method readMethod = propertyDescriptor.getReadMethod();
Method writeMethod = propertyDescriptor.getWriteMethod();
elementDescriptor.setLocalName( propertyDescriptor.getName() );
elementDescriptor.setPropertyType( type );
// XXX: associate more bean information with the descriptor?
//nodeDescriptor.setDisplayName( propertyDescriptor.getDisplayName() );
//nodeDescriptor.setShortDescription( propertyDescriptor.getShortDescription() );
if ( readMethod == null ) {
log.trace( "No read method" );
return;
}
if ( log.isTraceEnabled() ) {
log.trace( "Read method=" + readMethod.getName() );
}
// choose response from property type
// XXX: ignore class property ??
if ( Class.class.equals( type ) && "class".equals( propertyDescriptor.getName() ) ) {
log.trace( "Ignoring class property" );
return;
}
if ( isPrimitiveType( type ) ) {
elementDescriptor.setTextExpression( new MethodExpression( readMethod ) );
} else if ( isLoopType( type ) ) {
log.trace("Loop type ??");
// don't wrap this in an extra element as its specified in the
// XML descriptor so no need.
elementDescriptor.setContextExpression(
new IteratorExpression( new MethodExpression( readMethod ) )
);
writeMethod = null;
} else {
log.trace( "Standard property" );
elementDescriptor.setContextExpression( new MethodExpression( readMethod ) );
}
// see if we have a custom method update name
if (updateMethodName == null) {
// set standard write method
if ( writeMethod != null ) {
elementDescriptor.setUpdater( new MethodUpdater( writeMethod ) );
}
} else {
// see if we can find and set the custom method
if ( log.isTraceEnabled() ) {
log.trace( "Finding custom method: " );
log.trace( " on:" + beanClass );
log.trace( " name:" + updateMethodName );
}
Method updateMethod = null;
Method[] methods = beanClass.getMethods();
for ( int i = 0, size = methods.length; i < size; i++ ) {
Method method = methods[i];
if ( updateMethodName.equals( method.getName() ) ) {
// we have a matching name
// check paramters are correct
if (methods[i].getParameterTypes().length == 1) {
// we'll use first match
updateMethod = methods[i];
if ( log.isTraceEnabled() ) {
log.trace("Matched method:" + updateMethod);
}
// done since we're using the first match
break;
}
}
}
if (updateMethod == null) {
if ( log.isInfoEnabled() ) {
log.info("No method with name '" + updateMethodName + "' found for update");
}
} else {
elementDescriptor.setUpdater( new MethodUpdater( updateMethod ) );
elementDescriptor.setSingularPropertyType( updateMethod.getParameterTypes()[0] );
if ( log.isTraceEnabled() ) {
log.trace( "Set custom updater on " + elementDescriptor);
}
}
}
}
/**
* Configure an <code>AttributeDescriptor</code> from a <code>PropertyDescriptor</code>
*
* @param attributeDescriptor configure this <code>AttributeDescriptor</code>
* @param propertyDescriptor configure from this <code>PropertyDescriptor</code>
* @deprecated 0.6 moved into AttributeRule
*/
public static void configureProperty(
AttributeDescriptor attributeDescriptor,
PropertyDescriptor propertyDescriptor ) {
Class type = propertyDescriptor.getPropertyType();
Method readMethod = propertyDescriptor.getReadMethod();
Method writeMethod = propertyDescriptor.getWriteMethod();
if ( readMethod == null ) {
log.trace( "No read method" );
return;
}
if ( log.isTraceEnabled() ) {
log.trace( "Read method=" + readMethod );
}
// choose response from property type
// XXX: ignore class property ??
if ( Class.class.equals( type ) && "class".equals( propertyDescriptor.getName() ) ) {
log.trace( "Ignoring class property" );
return;
}
if ( isLoopType( type ) ) {
log.warn( "Using loop type for an attribute. Type = "
+ type.getName() + " attribute: " + attributeDescriptor.getQualifiedName() );
}
log.trace( "Standard property" );
attributeDescriptor.setTextExpression( new MethodExpression( readMethod ) );
if ( writeMethod != null ) {
attributeDescriptor.setUpdater( new MethodUpdater( writeMethod ) );
}
attributeDescriptor.setLocalName( propertyDescriptor.getName() );
attributeDescriptor.setPropertyType( type );
// XXX: associate more bean information with the descriptor?
//nodeDescriptor.setDisplayName( propertyDescriptor.getDisplayName() );
//nodeDescriptor.setShortDescription( propertyDescriptor.getShortDescription() );
}
/**
* Add any addPropety(PropertyType) methods as Updaters
* which are often used for 1-N relationships in beans.
* <br>
* The tricky part here is finding which ElementDescriptor corresponds
* to the method. e.g. a property 'items' might have an Element descriptor
* which the method addItem() should match to.
* <br>
* So the algorithm we'll use
* by default is to take the decapitalized name of the property being added
* and find the first ElementDescriptor that matches the property starting with
* the string. This should work for most use cases.
* e.g. addChild() would match the children property.
*
* @param introspector use this <code>XMLIntrospector</code> for introspection
* @param rootDescriptor add defaults to this descriptor
* @param beanClass the <code>Class</code> to which descriptor corresponds
* @deprecated 0.6 use the method in XMLIntrospector instead
*/
public static void defaultAddMethods(
XMLIntrospector introspector,
ElementDescriptor rootDescriptor,
Class beanClass ) {
// lets iterate over all methods looking for one of the form
// add*(PropertyType)
if ( beanClass != null ) {
Method[] methods = beanClass.getMethods();
for ( int i = 0, size = methods.length; i < size; i++ ) {
Method method = methods[i];
String name = method.getName();
if ( name.startsWith( "add" ) ) {
// XXX: should we filter out non-void returning methods?
// some beans will return something as a helper
Class[] types = method.getParameterTypes();
if ( types != null) {
if ( log.isTraceEnabled() ) {
log.trace("Searching for match for " + method);
}
if ( ( types.length == 1 ) || types.length == 2 ) {
String propertyName = Introspector.decapitalize( name.substring(3) );
if (propertyName.length() == 0)
continue;
if ( log.isTraceEnabled() ) {
log.trace( name + "->" + propertyName );
}
// now lets try find the ElementDescriptor which displays
// a property which starts with propertyName
// and if so, we'll set a new Updater on it if there
// is not one already
ElementDescriptor descriptor =
findGetCollectionDescriptor(
introspector,
rootDescriptor,
propertyName );
if ( log.isDebugEnabled() ) {
log.debug( "!! " + propertyName + " -> " + descriptor );
log.debug( "!! " + name + " -> "
+ (descriptor!=null?descriptor.getPropertyName():"") );
}
if ( descriptor != null ) {
boolean isMapDescriptor
= Map.class.isAssignableFrom( descriptor.getPropertyType() );
if ( !isMapDescriptor && types.length == 1 ) {
// this may match a standard collection or iteration
log.trace("Matching collection or iteration");
descriptor.setUpdater( new MethodUpdater( method ) );
descriptor.setSingularPropertyType( types[0] );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -