gpt4 book ai didi

java - java.beans.PropertyDescriptor(String, Class) 的混淆行为

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:07:00 24 4
gpt4 key购买 nike

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

想法?

最佳答案

编辑: 我想我知道你的问题是什么。如果您的类中不存在该属性,那么您将收到“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;
}

关于java - java.beans.PropertyDescriptor(String, Class) 的混淆行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10303009/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com