Java traverse the object all properties

To obtain all the properties of the object may be used getDeclaredFields ()
method returns an array of Field
traverse the array through all attributes several
Note that using this method throws an exception 4
then choose to perform according to the type of the attribute corresponding to the content

public  static  void eachProperties (Object Model) throws a NoSuchMethodException, IllegalAccessException, an IllegalArgumentException, a InvocationTargetException { 
    Field [] Field = model.getClass () getDeclaredFields ();. // get all the attributes of the entity class, returns an array of Field 
    for ( int J = 0 ; J <field.length; J ++) { // iterate through all attributes 
        String name = Field [J] .getName (); // Get the name attribute of 
 
        the System. OUT .println ( " attribute name: " + name); 
        name = name.substring ( 0 , . 1) .toUpperCase () + name.substring ( 1 ); // The first character capitalized property, construction convenient get, set method 
        String = Field, of the type [J] .getGenericType () toString ();. // Gets the type of property 
        IF (type.equals ( " class java.lang.String " )) { // if the type is a class type, comprising a front "class", the class name followed 
          ... 
        } 
        IF (type.equals ( " class Java. lang.Integer " )) { 
          ... 
        } 
        IF (type.equals ( " class a java.lang.Short " )) { 
          ... 
        } 
        IF (type.equals ( "class java.lang.Double")){
          ...
        }
        if(type.equals("class java.lang.Boolean")){
          ...
        }
        if(type.equals("class java.util.Date")){
          ...
        }
    }
}

The specific content of execution is the focus
we know the properties of the model will have a corresponding getter and setter methods
just need to get the corresponding getter and setter methods to get and set properties
here need to use methods getMethod

Get getter method

Partakers with parameters and methods with no arguments, we know getter methods are no parameters
to obtain getter follows

Method m = model.getClass().getMethod("get"+name);

Get setter methods

If a setter method with parameters, the type of the parameter should be packaged as a Class <?> Generic array passing getMethodthe second parameter of the method
parameters such as type String follows setter

Method m = model.getClass().getMethod("set"+name, new Class[] {String.class});

Execution getter method

String value = (String) m.invoke(model);

Setter method execution

m.invoke(model,new Object[] {new String("new value")});

Here are some online friends packed javabean reflection method:

/** 
 * 根据属性名获取属性值 
 * */  
   private Object getFieldValueByName(String fieldName, Object o) {  
       try {    
           String firstLetter = fieldName.substring(0, 1).toUpperCase();    
           String getter = "get" + firstLetter + fieldName.substring(1);    
           Method method = o.getClass().getMethod(getter, new Class[] {});    
           Object value = method.invoke(o, new Object[] {});    
           return value;    
       } catch (Exception e) {    
           log.error(e.getMessage(),e);    
           return null;    
       }    
   }   
     
   /** 
    * 获取属性名数组 
    * */  
   private String[] getFiledName(Object o){  
    Field[] fields=o.getClass().getDeclaredFields();  
        String[] fieldNames=new String[fields.length];  
    for(int i=0;i<fields.length;i++){  
        System.out.println(fields[i].getType());  
        fieldNames[i]=fields[i].getName();  
    }  
    return fieldNames;  
   }  
     
   /** 
    * 获取属性类型(type),属性名(name),属性值(value)的map组成的list 
    * */  
   private List getFiledsInfo(Object o){  
    Field[] fields=o.getClass().getDeclaredFields();  
        String[] fieldNames=new String[fields.length];  
        List list = new ArrayList();  
        Map infoMap=null;  
    for(int i=0;i<fields.length;i++){  
        Infomap=  .getFiledName (O);  new new the HashMap ();   
        infoMap.put ( " type " , Fields [I] .getType () toString ().);   
        infoMap.put ( " name " , Fields [I] .getName ());   
        infoMap.put ( " value " , getFieldValueByName (Fields [I] .getName (), O));   
        List.add (Infomap);   
    }   
    return List;   
   }   
     
   / * * 
    * all attribute values acquired object, returns an array of 
    * * /   
   public object [] getFiledValues (Object O) {   
    String [] the fieldNames = the this 
    Object [] value = new new Object[fieldNames.length];  
    for(int i=0;i<fieldNames.length;i++){  
        value[i]=this.getFieldValueByName(fieldNames[i], o);  
    }  
    return value;  
   } 

Guess you like

Origin www.cnblogs.com/xiaofengfree/p/12049989.html