java reflects a simple method of traversing entity class attributes and types assigning and getting values

  • 2020-06-23 00:23:28
  • OfStack

Examples are as follows:


import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;

/**
 *  Gets the property name and type of the entity type 
 * @param model  For the entity class 
 * @author kou  Is the incoming parameter 
 */
public class GetModelNameAndType
{

  public static void testReflect(Object model) throws SecurityException,
      NoSuchMethodException, IllegalArgumentException,
      IllegalAccessException, InvocationTargetException, InstantiationException
  {
    //  Gets all properties of the entity class, returns Field An array of 
    Field[] field = model.getClass().getDeclaredFields();
    //  Gets the name of the property 
    String[] modelName = new String[field.length];
    String[] modelType = new String[field.length];
    for (int i = 0; i < field.length; i++)
    {
      //  Gets the name of the property 
      String name = field[i].getName();
      modelName[i] = name;
      //  Get attribute type 
      String type = field[i].getGenericType().toString();
      modelType[i] = type;
      
      // Key... Accessible private variables 
      field[i].setAccessible(true);
      // Set properties 
      field[i].set(model, field[i].getType().getConstructor(field[i].getType()).newInstance("kou"));

      //  Capitalize the first letter of the property 
      name = name.replaceFirst(name.substring(0, 1), name.substring(0, 1)
          .toUpperCase());

      if (type.equals("class java.lang.String"))
      { 
        //  if type Is a class type "class " , followed by the class name 
        Method m = model.getClass().getMethod("get" + name);
        //  call getter Method gets the value of the property 
        String value = (String) m.invoke(model);
        if (value != null)
        {

          System.out.println("attribute value:" + value);
        }

      }
      if (type.equals("class java.lang.Integer"))
      {
        Method m = model.getClass().getMethod("get" + name);
        Integer value = (Integer) m.invoke(model);
        if (value != null)
        {
          System.out.println("attribute value:" + value);
        }
      }
      if (type.equals("class java.lang.Short"))
      {
        Method m = model.getClass().getMethod("get" + name);
        Short value = (Short) m.invoke(model);
        if (value != null)
        {
          System.out.println("attribute value:" + value);
        }
      }
      if (type.equals("class java.lang.Double"))
      {
        Method m = model.getClass().getMethod("get" + name);
        Double value = (Double) m.invoke(model);
        if (value != null)
        {
          System.out.println("attribute value:" + value);
        }
      }
      if (type.equals("class java.lang.Boolean"))
      {
        Method m = model.getClass().getMethod("get" + name);
        Boolean value = (Boolean) m.invoke(model);
        if (value != null)
        {
          System.out.println("attribute value:" + value);
        }
      }
      if (type.equals("class java.util.Date"))
      {
        Method m = model.getClass().getMethod("get" + name);
        Date value = (Date) m.invoke(model);
        if (value != null)
        {
          System.out.println("attribute value:"
              + value.toLocaleString());
        }
      }
    }
  }

  public static void main(String[] args)
  {
    try
    {
      testReflect(new VO());
    }
    catch (SecurityException e)
    {
      e.printStackTrace();
    }
    catch (IllegalArgumentException e)
    {
      e.printStackTrace();
    }
    catch (NoSuchMethodException e)
    {
      e.printStackTrace();
    }
    catch (IllegalAccessException e)
    {
      e.printStackTrace();
    }
    catch (InvocationTargetException e)
    {
      e.printStackTrace();
    }
    catch (InstantiationException e)
    {
      e.printStackTrace();
    }
  }

}

Related articles: