52ky 发表于 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++) {
      反射反射=新反射();
            字段.setAccessible(true);
            // 字段名
            reflect.setField(fields.getName());
            // 字段值
            尝试 {
反射.setValue(字段.get(t));
} 捕捉(IllegalArgumentException e1){
// TODO 自动生成的 catch 块
e1.printStackTrace();
} 捕捉(IllegalAccessException e1){
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
            arrayList.add(反射);
// if (fields.getType().getName().equals(
// java.lang.String.class.getName())) {
// // 字符串类型
// 尝试 {
// reflect.setValue(fields.get(t));
// } catch (IllegalArgumentException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// } 捕获 (IllegalAccessException e) {
// // TODO 自动生成的 catch 块
// e.printStackTrace();
// }
// } else if (fields.getType().getName().equals(
// java.lang.Integer.class.getName())
// ||字段.getType().getName().equals("int")) {
// // 整数类型
// 尝试 {
// reflect.setValue(fields.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 setAccessible(true);
//Field name
reflect. setField(fields.getName());
//Field value
Try{
Reflection SetValue (field . 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.getType(). getName(). equals(
// java. lang.String. class. getName())) {
/// / string type
//Try{
// reflect. setValue(fields.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.getType(). getName(). equals(
// java. lang.Integer. class. getName())
//?? field getType(). getName(). equals("int")) {
/// / integer type
//Try{
// reflect. setValue(fields.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;
}
)



页: [1]
查看完整版本: 反射泛型对象