有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

javabeans混淆了java的行为。豆。PropertyDescriptor(字符串,类)

我正在尝试为一个bean类创建一个PropertyDescriptor。我在打电话

new PropertyDescriptor(myProperty, myClass)

我看到一个例外,即“isMyProperty”方法不存在。 稍微窥探一下代码--

/**
 * 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.
 *
 * @param propertyName The programmatic name of the property.
 * @param beanClass The Class object for the target bean.  For
 *      example sun.beans.OurButton.class.
 * @exception IntrospectionException if an exception occurs during
 *              introspection.
 */
public PropertyDescriptor(String propertyName, Class<?> beanClass)
    throws IntrospectionException {
this(propertyName, beanClass, 
     "is" + capitalize(propertyName), 
     "set" + capitalize(propertyName));
}

文档中说它将查找“getFred”,但它总是使用"is" + capitalize(property)!这是java版本“1.6.0_31”

想法


共 (2) 个答案

  1. # 1 楼答案

    我对名为“ip_值”的字段也有类似的问题。 我最初的getter和setter方法分别是getIPValue()setIPValue()。 将它们分别重命名为getIp_value()setIp_value()后,问题得到了解决

  2. # 2 楼答案

    如果您的属性是原始布尔值,那么PropertyDescriptor将查找“isProperty”方法。 如果您的属性是装箱布尔值,那么PropertyDescriptor将查找“getProperty”方法