锁定老帖子 主题:如何获得pojo中嵌套属性的类型
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2012-05-02
写一个helper,注意map类,其它可以beanutils,也可用反射
象这样,几处关键代码: Field[] flds =class.getFields(); .. for(Field f : flds){ 设f可访问; f.getName()等于想匹配的; ... } |
|
返回顶楼 | |
发表时间:2012-05-03
yn5411 写道 写一个helper,注意map类,其它可以beanutils,也可用反射
象这样,几处关键代码: Field[] flds =class.getFields(); .. for(Field f : flds){ 设f可访问; f.getName()等于想匹配的; ... } 你这个不行吧,没有实现嵌套属性的访问。 |
|
返回顶楼 | |
发表时间:2012-05-03
一直听说“反射很耗性能的”,也一直听说要缓存起来
但是一直都没发现除了method能放进map还有什么能缓存的 而且把method放进map和反射的性能又没什么关系啊~ |
|
返回顶楼 | |
发表时间:2012-05-03
写这个东西还是有点耗时间的,LZ要感谢我哦。
package test; import java.lang.reflect.Field; import java.util.Date; /** * * @author YDSoft-15 * */ public class ObjectFields { /** * @param args */ public static void main(String[] args) { ObjectFields of = new ObjectFields(); System.out.println(of.ObjectFields2String(new A())); } /** * * @param obj * @return 对象存在的属性值 */ public String ObjectFields2String(Object obj) { return ObjectFields2String(new StringBuffer(), obj, 0); } /** * * @param sb * @param obj * @param index * @return 对象存在的属性值 */ private String ObjectFields2String(StringBuffer sb, Object obj, int index) { Class<?> clas = obj.getClass();// 加载类 sb.append(clas.getName()); Field[] fields = clas.getDeclaredFields();// 获得对象属性集合 sb.append(" {"); printBlankSpace(sb, index + 1); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; String fieldName = field.getName();// 得到字段名 sb.append(fieldName); sb.append("("); Class<?> fieldType = field.getType();// 得到字段类别 sb.append(fieldType.getSimpleName()); sb.append(") = "); try { field.setAccessible(true); // 设置强制访问 Object fieldValue = field.get(obj);// 得到属性值 if (fieldType.getClassLoader() != null) {// 如果ClassLoader为ExtClassLoader、AppClassLoader ObjectFields2String(sb, fieldValue, index + 1);// 递归 } else { sb.append(fieldValue); } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } finally { field.setAccessible(false); } if (i != fields.length - 1) { printBlankSpace(sb, index + 1); } } printBlankSpace(sb, index); sb.append("}"); return sb.toString(); } private void printBlankSpace(StringBuffer sb, int index) { sb.append("\n"); for (int i = 0; i < index; i++) { sb.append(" "); } } } class A { String a = "A String of class A"; B b = new B(); long l = 123456l; Date d = new Date(); StringBuffer sb = new StringBuffer(); java.awt.Image img = null; public A() { sb.append("StringBuffer"); } } class B { String b = "A String of class B"; C c = new C(); } class C { String b = "A String of class C"; } |
|
返回顶楼 | |
发表时间:2012-05-03
test.A { a(String) = A String of class A b(B) = test.B { b(String) = A String of class B c(C) = test.C { b(String) = A String of class C } } l(long) = 123456 d(Date) = Thu May 03 18:10:51 CST 2012 sb(StringBuffer) = StringBuffer img(Image) = null } |
|
返回顶楼 | |
发表时间:2012-05-03
goldcrucifix 写道 yn5411 写道 写一个helper,注意map类,其它可以beanutils,也可用反射
象这样,几处关键代码: Field[] flds =class.getFields(); .. for(Field f : flds){ 设f可访问; f.getName()等于想匹配的; ... } 你这个不行吧,没有实现嵌套属性的访问。 怎么不能嵌套呢? 具体的就是map和普通class,你再好好想想。:) |
|
返回顶楼 | |
发表时间:2012-05-04
yn5411 写道 goldcrucifix 写道 yn5411 写道 写一个helper,注意map类,其它可以beanutils,也可用反射
象这样,几处关键代码: Field[] flds =class.getFields(); .. for(Field f : flds){ 设f可访问; f.getName()等于想匹配的; ... } 你这个不行吧,没有实现嵌套属性的访问。 怎么不能嵌套呢? 具体的就是map和普通class,你再好好想想。:) 你是说把所有的类和嵌套类放到Map中? 那如果map中的类有相同的属性怎么办? 还有就是事先就不知道我的嵌套类有哪些。 |
|
返回顶楼 | |
发表时间:2012-05-04
if(i!=我){} 写道 写这个东西还是有点耗时间的,LZ要感谢我哦。
package test; import java.lang.reflect.Field; import java.util.Date; /** * * @author YDSoft-15 * */ public class ObjectFields { /** * @param args */ public static void main(String[] args) { ObjectFields of = new ObjectFields(); System.out.println(of.ObjectFields2String(new A())); } /** * * @param obj * @return 对象存在的属性值 */ public String ObjectFields2String(Object obj) { return ObjectFields2String(new StringBuffer(), obj, 0); } /** * * @param sb * @param obj * @param index * @return 对象存在的属性值 */ private String ObjectFields2String(StringBuffer sb, Object obj, int index) { Class<?> clas = obj.getClass();// 加载类 sb.append(clas.getName()); Field[] fields = clas.getDeclaredFields();// 获得对象属性集合 sb.append(" {"); printBlankSpace(sb, index + 1); for (int i = 0; i < fields.length; i++) { Field field = fields[i]; String fieldName = field.getName();// 得到字段名 sb.append(fieldName); sb.append("("); Class<?> fieldType = field.getType();// 得到字段类别 sb.append(fieldType.getSimpleName()); sb.append(") = "); try { field.setAccessible(true); // 设置强制访问 Object fieldValue = field.get(obj);// 得到属性值 if (fieldType.getClassLoader() != null) {// 如果ClassLoader为ExtClassLoader、AppClassLoader ObjectFields2String(sb, fieldValue, index + 1);// 递归 } else { sb.append(fieldValue); } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } finally { field.setAccessible(false); } if (i != fields.length - 1) { printBlankSpace(sb, index + 1); } } printBlankSpace(sb, index); sb.append("}"); return sb.toString(); } private void printBlankSpace(StringBuffer sb, int index) { sb.append("\n"); for (int i = 0; i < index; i++) { sb.append(" "); } } } class A { String a = "A String of class A"; B b = new B(); long l = 123456l; Date d = new Date(); StringBuffer sb = new StringBuffer(); java.awt.Image img = null; public A() { sb.append("StringBuffer"); } } class B { String b = "A String of class B"; C c = new C(); } class C { String b = "A String of class C"; } class B { String b = "A String of class B"; C c; } 已经报错了,不是真正的pojo类 |
|
返回顶楼 | |
发表时间:2012-05-04
报错你Debug了吗?问题出在哪儿?要不你给我个“真正的”“POJO类”,让我测……
|
|
返回顶楼 | |
发表时间:2012-05-04
最后修改:2012-05-04
java.util.GregorianCalendar { BC(int) = 0 BCE(int) = 0 AD(int) = 1 CE(int) = 1 EPOCH_OFFSET(int) = 719163 EPOCH_YEAR(int) = 1970 MONTH_LENGTH(int[]) = [I@79616c7 LEAP_MONTH_LENGTH(int[]) = [I@eb166b5 ONE_SECOND(int) = 1000 ONE_MINUTE(int) = 60000 ONE_HOUR(int) = 3600000 ONE_DAY(long) = 86400000 ONE_WEEK(long) = 604800000 MIN_VALUES(int[]) = [I@33837697 LEAST_MAX_VALUES(int[]) = [I@7bdecdec MAX_VALUES(int[]) = [I@60a1807c serialVersionUID(long) = -8125100834729963327 gcal(Gregorian) = sun.util.calendar.Gregorian@afa7e74 jcal(JulianCalendar) = null jeras(Era[]) = null DEFAULT_GREGORIAN_CUTOVER(long) = -12219292800000 gregorianCutover(long) = -12219292800000 gregorianCutoverDate(long) = 577736 gregorianCutoverYear(int) = 1582 gregorianCutoverYearJulian(int) = 1582 gdate(Date) = 2012-05-04T00:00:00.000Z cdate(Date) = 2012-05-04T00:00:00.000Z calsys(BaseCalendar) = sun.util.calendar.Gregorian@afa7e74 zoneOffsets(int[]) = [I@2da679b7 originalFields(int[]) = null cachedFixedDate(long) = 734627 $assertionsDisabled(boolean) = true } java.util.HashMap { DEFAULT_INITIAL_CAPACITY(int) = 16 MAXIMUM_CAPACITY(int) = 1073741824 DEFAULT_LOAD_FACTOR(float) = 0.75 table(Entry[]) = [Ljava.util.HashMap$Entry;@24a20892 size(int) = 0 threshold(int) = 12 loadFactor(float) = 0.75 modCount(int) = 0 entrySet(Set) = null serialVersionUID(long) = 362498820763181265 } 有错,已经改了,加了两个判断条件,但决定不贴出来了,Debug时很容易发现。 cn.ydsoft.xcsqfwpt.foreground.level2.action.Level2NewsListPageAction { logger(Logger) = org.apache.log4j.Logger { FQCN(String) = org.apache.log4j.Level class$org$apache$log4j$Level(Class) = class org.apache.log4j.Level } serialVersionUID(long) = 1 administrativeAreaService(AdministrativeAreaService) = null level2NewsListService(Level2NewsListService) = null state(String) = null message(String) = null l0(String) = null l1(String) = null l2(String) = null l3(String) = null l4(String) = null l5(String) = null l6(String) = null lm0(List) = null pageInfo(PageInfo) = cn.ydsoft.xcsqfwpt.bean.PageInfo { pageSize(int) = 10 pageNumber(int) = 1 DEFAULT_PAGE_SIZE(int) = 10 DEFAULT_PAGE_NUMBER(int) = 1 totalRow(int) = 0 totalPage(int) = 0 previousPage(int) = 0 nextPage(int) = 0 startRow(int) = 0 endRow(int) = 0 date(List) = null visiblePages(List) = null startPage(int) = 0 endPage(int) = 0 MAX_VISIBLE_PAGES(int) = 10 previousPages(int) = 0 followingPages(int) = 0 } pageNumber(int) = 0 xw01(Xw01) = null nr01(Nr01) = null zh01(Zh01) = null zx01(Zx01) = null dw02(Dw02) = null rx01(Rx01) = null zq01(Zq01) = null zq02(Zq02) = null fw01(Fw01) = null fw02(Fw02) = null es01(Es01) = null fj01(Fj01) = null bbs(String) = null sslb(String) = null ssnr(String) = null } |
|
返回顶楼 | |