当前位置: 移动技术网 > IT编程>开发语言>Java > java反射遍历实体类属性和类型,并赋值和获取值的简单方法

java反射遍历实体类属性和类型,并赋值和获取值的简单方法

2019年07月22日  | 移动技术网IT编程  | 我要评论

实例如下:

import java.lang.reflect.field;
import java.lang.reflect.invocationtargetexception;
import java.lang.reflect.method;
import java.util.date;

/**
 * 获取实体类型的属性名和类型
 * @param model 为实体类
 * @author kou 为传入参数
 */
public class getmodelnameandtype
{

  public static void testreflect(object model) throws securityexception,
      nosuchmethodexception, illegalargumentexception,
      illegalaccessexception, invocationtargetexception, instantiationexception
  {
    // 获取实体类的所有属性,返回field数组
    field[] field = model.getclass().getdeclaredfields();
    // 获取属性的名字
    string[] modelname = new string[field.length];
    string[] modeltype = new string[field.length];
    for (int i = 0; i < field.length; i++)
    {
      // 获取属性的名字
      string name = field[i].getname();
      modelname[i] = name;
      // 获取属性类型
      string type = field[i].getgenerictype().tostring();
      modeltype[i] = type;
      
      //关键。。。可访问私有变量
      field[i].setaccessible(true);
      //给属性设置
      field[i].set(model, field[i].gettype().getconstructor(field[i].gettype()).newinstance("kou"));

      // 将属性的首字母大写
      name = name.replacefirst(name.substring(0, 1), name.substring(0, 1)
          .touppercase());

      if (type.equals("class java.lang.string"))
      { 
        // 如果type是类类型,则前面包含"class ",后面跟类名
        method m = model.getclass().getmethod("get" + name);
        // 调用getter方法获取属性值
        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();
    }
  }

}

以上这篇java反射遍历实体类属性和类型,并赋值和获取值的简单方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网