![](/img/trans.png)
[英]Could not find java.beans.propertydescriptor on android
[英]Confusing behavior of java.beans.PropertyDescriptor(String, Class)
我正在嘗試為我擁有的 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;
}
如果您的屬性是原始布爾值,則 PropertyDescriptor 尋找“isProperty”方法。 如果您的屬性是裝箱布爾值,則 PropertyDescriptor 尋找“getProperty”方法。
我有一個類似的問題,我是這樣解決的。
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);
}
我有一個名為“ip_value”的字段的類似問題。 我最初的 getter 和 setter 方法分別是getIPValue()
和setIPValue()
。 將它們分別重命名為getIp_value()
和setIp_value()
,問題就解決了。
得到相同的“沒有這樣的方法 isMyProperty”異常並閱讀以上所有內容后,我重新檢查了明顯的......我的錯。
仔細檢查您是否確實擁有正確的 getX 和 setX。 具體檢查函數簽名。
我沒有。 當這種情況發生時,你不討厭它嗎 ;) 野餐
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.