Java USES reflection to automatically encapsulate methods into entity objects

  • 2020-04-01 03:40:40
  • OfStack

This article illustrates how Java USES reflection to automatically encapsulate entity objects. Share with you for your reference. Specific analysis is as follows:

When using this method, the name of the parameter to be passed must end with the line number, remove the line number is the property name, such as the page passed name+rowNo, then the entity object property name should be name. The following code

//Gets the page data, which is automatically encapsulated as a bean object 
public List getObjectList(Class clazz,String[] rowNos) throws Exception{
        List objList = new ArrayList();
        for(int i=0;rowNos!=null && i<rowNos.length;i++){
            //Create an object instance
            Object object = clazz.newInstance();
            //Gets the property of the class declaration
            Field[] fields = clazz.getDeclaredFields();
            
            StringBuffer buffer = null;
            //Traversing the properties, perform encapsulation
            for(int j=0;j<fields.length;j++){
                //Gets the name of the property
                String fieldName = fields[j].getName();
                //Get the name of the parameter
                String paraName = fields[j].getName()+rowNos[i];
                
                //If the value of the get parameter is null, the loop continues
                String value = getValueNull(paraName);
                if(value==null){
                    continue;
                }
                
                //Parameter value
                Object[] paramValue =new Object[1];
                if(fields[j].getType().toString().equals("class java.lang.String")){
                    paramValue[0]=value;
                }
                if(fields[j].getType().toString().equals("class java.lang.Integer")){
                    paramValue[0]=new Integer(value);
                }
                if(fields[j].getType().toString().equals("class java.lang.Double")){
                    paramValue[0]=new Double(value);
                }
                if(fields[j].getType().toString().equals("class java.util.Date")){
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
                    paramValue[0]=sdf.parse(value);
                }
                
                //Parameter type
                Class[] paramType= {fields[j].getType()};
                
                //Get the name of the set method
                buffer = new StringBuffer("set");
                buffer.append(fieldName.substring(0, 1).toUpperCase());
                buffer.append(fieldName.substring(1));
                //Gets to put back
                Method method = clazz.getDeclaredMethod(buffer.toString(), paramType);
                //Execute method
                method.invoke(object,paramValue);
            }
            //Put the current object in list
            objList.add(object);
        }
        return objList;
}

I hope this article has been helpful to your Java programming.


Related articles: