1 53 54 package org.swixml; 55 56 import java.lang.reflect.Constructor ; 57 import java.lang.reflect.InvocationTargetException ; 58 import java.lang.reflect.Method ; 59 import java.util.*; 60 61 69 public final class DefaultFactory implements Factory { 70 71 72 private final Collection setters = new ArrayList(); 73 74 75 private final Class template; 76 77 78 protected Class [] parameterPriority = {String .class, float.class, double.class, boolean.class, char.class, long.class, byte.class, int.class}; 79 80 89 public DefaultFactory( Class template ) { 90 this.template = template; 91 this.registerSetters(); 95 } 96 97 102 protected int priority( Class type ) { 103 for (int i = 0; i < parameterPriority.length; i++) { 104 if (type.isAssignableFrom( parameterPriority[i] )) 105 return i; 106 } 107 return -1; 108 } 109 110 120 protected void registerSetters() { 121 Method [] methods = template.getMethods(); 125 for (int i = 0; i < methods.length; i++) { 126 String methodeName = methods[i].getName(); 127 if (methodeName.startsWith( Factory.SETTER_ID )) { 128 if (methods[i].getParameterTypes().length == 1) { 129 Class paraType = methods[i].getParameterTypes()[0]; 130 if (ConverterLibrary.getInstance().hasConverter( paraType )) { 131 Method m = this.getSetter( methodeName ); 135 if (m != null) { 136 Class cm = m.getDeclaringClass(); 142 Class cmi = methods[i].getDeclaringClass(); 143 144 if (cm.equals( cmi )) { 145 Class pType = m.getParameterTypes()[0]; 146 if (priority( pType ) < priority( paraType )) { 147 setters.remove( m ); 148 setters.add( methods[i] ); 149 } 150 } else if (cm.isAssignableFrom( cmi )) { 151 setters.remove( m ); 152 setters.add( methods[i] ); 153 } 154 } else { 155 setters.add( methods[i] ); 156 } 157 } 158 } 159 } 160 } 161 } 163 164 170 public Object newInstance() throws Exception { 171 return template.newInstance(); 172 } 173 174 175 183 public Object newInstance( Object parameter ) throws Exception { 184 Class pType = parameter.getClass(); Constructor [] ctors = template.getConstructors(); 186 for (int i = 0; i < ctors.length; i++) { 187 Class [] paraTypes = ctors[i].getParameterTypes(); 188 if (0 < paraTypes.length && paraTypes[0].isAssignableFrom( pType )) { 189 return ctors[i].newInstance( new Object []{parameter} ); 190 } 191 } 192 return template.newInstance(); 193 } 194 195 196 209 public Object newInstance( Object [] parameter ) throws InstantiationException , IllegalAccessException , InvocationTargetException { 210 if (parameter != null) { 211 Class pTypes[] = new Class [parameter.length]; Constructor constructors[] = template.getConstructors(); 213 Constructor ctor = null; 214 215 for (int i = 0; i < pTypes.length; i++) { 219 pTypes[i] = parameter[i].getClass(); 220 } 221 222 for (int i = 0; ctor == null && i < constructors.length; i++) { 226 Class cParams[] = constructors[i].getParameterTypes(); 228 if (cParams.length == pTypes.length) { 229 ctor = constructors[i]; for (int j = 0; ctor != null && j < cParams.length; j++) { 231 if (cParams[j].equals( Object .class )) { 232 if (!cParams[j].equals( pTypes[j] )) { 233 ctor = null; } 235 } else { 236 if (!cParams[j].isAssignableFrom( pTypes[j] )) { 237 ctor = null; } 239 } 240 } } 242 } 244 if (ctor != null) { 248 return ctor.newInstance( parameter ); 249 } else { throw new IllegalArgumentException ( "unable to find constructor, accepting:" + pTypes ); 251 } 252 } else { 253 return template.newInstance(); 254 } 255 } 256 257 258 261 public Class getTemplate() { 262 return template; 263 } 264 265 268 public Collection getSetters() { 269 return setters; 270 } 271 272 278 public Method getSetter( Class template ) { 279 Method method = null; 280 Iterator it = setters.iterator(); 281 while (it != null && it.hasNext()) { 282 Method m = (Method ) it.next(); 283 Class [] paraTypes = m.getParameterTypes(); 284 if (paraTypes != null && 0 < paraTypes.length && template.equals( paraTypes[0] )) { 285 method = m; 286 break; 287 } 288 } return method; 290 } 291 292 302 public Method getSetter( String name ) { 303 Method method = null; 304 Iterator it = setters.iterator(); 305 while (it != null && it.hasNext()) { 306 Method m = (Method ) it.next(); 307 if (m.getName().equals( name )) { 308 method = m; 309 break; 310 } 311 } 312 return method; 313 } 314 315 328 public Method guessSetter( String name ) { 329 330 Method method = null; 331 Iterator it = setters.iterator(); 332 name = (Factory.SETTER_ID + name).toLowerCase(); 333 while (it != null && it.hasNext()) { 334 Method m = (Method ) it.next(); 335 if (m.getName().toLowerCase().equals( name )) { 336 method = m; 337 break; 338 } 339 } 340 return method; 341 } 342 343 347 public void removeSetter( Method method ) { 348 setters.remove( method ); 349 } 350 } | Popular Tags |