Home >Java >javaTutorial >Java sample code sharing for obtaining object attribute values based on reflection
This article mainly introduces the method of java to obtain the object attribute value based on reflection, and analyzes the related implementation methods and operating skills of java based on reflection to obtain the object attribute value in the form of examples. , Friends who need it can refer to
The example in this article describes the method of obtaining object attribute values in Java based on reflection. Share it with everyone for your reference, the details are as follows:
Obtain the attributes and attribute values in the object through the reflection mechanism. It is no problem to be private in the object, but sometimes exceptions will be 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("汽车","发动机"); 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 a private attribute, you need to call its get method. The specific method is:
Change the field2Value method to the following method , so that the private attribute value can also be obtained in other classes
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; }
The above is the detailed content of Java sample code sharing for obtaining object attribute values based on reflection. For more information, please follow other related articles on the PHP Chinese website!