Because the project needs to be used, it simply encapsulates some common operations:
[Java]View Plaincopy
- /**
- * Get property values based on property name
- * */
- private Object Getfieldvaluebyname (String fieldName, object o) {
- try {
- String firstletter = fieldname.substring (0, 1). toUpperCase ();
- String getter = "Get" + Firstletter + fieldname.substring (1);
- method = O.getclass (). GetMethod (getter, new class[] {});
- Object value = Method.invoke (o, new object[] {});
- return value;
- } catch (Exception e) {
- Log.error (E.getmessage (), E);
- return null;
- }
- }
- /**
- * Get attribute an array group
- * */
- Private string[] Getfiledname (Object o) {
- Field[] Fields=o.getclass (). Getdeclaredfields ();
- String[] fieldnames=new string[fields.length];
- For (int i=0;i<fields.length;i++) {
- System.out.println (Fields[i].gettype ());
- Fieldnames[i]=fields[i].getname ();
- }
- return fieldnames;
- }
- /**
- * Gets the attribute type (type), the property name (name), and the property value of the map consisting of the list
- * */
- private List getfiledsinfo (Object o) {
- Field[] Fields=o.getclass (). Getdeclaredfields ();
- String[] fieldnames=new string[fields.length];
- List List = new ArrayList ();
- Map infomap=null;
- For (int i=0;i<fields.length;i++) {
- Infomap = new HashMap ();
- Infomap.put ("type", Fields[i].gettype (). toString ());
- Infomap.put ("name", Fields[i].getname ());
- Infomap.put ("value", Getfieldvaluebyname (Fields[i].getname (), O));
- List.add (INFOMAP);
- }
- return list;
- }
- /**
- * Gets all property values of an object, returns an array of objects
- * */
- Public object[] getfiledvalues (Object o) {
- String[] fieldnames=this.getfiledname (o);
- Object[] value=new object[fieldnames.length];
- For (int i=0;i<fieldnames.length;i++) {
- value[i]=This.getfieldvaluebyname (fieldnames[i], O);
- }
- return value;
- }
Java Gets the object property type, property name, property value