Java examples for Reflection:Property
get Properties By Reflect
/*// ww w .j a v a 2 s . com * Comet4J Copyright(c) 2011, http://code.google.com/p/comet4j/ This code is * licensed under BSD license. Use it as you wish, but keep this copyright * intact. */ //package com.java2s; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] argv) throws Exception { Object obj = "java2s.com"; System.out.println(getPropertiesByReflect(obj)); } @SuppressWarnings("rawtypes") public static Map getPropertiesByReflect(Object obj) throws Exception { if (obj == null) return null; Field[] fields = obj.getClass().getDeclaredFields(); if (fields == null || fields.length == 0) return null; Map<String, Object> map = new HashMap<String, Object>(); AccessibleObject.setAccessible(fields, true); for (Field field : fields) { map.put(field.getName(), field.get(obj)); } if (map.size() < 1) return null; return map; } }