Java reflection mechanism--get property values based on property name

Source: Internet
Author: User
Tags object object

1. Consider the properties in the security access scope, no access to the property is not read [Java]View PlainCopy
  1. /**
  2. * Get property values based on property name
  3. *
  4. * @param fieldName
  5. * @param Object
  6. * @return
  7. */
  8. private String Getfieldvaluebyfieldname (string FieldName, Object object) {
  9. try {
  10. Field field = Object.getclass (). GetField (FieldName);
  11. //Set access permissions for objects, guaranteeing access to private properties
  12. return (String) Field.get (object);
  13. } catch (Exception e) {
  14. return null;
  15. }
  16. }
2. Regardless of the inherited attributes from the ancestor class, get only the current class properties, including the four class access permissions, Private,protect,default,public [Java]View PlainCopy
  1. /**
  2. * Get property values based on property name
  3. *
  4. * @param fieldName
  5. * @param Object
  6. * @return
  7. */
  8. private String Getfieldvaluebyfieldname (string FieldName, Object object) {
  9. try {
  10. Field field = Object.getclass (). Getdeclaredfield (FieldName);
  11. //Set access permissions for objects, guaranteeing access to private properties
  12. Field.setaccessible (true);
  13. return (String) Field.get (object);
  14. } catch (Exception e) {
  15. return null;
  16. }
  17. }

3. Consider the inherited properties of the parent class, including the four class access rights, Private,protect,default,public

[Java]View PlainCopy
    1. /**
    2. * Get attribute elements based on attribute name, including various security scopes and all parent classes
    3. *
    4. * @param fieldName
    5. * @param Object
    6. * @return
    7. */
    8. Private Field getfieldbyclasss (String fieldName, Object object) {
    9. Field field = null;
    10. Class<?> clazz = Object.getclass ();
    11. For (; Clazz! = Object.  Class Clazz = Clazz.getsuperclass ()) {
    12. try {
    13. field = Clazz.getdeclaredfield (FieldName);
    14. } catch (Exception e) {
    15. //There is nothing to be thrown out here.
    16. //If the exception is printed or thrown outward, it will not enter
    17. }
    18. }
    19. return field;
    20. }

Java reflection mechanism--get property values based on property name

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.