Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kei2100/5317116 to your computer and use it in GitHub Desktop.
Save kei2100/5317116 to your computer and use it in GitHub Desktop.
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import org.apache.commons.beanutils.PropertyUtils;
/**
* JavaBeanをMapにディープdescribeします。PropertyUtils使って。
*
* @author kei2100
* */
@SuppressWarnings("rawtypes")
public class BeanDeepDescriber {
public static Map<String, Object> describe(Object bean) throws Exception {
@SuppressWarnings("unchecked")
Map<String, Object> described = PropertyUtils.describe(bean);
for (Entry<String, Object> entry : described.entrySet()) {
Object value = entry.getValue();
String key = entry.getKey();
// PropertyUtils.describeすると、bean自体のclassが、"class"というkeyで登録されるのでスキップ
if (key.equals("class")) continue;
// 必要に応じてvalueを再describeする
value = describeIfNecessary(value);
described.put(key, value);
}
// described.remove("class");
return described;
}
/*
* 様々な型のオブジェクトを必要に応じてdescribeする。
*/
private static Object describeIfNecessary(Object value) throws Exception {
if (value == null) {
return null;
}
if (isPrimitiveWrapperOrString(value)) {
return value;
}
if (value.getClass().isArray()) {
return arrayDescribe((Object[]) value);
}
if (value instanceof Collection<?>) {
return collectionDescribe((Collection<?>) value);
}
// ラッパー型や、コレクション、配列でない場合はJavaBeanとして判断して、再帰describe
return describe(value);
}
/*
* 配列のdescribe
*/
private static Object[] arrayDescribe(Object[] values) throws Exception {
Object[] described = new Object[values.length];
for (int i = 0; i < values.length; i++) {
Object value = values[i];
value = describeIfNecessary(value);
described[i] = value;
}
return described;
}
/*
* コレクションのdescribe
*/
private static Collection<?> collectionDescribe(Collection<?> value) throws Exception {
if (value instanceof List) {
return listDescribe((List) value);
}
// 他のコレクションは未実装なので必要に応じて実装してください。
throw new UnsupportedOperationException(value.getClass().toString() + ", can not describe.");
}
// リストのdescribe
@SuppressWarnings("unchecked")
private static List listDescribe(List list) throws Exception{
list = new ArrayList(list);
for (int i = 0; i < list.size(); i++) {
Object value = list.get(i);
value = describeIfNecessary(value);
list.set(i, value);
}
return list;
}
/*
* プリミティブラッパー型かあるいはStringかの判断
*/
private static boolean isPrimitiveWrapperOrString(Object value) {
Class<? extends Object> clazz = value.getClass();
if (clazz.equals(Boolean.class)) return true;
if (clazz.equals(Character.class)) return true;
if (clazz.equals(Byte.class)) return true;
if (clazz.equals(Short.class)) return true;
if (clazz.equals(Integer.class)) return true;
if (clazz.equals(Long.class)) return true;
if (clazz.equals(Float.class)) return true;
if (clazz.equals(Double.class)) return true;
if (clazz.equals(String.class)) return true;
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment