6

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

想法?

4

5 回答 5

7

编辑:我想我知道你的问题是什么。如果您的类中不存在该属性,那么您将收到“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 作为读取方法,如果它不存在,则使用 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;
}
于 2012-04-24T19:25:13.960 回答
2

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

于 2013-07-18T15:51:10.617 回答
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);
}
于 2015-12-13T19:49:40.827 回答
0

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

于 2017-04-11T06:08:03.980 回答
0

得到相同的“没有这样的方法 isMyProperty”异常并阅读了以上所有内容后,我重新检查了明显的......我的错。

仔细检查您是否确实拥有正确的 getX 和 setX。具体检查函数签名。

我没有。当这种情况发生时你不讨厌它;) PICNIC

于 2020-08-17T10:42:33.407 回答