java - Concatenate getters results with reflection -


as user requirement have extract data few different excel files , each of them concatenate values (for each row, converted strings needed) save result db. have mapped excel files different bean (pojo) each of them. call every getter reflection , , single method, handle of them (and future modifications!) without having manually invoke each getter (easy, error-prone , requesting code change every excel file modification). i've got far :

public string getconcatenatedvalues(class beanclass, object mybean) throws introspectionexception, illegalargumentexception, illegalaccessexception, invocationtargetexception{      string output = "";      for(propertydescriptor propertydescriptor : introspector.getbeaninfo(beanclass, beanbase.class).getpropertydescriptors()){          method mymethod = propertydescriptor.getreadmethod();         object resultfromgetter = mymethod.invoke(mybean); //missing logic here          output += resultfromgetter.tostring();     }       return output; } 

basebean here class every bean extends of not want call getters. i'm interested in beanclass getters. i'd need dynamic cast class returned mymethod.getreturntype() able call tostring() method of correct class. correct approach? doable reflection alone?

have @ apache commons beanutils seems fit requierment. specifically, propertyutils.describe() gives complete list of getters given pojo, while propertyutils.getsimpleproperty(pojo, propname) give value of specified property (as object), handling reflection calls internally. of course there propertyutils.getpropertytype(pojo, propname) if need


Comments

Popular posts from this blog

javascript - Google App Script ContentService downloadAsFile not working -

javascript - Function overwritting -

php - Find a regex to take part of Email -