下面列出了怎么用java.beans.IndexedPropertyDescriptor的API类实例代码及写法,或者点击链接到github查看源代码。
@Nullable
private PropertyDescriptor findExistingPropertyDescriptor(String propertyName, Class<?> propertyType) {
for (PropertyDescriptor pd : this.propertyDescriptors) {
final Class<?> candidateType;
final String candidateName = pd.getName();
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
candidateType = ipd.getIndexedPropertyType();
if (candidateName.equals(propertyName) &&
(candidateType.equals(propertyType) || candidateType.equals(propertyType.getComponentType()))) {
return pd;
}
}
else {
candidateType = pd.getPropertyType();
if (candidateName.equals(propertyName) &&
(candidateType.equals(propertyType) || propertyType.equals(candidateType.getComponentType()))) {
return pd;
}
}
}
return null;
}
public void test_MixedSimpleClass56() throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(MixedSimpleClass56.class);
Method getter = MixedSimpleClass56.class.getMethod("getList",
new Class<?>[] { int.class });
Method setter = MixedSimpleClass56.class.getMethod("setList",
new Class<?>[] { int.class, boolean.class });
assertEquals(2, beanInfo.getPropertyDescriptors().length);
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
assertNull(pd.getReadMethod());
assertNull(pd.getWriteMethod());
assertTrue(pd instanceof IndexedPropertyDescriptor);
assertEquals(getter,
((IndexedPropertyDescriptor) pd).getIndexedReadMethod());
assertEquals(setter,
((IndexedPropertyDescriptor) pd)
.getIndexedWriteMethod());
}
}
}
public void testEquals_ReadMethod() throws SecurityException,
NoSuchMethodException, IntrospectionException {
String propertyName = "PropertyFour";
Class<MockJavaBean> beanClass = MockJavaBean.class;
Method readMethod = beanClass.getMethod("getPropertyFive",
(Class[]) null);
Method writeMethod = beanClass.getMethod("set" + propertyName,
new Class[] { String[].class });
Method indexedReadMethod = beanClass.getMethod("get" + propertyName,
new Class[] { Integer.TYPE });
Method indexedWriteMethod = beanClass.getMethod("set" + propertyName,
new Class[] { Integer.TYPE, String.class });
IndexedPropertyDescriptor ipd = new IndexedPropertyDescriptor(
propertyName, readMethod, writeMethod, indexedReadMethod,
indexedWriteMethod);
IndexedPropertyDescriptor ipd2 = new IndexedPropertyDescriptor(
propertyName, beanClass);
assertFalse(ipd.equals(ipd2));
}
private PropertyDescriptor findExistingPropertyDescriptor(String propertyName, Class<?> propertyType) {
for (PropertyDescriptor pd : this.propertyDescriptors) {
final Class<?> candidateType;
final String candidateName = pd.getName();
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
candidateType = ipd.getIndexedPropertyType();
if (candidateName.equals(propertyName) &&
(candidateType.equals(propertyType) || candidateType.equals(propertyType.getComponentType()))) {
return pd;
}
}
else {
candidateType = pd.getPropertyType();
if (candidateName.equals(propertyName) &&
(candidateType.equals(propertyType) || propertyType.equals(candidateType.getComponentType()))) {
return pd;
}
}
}
return null;
}
public void test_MixedBooleanExtendClass13() throws Exception {
BeanInfo info = Introspector
.getBeanInfo(MixedBooleanExtendClass13.class);
Method getter = MixedBooleanSimpleClass42.class
.getDeclaredMethod("isList");
Method setter = MixedBooleanSimpleClass42.class.getDeclaredMethod(
"setList", boolean.class);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
assertFalse(pd instanceof IndexedPropertyDescriptor);
assertEquals(getter, pd.getReadMethod());
assertEquals(setter, pd.getWriteMethod());
}
}
}
public void testHashCode() throws Exception {
String propertyName = "PropertyFour";
Class<MockJavaBean> beanClass = MockJavaBean.class;
Method readMethod = beanClass.getMethod("get" + propertyName,
(Class[]) null);
Method writeMethod = beanClass.getMethod("set" + propertyName,
new Class[] { String[].class });
Method indexedReadMethod = beanClass.getMethod("get" + propertyName,
new Class[] { Integer.TYPE });
Method indexedWriteMethod = beanClass.getMethod("set" + propertyName,
new Class[] { Integer.TYPE, String.class });
IndexedPropertyDescriptor ipd = new IndexedPropertyDescriptor(
propertyName, readMethod, writeMethod, indexedReadMethod,
indexedWriteMethod);
IndexedPropertyDescriptor ipd2 = new IndexedPropertyDescriptor(
propertyName, beanClass);
assertEquals(ipd, ipd2);
assertEquals(ipd.hashCode(), ipd2.hashCode());
}
private static void test(Class<?> type, boolean read, boolean write, boolean readIndexed, boolean writeIndexed) {
PropertyDescriptor pd = BeanUtils.findPropertyDescriptor(type, "size");
if (pd != null) {
test(type, "read", read, null != pd.getReadMethod());
test(type, "write", write, null != pd.getWriteMethod());
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
test(type, "indexed read", readIndexed, null != ipd.getIndexedReadMethod());
test(type, "indexed write", writeIndexed, null != ipd.getIndexedWriteMethod());
} else if (readIndexed || writeIndexed) {
error(type, "indexed property does not exist");
}
} else if (read || write || readIndexed || writeIndexed) {
error(type, "property does not exist");
}
}
public static void main(String[] args) throws Exception {
Class<?> bt = Accessor.getBeanType();
Class<?> lt = Accessor.getListenerType();
// test PropertyDescriptor
PropertyDescriptor pd = new PropertyDescriptor("boolean", bt);
test(pd.getReadMethod());
test(pd.getWriteMethod());
// test IndexedPropertyDescriptor
IndexedPropertyDescriptor ipd = new IndexedPropertyDescriptor("indexed", bt);
test(ipd.getReadMethod());
test(ipd.getWriteMethod());
test(ipd.getIndexedReadMethod());
test(ipd.getIndexedWriteMethod());
// test EventSetDescriptor
EventSetDescriptor esd = new EventSetDescriptor(bt, "test", lt, "process");
test(esd.getAddListenerMethod());
test(esd.getRemoveListenerMethod());
test(esd.getGetListenerMethod());
test(esd.getListenerMethods());
}
public void testIndexedPropertyDescriptorStringClassStringStringStringString_WriteMethodNull()
throws IntrospectionException {
String propertyName = "PropertyFour";
Class<MockJavaBean> beanClass = MockJavaBean.class;
IndexedPropertyDescriptor ipd = new IndexedPropertyDescriptor(
propertyName, beanClass, "get" + propertyName, null, "get"
+ propertyName, "set" + propertyName);
assertNotNull(ipd.getReadMethod());
assertNull(ipd.getWriteMethod());
assertEquals(String.class, ipd.getIndexedPropertyType());
new IndexedPropertyDescriptor(
propertyName, beanClass, "get" + propertyName, "set"+propertyName, "", "set" + propertyName);
try{
new IndexedPropertyDescriptor(
propertyName, beanClass, "get" + propertyName, "set"+propertyName, "get" + propertyName, "");
fail();
}catch(Exception e){
}
}
public void test_MixedBooleanSimpleClass6() throws Exception {
BeanInfo info = Introspector
.getBeanInfo(MixedBooleanSimpleClass6.class);
Method getter = MixedBooleanSimpleClass6.class
.getDeclaredMethod("getList");
Method setter = MixedBooleanSimpleClass6.class.getDeclaredMethod(
"setList", boolean.class);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
assertFalse(pd instanceof IndexedPropertyDescriptor);
assertEquals(getter, pd.getReadMethod());
assertEquals(setter, pd.getWriteMethod());
}
}
}
private static void test(Class<?> type, boolean read, boolean write, boolean readIndexed, boolean writeIndexed) {
PropertyDescriptor pd = BeanUtils.findPropertyDescriptor(type, "size");
if (pd != null) {
test(type, "read", read, null != pd.getReadMethod());
test(type, "write", write, null != pd.getWriteMethod());
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
test(type, "indexed read", readIndexed, null != ipd.getIndexedReadMethod());
test(type, "indexed write", writeIndexed, null != ipd.getIndexedWriteMethod());
} else if (readIndexed || writeIndexed) {
error(type, "indexed property does not exist");
}
} else if (read || write || readIndexed || writeIndexed) {
error(type, "property does not exist");
}
}
public void test_MixedSimpleClass23() throws Exception {
BeanInfo info = Introspector.getBeanInfo(MixedSimpleClass23.class);
Method setter = MixedSimpleClass23.class.getDeclaredMethod("setList",
int.class, Object.class);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
assertTrue(pd instanceof IndexedPropertyDescriptor);
assertNull(pd.getReadMethod());
assertNull(pd.getWriteMethod());
assertNull(((IndexedPropertyDescriptor) pd)
.getIndexedReadMethod());
assertEquals(setter, ((IndexedPropertyDescriptor) pd)
.getIndexedWriteMethod());
}
}
}
public void test_MixedSimpleClass48() throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(MixedSimpleClass48.class);
Method getter = MixedSimpleClass48.class.getMethod("getList",
new Class<?>[] { int.class });
assertEquals(2, beanInfo.getPropertyDescriptors().length);
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
assertNull(pd.getReadMethod());
assertNull(pd.getWriteMethod());
assertTrue(pd instanceof IndexedPropertyDescriptor);
assertEquals(getter,
((IndexedPropertyDescriptor) pd).getIndexedReadMethod());
assertNull(((IndexedPropertyDescriptor) pd)
.getIndexedWriteMethod());
}
}
}
public void test_MixedBooleanSimpleClass11() throws Exception {
BeanInfo info = Introspector
.getBeanInfo(MixedBooleanSimpleClass11.class);
Method setter = MixedBooleanSimpleClass11.class.getDeclaredMethod(
"setList", int.class, boolean.class);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
assertTrue(pd instanceof IndexedPropertyDescriptor);
assertNull(pd.getReadMethod());
assertNull(pd.getWriteMethod());
assertNull(((IndexedPropertyDescriptor) pd)
.getIndexedReadMethod());
assertEquals(setter, ((IndexedPropertyDescriptor) pd)
.getIndexedWriteMethod());
}
}
}
public static void main(String[] args) throws Exception {
Class<?> bt = Accessor.getBeanType();
Class<?> lt = Accessor.getListenerType();
// test PropertyDescriptor
PropertyDescriptor pd = new PropertyDescriptor("boolean", bt);
test(pd.getReadMethod());
test(pd.getWriteMethod());
// test IndexedPropertyDescriptor
IndexedPropertyDescriptor ipd = new IndexedPropertyDescriptor("indexed", bt);
test(ipd.getReadMethod());
test(ipd.getWriteMethod());
test(ipd.getIndexedReadMethod());
test(ipd.getIndexedWriteMethod());
// test EventSetDescriptor
EventSetDescriptor esd = new EventSetDescriptor(bt, "test", lt, "process");
test(esd.getAddListenerMethod());
test(esd.getRemoveListenerMethod());
test(esd.getGetListenerMethod());
test(esd.getListenerMethods());
}
public void test_MixedBooleanSimpleClass15() throws Exception {
BeanInfo info = Introspector
.getBeanInfo(MixedBooleanSimpleClass15.class);
Method getter = MixedBooleanSimpleClass15.class.getDeclaredMethod(
"getList", int.class);
Method setter = MixedBooleanSimpleClass15.class.getDeclaredMethod(
"setList", int.class, boolean.class);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
assertTrue(pd instanceof IndexedPropertyDescriptor);
assertNull(pd.getReadMethod());
assertNull(pd.getWriteMethod());
assertEquals(getter, ((IndexedPropertyDescriptor) pd)
.getIndexedReadMethod());
assertEquals(setter, ((IndexedPropertyDescriptor) pd)
.getIndexedWriteMethod());
}
}
}
@Nullable
private PropertyDescriptor findExistingPropertyDescriptor(String propertyName, Class<?> propertyType) {
for (PropertyDescriptor pd : this.propertyDescriptors) {
final Class<?> candidateType;
final String candidateName = pd.getName();
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
candidateType = ipd.getIndexedPropertyType();
if (candidateName.equals(propertyName) &&
(candidateType.equals(propertyType) || candidateType.equals(propertyType.getComponentType()))) {
return pd;
}
}
else {
candidateType = pd.getPropertyType();
if (candidateName.equals(propertyName) &&
(candidateType.equals(propertyType) || propertyType.equals(candidateType.getComponentType()))) {
return pd;
}
}
}
return null;
}
public void test_MixedSimpleClass34() throws Exception {
BeanInfo info = Introspector.getBeanInfo(MixedSimpleClass34.class);
Method indexedGetter = MixedSimpleClass34.class.getDeclaredMethod(
"getList", int.class);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
assertTrue(pd instanceof IndexedPropertyDescriptor);
assertNull(pd.getReadMethod());
assertNull(pd.getWriteMethod());
assertEquals(indexedGetter, ((IndexedPropertyDescriptor) pd)
.getIndexedReadMethod());
assertNull(((IndexedPropertyDescriptor) pd)
.getIndexedWriteMethod());
}
}
}
public void test_MixedBooleanExtendClass10() throws Exception {
BeanInfo info = Introspector
.getBeanInfo(MixedBooleanExtendClass10.class);
Method setter = MixedBooleanExtendClass10.class.getDeclaredMethod(
"setList", int.class, boolean.class);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
assertTrue(pd instanceof IndexedPropertyDescriptor);
assertNull(pd.getReadMethod());
assertNull(pd.getWriteMethod());
assertNull(((IndexedPropertyDescriptor) pd)
.getIndexedReadMethod());
assertEquals(setter, ((IndexedPropertyDescriptor) pd)
.getIndexedWriteMethod());
break;
}
}
}
public static void main(String[] args) throws Exception {
Class<?> bt = Accessor.getBeanType();
Class<?> lt = Accessor.getListenerType();
// test PropertyDescriptor
PropertyDescriptor pd = new PropertyDescriptor("boolean", bt);
test(pd.getReadMethod());
test(pd.getWriteMethod());
// test IndexedPropertyDescriptor
IndexedPropertyDescriptor ipd = new IndexedPropertyDescriptor("indexed", bt);
test(ipd.getReadMethod());
test(ipd.getWriteMethod());
test(ipd.getIndexedReadMethod());
test(ipd.getIndexedWriteMethod());
// test EventSetDescriptor
EventSetDescriptor esd = new EventSetDescriptor(bt, "test", lt, "process");
test(esd.getAddListenerMethod());
test(esd.getRemoveListenerMethod());
test(esd.getGetListenerMethod());
test(esd.getListenerMethods());
}
public void test_MixedBooleanExtendClass6() throws Exception {
BeanInfo info = Introspector
.getBeanInfo(MixedBooleanExtendClass6.class);
Method getter = MixedBooleanExtendClass6.class
.getDeclaredMethod("isList");
Method setter = MixedBooleanSimpleClass25.class.getDeclaredMethod(
"setList", boolean.class);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
assertFalse(pd instanceof IndexedPropertyDescriptor);
assertEquals(getter, pd.getReadMethod());
assertEquals(setter, pd.getWriteMethod());
break;
}
}
}
private static PropertyDescriptor create(PropertyDescriptor pd) {
try {
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
return new IndexedPropertyDescriptor(
ipd.getName(),
ipd.getReadMethod(),
ipd.getWriteMethod(),
ipd.getIndexedReadMethod(),
ipd.getIndexedWriteMethod());
} else {
return new PropertyDescriptor(
pd.getName(),
pd.getReadMethod(),
pd.getWriteMethod());
}
}
catch (IntrospectionException exception) {
exception.printStackTrace();
return null;
}
}
private static void test(Class<?> type, boolean read, boolean write, boolean readIndexed, boolean writeIndexed) {
PropertyDescriptor pd = BeanUtils.findPropertyDescriptor(type, "size");
if (pd != null) {
test(type, "read", read, null != pd.getReadMethod());
test(type, "write", write, null != pd.getWriteMethod());
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
test(type, "indexed read", readIndexed, null != ipd.getIndexedReadMethod());
test(type, "indexed write", writeIndexed, null != ipd.getIndexedWriteMethod());
} else if (readIndexed || writeIndexed) {
error(type, "indexed property does not exist");
}
} else if (read || write || readIndexed || writeIndexed) {
error(type, "property does not exist");
}
}
public void test_MixedBooleanExtendClass3() throws Exception {
BeanInfo info = Introspector
.getBeanInfo(MixedBooleanExtendClass3.class);
Method getter = MixedBooleanSimpleClass1.class
.getDeclaredMethod("isList");
Method setter = MixedBooleanExtendClass3.class.getDeclaredMethod(
"setList", boolean.class);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
assertFalse(pd instanceof IndexedPropertyDescriptor);
assertEquals(getter, pd.getReadMethod());
assertEquals(setter, pd.getWriteMethod());
break;
}
}
}
public void test_MixedSimpleClass30() throws Exception {
BeanInfo info = Introspector.getBeanInfo(MixedSimpleClass30.class);
Method indexedGetter = MixedSimpleClass30.class.getDeclaredMethod(
"getList", int.class);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
assertTrue(pd instanceof IndexedPropertyDescriptor);
assertNull(pd.getReadMethod());
assertNull(pd.getWriteMethod());
assertEquals(indexedGetter, ((IndexedPropertyDescriptor) pd)
.getIndexedReadMethod());
assertNull(((IndexedPropertyDescriptor) pd)
.getIndexedWriteMethod());
}
}
}
public void test_MixedSimpleClass50() throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(MixedSimpleClass50.class);
Method setter = MixedSimpleClass50.class.getMethod("setList",
new Class<?>[] { int.class, boolean.class });
assertEquals(2, beanInfo.getPropertyDescriptors().length);
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
assertNull(pd.getReadMethod());
assertNull(pd.getWriteMethod());
assertTrue(pd instanceof IndexedPropertyDescriptor);
assertNull(((IndexedPropertyDescriptor) pd)
.getIndexedReadMethod());
assertEquals(setter,
((IndexedPropertyDescriptor) pd)
.getIndexedWriteMethod());
}
}
}
private static void test(Class<?> type, boolean read, boolean write, boolean readIndexed, boolean writeIndexed) {
PropertyDescriptor pd = BeanUtils.findPropertyDescriptor(type, "size");
if (pd != null) {
test(type, "read", read, null != pd.getReadMethod());
test(type, "write", write, null != pd.getWriteMethod());
if (pd instanceof IndexedPropertyDescriptor) {
IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd;
test(type, "indexed read", readIndexed, null != ipd.getIndexedReadMethod());
test(type, "indexed write", writeIndexed, null != ipd.getIndexedWriteMethod());
} else if (readIndexed || writeIndexed) {
error(type, "indexed property does not exist");
}
} else if (read || write || readIndexed || writeIndexed) {
error(type, "property does not exist");
}
}
public void test_MixedExtendClass10() throws Exception {
BeanInfo info = Introspector.getBeanInfo(MixedExtendClass10.class);
Method setter = MixedExtendClass10.class.getDeclaredMethod("setList",
int.class, Object.class);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
assertTrue(pd instanceof IndexedPropertyDescriptor);
assertNull(pd.getReadMethod());
assertNull(pd.getWriteMethod());
assertNull(((IndexedPropertyDescriptor) pd)
.getIndexedReadMethod());
assertEquals(setter, ((IndexedPropertyDescriptor) pd)
.getIndexedWriteMethod());
break;
}
}
}
public void test_MixedSimpleClass54() throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(MixedSimpleClass54.class);
Method setter = MixedSimpleClass54.class.getMethod("setList",
new Class<?>[] { int.class, boolean.class });
assertEquals(2, beanInfo.getPropertyDescriptors().length);
for (PropertyDescriptor pd : beanInfo.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
assertNull(pd.getReadMethod());
assertNull(pd.getWriteMethod());
assertTrue(pd instanceof IndexedPropertyDescriptor);
assertNull(((IndexedPropertyDescriptor) pd)
.getIndexedReadMethod());
assertEquals(setter,
((IndexedPropertyDescriptor) pd)
.getIndexedWriteMethod());
}
}
}
public void test_MixedSimpleClass13() throws Exception {
BeanInfo info = Introspector.getBeanInfo(MixedSimpleClass13.class);
Method getter = MixedSimpleClass13.class.getDeclaredMethod("getList",
int.class);
Method setter = MixedSimpleClass13.class.getDeclaredMethod("setList",
int.class, Object.class);
for (PropertyDescriptor pd : info.getPropertyDescriptors()) {
if (propertyName.equals(pd.getName())) {
assertTrue(pd instanceof IndexedPropertyDescriptor);
assertNull(pd.getReadMethod());
assertNull(pd.getWriteMethod());
assertEquals(getter, ((IndexedPropertyDescriptor) pd)
.getIndexedReadMethod());
assertEquals(setter, ((IndexedPropertyDescriptor) pd)
.getIndexedWriteMethod());
}
}
}