写的一个简单的工具类,可以做对象类型的判断和迭代出一个对象所有属性的值

import java.lang.reflect.Field;

/**
 * @author songzheng
 */
public class TypeUtil {
    /**
     * 得到某个对象类型对象
     */
	public static Class<?> getClass(Object obj) {
    	if (obj instanceof Integer) {
    		return Integer.TYPE; 
    	} else if (obj instanceof Boolean) {
    		return Boolean.TYPE;
    	} else if (obj instanceof Long) {
    		return Long.TYPE;
    	} else if (obj instanceof Short) {
    		return Short.TYPE;
    	} else if (obj instanceof Double) {
    		return Double.TYPE;
    	} else if (obj instanceof Float) {
    		return Float.TYPE;
    	} else if (obj instanceof Character) {
    		return Character.TYPE;
    	} else if (obj instanceof Byte) {
    		return Byte.TYPE;
    	}
    	return obj.getClass();
    }
	/**
	 * 判断某个对象是否为装箱的基本类型对象或基本类型
	 */
	public static boolean isBasicType(Object obj) {
		final String[] types = {"int", "char", "boolean","long", "double", "float", "byte", "short"};
		String clazzName = getClass(obj).getName();
	    int j = 0;
		for (int i = 0; i < types.length; i++) {
	    	if (clazzName.equals(types[i])) {
	    		break;
	    	}
	    	j++;
	    }
		if (j <= 7) {
			return true; 
		} else {
			return false;
		}
	}
	/**
	 * 判断某对象是否为字符串
	 */
	public static boolean isString(Object obj) {
		return obj instanceof String;
	}
	/**
	 * 迭代出一个对象的全部基本属性
	 */
	public static void recurse(Field[] fs, Object obj, List<Object> list) throws IllegalArgumentException, IllegalAccessException {
		
		for (int i = 0; i < fs.length; i++) {
			int j = 0;
			Field f = fs[i];
			f.setAccessible(true);
			Object v = f.get(obj);
			if (isBasicType(v)) {
				j++;
				list.add(v);
			} 
			if (isString(v)) {
				j++;
				list.add(v);
			}
			if (j == fs.length)
				continue;
			if (!(isBasicType(v) || isString(v))) {
			    recurse(v.getClass().getFields(), v, list);
			}
		}
	}
public static void main(String[] args) throws IllegalArgumentException, IllegalAccessException {

		List<Object> list = new ArrayList<Object>();
		Name n = new Name("sz", "yy", new Address("aa", "bb"));
		Person p = new Person(11, n);
		recurse(p.getClass().getDeclaredFields() ,p, list);
		System.out.println(list);
	}
}
class Person {
	
	private int age;
	private Name name;
	public Person(int age, Name name) {
		this.age = age;
		this.name = name;
	}
}
class Name {
	public Name(String first, String second, Address address) {
		this.first = first;
		this.second = second;
		this.address = address;
	}
	public String first;
	public String second;
	public Address address;
	
}
class Address {
	public Address(String age, String name) {
		this.age = age;
		this.name = name;
	}
	public String age;
	private String name;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值