A PropertyDescriptor describes one property that a Java Bean
exports via a pair of accessor methods.
| Constructor: |
PropertyDescriptor(PropertyDescriptor old) {
super(old);
propertyTypeRef = old.propertyTypeRef;
readMethodRef = old.readMethodRef;
writeMethodRef = old.writeMethodRef;
propertyEditorClassRef = old.propertyEditorClassRef;
writeMethodName = old.writeMethodName;
readMethodName = old.readMethodName;
baseName = old.baseName;
bound = old.bound;
constrained = old.constrained;
}
|
public PropertyDescriptor(String propertyName,
Class beanClass) throws IntrospectionException {
this(propertyName, beanClass,
Introspector.IS_PREFIX + NameGenerator.capitalize(propertyName),
Introspector.SET_PREFIX + NameGenerator.capitalize(propertyName));
}
Constructs a PropertyDescriptor for a property that follows
the standard Java convention by having getFoo and setFoo
accessor methods. Thus if the argument name is "fred", it will
assume that the writer method is "setFred" and the reader method
is "getFred" (or "isFred" for a boolean property). Note that the
property name should start with a lower case character, which will
be capitalized in the method names. Parameters:
propertyName - The programmatic name of the property.
beanClass - The Class object for the target bean. For
example sun.beans.OurButton.class.
Throws:
IntrospectionException - if an exception occurs during
introspection.
- exception:
IntrospectionException - if an exception occurs during
introspection.
|
PropertyDescriptor(PropertyDescriptor x,
PropertyDescriptor y) {
super(x,y);
if (y.baseName != null) {
baseName = y.baseName;
} else {
baseName = x.baseName;
}
if (y.readMethodName != null) {
readMethodName = y.readMethodName;
} else {
readMethodName = x.readMethodName;
}
if (y.writeMethodName != null) {
writeMethodName = y.writeMethodName;
} else {
writeMethodName = x.writeMethodName;
}
if (y.propertyTypeRef != null) {
propertyTypeRef = y.propertyTypeRef;
} else {
propertyTypeRef = x.propertyTypeRef;
}
// Figure out the merged read method.
Method xr = x.getReadMethod();
Method yr = y.getReadMethod();
// Normally give priority to y's readMethod.
try {
if (yr != null && yr.getDeclaringClass() == getClass0()) {
setReadMethod(yr);
} else {
setReadMethod(xr);
}
} catch (IntrospectionException ex) {
// fall through
}
// However, if both x and y reference read methods in the same class,
// give priority to a boolean "is" method over a boolean "get" method.
if (xr != null && yr != null &&
xr.getDeclaringClass() == yr.getDeclaringClass() &&
getReturnType(getClass0(), xr) == boolean.class &&
getReturnType(getClass0(), yr) == boolean.class &&
xr.getName().indexOf(Introspector.IS_PREFIX) == 0 &&
yr.getName().indexOf(Introspector.GET_PREFIX) == 0) {
try {
setReadMethod(xr);
} catch (IntrospectionException ex) {
// fall through
}
}
Method xw = x.getWriteMethod();
Method yw = y.getWriteMethod();
try {
if (yw != null && yw.getDeclaringClass() == getClass0()) {
setWriteMethod(yw);
} else {
setWriteMethod(xw);
}
} catch (IntrospectionException ex) {
// Fall through
}
if (y.getPropertyEditorClass() != null) {
setPropertyEditorClass(y.getPropertyEditorClass());
} else {
setPropertyEditorClass(x.getPropertyEditorClass());
}
bound = x.bound | y.bound;
constrained = x.constrained | y.constrained;
}
Package-private constructor.
Merge two property descriptors. Where they conflict, give the
second argument (y) priority over the first argument (x). Parameters:
x - The first (lower priority) PropertyDescriptor
y - The second (higher priority) PropertyDescriptor
|
public PropertyDescriptor(String propertyName,
Method readMethod,
Method writeMethod) throws IntrospectionException {
if (propertyName == null || propertyName.length() == 0) {
throw new IntrospectionException("bad property name");
}
setName(propertyName);
setReadMethod(readMethod);
setWriteMethod(writeMethod);
}
This constructor takes the name of a simple property, and Method
objects for reading and writing the property. Parameters:
propertyName - The programmatic name of the property.
readMethod - The method used for reading the property value.
May be null if the property is write-only.
writeMethod - The method used for writing the property value.
May be null if the property is read-only.
Throws:
IntrospectionException - if an exception occurs during
introspection.
- exception:
IntrospectionException - if an exception occurs during
introspection.
|
public PropertyDescriptor(String propertyName,
Class beanClass,
String readMethodName,
String writeMethodName) throws IntrospectionException {
if (beanClass == null) {
throw new IntrospectionException("Target Bean class is null");
}
if (propertyName == null || propertyName.length() == 0) {
throw new IntrospectionException("bad property name");
}
if ("".equals(readMethodName) || "".equals(writeMethodName)) {
throw new IntrospectionException("read or write method name should not be the empty string");
}
setName(propertyName);
setClass0(beanClass);
this.readMethodName = readMethodName;
if (readMethodName != null && getReadMethod() == null) {
throw new IntrospectionException("Method not found: " + readMethodName);
}
this.writeMethodName = writeMethodName;
if (writeMethodName != null && getWriteMethod() == null) {
throw new IntrospectionException("Method not found: " + writeMethodName);
}
// If this class or one of its base classes allow PropertyChangeListener,
// then we assume that any properties we discover are "bound".
// See Introspector.getTargetPropertyInfo() method.
String name = "addPropertyChangeListener";
Class[] args = {PropertyChangeListener.class};
this.bound = (null != Introspector.findMethod(beanClass, name, args.length, args));
}
This constructor takes the name of a simple property, and method
names for reading and writing the property. Parameters:
propertyName - The programmatic name of the property.
beanClass - The Class object for the target bean. For
example sun.beans.OurButton.class.
readMethodName - The name of the method used for reading the property
value. May be null if the property is write-only.
writeMethodName - The name of the method used for writing the property
value. May be null if the property is read-only.
Throws:
IntrospectionException - if an exception occurs during
introspection.
- exception:
IntrospectionException - if an exception occurs during
introspection.
|
PropertyDescriptor(Class bean,
String base,
Method read,
Method write) throws IntrospectionException {
if (bean == null) {
throw new IntrospectionException("Target Bean class is null");
}
setClass0(bean);
setName(Introspector.decapitalize(base));
setReadMethod(read);
setWriteMethod(write);
this.baseName = base;
}
Creates PropertyDescriptor for the specified bean
with the specified name and methods to read/write the property value. Parameters:
bean - the type of the target bean
base - the base name of the property (the rest of the method name)
read - the method used for reading the property value
write - the method used for writing the property value
Throws:
IntrospectionException - if an exception occurs during introspection
- exception:
IntrospectionException - if an exception occurs during introspection
- since:
1.7 -
|
| Method from java.beans.PropertyDescriptor Detail: |
boolean compareMethods(Method a,
Method b) {
// Note: perhaps this should be a protected method in FeatureDescriptor
if ((a == null) != (b == null)) {
return false;
}
if (a != null && b != null) {
if (!a.equals(b)) {
return false;
}
}
return true;
}
Package private helper method for Descriptor .equals methods. |
public PropertyEditor createPropertyEditor(Object bean) {
Object editor = null;
Class cls = getPropertyEditorClass();
if (cls != null) {
Constructor ctor = null;
if (bean != null) {
try {
ctor = cls.getConstructor(new Class[] { Object.class });
} catch (Exception ex) {
// Fall through
}
}
try {
if (ctor == null) {
editor = cls.newInstance();
} else {
editor = ctor.newInstance(new Object[] { bean });
}
} catch (Exception ex) {
// A serious error has occured.
// Proably due to an invalid property editor.
throw new RuntimeException("PropertyEditor not instantiated",
ex);
}
}
return (PropertyEditor)editor;
}
Constructs an instance of a property editor using the current
property editor class.
If the property editor class has a public constructor that takes an
Object argument then it will be invoked using the bean parameter
as the argument. Otherwise, the default constructor will be invoked. |
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj != null && obj instanceof PropertyDescriptor) {
PropertyDescriptor other = (PropertyDescriptor)obj;
Method otherReadMethod = other.getReadMethod();
Method otherWriteMethod = other.getWriteMethod();
if (!compareMethods(getReadMethod(), otherReadMethod)) {
return false;
}
if (!compareMethods(getWriteMethod(), otherWriteMethod)) {
return false;
}
if (getPropertyType() == other.getPropertyType() &&
getPropertyEditorClass() == other.getPropertyEditorClass() &&
bound == other.isBound() && constrained == other.isConstrained() &&
writeMethodName == other.writeMethodName &&
readMethodName == other.readMethodName) {
return true;
}
}
return false;
}
Compares this PropertyDescriptor against the specified object.
Returns true if the objects are the same. Two PropertyDescriptors
are the same if the read, write, property types, property editor and
flags are equivalent. |
String getBaseName() {
if (baseName == null) {
baseName = NameGenerator.capitalize(getName());
}
return baseName;
}
|
public Class getPropertyEditorClass() {
return (this.propertyEditorClassRef != null)
? this.propertyEditorClassRef.get()
: null;
}
Gets any explicit PropertyEditor Class that has been registered
for this property. |
public synchronized Class getPropertyType() {
Class type = getPropertyType0();
if (type == null) {
try {
type = findPropertyType(getReadMethod(), getWriteMethod());
setPropertyType(type);
} catch (IntrospectionException ex) {
// Fall
}
}
return type;
}
Gets the Class object for the property. |
public synchronized Method getReadMethod() {
Method readMethod = getReadMethod0();
if (readMethod == null) {
Class cls = getClass0();
if (cls == null || (readMethodName == null && readMethodRef == null)) {
// The read method was explicitly set to null.
return null;
}
if (readMethodName == null) {
Class type = getPropertyType0();
if (type == boolean.class || type == null) {
readMethodName = Introspector.IS_PREFIX + getBaseName();
} else {
readMethodName = Introspector.GET_PREFIX + getBaseName();
}
}
// Since there can be multiple write methods but only one getter
// method, find the getter method first so that you know what the
// property type is. For booleans, there can be "is" and "get"
// methods. If an "is" method exists, this is the official
// reader method so look for this one first.
readMethod = Introspector.findMethod(cls, readMethodName, 0);
if (readMethod == null) {
readMethodName = Introspector.GET_PREFIX + getBaseName();
readMethod = Introspector.findMethod(cls, readMethodName, 0);
}
try {
setReadMethod(readMethod);
} catch (IntrospectionException ex) {
// fall
}
}
return readMethod;
}
Gets the method that should be used to read the property value. |
public synchronized Method getWriteMethod() {
Method writeMethod = getWriteMethod0();
if (writeMethod == null) {
Class cls = getClass0();
if (cls == null || (writeMethodName == null && writeMethodRef == null)) {
// The write method was explicitly set to null.
return null;
}
// We need the type to fetch the correct method.
Class type = getPropertyType0();
if (type == null) {
try {
// Can't use getPropertyType since it will lead to recursive loop.
type = findPropertyType(getReadMethod(), null);
setPropertyType(type);
} catch (IntrospectionException ex) {
// Without the correct property type we can't be guaranteed
// to find the correct method.
return null;
}
}
if (writeMethodName == null) {
writeMethodName = Introspector.SET_PREFIX + getBaseName();
}
writeMethod = Introspector.findMethod(cls, writeMethodName, 1,
(type == null) ? null : new Class[] { type });
try {
setWriteMethod(writeMethod);
} catch (IntrospectionException ex) {
// fall through
}
}
return writeMethod;
}
Gets the method that should be used to write the property value. |
public int hashCode() {
int result = 7;
result = 37 * result + ((getPropertyType() == null) ? 0 :
getPropertyType().hashCode());
result = 37 * result + ((getReadMethod() == null) ? 0 :
getReadMethod().hashCode());
result = 37 * result + ((getWriteMethod() == null) ? 0 :
getWriteMethod().hashCode());
result = 37 * result + ((getPropertyEditorClass() == null) ? 0 :
getPropertyEditorClass().hashCode());
result = 37 * result + ((writeMethodName == null) ? 0 :
writeMethodName.hashCode());
result = 37 * result + ((readMethodName == null) ? 0 :
readMethodName.hashCode());
result = 37 * result + getName().hashCode();
result = 37 * result + ((bound == false) ? 0 : 1);
result = 37 * result + ((constrained == false) ? 0 : 1);
return result;
}
|
public boolean isBound() {
return bound;
}
Updates to "bound" properties will cause a "PropertyChange" event to
get fired when the property is changed. |
public boolean isConstrained() {
return constrained;
}
Attempted updates to "Constrained" properties will cause a "VetoableChange"
event to get fired when the property is changed. |
public void setBound(boolean bound) {
this.bound = bound;
}
Updates to "bound" properties will cause a "PropertyChange" event to
get fired when the property is changed. |
void setClass0(Class clz) {
if (getClass0() != null && clz.isAssignableFrom(getClass0())) {
// dont replace a subclass with a superclass
return;
}
super.setClass0(clz);
}
Overridden to ensure that a super class doesn't take precedent |
public void setConstrained(boolean constrained) {
this.constrained = constrained;
}
Attempted updates to "Constrained" properties will cause a "VetoableChange"
event to get fired when the property is changed. |
public void setPropertyEditorClass(Class propertyEditorClass) {
this.propertyEditorClassRef = getWeakReference((Class)propertyEditorClass);
}
Normally PropertyEditors will be found using the PropertyEditorManager.
However if for some reason you want to associate a particular
PropertyEditor with a given property, then you can do it with
this method. |
public synchronized void setReadMethod(Method readMethod) throws IntrospectionException {
if (readMethod == null) {
readMethodName = null;
readMethodRef = null;
return;
}
// The property type is determined by the read method.
setPropertyType(findPropertyType(readMethod, getWriteMethod0()));
setClass0(readMethod.getDeclaringClass());
readMethodName = readMethod.getName();
this.readMethodRef = getSoftReference(readMethod);
setTransient(readMethod.getAnnotation(Transient.class));
}
Sets the method that should be used to read the property value. |
public synchronized void setWriteMethod(Method writeMethod) throws IntrospectionException {
if (writeMethod == null) {
writeMethodName = null;
writeMethodRef = null;
return;
}
// Set the property type - which validates the method
setPropertyType(findPropertyType(getReadMethod(), writeMethod));
setClass0(writeMethod.getDeclaringClass());
writeMethodName = writeMethod.getName();
this.writeMethodRef = getSoftReference(writeMethod);
setTransient(writeMethod.getAnnotation(Transient.class));
}
Sets the method that should be used to write the property value. |