1. Consider the properties in the security access scope, no access to the property is not read
[Java]View PlainCopy
- /**
- * Get property values based on property name
- *
- * @param fieldName
- * @param Object
- * @return
- */
- private String Getfieldvaluebyfieldname (string FieldName, Object object) {
- try {
- Field field = Object.getclass (). GetField (FieldName);
- //Set access permissions for objects, guaranteeing access to private properties
- return (String) Field.get (object);
- } catch (Exception e) {
- return null;
- }
- }
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
- /**
- * Get property values based on property name
- *
- * @param fieldName
- * @param Object
- * @return
- */
- private String Getfieldvaluebyfieldname (string FieldName, Object object) {
- try {
- Field field = Object.getclass (). Getdeclaredfield (FieldName);
- //Set access permissions for objects, guaranteeing access to private properties
- Field.setaccessible (true);
- return (String) Field.get (object);
- } catch (Exception e) {
- return null;
- }
- }
3. Consider the inherited properties of the parent class, including the four class access rights, Private,protect,default,public
[Java]View PlainCopy
- /**
- * Get attribute elements based on attribute name, including various security scopes and all parent classes
- *
- * @param fieldName
- * @param Object
- * @return
- */
- Private Field getfieldbyclasss (String fieldName, Object object) {
- Field field = null;
- Class<?> clazz = Object.getclass ();
- For (; Clazz! = Object. Class Clazz = Clazz.getsuperclass ()) {
- try {
- field = Clazz.getdeclaredfield (FieldName);
- } catch (Exception e) {
- //There is nothing to be thrown out here.
- //If the exception is printed or thrown outward, it will not enter
- }
- }
- return field;
- }
Java reflection mechanism--get property values based on property name