1 package org.josql.internal; 2 3 import java.util.List ; 4 import java.util.ArrayList ; 5 import java.util.TreeMap ; 6 7 import java.lang.reflect.Method ; 8 import java.lang.reflect.Modifier ; 9 import java.lang.reflect.Field ; 10 import java.lang.reflect.InvocationTargetException ; 11 12 import java.util.StringTokenizer ; 13 14 import com.gentlyweb.utils.Getter; 15 16 120 public class Setter 121 { 122 123 private Getter getter = null; 124 private Object setter = null; 125 private Class clazz = null; 126 127 131 public Setter (String ref, 132 Class clazz, 133 Class [] parmTypes) 134 throws IllegalArgumentException , 135 NoSuchMethodException 136 { 137 138 this.clazz = clazz; 139 140 StringTokenizer t = new StringTokenizer (ref, 141 "."); 142 143 StringBuffer getRef = new StringBuffer (); 144 145 if (t.countTokens () > 1) 146 { 147 148 while (t.hasMoreTokens ()) 150 { 151 152 getRef.append (t.nextToken ()); 153 154 if (t.countTokens () > 1) 155 { 156 157 getRef.append ('.'); 158 159 } 160 161 if (t.countTokens () == 1) 162 { 163 164 this.getter = new Getter (getRef.toString (), 166 clazz); 167 168 clazz = this.getter.getType (); 170 171 break; 172 173 } 174 175 } 176 177 } 178 179 String set = t.nextToken (); 181 182 Field [] fields = clazz.getFields (); 184 185 Field f = null; 186 187 for (int i = 0; i < fields.length; i++) 189 { 190 191 if (fields[i].getName ().equals (set)) 192 { 193 194 f = fields[i]; 196 197 this.setter = f; 198 199 return; 200 201 } 202 203 } 204 205 207 210 StringBuffer name = new StringBuffer (set); 212 213 name.setCharAt (0, 214 Character.toUpperCase (name.charAt (0))); 215 216 name.insert (0, 217 "set"); 218 219 String nName = name.toString (); 220 221 List meths = new ArrayList (); 222 223 Utilities.getMethods (clazz, 224 nName, 225 Modifier.PUBLIC, 226 meths); 227 228 TreeMap sm = new TreeMap (); 229 230 for (int i = 0; i < meths.size (); i++) 232 { 233 234 Method m = (Method ) meths.get (i); 235 236 Class [] mpts = m.getParameterTypes (); 237 238 int score = Utilities.matchMethodArgs (mpts, 239 parmTypes); 240 241 if (score > 0) 242 { 243 244 sm.put (new Integer (score), 245 m); 246 247 } 248 249 } 250 251 if (sm.size () > 0) 253 { 254 255 this.setter = (Method ) sm.get (sm.lastKey ()); 256 257 } 258 259 if (this.setter == null) 260 { 261 262 meths = new ArrayList (); 263 264 Utilities.getMethods (clazz, 265 set, 266 Modifier.PUBLIC, 267 meths); 268 269 sm = new TreeMap (); 270 271 for (int i = 0; i < meths.size (); i++) 273 { 274 275 Method m = (Method ) meths.get (i); 276 277 Class [] mpts = m.getParameterTypes (); 278 279 int score = Utilities.matchMethodArgs (mpts, 280 parmTypes); 281 282 if (score > 0) 283 { 284 285 sm.put (new Integer (score), 286 m); 287 288 } 289 290 } 291 292 if (sm.size () > 0) 294 { 295 296 this.setter = (Method ) sm.get (sm.lastKey ()); 297 298 } 299 300 } 301 302 if (this.setter == null) 303 { 304 305 throw new IllegalArgumentException ("Unable to find required method: " + 306 nName + 307 " or: " + 308 set + 309 " in class: " + 310 clazz.getName () + 311 " taking parms: " + 312 parmTypes); 313 314 } 315 316 } 317 318 public Class getBaseClass () 319 { 320 321 return this.clazz; 322 323 } 324 325 public void setValue (Object target, 326 Object value) 327 throws IllegalAccessException , 328 InvocationTargetException , 329 IllegalArgumentException 330 { 331 332 Object [] vals = {value}; 333 334 this.setValue (target, 335 vals); 336 337 } 338 339 public void setValue (Object target, 340 Object [] values) 341 throws IllegalAccessException , 342 InvocationTargetException , 343 IllegalArgumentException 344 { 345 346 if (this.getter != null) 348 { 349 350 target = this.getter.getValue (target); 351 352 } 353 354 if (this.setter instanceof Field ) 356 { 357 358 Field f = (Field ) this.setter; 359 360 f.set (target, 361 values[0]); 362 363 return; 364 365 } 366 367 if (this.setter instanceof Method ) 368 { 369 370 Method m = (Method ) this.setter; 371 372 m.invoke (target, 373 Utilities.convertArgs (values, 374 m.getParameterTypes ())); 375 376 } 377 378 } 379 380 386 public Class getType () 387 { 388 389 if (this.setter instanceof Method ) 391 { 392 393 Method m = (Method ) this.setter; 394 395 Class [] parms = m.getParameterTypes (); 396 397 return parms[0]; 398 399 } 400 401 if (this.setter instanceof Field ) 402 { 403 404 Field f = (Field ) this.setter; 406 407 return f.getType (); 408 409 } 410 411 return null; 412 413 } 414 415 } 416 | Popular Tags |