方法日志打印demo(可灵活处理方法参数)

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class LogTest {
	public void logEnterMethod(String message, Object... parameters) {
		System.out.print(message + "(");
		for (int i = 0; i < parameters.length; i++) {
			if (parameters[i] instanceof List) {
				processList((List) parameters[i]);
			} else if (parameters[i] instanceof Map) {
				processMap((Map) parameters[i]);
			} else if (parameters[i] instanceof String
					|| parameters[i] instanceof Number) {
				System.out.print(parameters[i]);
			} else if (parameters[i] instanceof Object) {
				processObject(parameters[i]);
			} else {
				System.out.print(parameters[i]);
			}

			if (i < parameters.length - 1)
				System.out.print(",");
		}
		System.out.println(")");
	}

	private void processList(List list) {
		int size = list.size() > 10 ? 10 : list.size();
		for (int i = 0; i < size; i++) {
			System.out.print("[");
			System.out.print(list.get(i));
			if (i < size - 1) {
				System.out.print(",");
			}
		}
		System.out.print("]");
	}

	private void processMap(Map map) {
		System.out.print("[");
		Iterator it = map.entrySet().iterator();
		int size = map.size() > 10 ? 10 : map.size();
		for (int i = 0; i < size && it.hasNext(); i++) {
			Entry e = (Entry) it.next();
			System.out.print(e.getKey() + "=" + e.getValue());
			if (i < size - 1) {
				System.out.print(",");
			}

		}
		System.out.print("]");
	}

	private void processObject(Object o) {
		System.out.print("[");
		System.out.print(o + " ");
		List list = getFiledsInfo(o);
		int size = list.size() > 10 ? 10 : list.size();
		for (int i = 0; i < size; i++) {
			Map map = (Map) list.get(i);
			System.out.print(map.get("type") + ":" + map.get("name") + "="
					+ map.get("value"));
			if (i < size - 1) {
				System.out.print(",");
			}

		}
		System.out.println("]");
	}

	/**
	 * 根据属性名获取属性值
	 * */
	private Object getFieldValueByName(String fieldName, Object o) {
		try {
			String firstLetter = fieldName.substring(0, 1).toUpperCase();
			String getter = "get" + firstLetter + fieldName.substring(1);
			Method method = o.getClass().getMethod(getter, new Class[] {});
			Object value = method.invoke(o, new Object[] {});
			return value;
		} catch (Exception e) {
			// log.error(e.getMessage(),e);
			return null;
		}
	}

	/**
	 * 获取属性类型(type),属性名(name),属性值(value)的map组成的list
	 * */
	private List getFiledsInfo(Object o) {
		Field[] fields = o.getClass().getDeclaredFields();
		String[] fieldNames = new String[fields.length];
		List list = new ArrayList();
		Map infoMap = null;
		for (int i = 0; i < fields.length; i++) {
			infoMap = new HashMap();
			infoMap.put("type", fields[i].getType().toString());
			infoMap.put("name", fields[i].getName());
			infoMap.put("value", getFieldValueByName(fields[i].getName(), o));
			list.add(infoMap);
		}
		return list;
	}

}

public class Student {
	private int number;
	private String name;
	private double identifer;
	public int getNumber() {
		return number;
	}
	public void setNumber(int number) {
		this.number = number;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getIdentifer() {
		return identifer;
	}
	public void setIdentifer(double identifer) {
		this.identifer = identifer;
	}
}

import java.util.*;

public class TestMain {

	public LogTest logger;
	public TestMain() {
		logger = new LogTest();
	}
	public void method1(int x, int y) {
		logger.logEnterMethod("method1(int x, int y)", x, y);
	}
	public void method2(int x, String y) {
		logger.logEnterMethod("method2(int x, String y)", x, y);
	}
	public void method3(List<Integer> list) {
		logger.logEnterMethod("method3(List<Integer> list)", list);
	}
	public void method4(Map map){
		logger.logEnterMethod("method4(Map map)", map);
	}
	public void method5(Student stu){
		logger.logEnterMethod("method5(Student stu)", stu);
		
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		TestMain testMain = new TestMain();
		testMain.method1(2, 3);
		testMain.method2(2, "hello world!");
		List<Integer> list = new ArrayList<Integer>();
		list.add(10);
		list.add(20);
		testMain.method3(list);
		Map map = new HashMap();
		map.put(1, "123");
		map.put("2", "kkkk");
		testMain.method4(map);
		Student stu = new Student();
		stu.setIdentifer(1398119910);
		stu.setName("zhangsan");
		stu.setNumber(2009010397);;
		testMain.method5(stu);
	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值