gpt4 book ai didi

JavaBeans Introspector 无法正确查找具有接口(interface)层次结构类型的属性

转载 作者:太空宇宙 更新时间:2023-11-04 08:45:27 24 4
gpt4 key购买 nike

我阅读了 JavaBeans 规范,但没有发现这种行为。这是一个错误吗?

  • testPropertyType 失败,因为需要 Data

  • testPropertyReadable 成功,因为存在 DefaultBean.getMyData 返回 Data 方法

  • testPropertyWritable 失败,因为 DefaultBean.setMyData(Data) 方法不存在

在 JavaSE 6 上测试

import java.beans.BeanInfo;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import org.junit.Test;

public class DefaultBeanTest {

@Test
public void testPropertyType()
throws Exception
{
final BeanInfo beanInfo = Introspector.getBeanInfo(DefaultBean.class);
for (final PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
if ("class".equals(property.getName())) {
continue;
}

if ("myData".equals(property.getName())) {
if (!property.getPropertyType().equals(ModifiableData.class)) {
throw new AssertionError("expects " + ModifiableData.class + " but was "
+ property.getPropertyType());
}
}
}
}


@Test
public void testPropertyReadable()
throws Exception
{
final BeanInfo beanInfo = Introspector.getBeanInfo(DefaultBean.class);
for (final PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
if ("class".equals(property.getName())) {
continue;
}

if ("myData".equals(property.getName())) {
if (property.getReadMethod() == null) {
throw new AssertionError("expects read method");
}
}
}
}


@Test
public void testPropertyWritable()
throws Exception
{
final BeanInfo beanInfo = Introspector.getBeanInfo(DefaultBean.class);
for (final PropertyDescriptor property : beanInfo.getPropertyDescriptors()) {
if ("class".equals(property.getName())) {
continue;
}

if ("myData".equals(property.getName())) {
if (property.getWriteMethod() == null) {
throw new AssertionError("expects write method");
}
}
}
}


static interface Data {

}

static interface ModifiableData
extends Data {
}

static class DefaultData
implements ModifiableData {

}

static interface Bean {

Data getMyData();

}

static interface ModifiableBean
extends Bean {

ModifiableData getMyData();


void setMyData(
ModifiableData data);
}

static class DefaultBean
implements ModifiableBean {

@Override
public ModifiableData getMyData()
{
return this.data;
}


@Override
public void setMyData(
final ModifiableData data)
{
this.data = data;
}


private ModifiableData data;

}

}

最佳答案

关于JavaBeans Introspector 无法正确查找具有接口(interface)层次结构类型的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4438815/

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