package com.sample; import javax.xml.bind.annotation.XmlType; import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by konishiyuji on 2015/12/26. */ public class Main { public static void main(String[] args) { XyzDto dto = new XyzDto(); Map map = getMap(dto); System.out.println(map); } public static Map getMap(Object dto) { Class clazz = dto.getClass(); Annotation[] annotations = clazz.getAnnotations(); Annotation annotation = null; int i; for (i = 0; i < annotations.length; i++) { annotation = annotations[i]; if (annotation instanceof XmlType) { break; } } if (i >= annotations.length){ return null; } XmlType xmlType = (XmlType) annotation; String[] propOrder = xmlType.propOrder(); HashMap map = new HashMap(); try { for (int j = 0; j < propOrder.length; j++) { String key = propOrder[j]; Field field = clazz.getDeclaredField(key); field.setAccessible(true); Object value = field.get(dto); map.put(key, value); } } catch (NoSuchFieldException | IllegalAccessException e) { return null; } return map; } }