令人困惑的java.beans.PropertyDescriptor行为(String,Class)

时间:2012-04-24 17:32:42

标签: java javabeans

我正在尝试为我拥有的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”

思想?

5 个答案:

答案 0 :(得分:6)

编辑:我想我知道你的问题是什么。如果您的类中不存在该属性,那么您将获得“isProperty”方法错误。看我的例子:

    {
        PropertyDescriptor desc = new PropertyDescriptor("uuid", Company.class);
        Method m = desc.getReadMethod();
        System.out.println(m.getName()); /* prints getUuid */
    }
    {
        PropertyDescriptor desc = new PropertyDescriptor("uuid11", Company.class);
        Method m = desc.getReadMethod();
        System.out.println(m.getName()); /* throws Method not found: isUuid11 */
    }

<强>原始

看起来它只是默认为isProperty作为read方法,如果它不存在,它使用getProperty。看看getReadMethod方法,它的位置:

if (readMethod == null) {
    readMethodName = "get" + getBaseName();

所以它首先尝试使用isProperty方法,如果它没有该方法,则查找getProperty。

这是完整的方法:

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 = "is" + getBaseName();
        } else {
            readMethodName = "get" + 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 = "get" + getBaseName();
        readMethod = Introspector.findMethod(cls, readMethodName, 0);
    }
    try {
        setReadMethod(readMethod);
    } catch (IntrospectionException ex) {
    // fall
    }
}
return readMethod;
}

答案 1 :(得分:2)

如果你的属性是原始布尔值,那么PropertyDescriptor寻找“isProperty”方法。 如果你的属性是盒装布尔值,那么PropertyDescriptor会寻找“getProperty”方法。

答案 2 :(得分:0)

我有类似的问题,我这样解决了。

static Object get(final String name, final Object obj)
    throws ReflectiveOperationException {

    final Class<?> klass = obj.getClass();

    try {
        final BeanInfo info = Introspector.getBeanInfo(klass);
        for (final PropertyDescriptor descriptor
             : info.getPropertyDescriptors()) {
            if (name.equals(descriptor.getName())) {
                final Method reader = descriptor.getReadMethod();
                if (reader != null) {
                    if (!reader.isAccessible()) {
                        reader.setAccessible(true);
                    }
                    return reader.invoke(obj);
                }
                break; // anyway
            }
        }
    } catch (final IntrospectionException ie) {
        ie.printStackTrace(System.err);
    }

    final Field field = findField(obj.getClass(), name);
    logger.log(Level.WARNING, "trying to get value directly from {0}",
               new Object[]{field});
    if (!field.isAccessible()) {
        field.setAccessible(true);
    }
    return field.get(obj);
}

答案 3 :(得分:0)

我在名为&#34; ip_value&#34;的字段中遇到了类似的问题。 我最初的getter和setter方法分别是getIPValue()setIPValue()。 将它们重命名为getIp_value()setIp_value()后,问题就解决了。

答案 4 :(得分:0)

具有相同的“没有这样的方法isMyProperty”异常,并阅读了以上所有内容,我重新检查了明显的……我的坏处。

仔细检查您实际上是否拥有正确的getX和setX。 具体检查功能签名。

我没有。发生这种情况时,您不讨厌它吗?) PICNIC