[软件设计/软件工程] 反射泛型对象

[复制链接]
发表于 2022-5-2 10:29:09
/*
* 获取T对象的字段和字段值
*/
公共 ArrayList<Reflect> GetClassFieldAndValue(T t) {
        如果(t == null)返回null;
        ArrayList<Reflect> arrayList=new ArrayList<Reflect>();
        
        字段[] 字段 = t.getClass().getDeclaredFields();
        for (int j = 0; j < fields.length; j++) {
        反射反射=新反射();
            字段[j].setAccessible(true);
            // 字段名
            reflect.setField(fields[j].getName());
            // 字段值
            尝试 {
反射.setValue(字段[j].get(t));
} 捕捉(IllegalArgumentException e1){
// TODO 自动生成的 catch 块
e1.printStackTrace();
} 捕捉(IllegalAccessException e1){
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
            arrayList.add(反射);
// if (fields[j].getType().getName().equals(
// java.lang.String.class.getName())) {
// // 字符串类型
// 尝试 {
// reflect.setValue(fields[j].get(t));
// } catch (IllegalArgumentException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// } 捕获 (IllegalAccessException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// }
// } else if (fields[j].getType().getName().equals(
// java.lang.Integer.class.getName())
// ||字段[j].getType().getName().equals("int")) {
// // 整数类型
// 尝试 {
// reflect.setValue(fields[j].get(t));
// } catch (IllegalArgumentException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// } 捕获 (IllegalAccessException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// }
// }
            // 其他类型。 . .
        }
        返回数组列表;
    }

(/*
*Gets the field and field value of the t object
*/
Public ArrayList < reflect > getclassfieldandvalue (T){
If (t = = null) returns NULL;
ArrayList<Reflect> arrayList=new ArrayList<Reflect>();
Field [] field = t.getclass() getDeclaredFields();
for (int j = 0; j < fields.length; j++) {
Reflection = new reflection ();
Field [J] setAccessible(true);
//Field name
reflect. setField(fields[j].getName());
//Field value
Try{
Reflection SetValue (field [J]. Get (T));
}Capture (illegalargumentexception E1){
//Automatically generated tocatch block
e1. printStackTrace();
}Capture (illegalaccessexception E1){
//Catch block automatically generated by todo
e1. printStackTrace();
}
arrayList. Add (reflection);
// if (fields[j].getType(). getName(). equals(
// java. lang.String. class. getName())) {
/// / string type
//Try{
// reflect. setValue(fields[j].get(t));
// } catch (IllegalArgumentException e) {
/// / catch block automatically generated by todo
// e.printStackTrace();
//} capture (illegalaccessexception E){
/// / catch block automatically generated by todo
// e.printStackTrace();
// }
// } else if (fields[j].getType(). getName(). equals(
// java. lang.Integer. class. getName())
//?? field [J] getType(). getName(). equals("int")) {
/// / integer type
//Try{
// reflect. setValue(fields[j].get(t));
// } catch (IllegalArgumentException e) {
/// / catch block automatically generated by todo
// e.printStackTrace();
//} capture (illegalaccessexception E){
/// / catch block automatically generated by todo
// e.printStackTrace();
// }
// }
//Other types
}
Return array list;
}
)





上一篇:ZOJ 2702 UNRHYMABLE RHYMES(DP)
下一篇:11GOCP 1Z0-052 :2013-09-11 MGR_ROLE ROLE......A66

使用道具 举报

Archiver|手机版|小黑屋|吾爱开源 |网站地图

Copyright 2011 - 2012 Lnqq.NET.All Rights Reserved( ICP备案粤ICP备14042591号-1粤ICP14042591号 )

关于本站 - 版权申明 - 侵删联系 - Ln Studio! - 广告联系

本站资源来自互联网,仅供用户测试使用,相关版权归原作者所有

快速回复 返回顶部 返回列表