您的位置:首页 > 其它

输出对象的各个公开方法的返回值,不深入递归,建议仅在测试时使用,比如你想了解一下这个类的返回值

2016-07-31 21:16 585 查看
输出对象的各个公开方法的返回值,不深入递归
建议仅在测试时使用,比如你想了解一下这个类的返回值
可以考虑实际需求修改是否跳过对象的判断函数 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐