通过字段类型获取值
//package com.nowjava; import java.lang.reflect.Field; /* 来 自 n o w j a v a . c o m*/ public class Main { @SuppressWarnings("unchecked") public static <T> T getValueByFieldType(Object obj, Class<T> fieldType) { Object value = null; for (Class<?> superClass = obj.getClass(); superClass != Object.class; superClass = superClass .getSuperclass()) { try { Field[] fields = superClass.getDeclaredFields(); for (Field f : fields) { if (f.getType() == fieldType) {/** from n o w j a v a . c o m**/ if (f.isAccessible()) { value = f.get(obj); break; } else { f.setAccessible(true); value = f.get(obj); f.setAccessible(false); break; } } }