Groovy - Get BeanInfo Props for name, type and value


/ Published in: Groovy
Save to your folder(s)

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


Copy this code and paste it in your HTML
  1. def getBeanInfoProps( aObj = null, aClass = null ) {
  2.  
  3. if ( aClass == null ) {
  4. aClass = aObj?.getClass();
  5. }
  6. assert( [aObj, aClass].count(null) != 2 );
  7.  
  8. def map = [:];
  9.  
  10. def lst = java.beans.Introspector.getBeanInfo( aClass ).propertyDescriptors.collect { it }
  11. lst.eachWithIndex { itr, idx ->
  12. String theName = "${itr?.getReadMethod()?.name}".trim();
  13. if (theName.size() >= 3) {
  14. def startIdx = theName.startsWith('is') ? 2 : 3 ; // is or get
  15. def propNameWrongCase = theName.substring( startIdx )
  16. def propName = java.beans.Introspector.decapitalize(propNameWrongCase);
  17.  
  18. def nestedMap = [:]
  19. map[ propName ] = nestedMap;
  20.  
  21. nestedMap.name = propName;
  22. nestedMap.type = itr.getPropertyType();
  23. nestedMap.value = (aObj == null) ? null : aObj[ propName ] ;
  24. }
  25. }
  26. // println map.values().join('\n')
  27. return map;
  28. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.