java method of obtaining object property values based on reflection

  • 2020-06-15 08:56:05
  • OfStack

This article gives an example of how java gets object attribute values based on reflection. To share for your reference, specific as follows:

Properties and property values in the object are obtained through reflection mechanisms. private is fine in the object, but exceptions are sometimes reported in other classes. The following example is in this object


/**
* Engine entity. @author MyEclipse Persistence Tools
*/
public class Engine implements java.io.Serializable {
 // Fields
 private Long engineId;
 private String engineName;
 private String engineType;
 // Constructors
 /** default constructor */
 public Engine() {
 }
 /** minimal constructor */
 public Engine(Long engineId) {
 this.engineId = engineId;
 }
 public Engine(String engineName, String engineType) {
 this.engineName = engineName;
 this.engineType = engineType;
 }
 /** full constructor */
 public Engine(Long engineId, String engineName, String engineType) {
 this.engineId = engineId;
 this.engineName = engineName;
 this.engineType = engineType;
 }
 // Property accessors
 public Long getEngineId() {
 return this.engineId;
 }
 public void setEngineId(Long engineId) {
 this.engineId = engineId;
 }
 public String getEngineName() {
 return this.engineName;
 }
 public void setEngineName(String engineName) {
 this.engineName = engineName;
 }
 public String getEngineType() {
 return this.engineType;
 }
 public void setEngineType(String engineType) {
 this.engineType = engineType;
 }
public static void main(String[] args) throws Exception {
 Engine m = new Engine(" The car "," The engine ");
 Class clazz = m.getClass();
 Field[] f = clazz.getDeclaredFields();
 String[] name = field2Name(f);
 Object[] value = field2Value(f, m);
 showNameAndValue(name, value);
 }
 public static void showNameAndValue(String[] name, Object[] value) {
 for (int i = 0; i < name.length; i++) {
  System.out.println("--" + i + "--");
  System.out.println("name:" + name[i]);
  System.out.println("Value:" + value[i]);
  System.out.println("--   --");
 }
 }
 public static String[] field2Name(Field[] f) {
 String[] name = new String[f.length];
 for (int i = 0; i < f.length; i++) {
  name[i] = f[i].getName();
 }
 return name;
 }
 public static Object[] field2Value(Field[] f, Object o) throws Exception {
 Object[] value = new Object[f.length];
 for (int i = 0; i < f.length; i++) {
  value[i] = f[i].get(o);
 }
 return value;
 }
}

If you want to call the attribute value of an private attribute, you need to call its get method.

Change the field2Value method to the following so you can get the private attribute values in other classes as well


public static Object[] field2Value(Field[] fields, Object o) throws Exception {
 Object[] values = new Object[fields.length];
 Class classType = o.getClass();
 for (int i = 0; i < fields.length; i++) {
  String fieldName = fields[i].getName();
  String firstLetter = fieldName.substring(0, 1).toUpperCase();
  String getMethodName = "get" + firstLetter + fieldName.substring(1);
  Method getMethod = classType.getMethod(getMethodName, new Class[] {});
  values[i] = getMethod.invoke(o, new Object[] {});
 }
 return values;
}

For more java related content, you can check out java date and time operation tips, Java picture operation Tips, Java Operation Tips for DOM nodes, Java File and directory operation Tips and Java Data Structure and Algorithm Tutorial.

I hope this article has been helpful in java programming.


Related articles: