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

输出对象的各个公开方法的返回值,不深入递归

建议仅在测试时使用,比如你想了解一下这个类的返回值

可以考虑实际需求修改是否跳过对象的判断函数 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



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值