author | malenkov |
Tue, 31 Jan 2012 14:20:47 +0400 | |
changeset 11668 | 138b661e3841 |
parent 11269 | 138525605d98 |
child 13356 | 89a34c00fd8c |
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 |
||
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
38 |
private Reference<? extends Class<?>> propertyTypeRef; |
2 | 39 |
private Reference<Method> readMethodRef; |
40 |
private Reference<Method> writeMethodRef; |
|
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
41 |
private Reference<? extends Class<?>> propertyEditorClassRef; |
2 | 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. |
|
6657
15dbb366c6a3
6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents:
5947
diff
changeset
|
115 |
Class[] args = { PropertyChangeListener.class }; |
15dbb366c6a3
6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents:
5947
diff
changeset
|
116 |
this.bound = null != Introspector.findMethod(beanClass, "addPropertyChangeListener", args.length, args); |
2 | 117 |
} |
118 |
||
119 |
/** |
|
120 |
* This constructor takes the name of a simple property, and Method |
|
121 |
* objects for reading and writing the property. |
|
122 |
* |
|
123 |
* @param propertyName The programmatic name of the property. |
|
124 |
* @param readMethod The method used for reading the property value. |
|
125 |
* May be null if the property is write-only. |
|
126 |
* @param writeMethod The method used for writing the property value. |
|
127 |
* May be null if the property is read-only. |
|
128 |
* @exception IntrospectionException if an exception occurs during |
|
129 |
* introspection. |
|
130 |
*/ |
|
131 |
public PropertyDescriptor(String propertyName, Method readMethod, Method writeMethod) |
|
132 |
throws IntrospectionException { |
|
133 |
if (propertyName == null || propertyName.length() == 0) { |
|
134 |
throw new IntrospectionException("bad property name"); |
|
135 |
} |
|
136 |
setName(propertyName); |
|
137 |
setReadMethod(readMethod); |
|
138 |
setWriteMethod(writeMethod); |
|
139 |
} |
|
140 |
||
141 |
/** |
|
142 |
* Creates <code>PropertyDescriptor</code> for the specified bean |
|
143 |
* with the specified name and methods to read/write the property value. |
|
144 |
* |
|
145 |
* @param bean the type of the target bean |
|
146 |
* @param base the base name of the property (the rest of the method name) |
|
147 |
* @param read the method used for reading the property value |
|
148 |
* @param write the method used for writing the property value |
|
149 |
* @exception IntrospectionException if an exception occurs during introspection |
|
150 |
* |
|
151 |
* @since 1.7 |
|
152 |
*/ |
|
153 |
PropertyDescriptor(Class<?> bean, String base, Method read, Method write) throws IntrospectionException { |
|
154 |
if (bean == null) { |
|
155 |
throw new IntrospectionException("Target Bean class is null"); |
|
156 |
} |
|
157 |
setClass0(bean); |
|
158 |
setName(Introspector.decapitalize(base)); |
|
159 |
setReadMethod(read); |
|
160 |
setWriteMethod(write); |
|
161 |
this.baseName = base; |
|
162 |
} |
|
163 |
||
164 |
/** |
|
4392
4a92100685fa
4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents:
3241
diff
changeset
|
165 |
* Returns the Java type info for the property. |
4a92100685fa
4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents:
3241
diff
changeset
|
166 |
* Note that the {@code Class} object may describe |
4a92100685fa
4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents:
3241
diff
changeset
|
167 |
* primitive Java types such as {@code int}. |
4a92100685fa
4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents:
3241
diff
changeset
|
168 |
* This type is returned by the read method |
4a92100685fa
4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents:
3241
diff
changeset
|
169 |
* 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
|
170 |
* 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
|
171 |
* that does not support non-indexed access. |
2 | 172 |
* |
4392
4a92100685fa
4638075: DOC: Doc for java.beans.PropertyDescriptor.getPropertyType() is incorrect.
malenkov
parents:
3241
diff
changeset
|
173 |
* @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
|
174 |
* or {@code null} if the type cannot be determined |
2 | 175 |
*/ |
176 |
public synchronized Class<?> getPropertyType() { |
|
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
177 |
Class<?> type = getPropertyType0(); |
2 | 178 |
if (type == null) { |
179 |
try { |
|
180 |
type = findPropertyType(getReadMethod(), getWriteMethod()); |
|
181 |
setPropertyType(type); |
|
182 |
} catch (IntrospectionException ex) { |
|
183 |
// Fall |
|
184 |
} |
|
185 |
} |
|
186 |
return type; |
|
187 |
} |
|
188 |
||
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
189 |
private void setPropertyType(Class<?> type) { |
2 | 190 |
this.propertyTypeRef = getWeakReference(type); |
191 |
} |
|
192 |
||
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
193 |
private Class<?> getPropertyType0() { |
2 | 194 |
return (this.propertyTypeRef != null) |
195 |
? this.propertyTypeRef.get() |
|
196 |
: null; |
|
197 |
} |
|
198 |
||
199 |
/** |
|
200 |
* Gets the method that should be used to read the property value. |
|
201 |
* |
|
202 |
* @return The method that should be used to read the property value. |
|
203 |
* May return null if the property can't be read. |
|
204 |
*/ |
|
205 |
public synchronized Method getReadMethod() { |
|
206 |
Method readMethod = getReadMethod0(); |
|
207 |
if (readMethod == null) { |
|
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
208 |
Class<?> cls = getClass0(); |
2 | 209 |
if (cls == null || (readMethodName == null && readMethodRef == null)) { |
210 |
// The read method was explicitly set to null. |
|
211 |
return null; |
|
212 |
} |
|
213 |
if (readMethodName == null) { |
|
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
214 |
Class<?> type = getPropertyType0(); |
2 | 215 |
if (type == boolean.class || type == null) { |
216 |
readMethodName = Introspector.IS_PREFIX + getBaseName(); |
|
217 |
} else { |
|
218 |
readMethodName = Introspector.GET_PREFIX + getBaseName(); |
|
219 |
} |
|
220 |
} |
|
221 |
||
222 |
// Since there can be multiple write methods but only one getter |
|
223 |
// method, find the getter method first so that you know what the |
|
224 |
// property type is. For booleans, there can be "is" and "get" |
|
225 |
// methods. If an "is" method exists, this is the official |
|
226 |
// reader method so look for this one first. |
|
6657
15dbb366c6a3
6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents:
5947
diff
changeset
|
227 |
readMethod = Introspector.findMethod(cls, readMethodName, 0); |
2 | 228 |
if (readMethod == null) { |
229 |
readMethodName = Introspector.GET_PREFIX + getBaseName(); |
|
6657
15dbb366c6a3
6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents:
5947
diff
changeset
|
230 |
readMethod = Introspector.findMethod(cls, readMethodName, 0); |
2 | 231 |
} |
232 |
try { |
|
233 |
setReadMethod(readMethod); |
|
234 |
} catch (IntrospectionException ex) { |
|
235 |
// fall |
|
236 |
} |
|
237 |
} |
|
238 |
return readMethod; |
|
239 |
} |
|
240 |
||
241 |
/** |
|
242 |
* Sets the method that should be used to read the property value. |
|
243 |
* |
|
244 |
* @param readMethod The new read method. |
|
245 |
*/ |
|
246 |
public synchronized void setReadMethod(Method readMethod) |
|
247 |
throws IntrospectionException { |
|
248 |
if (readMethod == null) { |
|
249 |
readMethodName = null; |
|
250 |
readMethodRef = null; |
|
251 |
return; |
|
252 |
} |
|
253 |
// The property type is determined by the read method. |
|
254 |
setPropertyType(findPropertyType(readMethod, getWriteMethod0())); |
|
255 |
setClass0(readMethod.getDeclaringClass()); |
|
256 |
||
257 |
readMethodName = readMethod.getName(); |
|
258 |
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
|
259 |
setTransient(readMethod.getAnnotation(Transient.class)); |
2 | 260 |
} |
261 |
||
262 |
/** |
|
263 |
* Gets the method that should be used to write the property value. |
|
264 |
* |
|
265 |
* @return The method that should be used to write the property value. |
|
266 |
* May return null if the property can't be written. |
|
267 |
*/ |
|
268 |
public synchronized Method getWriteMethod() { |
|
269 |
Method writeMethod = getWriteMethod0(); |
|
270 |
if (writeMethod == null) { |
|
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
271 |
Class<?> cls = getClass0(); |
2 | 272 |
if (cls == null || (writeMethodName == null && writeMethodRef == null)) { |
273 |
// The write method was explicitly set to null. |
|
274 |
return null; |
|
275 |
} |
|
276 |
||
277 |
// We need the type to fetch the correct method. |
|
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
278 |
Class<?> type = getPropertyType0(); |
2 | 279 |
if (type == null) { |
280 |
try { |
|
281 |
// Can't use getPropertyType since it will lead to recursive loop. |
|
282 |
type = findPropertyType(getReadMethod(), null); |
|
283 |
setPropertyType(type); |
|
284 |
} catch (IntrospectionException ex) { |
|
285 |
// Without the correct property type we can't be guaranteed |
|
286 |
// to find the correct method. |
|
287 |
return null; |
|
288 |
} |
|
289 |
} |
|
290 |
||
291 |
if (writeMethodName == null) { |
|
292 |
writeMethodName = Introspector.SET_PREFIX + getBaseName(); |
|
293 |
} |
|
294 |
||
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
295 |
Class<?>[] args = (type == null) ? null : new Class<?>[] { type }; |
6657
15dbb366c6a3
6976577: JCK7 api/java_beans/EventSetDescriptor/descriptions.html#Ctor1 fails since jdk7 b102
malenkov
parents:
5947
diff
changeset
|
296 |
writeMethod = Introspector.findMethod(cls, writeMethodName, 1, args); |
3241
6fd229e009e7
6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents:
466
diff
changeset
|
297 |
if (writeMethod != null) { |
6fd229e009e7
6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents:
466
diff
changeset
|
298 |
if (!writeMethod.getReturnType().equals(void.class)) { |
6fd229e009e7
6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents:
466
diff
changeset
|
299 |
writeMethod = null; |
6fd229e009e7
6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents:
466
diff
changeset
|
300 |
} |
6fd229e009e7
6723447: Introspector doesn't check return type for indexed property setters
malenkov
parents:
466
diff
changeset
|
301 |
} |
2 | 302 |
try { |
303 |
setWriteMethod(writeMethod); |
|
304 |
} catch (IntrospectionException ex) { |
|
305 |
// fall through |
|
306 |
} |
|
307 |
} |
|
308 |
return writeMethod; |
|
309 |
} |
|
310 |
||
311 |
/** |
|
312 |
* Sets the method that should be used to write the property value. |
|
313 |
* |
|
314 |
* @param writeMethod The new write method. |
|
315 |
*/ |
|
316 |
public synchronized void setWriteMethod(Method writeMethod) |
|
317 |
throws IntrospectionException { |
|
318 |
if (writeMethod == null) { |
|
319 |
writeMethodName = null; |
|
320 |
writeMethodRef = null; |
|
321 |
return; |
|
322 |
} |
|
323 |
// Set the property type - which validates the method |
|
324 |
setPropertyType(findPropertyType(getReadMethod(), writeMethod)); |
|
325 |
setClass0(writeMethod.getDeclaringClass()); |
|
326 |
||
327 |
writeMethodName = writeMethod.getName(); |
|
328 |
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
|
329 |
setTransient(writeMethod.getAnnotation(Transient.class)); |
2 | 330 |
} |
331 |
||
332 |
private Method getReadMethod0() { |
|
333 |
return (this.readMethodRef != null) |
|
334 |
? this.readMethodRef.get() |
|
335 |
: null; |
|
336 |
} |
|
337 |
||
338 |
private Method getWriteMethod0() { |
|
339 |
return (this.writeMethodRef != null) |
|
340 |
? this.writeMethodRef.get() |
|
341 |
: null; |
|
342 |
} |
|
343 |
||
344 |
/** |
|
345 |
* Overridden to ensure that a super class doesn't take precedent |
|
346 |
*/ |
|
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
347 |
void setClass0(Class<?> clz) { |
2 | 348 |
if (getClass0() != null && clz.isAssignableFrom(getClass0())) { |
349 |
// dont replace a subclass with a superclass |
|
350 |
return; |
|
351 |
} |
|
352 |
super.setClass0(clz); |
|
353 |
} |
|
354 |
||
355 |
/** |
|
356 |
* Updates to "bound" properties will cause a "PropertyChange" event to |
|
357 |
* get fired when the property is changed. |
|
358 |
* |
|
359 |
* @return True if this is a bound property. |
|
360 |
*/ |
|
361 |
public boolean isBound() { |
|
362 |
return bound; |
|
363 |
} |
|
364 |
||
365 |
/** |
|
366 |
* Updates to "bound" properties will cause a "PropertyChange" event to |
|
367 |
* get fired when the property is changed. |
|
368 |
* |
|
369 |
* @param bound True if this is a bound property. |
|
370 |
*/ |
|
371 |
public void setBound(boolean bound) { |
|
372 |
this.bound = bound; |
|
373 |
} |
|
374 |
||
375 |
/** |
|
376 |
* Attempted updates to "Constrained" properties will cause a "VetoableChange" |
|
377 |
* event to get fired when the property is changed. |
|
378 |
* |
|
379 |
* @return True if this is a constrained property. |
|
380 |
*/ |
|
381 |
public boolean isConstrained() { |
|
382 |
return constrained; |
|
383 |
} |
|
384 |
||
385 |
/** |
|
386 |
* Attempted updates to "Constrained" properties will cause a "VetoableChange" |
|
387 |
* event to get fired when the property is changed. |
|
388 |
* |
|
389 |
* @param constrained True if this is a constrained property. |
|
390 |
*/ |
|
391 |
public void setConstrained(boolean constrained) { |
|
392 |
this.constrained = constrained; |
|
393 |
} |
|
394 |
||
395 |
||
396 |
/** |
|
397 |
* Normally PropertyEditors will be found using the PropertyEditorManager. |
|
398 |
* However if for some reason you want to associate a particular |
|
399 |
* PropertyEditor with a given property, then you can do it with |
|
400 |
* this method. |
|
401 |
* |
|
402 |
* @param propertyEditorClass The Class for the desired PropertyEditor. |
|
403 |
*/ |
|
404 |
public void setPropertyEditorClass(Class<?> propertyEditorClass) { |
|
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
405 |
this.propertyEditorClassRef = getWeakReference(propertyEditorClass); |
2 | 406 |
} |
407 |
||
408 |
/** |
|
409 |
* Gets any explicit PropertyEditor Class that has been registered |
|
410 |
* for this property. |
|
411 |
* |
|
412 |
* @return Any explicit PropertyEditor Class that has been registered |
|
413 |
* for this property. Normally this will return "null", |
|
414 |
* indicating that no special editor has been registered, |
|
415 |
* so the PropertyEditorManager should be used to locate |
|
416 |
* a suitable PropertyEditor. |
|
417 |
*/ |
|
418 |
public Class<?> getPropertyEditorClass() { |
|
419 |
return (this.propertyEditorClassRef != null) |
|
420 |
? this.propertyEditorClassRef.get() |
|
421 |
: null; |
|
422 |
} |
|
423 |
||
424 |
/** |
|
425 |
* Constructs an instance of a property editor using the current |
|
426 |
* property editor class. |
|
427 |
* <p> |
|
428 |
* If the property editor class has a public constructor that takes an |
|
429 |
* Object argument then it will be invoked using the bean parameter |
|
430 |
* as the argument. Otherwise, the default constructor will be invoked. |
|
431 |
* |
|
432 |
* @param bean the source object |
|
433 |
* @return a property editor instance or null if a property editor has |
|
434 |
* not been defined or cannot be created |
|
435 |
* @since 1.5 |
|
436 |
*/ |
|
437 |
public PropertyEditor createPropertyEditor(Object bean) { |
|
438 |
Object editor = null; |
|
439 |
||
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
440 |
Class<?> cls = getPropertyEditorClass(); |
2 | 441 |
if (cls != null) { |
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
442 |
Constructor<?> ctor = null; |
2 | 443 |
if (bean != null) { |
444 |
try { |
|
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
445 |
ctor = cls.getConstructor(new Class<?>[] { Object.class }); |
2 | 446 |
} catch (Exception ex) { |
447 |
// Fall through |
|
448 |
} |
|
449 |
} |
|
450 |
try { |
|
451 |
if (ctor == null) { |
|
452 |
editor = cls.newInstance(); |
|
453 |
} else { |
|
454 |
editor = ctor.newInstance(new Object[] { bean }); |
|
455 |
} |
|
456 |
} catch (Exception ex) { |
|
11087
3217e8ad1d4a
7087876: java/beans/PropertyDescriptor.html#createPropertyEditor() throws RE if editor cannot be created
malenkov
parents:
6657
diff
changeset
|
457 |
// Fall through |
2 | 458 |
} |
459 |
} |
|
460 |
return (PropertyEditor)editor; |
|
461 |
} |
|
462 |
||
463 |
||
464 |
/** |
|
465 |
* Compares this <code>PropertyDescriptor</code> against the specified object. |
|
466 |
* Returns true if the objects are the same. Two <code>PropertyDescriptor</code>s |
|
467 |
* are the same if the read, write, property types, property editor and |
|
468 |
* flags are equivalent. |
|
469 |
* |
|
470 |
* @since 1.4 |
|
471 |
*/ |
|
472 |
public boolean equals(Object obj) { |
|
473 |
if (this == obj) { |
|
474 |
return true; |
|
475 |
} |
|
476 |
if (obj != null && obj instanceof PropertyDescriptor) { |
|
477 |
PropertyDescriptor other = (PropertyDescriptor)obj; |
|
478 |
Method otherReadMethod = other.getReadMethod(); |
|
479 |
Method otherWriteMethod = other.getWriteMethod(); |
|
480 |
||
481 |
if (!compareMethods(getReadMethod(), otherReadMethod)) { |
|
482 |
return false; |
|
483 |
} |
|
484 |
||
485 |
if (!compareMethods(getWriteMethod(), otherWriteMethod)) { |
|
486 |
return false; |
|
487 |
} |
|
488 |
||
489 |
if (getPropertyType() == other.getPropertyType() && |
|
490 |
getPropertyEditorClass() == other.getPropertyEditorClass() && |
|
491 |
bound == other.isBound() && constrained == other.isConstrained() && |
|
492 |
writeMethodName == other.writeMethodName && |
|
493 |
readMethodName == other.readMethodName) { |
|
494 |
return true; |
|
495 |
} |
|
496 |
} |
|
497 |
return false; |
|
498 |
} |
|
499 |
||
500 |
/** |
|
501 |
* Package private helper method for Descriptor .equals methods. |
|
502 |
* |
|
503 |
* @param a first method to compare |
|
504 |
* @param b second method to compare |
|
505 |
* @return boolean to indicate that the methods are equivalent |
|
506 |
*/ |
|
507 |
boolean compareMethods(Method a, Method b) { |
|
508 |
// Note: perhaps this should be a protected method in FeatureDescriptor |
|
509 |
if ((a == null) != (b == null)) { |
|
510 |
return false; |
|
511 |
} |
|
512 |
||
513 |
if (a != null && b != null) { |
|
514 |
if (!a.equals(b)) { |
|
515 |
return false; |
|
516 |
} |
|
517 |
} |
|
518 |
return true; |
|
519 |
} |
|
520 |
||
521 |
/** |
|
522 |
* Package-private constructor. |
|
523 |
* Merge two property descriptors. Where they conflict, give the |
|
524 |
* second argument (y) priority over the first argument (x). |
|
525 |
* |
|
526 |
* @param x The first (lower priority) PropertyDescriptor |
|
527 |
* @param y The second (higher priority) PropertyDescriptor |
|
528 |
*/ |
|
529 |
PropertyDescriptor(PropertyDescriptor x, PropertyDescriptor y) { |
|
530 |
super(x,y); |
|
531 |
||
532 |
if (y.baseName != null) { |
|
533 |
baseName = y.baseName; |
|
534 |
} else { |
|
535 |
baseName = x.baseName; |
|
536 |
} |
|
537 |
||
538 |
if (y.readMethodName != null) { |
|
539 |
readMethodName = y.readMethodName; |
|
540 |
} else { |
|
541 |
readMethodName = x.readMethodName; |
|
542 |
} |
|
543 |
||
544 |
if (y.writeMethodName != null) { |
|
545 |
writeMethodName = y.writeMethodName; |
|
546 |
} else { |
|
547 |
writeMethodName = x.writeMethodName; |
|
548 |
} |
|
549 |
||
550 |
if (y.propertyTypeRef != null) { |
|
551 |
propertyTypeRef = y.propertyTypeRef; |
|
552 |
} else { |
|
553 |
propertyTypeRef = x.propertyTypeRef; |
|
554 |
} |
|
555 |
||
556 |
// Figure out the merged read method. |
|
557 |
Method xr = x.getReadMethod(); |
|
558 |
Method yr = y.getReadMethod(); |
|
559 |
||
560 |
// Normally give priority to y's readMethod. |
|
561 |
try { |
|
11668
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
562 |
if (isAssignable(xr, yr)) { |
2 | 563 |
setReadMethod(yr); |
564 |
} else { |
|
565 |
setReadMethod(xr); |
|
566 |
} |
|
567 |
} catch (IntrospectionException ex) { |
|
568 |
// fall through |
|
569 |
} |
|
570 |
||
571 |
// However, if both x and y reference read methods in the same class, |
|
572 |
// give priority to a boolean "is" method over a boolean "get" method. |
|
573 |
if (xr != null && yr != null && |
|
574 |
xr.getDeclaringClass() == yr.getDeclaringClass() && |
|
575 |
getReturnType(getClass0(), xr) == boolean.class && |
|
576 |
getReturnType(getClass0(), yr) == boolean.class && |
|
577 |
xr.getName().indexOf(Introspector.IS_PREFIX) == 0 && |
|
578 |
yr.getName().indexOf(Introspector.GET_PREFIX) == 0) { |
|
579 |
try { |
|
580 |
setReadMethod(xr); |
|
581 |
} catch (IntrospectionException ex) { |
|
582 |
// fall through |
|
583 |
} |
|
584 |
} |
|
585 |
||
586 |
Method xw = x.getWriteMethod(); |
|
587 |
Method yw = y.getWriteMethod(); |
|
588 |
||
589 |
try { |
|
590 |
if (yw != null && yw.getDeclaringClass() == getClass0()) { |
|
591 |
setWriteMethod(yw); |
|
592 |
} else { |
|
593 |
setWriteMethod(xw); |
|
594 |
} |
|
595 |
} catch (IntrospectionException ex) { |
|
596 |
// Fall through |
|
597 |
} |
|
598 |
||
599 |
if (y.getPropertyEditorClass() != null) { |
|
600 |
setPropertyEditorClass(y.getPropertyEditorClass()); |
|
601 |
} else { |
|
602 |
setPropertyEditorClass(x.getPropertyEditorClass()); |
|
603 |
} |
|
604 |
||
605 |
||
606 |
bound = x.bound | y.bound; |
|
607 |
constrained = x.constrained | y.constrained; |
|
608 |
} |
|
609 |
||
610 |
/* |
|
611 |
* Package-private dup constructor. |
|
612 |
* This must isolate the new object from any changes to the old object. |
|
613 |
*/ |
|
614 |
PropertyDescriptor(PropertyDescriptor old) { |
|
615 |
super(old); |
|
616 |
propertyTypeRef = old.propertyTypeRef; |
|
617 |
readMethodRef = old.readMethodRef; |
|
618 |
writeMethodRef = old.writeMethodRef; |
|
619 |
propertyEditorClassRef = old.propertyEditorClassRef; |
|
620 |
||
621 |
writeMethodName = old.writeMethodName; |
|
622 |
readMethodName = old.readMethodName; |
|
623 |
baseName = old.baseName; |
|
624 |
||
625 |
bound = old.bound; |
|
626 |
constrained = old.constrained; |
|
627 |
} |
|
628 |
||
629 |
/** |
|
630 |
* Returns the property type that corresponds to the read and write method. |
|
631 |
* The type precedence is given to the readMethod. |
|
632 |
* |
|
633 |
* @return the type of the property descriptor or null if both |
|
634 |
* read and write methods are null. |
|
635 |
* @throws IntrospectionException if the read or write method is invalid |
|
636 |
*/ |
|
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
637 |
private Class<?> findPropertyType(Method readMethod, Method writeMethod) |
2 | 638 |
throws IntrospectionException { |
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
639 |
Class<?> propertyType = null; |
2 | 640 |
try { |
641 |
if (readMethod != null) { |
|
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
642 |
Class<?>[] params = getParameterTypes(getClass0(), readMethod); |
2 | 643 |
if (params.length != 0) { |
644 |
throw new IntrospectionException("bad read method arg count: " |
|
645 |
+ readMethod); |
|
646 |
} |
|
647 |
propertyType = getReturnType(getClass0(), readMethod); |
|
648 |
if (propertyType == Void.TYPE) { |
|
649 |
throw new IntrospectionException("read method " + |
|
650 |
readMethod.getName() + " returns void"); |
|
651 |
} |
|
652 |
} |
|
653 |
if (writeMethod != null) { |
|
11120
f8576c769572
7116954: Misc warnings in java.beans/java.beans.context
mcimadamore
parents:
6657
diff
changeset
|
654 |
Class<?>[] params = getParameterTypes(getClass0(), writeMethod); |
2 | 655 |
if (params.length != 1) { |
656 |
throw new IntrospectionException("bad write method arg count: " |
|
657 |
+ writeMethod); |
|
658 |
} |
|
659 |
if (propertyType != null && propertyType != params[0]) { |
|
660 |
throw new IntrospectionException("type mismatch between read and write methods"); |
|
661 |
} |
|
662 |
propertyType = params[0]; |
|
663 |
} |
|
664 |
} catch (IntrospectionException ex) { |
|
665 |
throw ex; |
|
666 |
} |
|
667 |
return propertyType; |
|
668 |
} |
|
669 |
||
670 |
||
671 |
/** |
|
672 |
* Returns a hash code value for the object. |
|
673 |
* See {@link java.lang.Object#hashCode} for a complete description. |
|
674 |
* |
|
675 |
* @return a hash code value for this object. |
|
676 |
* @since 1.5 |
|
677 |
*/ |
|
678 |
public int hashCode() { |
|
679 |
int result = 7; |
|
680 |
||
681 |
result = 37 * result + ((getPropertyType() == null) ? 0 : |
|
682 |
getPropertyType().hashCode()); |
|
683 |
result = 37 * result + ((getReadMethod() == null) ? 0 : |
|
684 |
getReadMethod().hashCode()); |
|
685 |
result = 37 * result + ((getWriteMethod() == null) ? 0 : |
|
686 |
getWriteMethod().hashCode()); |
|
687 |
result = 37 * result + ((getPropertyEditorClass() == null) ? 0 : |
|
688 |
getPropertyEditorClass().hashCode()); |
|
689 |
result = 37 * result + ((writeMethodName == null) ? 0 : |
|
690 |
writeMethodName.hashCode()); |
|
691 |
result = 37 * result + ((readMethodName == null) ? 0 : |
|
692 |
readMethodName.hashCode()); |
|
693 |
result = 37 * result + getName().hashCode(); |
|
694 |
result = 37 * result + ((bound == false) ? 0 : 1); |
|
695 |
result = 37 * result + ((constrained == false) ? 0 : 1); |
|
696 |
||
697 |
return result; |
|
698 |
} |
|
699 |
||
700 |
// Calculate once since capitalize() is expensive. |
|
701 |
String getBaseName() { |
|
702 |
if (baseName == null) { |
|
703 |
baseName = NameGenerator.capitalize(getName()); |
|
704 |
} |
|
705 |
return baseName; |
|
706 |
} |
|
707 |
||
4960
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
4392
diff
changeset
|
708 |
void appendTo(StringBuilder sb) { |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
4392
diff
changeset
|
709 |
appendTo(sb, "bound", this.bound); |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
4392
diff
changeset
|
710 |
appendTo(sb, "constrained", this.constrained); |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
4392
diff
changeset
|
711 |
appendTo(sb, "propertyEditorClass", this.propertyEditorClassRef); |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
4392
diff
changeset
|
712 |
appendTo(sb, "propertyType", this.propertyTypeRef); |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
4392
diff
changeset
|
713 |
appendTo(sb, "readMethod", this.readMethodRef); |
99ac74ca2f2f
4498236: RFE: Provide a toString method for PropertyChangeEvent and other classes
malenkov
parents:
4392
diff
changeset
|
714 |
appendTo(sb, "writeMethod", this.writeMethodRef); |
2 | 715 |
} |
11668
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
716 |
|
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
717 |
private boolean isAssignable(Method m1, Method m2) { |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
718 |
if (m1 == null) { |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
719 |
return true; // choose second method |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
720 |
} |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
721 |
if (m2 == null) { |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
722 |
return false; // choose first method |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
723 |
} |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
724 |
if (!m1.getName().equals(m2.getName())) { |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
725 |
return true; // choose second method by default |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
726 |
} |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
727 |
Class<?> type1 = m1.getDeclaringClass(); |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
728 |
Class<?> type2 = m2.getDeclaringClass(); |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
729 |
if (!type1.isAssignableFrom(type2)) { |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
730 |
return false; // choose first method: it declared later |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
731 |
} |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
732 |
type1 = getReturnType(getClass0(), m1); |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
733 |
type2 = getReturnType(getClass0(), m2); |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
734 |
if (!type1.isAssignableFrom(type2)) { |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
735 |
return false; // choose first method: it overrides return type |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
736 |
} |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
737 |
Class<?>[] args1 = getParameterTypes(getClass0(), m1); |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
738 |
Class<?>[] args2 = getParameterTypes(getClass0(), m2); |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
739 |
if (args1.length != args2.length) { |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
740 |
return true; // choose second method by default |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
741 |
} |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
742 |
for (int i = 0; i < args1.length; i++) { |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
743 |
if (!args1[i].isAssignableFrom(args2[i])) { |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
744 |
return false; // choose first method: it overrides parameter |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
745 |
} |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
746 |
} |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
747 |
return true; // choose second method |
138b661e3841
7122138: IAE thrown because Introspector ignores synthetic methods
malenkov
parents:
11269
diff
changeset
|
748 |
} |
2 | 749 |
} |