服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java反射遍历实体类属性和类型,并赋值和获取值的简单方法

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

2020-09-10 14:17Java教程网 Java教程

下面小编就为大家带来一篇java反射遍历实体类属性和类型,并赋值和获取值的简单方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧

实例如下:

java" id="highlighter_757825">
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
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反射遍历实体类属性和类型,并赋值和获取值的简单方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。

延伸 · 阅读

精彩推荐