Return to Snippet

Revision: 63320
at April 25, 2013 10:58 by laurenceosx


Initial Code
def getBeanInfoProps( aObj = null, aClass = null ) {

	if ( aClass == null ) { 
		aClass = aObj?.getClass(); 
	}
	assert( [aObj, aClass].count(null) != 2 );
	
	def map = [:];

	def lst = java.beans.Introspector.getBeanInfo( aClass ).propertyDescriptors.collect { it }
	lst.eachWithIndex { itr, idx -> 
		String theName = "${itr?.getReadMethod()?.name}".trim();
		if (theName.size() >= 3) {
			def startIdx          = theName.startsWith('is') ? 2 : 3 ; // is or get
			def propNameWrongCase = theName.substring( startIdx )
			def propName          = java.beans.Introspector.decapitalize(propNameWrongCase);
			
			def nestedMap   = [:]
			map[ propName ] = nestedMap;
			
			nestedMap.name  = propName;
			nestedMap.type  = itr.getPropertyType();
			nestedMap.value = (aObj == null) ? null : aObj[ propName ] ;
		}
	}
	// println map.values().join('\n')
	return map;
}

Initial URL

                                

Initial Description
Get obj props using java.beans.Introspector.getBeanInfo()

Initial Title
Groovy - Get BeanInfo Props for name, type and value

Initial Tags
groovy

Initial Language
Groovy