java反射遍歷實(shí)體類屬性和類型,并賦值和獲取值的簡(jiǎn)單方法
更新時(shí)間:2017年04月13日 10:01:54 投稿:jingxian
下面小編就為大家?guī)硪黄猨ava反射遍歷實(shí)體類屬性和類型,并賦值和獲取值的簡(jiǎn)單方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
實(shí)例如下:
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Date;
/**
* 獲取實(shí)體類型的屬性名和類型
* @param model 為實(shí)體類
* @author kou 為傳入?yún)?shù)
*/
public class GetModelNameAndType
{
public static void testReflect(Object model) throws SecurityException,
NoSuchMethodException, IllegalArgumentException,
IllegalAccessException, InvocationTargetException, InstantiationException
{
// 獲取實(shí)體類的所有屬性,返回Field數(shù)組
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;
//關(guān)鍵。。??稍L問私有變量
field[i].setAccessible(true);
//給屬性設(shè)置
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);
// 調(diào)用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反射遍歷實(shí)體類屬性和類型,并賦值和獲取值的簡(jiǎn)單方法就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 并發(fā)編程之ThreadLocal詳解及實(shí)例
這篇文章主要介紹了Java 并發(fā)編程之ThreadLocal詳解及實(shí)例的相關(guān)資料,需要的朋友可以參考下2017-02-02
MyBatis3傳遞多個(gè)參數(shù)(Multiple Parameters)
這篇文章主要介紹了MyBatis3傳遞多個(gè)參數(shù),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07
SpringCloud?Feign?傳輸Date類型參數(shù)存在誤差的問題
這篇文章主要介紹了SpringCloud?Feign?傳輸Date類型參數(shù)存在誤差的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
使用mybatis-plus報(bào)錯(cuò)Invalid bound statement (not found)錯(cuò)誤
這篇文章主要介紹了使用mybatis-plus報(bào)錯(cuò)Invalid bound statement (not found)錯(cuò)誤,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
Spring Boot簡(jiǎn)介與快速搭建詳細(xì)步驟
SpringBoot其本身沒有添加什么新的技術(shù),就是整合了一些現(xiàn)有的框架,并提供了一些默認(rèn)的配置,就是這些默認(rèn)的配置,極大的提高了我們的開發(fā)效率。這篇文章主要介紹了Spring Boot簡(jiǎn)介與快速搭建,需要的朋友可以參考下2021-05-05

