Get all Visible property values for an entity
The following code provides two implementations, one based on list and one based on map; List-based implementations are more memory-efficient and more effective; If you have other special needs, you can use the following code to extend the actual reference, perhaps there is a need for map of the situation!
Of course, the use of beanutils.describe or propertyutils.describe one or two sentence code is done, but also need to add additional package reference, and the efficiency is not higher? I haven't done a test.
/** * Get all Visible property values for an entity * @param An instance of the object entity class * @return Returns an object array that contains all the visible property values of the entity */Public object[] Getfieldvalues (ObjectObject) {if( Object = = null) return NULL; field[] Fields =Object. GetClass (). Getdeclaredfields (); List<object> fieldvaluelist =NewArraylist<object> ();//map<string, object> fieldvaluemap = new hashmap<string, object> (); Try{ for(Field f:fields) {if(F.getmodifiers () >2) {Continue; } Fieldvaluelist.add (F.get (Object));//fieldvaluemap.put (F.getname (), F.get (object));} }Catch(IllegalArgumentException e) {E.printstacktrace (); }Catch(Illegalaccessexception e) {E.printstacktrace (); }returnFieldvaluelist.toarray ();//return fieldvaluemap.values (). ToArray ();}
In this example, using Getdeclaredfields, you can refer to the difference between getfields, according to the actual need to decide which one to use;
Beanutils.describe implementation of the function of this example
BeanUtils.describe(object).values().toArray();
After testing, this method is not the same as the result returned by this example code;
The difference between getfields () and Getdeclaredfields ()
GetFields () Gets all of the common (public) fields of a class, including the parent class.
Getdeclaredfields () Obtains all the declared fields of a class, that is, public, private, and proteced, but does not include the declaration field of the parent class.
Similarly, there are getconstructors () and Getdeclaredconstructors (), GetMethods () and Getdeclaredmethods ().
The difference between Beanutils.describe and Propertyutils.describe
These two methods basically implement the same principle, the internal code is much the same, but when reading a property of a bean is called the Getnestedproperty (bean, name) method of a class.
In fact, the main difference is in this method.
Propertyutils is the method that invokes the Propertyutilsbean, which reads the property value (called the GetXXX method), and value does not convert
Instead, Beanutils calls this getnestedproperty method of Beanutilsbean, which is called again after the value is read (Getconvertutils (). Convert (value)); The type is converted into a string.
So the value of the map that Beanutils.describe was converted to is a string
Beanutils.describe Method implementation
PublicMapdescribe(Object Paramobject)throwsIllegalaccessexception, InvocationTargetException, nosuchmethodexception {if(Paramobject = =NULL)return NewHashMap ();if( This. log.isdebugenabled ()) This. Log.debug ("describing Bean:"+ Paramobject.getclass (). GetName ()); HashMap Localhashmap =NewHashMap (); Object Localobject;if(ParamobjectinstanceofDynabean) {Localobject = ((Dynabean) paramobject). Getdynaclass (). Getdynaproperties (); for(inti =0; i < localobject.length; ++i) {String str1 = Localobject[i].getname (); Localhashmap.put (str1, GetProperty (Paramobject, str1)); } }Else{localobject = Getpropertyutils (). Getpropertydescriptors (Paramobject); Class Localclass = Paramobject.getclass (); for(intj =0; J < Localobject.length; ++J) {String str2 = Localobject[j].getname ();if(Getpropertyutils (). Getreadmethod (Localclass, localobject[j]) = =NULL)Continue; Localhashmap.put (str2, GetProperty (Paramobject, str2)); } }return(MAP) Localhashmap; }
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Java reflection gets all visible property values for an entity, returning an object array