1 24 package org.riotfamily.common.beans; 25 26 import java.beans.PropertyDescriptor ; 27 import java.beans.PropertyEditor ; 28 import java.lang.reflect.Method ; 29 import java.util.ArrayList ; 30 import java.util.Collection ; 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.List ; 34 import java.util.Map ; 35 import java.util.regex.Matcher ; 36 import java.util.regex.Pattern ; 37 38 import org.springframework.beans.BeanUtils; 39 import org.springframework.beans.FatalBeanException; 40 import org.springframework.util.Assert; 41 import org.springframework.util.ClassUtils; 42 import org.springframework.util.ReflectionUtils; 43 import org.springframework.util.StringUtils; 44 45 48 public final class PropertyUtils { 49 50 private static DefaultPropertyEditorRegistry registry = 51 new DefaultPropertyEditorRegistry(); 52 53 private static Pattern expressionPattern = Pattern.compile( 54 "\\$\\{(.*?)\\}"); 55 56 private PropertyUtils() { 57 } 58 59 public static Object getProperty(Object bean, String name) { 60 if (bean == null) { 61 return null; 62 } 63 ProtectedBeanWrapper wrapper = new ProtectedBeanWrapper(bean); 64 return wrapper.getPropertyValue(name); 65 } 66 67 public static Object getProperty(Object bean, String name, 68 Class requiredType) { 69 70 Object value = getProperty(bean, name); 71 if (value != null) { 72 Assert.isInstanceOf(requiredType, value); 73 } 74 return value; 75 } 76 77 public static String getPropertyAsString(Object bean, String name) { 78 return convertToString(getProperty(bean, name)); 79 } 80 81 public static void setProperty(Object bean, String name, Object value) { 82 ProtectedBeanWrapper wrapper = new ProtectedBeanWrapper(bean); 83 wrapper.setPropertyValue(name, value); 84 } 85 86 public static void setPropertyAsString(Object bean, String name, String s) { 87 Class type = getPropertyType(bean.getClass(), name); 88 Object value = convert(s, type); 89 setProperty(bean, name, value); 90 } 91 92 96 public static Map getProperties(Object bean) { 97 PropertyDescriptor [] pd = BeanUtils.getPropertyDescriptors(bean.getClass()); 98 HashMap properties = new HashMap (); 99 for (int i = 0; i < pd.length; i++) { 100 Object value = ReflectionUtils.invokeMethod(pd[i].getReadMethod(), bean); 101 properties.put(pd[i].getName(), value); 102 } 103 return properties; 104 } 105 106 110 public static Map getProperties(Object bean, String [] propertyNames) { 111 HashMap properties = new HashMap (); 112 for (int i = 0; i < propertyNames.length; i++) { 113 String name = propertyNames[i]; 114 properties.put(name, getProperty(bean, name)); 115 } 116 return properties; 117 } 118 119 122 public static String evaluate(String expression, Object bean) { 123 Matcher matcher = expressionPattern.matcher(expression); 124 StringBuffer sb = new StringBuffer (); 125 while (matcher.find()) { 126 String property = matcher.group(1); 127 Object value = getProperty(bean, property); 128 matcher.appendReplacement(sb, convertToString(value)); 129 } 130 matcher.appendTail(sb); 131 return sb.toString(); 132 } 133 134 public static Object convert(String s, Class targetClass) { 135 if (targetClass.equals(String .class)) { 136 return s; 137 } 138 PropertyEditor pe = registry.findEditor(targetClass); 139 Assert.notNull(pe, "No PropertyEditor found for class: " + targetClass); 140 synchronized (pe) { 141 pe.setAsText(s); 142 return pe.getValue(); 143 } 144 } 145 146 149 public static String convertToString(Object value) { 150 if (value != null) { 151 if (!(value instanceof String )) { 152 PropertyEditor pe = registry.findEditor(value.getClass()); 153 if (pe != null) { 154 synchronized (pe) { 155 pe.setValue(value); 156 return pe.getAsText(); 157 } 158 } 159 } 160 return value.toString(); 161 } 162 return null; 163 } 164 165 168 public static PropertyDescriptor getPropertyDescriptor( 169 Class clazz, String property) { 170 171 return BeanUtils.getPropertyDescriptor(clazz, property); 172 } 173 174 public static Class getPropertyType(Class clazz, String property) { 175 PropertyDescriptor pd = BeanUtils.getPropertyDescriptor(clazz, property); 176 Assert.notNull(pd, "Property '" + property + "' not found in class " + clazz); 177 return pd.getPropertyType(); 178 } 179 180 183 public static Method findReadMethod(Class clazz, String property) { 184 String methodName = "get" + StringUtils.capitalize(property); 185 Method readMethod = BeanUtils.findDeclaredMethod(clazz, methodName, null); 186 if (readMethod != null) { 187 readMethod.setAccessible(true); 188 } 189 return readMethod; 190 } 191 192 195 public static Method findWriteMethod(Class clazz, String property) { 196 String methodName = "set" + StringUtils.capitalize(property); 197 Method writeMethod = BeanUtils.findDeclaredMethodWithMinimalParameters( 198 clazz, methodName); 199 200 if (writeMethod != null) { 201 writeMethod.setAccessible(true); 202 } 203 return writeMethod; 204 } 205 206 209 public static Class getDeclaringClass(Class clazz, 210 String property) { 211 212 PropertyDescriptor [] descriptors = 213 BeanUtils.getPropertyDescriptors(clazz); 214 215 for (int i = 0; i < descriptors.length; i++) { 216 if (descriptors[i].getName().equals(property)) { 217 Method getter = descriptors[i].getReadMethod(); 218 if (getter == null) { 219 return clazz; 220 } 221 return getter.getDeclaringClass(); 222 } 223 } 224 return ClassUtils.getUserClass(clazz); 225 } 226 227 234 public static Object newInstance(String className) { 235 try { 236 Class clazz = ClassUtils.forName(className); 237 return BeanUtils.instantiateClass(clazz); 238 } 239 catch (ClassNotFoundException e) { 240 throw new FatalBeanException(e.getMessage(), e); 241 } 242 } 243 244 252 public static List partition(Collection c, String titleProperty) { 253 ArrayList groups = new ArrayList (); 254 Iterator it = c.iterator(); 255 ObjectGroup group = null; 256 while (it.hasNext()) { 257 Object item = it.next(); 258 Object title = getProperty(item, titleProperty); 259 if (group == null || (title != null 260 && !title.equals(group.getTitle()))) { 261 262 group = new ObjectGroup(title, item); 263 groups.add(group); 264 } 265 else { 266 group.add(item); 267 } 268 } 269 return groups; 270 } 271 272 } 273 | Popular Tags |