author | lana |
Tue, 01 Jun 2010 14:17:38 -0700 | |
changeset 5597 | ab490f66d2cf |
parent 5506 | 202f599c92aa |
child 5947 | 0e6f2837eeca |
permissions | -rw-r--r-- |
2 | 1 |
/* |
5506 | 2 |
* Copyright (c) 1996, 2010, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package java.beans; |
|
27 |
||
28 |
import java.lang.ref.Reference; |
|
29 |
import java.lang.reflect.Method; |
|
30 |
import java.lang.reflect.Constructor; |
|
31 |
||
32 |
/** |
|
33 |
* A PropertyDescriptor describes one property that a Java Bean |
|
34 |
* exports via a pair of accessor methods. |
|
35 |
*/ |
|
36 |
public class PropertyDescriptor extends FeatureDescriptor { |
|
37 |
||
38 |
private Reference<Class> propertyTypeRef; |
|
39 |
private Reference<Method> readMethodRef; |
|
40 |
private Reference<Method> writeMethodRef; |
|
41 |
private Reference<Class> propertyEditorClassRef; |
|
42 |
||
43 |
private boolean bound; |
|
44 |
private boolean constrained; |
|
45 |
||
46 |
// The base name of the method name which will be prefixed with the |
|
47 |
// read and write method. If name == "foo" then the baseName is "Foo" |
|
48 |
private String baseName; |
|
49 |
||
50 |
private String writeMethodName; |
|
51 |
private String readMethodName; |
|
52 |
||
53 |
/** |
|
54 |
* Constructs a PropertyDescriptor for a property that follows |
|
55 |
* the standard Java convention by having getFoo and setFoo |
|
56 |
* accessor methods. Thus if the argument name is "fred", it will |
|
57 |
* assume that the writer method is "setFred" and the reader method |
|
58 |
* is "getFred" (or "isFred" for a boolean property). Note that the |
|
59 |
* property name should start with a lower case character, which will |
|
60 |
* be capitalized in the method names. |
|
61 |
* |
|
62 |
* @param propertyName The programmatic name of the property. |
|
63 |
* @param beanClass The Class object for the target bean. For |
|
64 |
* example sun.beans.OurButton.class. |
|
65 |
* @exception IntrospectionException if an exception occurs during |
|
66 |
* introspection. |
|
67 |
*/ |
|
68 |
public PropertyDescriptor(String propertyName, Class<?> beanClass) |
|
69 |
throws IntrospectionException { |
|
70 |
this(propertyName, beanClass, |
|
71 |
Introspector.IS_PREFIX + NameGenerator.capitalize(propertyName), |
|
72 |
Introspector.SET_PREFIX + NameGenerator.capitalize(propertyName)); |
|
73 |
} |
|
74 |
||
75 |
/** |
|
76 |
* This constructor takes the name of a simple property, and method |
|
77 |
* names for reading and writing the property. |
|
78 |
* |
|
79 |
* @param propertyName The programmatic name of the property. |
|
80 |
* @param beanClass The Class object for the target bean. For |
|
81 |
* example sun.beans.OurButton.class. |
|
82 |
* @param readMethodName The name of the method used for reading the property |
|
83 |
* value. May be null if the property is write-only. |
|
84 |
* @param writeMethodName The name of the method used for writing the property |
|
85 |
* value. May be null if the property is read-only. |
|
86 |
* @exception IntrospectionException if an exception occurs during |
|
87 |
* introspection. |
|
88 |
*/ |
|
89 |
public PropertyDescriptor(String propertyName, Class<?> beanClass, |
|
90 |
String readMethodName, String writeMethodName) |
|
91 |
throws IntrospectionException { |
|
92 |
if (beanClass == null) { |
|
93 |
throw new IntrospectionException("Target Bean class is null"); |
|
94 |
} |
|
95 |
if (propertyName == null || propertyName.length() == 0) { |
|
96 |
throw new IntrospectionException("bad property name"); |
|
97 |
} |
|
98 |
if ("".equals(readMethodName) || "".equals(writeMethodName)) { |
|
99 |
throw new IntrospectionException("read or write method name should not be the empty string"); |
|
100 |
} |
|
101 |
setName(propertyName); |
|
102 |
setClass0(beanClass); |
|
103 |
||
104 |
this.readMethodName = readMethodName; |
|
105 |
if (readMethodName != null && getReadMethod() == null) { |
|
106 |
throw new IntrospectionException("Method not found: " + readMethodName); |
|
107 |
} |
|
108 |
this.writeMethodName = writeMethodName; |
|
109 |
if (writeMethodName != null && getWriteMethod() == null) { |
|
110 |
throw new IntrospectionException("Method not found: " + writeMethodName); |
|
111 |
} |
|
112 |
// If this class or one of its base classes allow PropertyChangeListener, |
|
113 |
// then we assume that any properties we discover are "bound". |
|
114 |
// See Introspector.getTargetPropertyInfo() method. |
|
115 |
String name = "addPropertyChangeListener"; |
|
116 |
Class[] args = {PropertyChangeListener.class}; |
|
117 |
this.bound = (null != Introspector.findMethod(beanClass, name, args.length, args)); |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* This constructor takes the name of a simple property, and Method |
|
122 |
* objects for reading and writing the property. |
|
123 |
* |
|
124 |
* @param propertyName The programmatic name of the property. |
|
125 |
* @param readMethod The method used for reading the property value. |
|
126 |
* May be null if the property is write-only. |
|
127 |
* @param writeMethod The method used for writing the property value. |
|
128 |
* May be null if the property is read-only. |
|
129 |
* @exception IntrospectionException if an exception occurs during |
|
130 |
* introspection. |
|
131 |
*/ |
|
132 |
public PropertyDescriptor(String propertyName, Method readMethod, Method writeMethod) |
|
133 |
throws IntrospectionException { |
|
134 |
if (propertyName == null || propertyName.length() == 0) { |
|
135 |
throw new IntrospectionException("bad property name"); |
|
136 |
} |
|
137 |
setName(propertyName); |
|
138 |
setReadMethod(readMethod); |
|
139 |
setWriteMethod(writeMethod); |
|
140 |
} |
|
141 |
||
142 |
/** |
|
143 |
* Creates <code>PropertyDescriptor</code> for the specified bean |
|
144 |
* with the specified name and methods to read/write the property value. |
|
145 |
* |
|
146 |
* @param bean the type of the target bean |
|
147 |
* @param base the base name of the property (the rest of the method name) |
|
148 |
* @param read the method used for reading the property value |
|
149 |
* @param write the method used for writing the property value |
|
150 |
* @exception IntrospectionException if an exception occurs during introspection |
|
151 |
* |
|
152 |
* @since 1.7 |
|
153 |
*/ |
|
154 |
PropertyDescriptor(Class<?> bean, String base, Method read, Method write) throws IntrospectionException { |
|
155 |
if (bean == null) { |
|
156 |
throw new IntrospectionException("Target Bean class is null"); |
|
157 |
} |
|
158 |
setClass0(bean); |
|
159 |
setName(Introspector.decapitalize(base)); |
|
160 |
setReadMethod(read); |
|
161 |
setWriteMethod(write); |
|
162 |
this.baseName = base; |
|
163 |
} |
|
164 |
||
165 |
/** |
|
4392
4a92100685fa
4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents:
3241
diff
changeset
|
166 |
* Returns the Java type info for the property. |
4a92100685fa
4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents:
3241
diff
changeset
|
167 |
* Note that the {@code Class} object may describe |
4a92100685fa
4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents:
3241
diff
changeset
|
168 |
* primitive Java types such as {@code int}. |
4a92100685fa
4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents:
3241
diff
changeset
|
169 |
* This type is returned by the read method |
4a92100685fa
4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents:
3241
diff
changeset
|
170 |
* or is used as the parameter type of the write method. |
4a92100685fa
4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents:
3241
diff
changeset
|
171 |
* Returns {@code null} if the type is an indexed property |
4a92100685fa
4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents:
3241
diff
changeset
|
172 |
* that does not support non-indexed access. |
2 | 173 |
* |
4392
4a92100685fa
4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents:
3241
diff
changeset
|
174 |
* @return the {@code Class} object that represents the Java type info, |
4a92100685fa
4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents:
3241
diff
changeset
|
175 |
* or {@code null} if the type cannot be determined |
2 | 176 |
*/ |
177 |
public synchronized Class<?> getPropertyType() { |
|
178 |
Class type = getPropertyType0(); |
|
179 |
if (type == null) { |
|
180 |
try { |
|
181 |
type = findPropertyType(getReadMethod(), getWriteMethod()); |
|
182 |
setPropertyType(type); |
|
183 |
} catch (IntrospectionException ex) { |
|
184 |
// Fall |
|
185 |
} |
|
186 |
} |
|
187 |
return type; |
|
188 |
} |
|
189 |
||
190 |
private void setPropertyType(Class type) { |
|
191 |
this.propertyTypeRef = getWeakReference(type); |
|
192 |
} |
|
193 |
||
194 |
private Class getPropertyType0() { |
|
195 |
return (this.propertyTypeRef != null) |
|
196 |
? this.propertyTypeRef.get() |
|
197 |
: null; |
|
198 |
} |
|
199 |
||
200 |
/** |
|
201 |
* Gets the method that should be used to read the property value. |
|
202 |
* |
|
203 |
* @return The method that should be used to read the property value. |
|
204 |
* May return null if the property can't be read. |
|
205 |
*/ |
|
206 |
public synchronized Method getReadMethod() { |
|
207 |
Method readMethod = getReadMethod0(); |
|
208 |
if (readMethod == null) { |
|
209 |
Class cls = getClass0(); |
|
210 |
if (cls == null || (readMethodName == null && readMethodRef == null)) { |
|
211 |
// The read method was explicitly set to null. |
|
212 |
return null; |
|
213 |
} |
|
214 |
if (readMethodName == null) { |
|
215 |
Class type = getPropertyType0(); |
|
216 |
if (type == boolean.class || type == null) { |
|
217 |
readMethodName = Introspector.IS_PREFIX + getBaseName(); |
|
218 |
} else { |
|
219 |
readMethodName = Introspector.GET_PREFIX + getBaseName(); |
|
220 |
} |
|
221 |
} |
|
222 |
||
223 |
// Since there can be multiple write methods but only one getter |
|
224 |
// method, find the getter method first so that you know what the |
|
225 |
// property type is. For booleans, there can be "is" and "get" |
|
226 |
// methods. If an "is" method exists, this is the official |
|
227 |
// reader method so look for this one first. |
|
228 |
readMethod = Introspector.findMethod(cls, readMethodName, 0); |
|
229 |
if (readMethod == null) { |
|
230 |
readMethodName = Introspector.GET_PREFIX + getBaseName(); |
|
231 |
readMethod = Introspector.findMethod(cls, readMethodName, 0); |
|
232 |
} |
|
233 |
try { |
|
234 |
setReadMethod(readMethod); |
|
235 |
} catch (IntrospectionException ex) { |
|
236 |
// fall |
|
237 |
} |
|
238 |
} |
|
239 |
return readMethod; |
|
240 |
} |
|
241 |
||
242 |
/** |
|
243 |
* Sets the method that should be used to read the property value. |
|
244 |
* |
|
245 |
* @param readMethod The new read method. |
|
246 |
*/ |
|
247 |
public synchronized void setReadMethod(Method readMethod) |
|
248 |
throws IntrospectionException { |
|
249 |
if (readMethod == null) { |
|
250 |
readMethodName = null; |
|
251 |
readMethodRef = null; |
|
252 |
return; |
|
253 |
} |
|
254 |
// The property type is determined by the read method. |
|
255 |
setPropertyType(findPropertyType(readMethod, getWriteMethod0())); |
|
256 |
setClass0(readMethod.getDeclaringClass()); |
|
257 |
||
258 |
readMethodName = readMethod.getName(); |
|
259 |
this.readMethodRef = getSoftReference(readMethod); |
|
466
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
260 |
setTransient(readMethod.getAnnotation(Transient.class)); |
2 | 261 |
} |
262 |
||
263 |
/** |
|
264 |
* Gets the method that should be used to write the property value. |
|
265 |
* |
|
266 |
* @return The method that should be used to write the property value. |
|
267 |
* May return null if the property can't be written. |
|
268 |
*/ |
|
269 |
public synchronized Method getWriteMethod() { |
|
270 |
Method writeMethod = getWriteMethod0(); |
|
271 |
if (writeMethod == null) { |
|
272 |
Class cls = getClass0(); |
|
273 |
if (cls == null || (writeMethodName == null && writeMethodRef == null)) { |
|
274 |
// The write method was explicitly set to null. |
|
275 |
return null; |
|
276 |
} |
|
277 |
||
278 |
// We need the type to fetch the correct method. |
|
279 |
Class type = getPropertyType0(); |
|
280 |
if (type == null) { |
|
281 |
try { |
|
282 |
// Can't use getPropertyType since it will lead to recursive loop. |
|
283 |
type = findPropertyType(getReadMethod(), null); |
|
284 |
setPropertyType(type); |
|
285 |
} catch (IntrospectionException ex) { |
|
286 |
// Without the correct property type we can't be guaranteed |
|
287 |
// to find the correct method. |
|
288 |
return null; |
|
289 |
} |
|
290 |
} |
|
291 |
||
292 |
if (writeMethodName == null) { |
|
293 |
writeMethodName = Introspector.SET_PREFIX + getBaseName(); |
|
294 |
} |
|
295 |
||
296 |
writeMethod = Introspector.findMethod(cls, writeMethodName, 1, |
|
297 |
(type == null) ? null : new Class[] { type }); |
|
3241
6fd229e009e7
6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents:
466
diff
changeset
|
298 |
if (writeMethod != null) { |
6fd229e009e7
6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents:
466
diff
changeset
|
299 |
if (!writeMethod.getReturnType().equals(void.class)) { |
6fd229e009e7
6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents:
466
diff
changeset
|
300 |
writeMethod = null; |
6fd229e009e7
6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents:
466
diff
changeset
|
301 |
} |
6fd229e009e7
6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents:
466
diff
changeset
|
302 |
} |
2 | 303 |
try { |
304 |
setWriteMethod(writeMethod); |
|
305 |
} catch (IntrospectionException ex) { |
|
306 |
// fall through |
|
307 |
} |
|
308 |
} |
|
309 |
return writeMethod; |
|
310 |
} |
|
311 |
||
312 |
/** |
|
313 |
* Sets the method that should be used to write the property value. |
|
314 |
* |
|
315 |
* @param writeMethod The new write method. |
|
316 |
*/ |
|
317 |
public synchronized void setWriteMethod(Method writeMethod) |
|
318 |
throws IntrospectionException { |
|
319 |
if (writeMethod == null) { |
|
320 |
writeMethodName = null; |
|
321 |
writeMethodRef = null; |
|
322 |
return; |
|
323 |
} |
|
324 |
// Set the property type - which validates the method |
|
325 |
setPropertyType(findPropertyType(getReadMethod(), writeMethod)); |
|
326 |
setClass0(writeMethod.getDeclaringClass()); |
|
327 |
||
328 |
writeMethodName = writeMethod.getName(); |
|
329 |
this.writeMethodRef = getSoftReference(writeMethod); |
|
466
6acd5ec503a8
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE
malenkov
parents:
2
diff
changeset
|
330 |
setTransient(writeMethod.getAnnotation(Transient.class)); |
2 | 331 |
} |
332 |
||
333 |
private Method getReadMethod0() { |
|
334 |
return (this.readMethodRef != null) |
|
335 |
? this.readMethodRef.get() |
|
336 |
: null; |
|
337 |
} |
|
338 |
||
339 |
private Method getWriteMethod0() { |
|
340 |
return (this.writeMethodRef != null) |
|
341 |
? this.writeMethodRef.get() |
|
342 |
: null; |
|
343 |
} |
|
344 |
||
345 |
/** |
|
346 |
* Overridden to ensure that a super class doesn't take precedent |
|
347 |
*/ |
|
348 |
void setClass0(Class clz) { |
|
349 |
if (getClass0() != null && clz.isAssignableFrom(getClass0())) { |
|
350 |
// dont replace a subclass with a superclass |
|
351 |
return; |
|
352 |
} |
|
353 |
super.setClass0(clz); |
|
354 |
} |
|
355 |
||
356 |
/** |
|
357 |
* Updates to "bound" properties will cause a "PropertyChange" event to |
|
358 |
* get fired when the property is changed. |
|
359 |
* |
|
360 |
* @return True if this is a bound property. |
|
361 |
*/ |
|
362 |
public boolean isBound() { |
|
363 |
return bound; |
|
364 |
} |
|
365 |
||
366 |
/** |
|
367 |
* Updates to "bound" properties will cause a "PropertyChange" event to |
|
368 |
* get fired when the property is changed. |
|
369 |
* |
|
370 |
* @param bound True if this is a bound property. |
|
371 |
*/ |
|
372 |
public void setBound(boolean bound) { |
|
373 |
this.bound = bound; |
|
374 |
} |
|
375 |
||
376 |
/** |
|
377 |
* Attempted updates to "Constrained" properties will cause a "VetoableChange" |
|
378 |
* event to get fired when the property is changed. |
|
379 |
* |
|
380 |
* @return True if this is a constrained property. |
|
381 |
*/ |
|
382 |
public boolean isConstrained() { |
|
383 |
return constrained; |
|
384 |
} |
|
385 |
||
386 |
/** |
|
387 |
* Attempted updates to "Constrained" properties will cause a "VetoableChange" |
|
388 |
* event to get fired when the property is changed. |
|
389 |
* |
|
390 |
* @param constrained True if this is a constrained property. |
|
391 |
*/ |
|
392 |
public void setConstrained(boolean constrained) { |
|
393 |
this.constrained = constrained; |
|
394 |
} |
|
395 |
||
396 |
||
397 |
/** |
|
398 |
* Normally PropertyEditors will be found using the PropertyEditorManager. |
|
399 |
* However if for some reason you want to associate a particular |
|
400 |
* PropertyEditor with a given property, then you can do it with |
|
401 |
* this method. |
|
402 |
* |
|
403 |
* @param propertyEditorClass The Class for the desired PropertyEditor. |
|
404 |
*/ |
|
405 |
public void setPropertyEditorClass(Class<?> propertyEditorClass) { |
|
406 |
this.propertyEditorClassRef = getWeakReference((Class)propertyEditorClass); |
|
407 |
} |
|
408 |
||
409 |
/** |
|
410 |
* Gets any explicit PropertyEditor Class that has been registered |
|
411 |
* for this property. |
|
412 |
* |
|
413 |
* @return Any explicit PropertyEditor Class that has been registered |
|
414 |
* for this property. Normally this will return "null", |
|
415 |
* indicating that no special editor has been registered, |
|
416 |
* so the PropertyEditorManager should be used to locate |
|
417 |
* a suitable PropertyEditor. |
|
418 |
*/ |
|
419 |
public Class<?> getPropertyEditorClass() { |
|
420 |
return (this.propertyEditorClassRef != null) |
|
421 |
? this.propertyEditorClassRef.get() |
|
422 |
: null; |
|
423 |
} |
|
424 |
||
425 |
/** |
|
426 |
* Constructs an instance of a property editor using the current |
|
427 |
* property editor class. |
|
428 |
* <p> |
|
429 |
* If the property editor class has a public constructor that takes an |
|
430 |
* Object argument then it will be invoked using the bean parameter |
|
431 |
* as the argument. Otherwise, the default constructor will be invoked. |
|
432 |
* |
|
433 |
* @param bean the source object |
|
434 |
* @return a property editor instance or null if a property editor has |
|
435 |
* not been defined or cannot be created |
|
436 |
* @since 1.5 |
|
437 |
*/ |
|
438 |
public PropertyEditor createPropertyEditor(Object bean) { |
|
439 |
Object editor = null; |
|
440 |
||
441 |
Class cls = getPropertyEditorClass(); |
|
442 |
if (cls != null) { |
|
443 |
Constructor ctor = null; |
|
444 |
if (bean != null) { |
|
445 |
try { |
|
446 |
ctor = cls.getConstructor(new Class[] { Object.class }); |
|
447 |
} catch (Exception ex) { |
|
448 |
// Fall through |
|
449 |
} |
|
450 |
} |
|
451 |
try { |
|
452 |
if (ctor == null) { |
|
453 |
editor = cls.newInstance(); |
|
454 |
} else { |
|
455 |
editor = ctor.newInstance(new Object[] { bean }); |
|
456 |
} |
|
457 |
} catch (Exception ex) { |
|
458 |
// A serious error has occured. |
|
459 |
// Proably due to an invalid property editor. |
|
460 |
throw new RuntimeException("PropertyEditor not instantiated", |
|
461 |
ex); |
|
462 |
} |
|
463 |
} |
|
464 |
return (PropertyEditor)editor; |
|
465 |
} |
|
466 |
||
467 |
||
468 |
/** |
|
469 |
* Compares this <code>PropertyDescriptor</code> against the specified object. |
|
470 |
* Returns true if the objects are the same. Two <code>PropertyDescriptor</code>s |
|
471 |
* are the same if the read, write, property types, property editor and |
|
472 |
* flags are equivalent. |
|
473 |
* |
|
474 |
* @since 1.4 |
|
475 |
*/ |
|
476 |
public boolean equals(Object obj) { |
|
477 |
if (this == obj) { |
|
478 |
return true; |
|
479 |
} |
|
480 |
if (obj != null && obj instanceof PropertyDescriptor) { |
|
481 |
PropertyDescriptor other = (PropertyDescriptor)obj; |
|
482 |
Method otherReadMethod = other.getReadMethod(); |
|
483 |
Method otherWriteMethod = other.getWriteMethod(); |
|
484 |
||
485 |
if (!compareMethods(getReadMethod(), otherReadMethod)) { |
|
486 |
return false; |
|
487 |
} |
|
488 |
||
489 |
if (!compareMethods(getWriteMethod(), otherWriteMethod)) { |
|
490 |
return false; |
|
491 |
} |
|
492 |
||
493 |
if (getPropertyType() == other.getPropertyType() && |
|
494 |
getPropertyEditorClass() == other.getPropertyEditorClass() && |
|
495 |
bound == other.isBound() && constrained == other.isConstrained() && |
|
496 |
writeMethodName == other.writeMethodName && |
|
497 |
readMethodName == other.readMethodName) { |
|
498 |
return true; |
|
499 |
} |
|
500 |
} |
|
501 |
return false; |
|
502 |
} |
|
503 |
||
504 |
/** |
|
505 |
* Package private helper method for Descriptor .equals methods. |
|
506 |
* |
|
507 |
* @param a first method to compare |
|
508 |
* @param b second method to compare |
|
509 |
* @return boolean to indicate that the methods are equivalent |
|
510 |
*/ |
|
511 |
boolean compareMethods(Method a, Method b) { |
|
512 |
// Note: perhaps this should be a protected method in FeatureDescriptor |
|
513 |
if ((a == null) != (b == null)) { |
|
514 |
return false; |
|
515 |
} |
|
516 |
||
517 |
if (a != null && b != null) { |
|
518 |
if (!a.equals(b)) { |
|
519 |
return false; |
|
520 |
} |
|
521 |
} |
|
522 |
return true; |
|
523 |
} |
|
524 |
||
525 |
/** |
|
526 |
* Package-private constructor. |
|
527 |
* Merge two property descriptors. Where they conflict, give the |
|
528 |
* second argument (y) priority over the first argument (x). |
|
529 |
* |
|
530 |
* @param x The first (lower priority) PropertyDescriptor |
|
531 |
* @param y The second (higher priority) PropertyDescriptor |
|
532 |
*/ |
|
533 |
PropertyDescriptor(PropertyDescriptor x, PropertyDescriptor y) { |
|
534 |
super(x,y); |
|
535 |
||
536 |
if (y.baseName != null) { |
|
537 |
baseName = y.baseName; |
|
538 |
} else { |
|
539 |
baseName = x.baseName; |
|
540 |
} |
|
541 |
||
542 |
if (y.readMethodName != null) { |
|
543 |
readMethodName = y.readMethodName; |
|
544 |
} else { |
|
545 |
readMethodName = x.readMethodName; |
|
546 |
} |
|
547 |
||
548 |
if (y.writeMethodName != null) { |
|
549 |
writeMethodName = y.writeMethodName; |
|
550 |
} else { |
|
551 |
writeMethodName = x.writeMethodName; |
|
552 |
} |
|
553 |
||
554 |
if (y.propertyTypeRef != null) { |
|
555 |
propertyTypeRef = y.propertyTypeRef; |
|
556 |
} else { |
|
557 |
propertyTypeRef = x.propertyTypeRef; |
|
558 |
} |
|
559 |
||
560 |
// Figure out the merged read method. |
|
561 |
Method xr = x.getReadMethod(); |
|
562 |
Method yr = y.getReadMethod(); |
|
563 |
||
564 |
// Normally give priority to y's readMethod. |
|
565 |
try { |
|
566 |
if (yr != null && yr.getDeclaringClass() == getClass0()) { |
|
567 |
setReadMethod(yr); |
|
568 |
} else { |
|
569 |
setReadMethod(xr); |
|
570 |
} |
|
571 |
} catch (IntrospectionException ex) { |
|
572 |
// fall through |
|
573 |
} |
|
574 |
||
575 |
// However, if both x and y reference read methods in the same class, |
|
576 |
// give priority to a boolean "is" method over a boolean "get" method. |
|
577 |
if (xr != null && yr != null && |
|
578 |
xr.getDeclaringClass() == yr.getDeclaringClass() && |
|
579 |
getReturnType(getClass0(), xr) == boolean.class && |
|
580 |
getReturnType(getClass0(), yr) == boolean.class && |
|
581 |
xr.getName().indexOf(Introspector.IS_PREFIX) == 0 && |
|
582 |
yr.getName().indexOf(Introspector.GET_PREFIX) == 0) { |
|
583 |
try { |
|
584 |
setReadMethod(xr); |
|
585 |
} catch (IntrospectionException ex) { |
|
586 |
// fall through |
|
587 |
} |
|
588 |
} |
|
589 |
||
590 |
Method xw = x.getWriteMethod(); |
|
591 |
Method yw = y.getWriteMethod(); |
|
592 |
||
593 |
try { |
|
594 |
if (yw != null && yw.getDeclaringClass() == getClass0()) { |
|
595 |
setWriteMethod(yw); |
|
596 |
} else { |
|
597 |
setWriteMethod(xw); |
|
598 |
} |
|
599 |
} catch (IntrospectionException ex) { |
|
600 |
// Fall through |
|
601 |
} |
|
602 |
||
603 |
if (y.getPropertyEditorClass() != null) { |
|
604 |
setPropertyEditorClass(y.getPropertyEditorClass()); |
|
605 |
} else { |
|
606 |
setPropertyEditorClass(x.getPropertyEditorClass()); |
|
607 |
} |
|
608 |
||
609 |
||
610 |
bound = x.bound | y.bound; |
|
611 |
constrained = x.constrained | y.constrained; |
|
612 |
} |
|
613 |
||
614 |
/* |
|
615 |
* Package-private dup constructor. |
|
616 |
* This must isolate the new object from any changes to the old object. |
|
617 |
*/ |
|
618 |
PropertyDescriptor(PropertyDescriptor old) { |
|
619 |
super(old); |
|
620 |
propertyTypeRef = old.propertyTypeRef; |
|
621 |
readMethodRef = old.readMethodRef; |
|
622 |
writeMethodRef = old.writeMethodRef; |
|
623 |
propertyEditorClassRef = old.propertyEditorClassRef; |
|
624 |
||
625 |
writeMethodName = old.writeMethodName; |
|
626 |
readMethodName = old.readMethodName; |
|
627 |
baseName = old.baseName; |
|
628 |
||
629 |
bound = old.bound; |
|
630 |
constrained = old.constrained; |
|
631 |
} |
|
632 |
||
633 |
/** |
|
634 |
* Returns the property type that corresponds to the read and write method. |
|
635 |
* The type precedence is given to the readMethod. |
|
636 |
* |
|
637 |
* @return the type of the property descriptor or null if both |
|
638 |
* read and write methods are null. |
|
639 |
* @throws IntrospectionException if the read or write method is invalid |
|
640 |
*/ |
|
641 |
private Class findPropertyType(Method readMethod, Method writeMethod) |
|
642 |
throws IntrospectionException { |
|
643 |
Class propertyType = null; |
|
644 |
try { |
|
645 |
if (readMethod != null) { |
|
646 |
Class[] params = getParameterTypes(getClass0(), readMethod); |
|
647 |
if (params.length != 0) { |
|
648 |
throw new IntrospectionException("bad read method arg count: " |
|
649 |
+ readMethod); |
|
650 |
} |
|
651 |
propertyType = getReturnType(getClass0(), readMethod); |
|
652 |
if (propertyType == Void.TYPE) { |
|
653 |
throw new IntrospectionException("read method " + |
|
654 |
readMethod.getName() + " returns void"); |
|
655 |
} |
|
656 |
} |
|
657 |
if (writeMethod != null) { |
|
658 |
Class params[] = getParameterTypes(getClass0(), writeMethod); |
|
659 |
if (params.length != 1) { |
|
660 |
throw new IntrospectionException("bad write method arg count: " |
|
661 |
+ writeMethod); |
|
662 |
} |
|
663 |
if (propertyType != null && propertyType != params[0]) { |
|
664 |
throw new IntrospectionException("type mismatch between read and write methods"); |
|
665 |
} |
|
666 |
propertyType = params[0]; |
|
667 |
} |
|
668 |
} catch (IntrospectionException ex) { |
|
669 |
throw ex; |
|
670 |
} |
|
671 |
return propertyType; |
|
672 |
} |
|
673 |
||
674 |
||
675 |
/** |
|
676 |
* Returns a hash code value for the object. |
|
677 |
* See {@link java.lang.Object#hashCode} for a complete description. |
|
678 |
* |
|
679 |
* @return a hash code value for this object. |
|
680 |
* @since 1.5 |
|
681 |
*/ |
|
682 |
public int hashCode() { |
|
683 |
int result = 7; |
|
684 |
||
685 |
result = 37 * result + ((getPropertyType() == null) ? 0 : |
|
686 |
getPropertyType().hashCode()); |
|
687 |
result = 37 * result + ((getReadMethod() == null) ? 0 : |
|
688 |
getReadMethod().hashCode()); |
|
689 |
result = 37 * result + ((getWriteMethod() == null) ? 0 : |
|
690 |
getWriteMethod().hashCode()); |
|
691 |
result = 37 * result + ((getPropertyEditorClass() == null) ? 0 : |
|
692 |
getPropertyEditorClass().hashCode()); |
|
693 |
result = 37 * result + ((writeMethodName == null) ? 0 : |
|
694 |
writeMethodName.hashCode()); |
|
695 |
result = 37 * result + ((readMethodName == null) ? 0 : |
|
696 |
readMethodName.hashCode()); |
|
697 |
result = 37 * result + getName().hashCode(); |
|
698 |
result = 37 * result + ((bound == false) ? 0 : 1); |
|
699 |
result = 37 * result + ((constrained == false) ? 0 : 1); |
|
700 |
||
701 |
return result; |
|
702 |
} |
|
703 |
||
704 |
// Calculate once since capitalize() is expensive. |
|
705 |
String getBaseName() { |
|
706 |
if (baseName == null) { |
|
707 |
baseName = NameGenerator.capitalize(getName()); |
|
708 |
} |
|
709 |
return baseName; |
|
710 |
} |
|
711 |
||
4960
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
4392
diff
changeset
|
712 |
void appendTo(StringBuilder sb) { |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
4392
diff
changeset
|
713 |
appendTo(sb, "bound", this.bound); |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
4392
diff
changeset
|
714 |
appendTo(sb, "constrained", this.constrained); |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
4392
diff
changeset
|
715 |
appendTo(sb, "propertyEditorClass", this.propertyEditorClassRef); |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
4392
diff
changeset
|
716 |
appendTo(sb, "propertyType", this.propertyTypeRef); |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
4392
diff
changeset
|
717 |
appendTo(sb, "readMethod", this.readMethodRef); |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
4392
diff
changeset
|
718 |
appendTo(sb, "writeMethod", this.writeMethodRef); |
2 | 719 |
} |
720 |
} |