1 package com.quadcap.http.servlets.jsp; 2 3 40 41 import java.beans.BeanInfo ; 42 import java.beans.Introspector ; 43 import java.beans.IntrospectionException ; 44 import java.beans.PropertyDescriptor ; 45 46 import java.lang.reflect.InvocationTargetException ; 47 import java.lang.reflect.Method ; 48 49 import javax.servlet.http.HttpServletRequest ; 50 51 import com.quadcap.util.Debug; 52 53 58 public class PropertyUtils { 59 public static void setPropertiesFromRequest(Object obj, 60 HttpServletRequest request) 61 throws IntrospectionException , InvocationTargetException , 62 IllegalAccessException 63 { 64 BeanInfo info = Introspector.getBeanInfo(obj.getClass()); 65 PropertyDescriptor [] pds = info.getPropertyDescriptors(); 66 for (int i = 0; i < pds.length; i++) { 67 PropertyDescriptor pd = pds[i]; 68 String name = pd.getName(); 69 String [] vals = request.getParameterValues(name); 70 if (vals == null || vals.length == 0 || vals[0].length() == 0) continue; 71 Class pt = pd.getPropertyType(); 72 if (pt.isArray()) { 73 setProps(obj, pd, vals); 74 } else { 75 setProp(obj, pd, vals[0]); 76 } 77 } 78 } 79 80 public static void setPropertyFromRequestParameter(Object obj, 81 HttpServletRequest request, 82 String name) 83 throws IntrospectionException , InvocationTargetException , 84 IllegalAccessException 85 { 86 BeanInfo info = Introspector.getBeanInfo(obj.getClass()); 87 PropertyDescriptor [] pds = info.getPropertyDescriptors(); 88 for (int i = 0; i < pds.length; i++) { 89 PropertyDescriptor pd = pds[i]; 90 String pname = pd.getName(); 91 if (!name.equals(pname)) continue; 92 String [] vals = request.getParameterValues(name); 93 if (vals == null || vals.length == 0 || vals[0].length() == 0) continue; 94 Class pt = pd.getPropertyType(); 95 Debug.println("pt.isArray() = " + pt.isArray()); 96 if (pt.isArray()) { 97 setProps(obj, pd, vals); 98 } else { 99 setProp(obj, pd, vals[0]); 100 } 101 } 102 } 103 104 public static void setPropertyFromValue(Object obj, String name, String val) 105 throws IntrospectionException , InvocationTargetException , 106 IllegalAccessException 107 { 108 BeanInfo info = Introspector.getBeanInfo(obj.getClass()); 109 PropertyDescriptor [] pds = info.getPropertyDescriptors(); 110 for (int i = 0; i < pds.length; i++) { 111 PropertyDescriptor pd = pds[i]; 112 String pname = pd.getName(); 113 if (name.equals(pname)) { 114 setProp(obj, pd, val); 115 } 116 } 117 } 118 119 public static void setProp(Object obj, PropertyDescriptor pd, String val) 120 throws IntrospectionException , InvocationTargetException , 121 IllegalAccessException 122 { 123 Class pt = pd.getPropertyType(); 124 Method w = pd.getWriteMethod(); 125 Object [] args = new Object [1]; 126 127 if (pt.getName().equals("java.lang.Boolean")) { 128 args[0] = Boolean.valueOf(val); 129 } else if (pt.getName().equals("java.lang.Integer")) { 130 args[0] = Integer.valueOf(val); 131 } else if (pt.getName().equals("java.lang.Byte")) { 132 args[0] = Byte.valueOf(val); 133 } else if (pt.getName().equals("java.lang.Short")) { 134 args[0] = Short.valueOf(val); 135 } else if (pt.getName().equals("java.lang.Long")) { 136 args[0] = Long.valueOf(val); 137 } else if (pt.getName().equals("java.lang.Double")) { 138 args[0] = Double.valueOf(val); 139 } else if (pt.getName().equals("java.lang.Float")) { 140 args[0] = Float.valueOf(val); 141 } else if (pt.getName().equals("java.lang.Integer")) { 142 args[0] = Integer.valueOf(val); 143 } else if (pt.getName().equals("java.lang.String")) { 144 args[0] = val; 145 } 146 w.invoke(obj, args); 147 } 148 149 public static void setProps(Object obj, PropertyDescriptor pd, String [] vals) 150 throws IntrospectionException , InvocationTargetException , 151 IllegalAccessException 152 { 153 Class pat = pd.getPropertyType(); 154 Class pt = pat.getComponentType(); 155 Method w = pd.getWriteMethod(); 156 Object [] args = new Object [1]; 157 158 if (pt.getName().equals("java.lang.Boolean")) { 159 Boolean [] args1 = new Boolean [vals.length]; 160 args[0] = args1; 161 for (int i = 0; i < vals.length; i++) { 162 args1[i] = Boolean.valueOf(vals[i]); 163 } 164 } else if (pt.getName().equals("java.lang.Integer")) { 165 Integer [] args1 = new Integer [vals.length]; 166 args[0] = args1; 167 for (int i = 0; i < vals.length; i++) { 168 args1[i] = Integer.valueOf(vals[i]); 169 } 170 } else if (pt.getName().equals("java.lang.Byte")) { 171 Byte [] args1 = new Byte [vals.length]; 172 args[0] = args1; 173 for (int i = 0; i < vals.length; i++) { 174 args1[i] = Byte.valueOf(vals[i]); 175 } 176 } else if (pt.getName().equals("java.lang.Short")) { 177 Short [] args1 = new Short [vals.length]; 178 args[0] = args1; 179 for (int i = 0; i < vals.length; i++) { 180 args1[i] = Short.valueOf(vals[i]); 181 } 182 } else if (pt.getName().equals("java.lang.Long")) { 183 Long [] args1 = new Long [vals.length]; 184 args[0] = args1; 185 for (int i = 0; i < vals.length; i++) { 186 args1[i] = Long.valueOf(vals[i]); 187 } 188 } else if (pt.getName().equals("java.lang.Double")) { 189 Double [] args1 = new Double [vals.length]; 190 args[0] = args1; 191 for (int i = 0; i < vals.length; i++) { 192 args1[i] = Double.valueOf(vals[i]); 193 } 194 } else if (pt.getName().equals("java.lang.Float")) { 195 Float [] args1 = new Float [vals.length]; 196 args[0] = args1; 197 for (int i = 0; i < vals.length; i++) { 198 args1[i] = Float.valueOf(vals[i]); 199 } 200 } else if (pt.getName().equals("java.lang.String")) { 201 args[0] = vals; 202 } 203 w.invoke(obj, args); 204 } 205 206 public static String getProperty(Object obj, String name) 207 throws IntrospectionException , InvocationTargetException , 208 IllegalAccessException 209 { 210 211 BeanInfo info = Introspector.getBeanInfo(obj.getClass()); 212 PropertyDescriptor [] pds = info.getPropertyDescriptors(); 213 for (int i = 0; i < pds.length; i++) { 214 PropertyDescriptor pd = pds[i]; 215 String pname = pd.getName(); 216 if (name.equals(pname)) { 217 Method r = pd.getReadMethod(); 218 Object [] args = new Object [0]; 219 Object res = r.invoke(obj, args); 220 return String.valueOf(res); 221 } 222 } 223 return ""; 224 } 225 } 226 | Popular Tags |