输出对象的各个公开方法的返回值,不深入递归,建议仅在测试时使用,比如你想了解一下这个类的返回值
2016-07-31 21:16
585 查看
输出对象的各个公开方法的返回值,不深入递归
建议仅在测试时使用,比如你想了解一下这个类的返回值
可以考虑实际需求修改是否跳过对象的判断函数 isSkipThisMethod
请仅在懒得看类的api说明时使用,谨慎使用
一些没有被跳过的函数执行后会对对象的内部数据执行较大的改变
贴上测试代码的结果:
[ObjName:对象名SW Class:java.lang.StringBuilder Hash:25041030]:
Those Method Has Their Result:
capacity: 24
chars: java.util.stream.IntPipeline$Head@91c18f
codePoints: java.util.stream.IntPipeline$Head@c355be
getClass: class java.lang.StringBuilder
hashCode: 25041030
length: 8
toString: 这是一段测试文本
trimToSize: result==null
建议仅在测试时使用,比如你想了解一下这个类的返回值
可以考虑实际需求修改是否跳过对象的判断函数 isSkipThisMethod
package com.gl.common; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.TreeSet; /** * 简易的打印对象信息的函数 * @author Administrator * */ public class PublicInfoPrinter { public static boolean isSmartSkip = true; public static boolean isDisplayException = false; /** * 获取对象的公开函数返回值.toString(); * * @param objName * 指定别名 * @param obj * 指定对象 * @return string */ public static String getInfoString(String objName, Object obj) { try { StringBuilder strBuilder = new StringBuilder(); Class<?> typeTmp = obj.getClass(); Method[] methods = typeTmp.getMethods(); TreeSet<String> needArgsMethods = new TreeSet<String>(); TreeSet<String> invocationErrorMethods = new TreeSet<String>(); TreeSet<String> methodResults = new TreeSet<String>(); strBuilder.append("[ObjName:" + objName + " Class:" + typeTmp.getName() + " Hash:" + obj.hashCode() + "]:\n"); if (methods == null) { strBuilder.append("methods == null\n"); return strBuilder.toString(); } Object result = null; for (Method method : methods) { if (isSmartSkip && isSkipThisMethod(method)) continue; try { result = method.invoke(obj); } catch (IllegalAccessException e) { if (isDisplayException) strBuilder.append("method:" + method.getName() + " Can't Access\n"); continue; } catch (IllegalArgumentException e) { if (isDisplayException) needArgsMethods.add(method.getName()); continue; } catch (InvocationTargetException e) { if (isDisplayException) invocationErrorMethods.add(method.getName()); continue; } result = (result == null ? "result==null" : result.toString()); methodResults.add(method.getName() + ": " + result); } if (!methodResults.isEmpty()) { strBuilder.append("Those Method Has Their Result:\n"); for (String str : methodResults) { strBuilder.append(str +"\n"); } strBuilder.append("\n"); } if (!needArgsMethods.isEmpty()) { strBuilder.append("\nThe Following Method Need More Args:\n"); int count = 0; for (String str : needArgsMethods) { strBuilder.append(str + ","); count++; if (count >= 5) { count = 0; strBuilder.append("\n"); } } strBuilder.append("\n"); } if (!invocationErrorMethods.isEmpty()) { strBuilder.append("\nThe Following Method Invoke Fail:\n"); int count = 0; for (String str : invocationErrorMethods) { strBuilder.append(str + ","); count++; if (count >= 5) { count = 0; strBuilder.append("\n"); } } strBuilder.append("\n"); } return strBuilder.toString(); } catch (Exception e) { return "GetInfoString Has An Exception"; } } public static boolean isSkipThisMethod(Method method) { String name = method.getName().toLowerCase(); if (method.getReturnType() == null) return true; int modifiers = method.getModifiers(); if(!Modifier.isPublic(modifiers)) return true; if(Modifier.isAbstract(modifiers)) return true; if (name.contains("stream")) return true; if (name.contains("reverse")) return true; if (name.indexOf("is") == 0) return false; if (name.indexOf("get") == 0) return false; return false; } public static void main(String args[]) { StringBuilder obj = new StringBuilder("这是一段测试文本"); System.out.println(PublicInfoPrinter.getInfoString("对象名SW", obj)); } }
请仅在懒得看类的api说明时使用,谨慎使用
一些没有被跳过的函数执行后会对对象的内部数据执行较大的改变
贴上测试代码的结果:
[ObjName:对象名SW Class:java.lang.StringBuilder Hash:25041030]:
Those Method Has Their Result:
capacity: 24
chars: java.util.stream.IntPipeline$Head@91c18f
codePoints: java.util.stream.IntPipeline$Head@c355be
getClass: class java.lang.StringBuilder
hashCode: 25041030
length: 8
toString: 这是一段测试文本
trimToSize: result==null
相关文章推荐
- 尝试有关对象指针的使用方法,里面有自己加的知识,更深入了解了
- 写一个递归方法,传入一个Map,返回这个Map对象中一共有多个Map类型的对象 Map中是可以添加Map,被添加的这个Map也可以添加Map,所以需要判断传入的Map对象的所有子节点,如果是Map就
- 例题:把一个超市购物的题做成函数。这个题做成函数没有必要,但可以了解输入参数,输出参数,返回值,函数体,还有结构体,ArryList集合,for循环算结果,以及集合里 .count的使用
- writeObject可以写n个,但是readObject()却只能读一次,你做一下测试。 如果你需要序列化好几个类的话,建议你用json;或者自己写两个方法,一个是将对象转换为字符串,一个是将字符串
- 深入了解Javascript中textRange对象使用方法
- 在一个有返回值的方法里使用了子线程,如何返回子线程中的值,如下代码中,我在子线程获取到了address的值,但是这个方法的返回结果是空,该如何解决?
- 【转载】深入了解C语言(函数的参数传递和函数使用参数的方法)
- 使用JSON做Ajax解析服务端返回的JSON对象方法
- acm技巧 使用文件输入输出方便测试的方法
- 用于组织,测试和操作对象及类的工具和技术-3 了解类中的方法
- 使用存储过程并返回值与及返回值的获得方法
- 使用存储过程并返回值与及返回值的获得方法
- 存储过程中使用RETURN语句返回数值,.Net里用ExecuteScalar方法结果将会导致一个未引用到具体对象的异常
- C#使用ExecuteReader返回DataReader既有查询结果集又有输出参数或返回值的使用注意事项
- 11_5_3给people添加GetOldest()方法,使用上面定义的重载运算符,返回一个Age最大的对象数组
- 怎样非递归逆序输出整数---------这个方法不知好不好,高手路过请指点,谢谢!
- 尝试一下用这个方法输出二进制
- 使用存储过程并返回值与及返回值的获得方法
- 使用存储过程并返回值与及返回值的获得方法
- 解决 ”不允许在查询中显式构造实体类型“问题及使用其他方法实现返回 List<Model对象>或者IQueryable<Model对象>对象