SoFunction
Updated on 2025-03-08

How to obtain the properties and methods of data class objects through reflection mechanism

1. Obtain all attributes of class objects and corresponding get and set methods through reflection mechanism

1. Iterate through all properties of class objects

//You can use the getDeclaredFields() method to get all the properties of the objectAutoClass autoClass = new AutoClass();		// Initialize a class firstField[] fields = ().getDeclaredFields();	// Get all properties of the objectfor (Field item : fields) {
    String name = ();	// Get the object attribute name    String typeName = ().getTypeName();	// Get the type of object attribute    ("Attribute name: %s, type: %s\n", name, typeName);
}

2. Get the get and set methods of the attributes

// Generally speaking, each property has its get and set methods// Get the get method through the method nameMethod getMethod = ().getMethod("getId");
// Call the get methodString invoke = (String) (autoClass);
(invoke);
// Get the set method through the method name. Since the set method has parameters, it is also necessary to define the parameter type of the set method here.Method setMethod = ().getMethod("setId", );
// Call the set method(autoClass, "gftz");
(());
// invokeIn the methodautoClassexpressautoClassThis specifies the corresponding method

2. Implement instantiation of class objects through reflection mechanism

1. Tools

package utils;
import ;
import ;
import ;
import ;
/**
  * @description: Data class related operations
  * @author: Huang Junyu
  * @create: 2022-07-07 22:05
  **/
// Since this tool class is used to make it universal, generics are used. If you don’t understand it, you need to Baidu yourself.public class ObjectOperate<T> {
/**
  * Set the properties of the data class object
  * @param obj instance of data class object
  * @param name attribute name
  * @param type attribute type name
  * @param val The attribute value that needs to be stored
  * @throws NoSuchMethodException
  * @throws InvocationTargetException
  * @throws IllegalAccessException
  */
    public void setValues(T obj, String name, String type, Object val) throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        Method method = null;
        // Get the corresponding method through the attribute type, force the property value to convert and initialize the corresponding attribute        // Most commonly used data types are considered here, which can be used as ready-to-use.        switch (type){
            case "int":
                method = ().getMethod(name, );
                (obj,(int)val);
                break;
            case "short":
                method = ().getMethod(name, );
                (obj,(short)val);
                break;
            case "long":
                method = ().getMethod(name, );
                (obj,(long)val);
                break;
            case "float":
                method = ().getMethod(name, );
                (obj,(float)val);
                break;
            case "double":
                method = ().getMethod(name, );
                (obj,(double)val);
                break;
            case "boolean":
                method = ().getMethod(name, );
                (obj,(boolean)val);
                break;
            case "":
                method = ().getMethod(name, );
                (obj,(String)val);
                break;
            case "":
                method = ().getMethod(name, );
                (obj,(Date)val);
                break;
            case "":
                method = ().getMethod(name, );
                (obj,(BigDecimal)val);
                break;
        }
    }
}

2. Use cases

// Simulate case dataList<Object> params = new ArrayList<>();
(().toString().replaceAll("-", ""));
(18);
(new Date());
(false);
(new BigDecimal(888888.8));
// Tools for initial call type operationObjectOperate<AutoClass> objectOperate = new ObjectOperate<>();
// AutoClass is used above, so the AutoClass class is no longer givenAutoClass autoClass = new AutoClass();
Field[] fields = ().getDeclaredFields();
for (int i = 0; i < ; i++) {
    String name = fields[i].getName();	// Attribute name    // Set the set method name as the attribute, such as: the attribute name is id, and the corresponding set method name is setId    name = "set"+(0, 1).toUpperCase() + (1);
    String typeName = fields[i].getGenericType().getTypeName();	//Attribute type name    try {
        (autoClass,name,typeName,(i));	// Initialize the object's properties    } catch (NoSuchMethodException e) {
        ();
    } catch (InvocationTargetException e) {
        ();
    } catch (IllegalAccessException e) {
        ();
    }
}
(());

This is the article about how Java can obtain the properties and methods of data class objects through reflection mechanism. For more related Java reflection to obtain the properties of data class objects, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!