2008-10-08 40 views
4

我剛剛注意到java.beans.Introspector getBeanInfo沒有獲取任何超級接口的屬性。例如:java.beans.Introspector getBeanInfo不能獲取任何超級接口的屬性

public interface Person { 
    String getName(); 
} 
public interface Employee extends Person { 
    int getSalary(); 
} 

對僱員的反思只會產生薪水,即使名稱是從Person繼承的。

這是爲什麼?我寧願不用反射來獲得所有的獲得者。

回答

1

使用

public static BeanInfo getBeanInfo(Class<?> beanClass, Introspector.USE_ALL_BEANINFO); 

,看看這會產生你正在尋找的結果嘗試。

+0

我試過了,它沒有工作。我的解決方案是通過調用類的getInterfaces()和getSuperclass()來反省超類和父接口。 – 2008-10-09 04:59:43

+0

你只想要屬性?您可以從BeanInfo獲取屬性描述符,我相信它包含了所有屬性,包括來自超類的屬性。 – MetroidFan2002 2008-10-25 05:28:32

0

在這種情況下,您應該編寫一個自定義的BeanInfo類。