Java Gets the object property type, property name, property value

Source: Internet
Author: User

Because the project needs to be used, it simply encapsulates some common operations:

[Java]View Plaincopy
  1. /**
  2. * Get property values based on property name
  3. * */
  4. private Object Getfieldvaluebyname (String fieldName, object o) {
  5. try {
  6. String firstletter = fieldname.substring (0, 1). toUpperCase ();
  7. String getter = "Get" + Firstletter + fieldname.substring (1);
  8. method = O.getclass (). GetMethod (getter, new class[] {});
  9. Object value = Method.invoke (o, new object[] {});
  10. return value;
  11. } catch (Exception e) {
  12. Log.error (E.getmessage (), E);
  13. return null;
  14. }
  15. }
  16. /** 
  17. * Get attribute an array group
  18. * */
  19. Private string[] Getfiledname (Object o) {
  20. Field[] Fields=o.getclass (). Getdeclaredfields ();
  21. String[] fieldnames=new string[fields.length];
  22. For (int i=0;i<fields.length;i++) {
  23. System.out.println (Fields[i].gettype ());
  24. Fieldnames[i]=fields[i].getname ();
  25. }
  26. return fieldnames;
  27. }
  28. /** 
  29. * Gets the attribute type (type), the property name (name), and the property value of the map consisting of the list
  30. * */
  31. private List getfiledsinfo (Object o) {
  32. Field[] Fields=o.getclass (). Getdeclaredfields ();
  33. String[] fieldnames=new string[fields.length];
  34. List List = new ArrayList ();
  35. Map infomap=null;
  36. For (int i=0;i<fields.length;i++) {
  37. Infomap = new HashMap ();
  38. Infomap.put ("type", Fields[i].gettype (). toString ());
  39. Infomap.put ("name", Fields[i].getname ());
  40. Infomap.put ("value", Getfieldvaluebyname (Fields[i].getname (), O));
  41. List.add (INFOMAP);
  42. }
  43. return list;
  44. }
  45. /** 
  46. * Gets all property values of an object, returns an array of objects
  47. * */
  48. Public object[] getfiledvalues (Object o) {
  49. String[] fieldnames=this.getfiledname (o);
  50. Object[] value=new object[fieldnames.length];
  51. For (int i=0;i<fieldnames.length;i++) {
  52. value[i]=This.getfieldvaluebyname (fieldnames[i], O);
  53. }
  54. return value;
  55. }

Java Gets the object property type, property name, property value

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.