1 7 8 package java.beans; 9 10 import java.lang.ref.Reference ; 11 12 import java.lang.reflect.Method ; 13 14 23 24 public class IndexedPropertyDescriptor extends PropertyDescriptor { 25 26 private Reference indexedPropertyTypeRef; 27 private Reference indexedReadMethodRef; 28 private Reference indexedWriteMethodRef; 29 30 private String indexedReadMethodName; 31 private String indexedWriteMethodName; 32 33 48 public IndexedPropertyDescriptor(String propertyName, Class <?> beanClass) 49 throws IntrospectionException { 50 this(propertyName, beanClass, 51 "get" + capitalize(propertyName), 52 "set" + capitalize(propertyName), 53 "get" + capitalize(propertyName), 54 "set" + capitalize(propertyName)); 55 } 56 57 79 public IndexedPropertyDescriptor(String propertyName, Class <?> beanClass, 80 String readMethodName, String writeMethodName, 81 String indexedReadMethodName, String indexedWriteMethodName) 82 throws IntrospectionException { 83 super(propertyName, beanClass, readMethodName, writeMethodName); 84 85 this.indexedReadMethodName = indexedReadMethodName; 86 if (indexedReadMethodName != null && getIndexedReadMethod() == null) { 87 throw new IntrospectionException ("Method not found: " + indexedReadMethodName); 88 } 89 90 this.indexedWriteMethodName = indexedWriteMethodName; 91 if (indexedWriteMethodName != null && getIndexedWriteMethod() == null) { 92 throw new IntrospectionException ("Method not found: " + indexedWriteMethodName); 93 } 94 findIndexedPropertyType(getIndexedReadMethod(), getIndexedWriteMethod()); 96 } 97 98 115 public IndexedPropertyDescriptor(String propertyName, Method readMethod, Method writeMethod, 116 Method indexedReadMethod, Method indexedWriteMethod) 117 throws IntrospectionException { 118 super(propertyName, readMethod, writeMethod); 119 120 setIndexedReadMethod0(indexedReadMethod); 121 setIndexedWriteMethod0(indexedWriteMethod); 122 123 setIndexedPropertyType(findIndexedPropertyType(indexedReadMethod, indexedWriteMethod)); 125 } 126 127 135 public synchronized Method getIndexedReadMethod() { 136 Method indexedReadMethod = getIndexedReadMethod0(); 137 if (indexedReadMethod == null) { 138 Class cls = getClass0(); 139 if (cls == null || 140 (indexedReadMethodName == null && indexedReadMethodRef == null)) { 141 return null; 143 } 144 if (indexedReadMethodName == null) { 145 Class type = getIndexedPropertyType0(); 146 if (type == boolean.class || type == null) { 147 indexedReadMethodName = "is" + getBaseName(); 148 } else { 149 indexedReadMethodName = "get" + getBaseName(); 150 } 151 } 152 153 Class [] args = { int.class }; 154 155 indexedReadMethod = Introspector.findMethod(cls, indexedReadMethodName, 156 1, args); 157 if (indexedReadMethod == null) { 158 indexedReadMethodName = "get" + getBaseName(); 160 indexedReadMethod = Introspector.findMethod(cls, indexedReadMethodName, 161 1, args); 162 } 163 setIndexedReadMethod0(indexedReadMethod); 164 } 165 return indexedReadMethod; 166 } 167 168 173 public synchronized void setIndexedReadMethod(Method readMethod) 174 throws IntrospectionException { 175 176 setIndexedPropertyType(findIndexedPropertyType(readMethod, 178 getIndexedWriteMethod0())); 179 setIndexedReadMethod0(readMethod); 180 } 181 182 private void setIndexedReadMethod0(Method readMethod) { 183 if (readMethod == null) { 184 indexedReadMethodName = null; 185 indexedReadMethodRef = null; 186 return; 187 } 188 setClass0(readMethod.getDeclaringClass()); 189 190 indexedReadMethodName = readMethod.getName(); 191 indexedReadMethodRef = createReference(readMethod); 192 } 193 194 195 202 public synchronized Method getIndexedWriteMethod() { 203 Method indexedWriteMethod = getIndexedWriteMethod0(); 204 if (indexedWriteMethod == null) { 205 Class cls = getClass0(); 206 if (cls == null || 207 (indexedWriteMethodName == null && indexedWriteMethodRef == null)) { 208 return null; 210 } 211 212 Class type = getIndexedPropertyType0(); 216 if (type == null) { 217 try { 218 type = findIndexedPropertyType(getIndexedReadMethod(), null); 219 setIndexedPropertyType(type); 220 } catch (IntrospectionException ex) { 221 Class propType = getPropertyType(); 223 if (propType.isArray()) { 224 type = propType.getComponentType(); 225 } 226 } 227 } 228 229 if (indexedWriteMethodName == null) { 230 indexedWriteMethodName = "set" + getBaseName(); 231 } 232 indexedWriteMethod = Introspector.findMethod(cls, indexedWriteMethodName, 233 2, (type == null) ? null : new Class [] { int.class, type }); 234 setIndexedWriteMethod0(indexedWriteMethod); 235 } 236 return indexedWriteMethod; 237 } 238 239 244 public synchronized void setIndexedWriteMethod(Method writeMethod) 245 throws IntrospectionException { 246 247 Class type = findIndexedPropertyType(getIndexedReadMethod(), 249 writeMethod); 250 setIndexedPropertyType(type); 251 setIndexedWriteMethod0(writeMethod); 252 } 253 254 private void setIndexedWriteMethod0(Method writeMethod) { 255 if (writeMethod == null) { 256 indexedWriteMethodName = null; 257 indexedWriteMethodRef = null; 258 return; 259 } 260 setClass0(writeMethod.getDeclaringClass()); 261 262 indexedWriteMethodName = writeMethod.getName(); 263 indexedWriteMethodRef = createReference(writeMethod); 264 } 265 266 273 public synchronized Class <?> getIndexedPropertyType() { 274 Class type = getIndexedPropertyType0(); 275 if (type == null) { 276 try { 277 type = findIndexedPropertyType(getIndexedReadMethod(), 278 getIndexedWriteMethod()); 279 setIndexedPropertyType(type); 280 } catch (IntrospectionException ex) { 281 } 283 } 284 return type; 285 } 286 287 289 private void setIndexedPropertyType(Class type) { 290 indexedPropertyTypeRef = createReference(type); 291 } 292 293 private Class getIndexedPropertyType0() { 294 return (Class )getObject(indexedPropertyTypeRef); 295 } 296 297 private Method getIndexedReadMethod0() { 298 return (Method )getObject(indexedReadMethodRef); 299 } 300 301 private Method getIndexedWriteMethod0() { 302 return (Method )getObject(indexedWriteMethodRef); 303 } 304 305 private Class findIndexedPropertyType(Method indexedReadMethod, 306 Method indexedWriteMethod) 307 throws IntrospectionException { 308 Class indexedPropertyType = null; 309 310 if (indexedReadMethod != null) { 311 Class params[] = indexedReadMethod.getParameterTypes(); 312 if (params.length != 1) { 313 throw new IntrospectionException ("bad indexed read method arg count"); 314 } 315 if (params[0] != Integer.TYPE) { 316 throw new IntrospectionException ("non int index to indexed read method"); 317 } 318 indexedPropertyType = indexedReadMethod.getReturnType(); 319 if (indexedPropertyType == Void.TYPE) { 320 throw new IntrospectionException ("indexed read method returns void"); 321 } 322 } 323 if (indexedWriteMethod != null) { 324 Class params[] = indexedWriteMethod.getParameterTypes(); 325 if (params.length != 2) { 326 throw new IntrospectionException ("bad indexed write method arg count"); 327 } 328 if (params[0] != Integer.TYPE) { 329 throw new IntrospectionException ("non int index to indexed write method"); 330 } 331 if (indexedPropertyType != null && indexedPropertyType != params[1]) { 332 throw new IntrospectionException ( 333 "type mismatch between indexed read and indexed write methods: " 334 + getName()); 335 } 336 indexedPropertyType = params[1]; 337 } 338 Class propertyType = getPropertyType(); 339 if (propertyType != null && (!propertyType.isArray() || 340 propertyType.getComponentType() != indexedPropertyType)) { 341 throw new IntrospectionException ("type mismatch between indexed and non-indexed methods: " 342 + getName()); 343 } 344 return indexedPropertyType; 345 } 346 347 355 public boolean equals(Object obj) { 356 if (this == obj) { 359 return true; 360 } 361 362 if (obj != null && obj instanceof IndexedPropertyDescriptor ) { 363 IndexedPropertyDescriptor other = (IndexedPropertyDescriptor )obj; 364 Method otherIndexedReadMethod = other.getIndexedReadMethod(); 365 Method otherIndexedWriteMethod = other.getIndexedWriteMethod(); 366 367 if (!compareMethods(getIndexedReadMethod(), otherIndexedReadMethod)) { 368 return false; 369 } 370 371 if (!compareMethods(getIndexedWriteMethod(), otherIndexedWriteMethod)) { 372 return false; 373 } 374 375 if (getIndexedPropertyType() != other.getIndexedPropertyType()) { 376 return false; 377 } 378 return super.equals(obj); 379 } 380 return false; 381 } 382 383 391 392 IndexedPropertyDescriptor(PropertyDescriptor x, PropertyDescriptor y) { 393 super(x,y); 394 if (x instanceof IndexedPropertyDescriptor ) { 395 IndexedPropertyDescriptor ix = (IndexedPropertyDescriptor )x; 396 try { 397 Method xr = ix.getIndexedReadMethod(); 398 if (xr != null) { 399 setIndexedReadMethod(xr); 400 } 401 402 Method xw = ix.getIndexedWriteMethod(); 403 if (xw != null) { 404 setIndexedWriteMethod(xw); 405 } 406 } catch (IntrospectionException ex) { 407 throw new AssertionError (ex); 409 } 410 } 411 if (y instanceof IndexedPropertyDescriptor ) { 412 IndexedPropertyDescriptor iy = (IndexedPropertyDescriptor )y; 413 try { 414 Method yr = iy.getIndexedReadMethod(); 415 if (yr != null && yr.getDeclaringClass() == getClass0()) { 416 setIndexedReadMethod(yr); 417 } 418 419 Method yw = iy.getIndexedWriteMethod(); 420 if (yw != null && yw.getDeclaringClass() == getClass0()) { 421 setIndexedWriteMethod(yw); 422 } 423 } catch (IntrospectionException ex) { 424 throw new AssertionError (ex); 426 } 427 } 428 } 429 430 434 IndexedPropertyDescriptor(IndexedPropertyDescriptor old) { 435 super(old); 436 indexedReadMethodRef = old.indexedReadMethodRef; 437 indexedWriteMethodRef = old.indexedWriteMethodRef; 438 indexedPropertyTypeRef = old.indexedPropertyTypeRef; 439 indexedWriteMethodName = old.indexedWriteMethodName; 440 indexedReadMethodName = old.indexedReadMethodName; 441 } 442 443 450 public int hashCode() { 451 int result = super.hashCode(); 452 453 result = 37 * result + ((indexedWriteMethodName == null) ? 0 : 454 indexedWriteMethodName.hashCode()); 455 result = 37 * result + ((indexedReadMethodName == null) ? 0 : 456 indexedReadMethodName.hashCode()); 457 result = 37 * result + ((getIndexedPropertyType() == null) ? 0 : 458 getIndexedPropertyType().hashCode()); 459 460 return result; 461 } 462 463 479 } 480 | Popular Tags |