下面列出了java.beans.BeanInfo#getBeanDescriptor ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
public void testBeanInfo_1() throws IntrospectionException {
Class<FakeFox011> beanClass = FakeFox011.class;
BeanInfo info = Introspector.getBeanInfo(beanClass);
assertNull(info.getAdditionalBeanInfo());
BeanDescriptor beanDesc = info.getBeanDescriptor();
assertEquals("FakeFox011", beanDesc.getName());
assertEquals(0, info.getEventSetDescriptors().length);
assertEquals(-1, info.getDefaultEventIndex());
assertEquals(0, info.getDefaultPropertyIndex());
MethodDescriptor[] methodDesc = info.getMethodDescriptors();
assertEquals(4, methodDesc.length);
PropertyDescriptor[] propertyDesc = info.getPropertyDescriptors();
assertEquals(2, propertyDesc.length);
for (PropertyDescriptor element : propertyDesc) {
if (element.getName().equals("class")) {
assertNull(element.getWriteMethod());
assertNotNull(element.getReadMethod());
}
}
}
public void testBeanInfo_2() throws IntrospectionException {
Class<FakeFox02> beanClass = FakeFox02.class;
BeanInfo info = Introspector.getBeanInfo(beanClass);
assertNull(info.getAdditionalBeanInfo());
BeanDescriptor beanDesc = info.getBeanDescriptor();
assertEquals("FakeFox02", beanDesc.getName());
assertEquals(0, info.getEventSetDescriptors().length);
assertEquals(-1, info.getDefaultEventIndex());
assertEquals(-1, info.getDefaultPropertyIndex());
PropertyDescriptor[] propertyDesc = info.getPropertyDescriptors();
for (PropertyDescriptor element : propertyDesc) {
if (element.getName().equals("fox02")) {
assertEquals("fox02.beaninfo", element.getDisplayName());
}
}
}
public void testSetBeanInfoSearchPath_SameClassesInDifferentPackage()
throws IntrospectionException {
// set the search path in the correct sequence
Introspector
.setBeanInfoSearchPath(new String[] {
"org.apache.harmony.beans.tests.support.mock.homonymy.mocksubject1.info",
"org.apache.harmony.beans.tests.support.mock.homonymy.mocksubject2.info", });
BeanInfo beanInfo = Introspector
.getBeanInfo(org.apache.harmony.beans.tests.support.mock.homonymy.mocksubject1.MockHomonymySubject.class);
BeanDescriptor beanDesc = beanInfo.getBeanDescriptor();
assertEquals(beanDesc.getName(), "mocksubject1");
assertEquals(
beanDesc.getBeanClass(),
org.apache.harmony.beans.tests.support.mock.homonymy.mocksubject1.MockHomonymySubject.class);
// set the search path in the reverse sequence
Introspector
.setBeanInfoSearchPath(new String[] {
"org.apache.harmony.beans.tests.support.mock.homonymy.mocksubject2.info",
"org.apache.harmony.beans.tests.support.mock.homonymy.mocksubject1.info", });
beanInfo = Introspector
.getBeanInfo(org.apache.harmony.beans.tests.support.mock.homonymy.mocksubject1.MockHomonymySubject.class);
beanDesc = beanInfo.getBeanDescriptor();
assertEquals(beanDesc.getName(), "mocksubject1");
assertEquals(
beanDesc.getBeanClass(),
org.apache.harmony.beans.tests.support.mock.homonymy.mocksubject1.MockHomonymySubject.class);
}
/**
* The test checks the getBeanDescriptor method
*/
public void testBeanDescriptor() throws Exception {
String[] oldBeanInfoSearchPath = Introspector.getBeanInfoSearchPath();
try {
Introspector
.setBeanInfoSearchPath(new String[] { "java.beans.infos" });
BeanInfo info = Introspector.getBeanInfo(SampleBean.class);
assertNotNull(info);
BeanDescriptor descriptor = info.getBeanDescriptor();
assertNotNull(descriptor);
assertEquals(SampleBean.class, descriptor.getBeanClass());
} finally {
Introspector.setBeanInfoSearchPath(oldBeanInfoSearchPath);
}
}
private Class getCustomizerClass(Breakpoint b) {
BeanInfo bi = findBeanInfo(b.getClass());
if (bi == null) {
try {
bi = Introspector.getBeanInfo(b.getClass());
} catch (Exception ex) {
Exceptions.printStackTrace(ex);
return null;
}
}
BeanDescriptor bd = bi.getBeanDescriptor();
if (bd == null) return null;
Class cc = bd.getCustomizerClass();
return cc;
}
private static void test(Class<?> type, Object iC, Object cD) throws Exception {
System.out.println(type);
BeanInfo info = Introspector.getBeanInfo(type);
BeanDescriptor bd = info.getBeanDescriptor();
test(bd, "isContainer", iC);
test(bd, "containerDelegate", cD);
}
public void testGetBeanInfoSearchPath_Default()
throws IntrospectionException, ClassNotFoundException {
BeanInfo info = Introspector.getBeanInfo(MockFooButton.class);
PropertyDescriptor[] pds = info.getPropertyDescriptors();
BeanDescriptor beanDesc;
assertEquals(2, pds.length);
assertEquals("class", pds[0].getName());
beanDesc = info.getBeanDescriptor();
assertEquals("MockFooButton", beanDesc.getName());
}
@Override
protected BeanInfo instantiate(Class<?> type, String prefix, String name) {
if (DEFAULT.equals(prefix)) {
prefix = DEFAULT_NEW;
}
// this optimization will only use the BeanInfo search path
// if is has changed from the original
// or trying to get the ComponentBeanInfo
BeanInfo info = !DEFAULT_NEW.equals(prefix) || "ComponentBeanInfo".equals(name)
? super.instantiate(type, prefix, name)
: null;
if (info != null) {
// make sure that the returned BeanInfo matches the class
BeanDescriptor bd = info.getBeanDescriptor();
if (bd != null) {
if (type.equals(bd.getBeanClass())) {
return info;
}
}
else {
PropertyDescriptor[] pds = info.getPropertyDescriptors();
if (pds != null) {
for (PropertyDescriptor pd : pds) {
Method method = pd.getReadMethod();
if (method == null) {
method = pd.getWriteMethod();
}
if (isValid(type, method)) {
return info;
}
}
}
else {
MethodDescriptor[] mds = info.getMethodDescriptors();
if (mds != null) {
for (MethodDescriptor md : mds) {
if (isValid(type, md.getMethod())) {
return info;
}
}
}
}
}
}
return null;
}
@Override
protected BeanInfo instantiate(Class<?> type, String prefix, String name) {
if (DEFAULT.equals(prefix)) {
prefix = DEFAULT_NEW;
}
// this optimization will only use the BeanInfo search path
// if is has changed from the original
// or trying to get the ComponentBeanInfo
BeanInfo info = !DEFAULT_NEW.equals(prefix) || "ComponentBeanInfo".equals(name)
? super.instantiate(type, prefix, name)
: null;
if (info != null) {
// make sure that the returned BeanInfo matches the class
BeanDescriptor bd = info.getBeanDescriptor();
if (bd != null) {
if (type.equals(bd.getBeanClass())) {
return info;
}
}
else {
PropertyDescriptor[] pds = info.getPropertyDescriptors();
if (pds != null) {
for (PropertyDescriptor pd : pds) {
Method method = pd.getReadMethod();
if (method == null) {
method = pd.getWriteMethod();
}
if (isValid(type, method)) {
return info;
}
}
}
else {
MethodDescriptor[] mds = info.getMethodDescriptors();
if (mds != null) {
for (MethodDescriptor md : mds) {
if (isValid(type, md.getMethod())) {
return info;
}
}
}
}
}
}
return null;
}
@Override
protected BeanInfo instantiate(Class<?> type, String prefix, String name) {
if (DEFAULT.equals(prefix)) {
prefix = DEFAULT_NEW;
}
// this optimization will only use the BeanInfo search path
// if is has changed from the original
// or trying to get the ComponentBeanInfo
BeanInfo info = !DEFAULT_NEW.equals(prefix) || "ComponentBeanInfo".equals(name)
? super.instantiate(type, prefix, name)
: null;
if (info != null) {
// make sure that the returned BeanInfo matches the class
BeanDescriptor bd = info.getBeanDescriptor();
if (bd != null) {
if (type.equals(bd.getBeanClass())) {
return info;
}
}
else {
PropertyDescriptor[] pds = info.getPropertyDescriptors();
if (pds != null) {
for (PropertyDescriptor pd : pds) {
Method method = pd.getReadMethod();
if (method == null) {
method = pd.getWriteMethod();
}
if (isValid(type, method)) {
return info;
}
}
}
else {
MethodDescriptor[] mds = info.getMethodDescriptors();
if (mds != null) {
for (MethodDescriptor md : mds) {
if (isValid(type, md.getMethod())) {
return info;
}
}
}
}
}
}
return null;
}
public BeanDescriptor getBeanDescriptor(Class<?> beanClass) {
BeanInfo beanInfo = getBeanInfo(beanClass);
return beanInfo.getBeanDescriptor();
}
@Override
protected BeanInfo instantiate(Class<?> type, String prefix, String name) {
if (DEFAULT.equals(prefix)) {
prefix = DEFAULT_NEW;
}
// this optimization will only use the BeanInfo search path
// if is has changed from the original
// or trying to get the ComponentBeanInfo
BeanInfo info = !DEFAULT_NEW.equals(prefix) || "ComponentBeanInfo".equals(name)
? super.instantiate(type, prefix, name)
: null;
if (info != null) {
// make sure that the returned BeanInfo matches the class
BeanDescriptor bd = info.getBeanDescriptor();
if (bd != null) {
if (type.equals(bd.getBeanClass())) {
return info;
}
}
else {
PropertyDescriptor[] pds = info.getPropertyDescriptors();
if (pds != null) {
for (PropertyDescriptor pd : pds) {
Method method = pd.getReadMethod();
if (method == null) {
method = pd.getWriteMethod();
}
if (isValid(type, method)) {
return info;
}
}
}
else {
MethodDescriptor[] mds = info.getMethodDescriptors();
if (mds != null) {
for (MethodDescriptor md : mds) {
if (isValid(type, md.getMethod())) {
return info;
}
}
}
}
}
}
return null;
}
@Override
protected BeanInfo instantiate(Class<?> type, String prefix, String name) {
if (DEFAULT.equals(prefix)) {
prefix = DEFAULT_NEW;
}
// this optimization will only use the BeanInfo search path
// if is has changed from the original
// or trying to get the ComponentBeanInfo
BeanInfo info = !DEFAULT_NEW.equals(prefix) || "ComponentBeanInfo".equals(name)
? super.instantiate(type, prefix, name)
: null;
if (info != null) {
// make sure that the returned BeanInfo matches the class
BeanDescriptor bd = info.getBeanDescriptor();
if (bd != null) {
if (type.equals(bd.getBeanClass())) {
return info;
}
}
else {
PropertyDescriptor[] pds = info.getPropertyDescriptors();
if (pds != null) {
for (PropertyDescriptor pd : pds) {
Method method = pd.getReadMethod();
if (method == null) {
method = pd.getWriteMethod();
}
if (isValid(type, method)) {
return info;
}
}
}
else {
MethodDescriptor[] mds = info.getMethodDescriptors();
if (mds != null) {
for (MethodDescriptor md : mds) {
if (isValid(type, md.getMethod())) {
return info;
}
}
}
}
}
}
return null;
}
@Override
protected BeanInfo instantiate(Class<?> type, String prefix, String name) {
if (DEFAULT.equals(prefix)) {
prefix = DEFAULT_NEW;
}
// this optimization will only use the BeanInfo search path
// if is has changed from the original
// or trying to get the ComponentBeanInfo
BeanInfo info = !DEFAULT_NEW.equals(prefix) || "ComponentBeanInfo".equals(name)
? super.instantiate(type, prefix, name)
: null;
if (info != null) {
// make sure that the returned BeanInfo matches the class
BeanDescriptor bd = info.getBeanDescriptor();
if (bd != null) {
if (type.equals(bd.getBeanClass())) {
return info;
}
}
else {
PropertyDescriptor[] pds = info.getPropertyDescriptors();
if (pds != null) {
for (PropertyDescriptor pd : pds) {
Method method = pd.getReadMethod();
if (method == null) {
method = pd.getWriteMethod();
}
if (isValid(type, method)) {
return info;
}
}
}
else {
MethodDescriptor[] mds = info.getMethodDescriptors();
if (mds != null) {
for (MethodDescriptor md : mds) {
if (isValid(type, md.getMethod())) {
return info;
}
}
}
}
}
}
return null;
}
public static void main(String[] args) throws Exception {
BeanInfo i = Introspector.getBeanInfo(TestClass.class, Object.class);
BeanDescriptor bd = i.getBeanDescriptor();
checkEq("description", bd.getShortDescription(), "user-defined-description");
checkEq("default property index", i.getDefaultPropertyIndex(), 1);
checkEq("default event index", i.getDefaultEventIndex(), 0);
checkEq("isContainer", i.getBeanDescriptor().getValue("isContainer"), true);
checkEq("containerDelegate",
i.getBeanDescriptor().getValue("containerDelegate"), "user-defined-delegate");
System.out.println("");
PropertyDescriptor[] pds = i.getPropertyDescriptors();
for (PropertyDescriptor pd: pds) {
String name = pd.getName();
switch (name) {
case "value":
checkEq("\"value\" isBound", pd.isBound(), true);
checkEq("\"value\" isConstrained", pd.isConstrained(), true);
checkEq("\"value\" isExpert", pd.isExpert(), true);
checkEq("\"value\" isHidden", pd.isHidden(), true);
checkEq("\"value\" isPreferred", pd.isPreferred(), true);
checkEq("\"value\" required", pd.getValue("required"), true);
checkEq("\"value\" visualUpdate", pd.getValue("visualUpdate"), true);
checkEq("\"value\" description", pd.getShortDescription(), "user-defined-value");
checkEnumEq(pd.getName(), pd.getValue("enumerationValues"),
new Object[]{
"EAST", 3, "javax.swing.SwingConstants.EAST",
"WEST", 7, "javax.swing.SwingConstants.WEST"});
System.out.println("");
break;
case "other":
checkEq("\"other\" isBound", pd.isBound(), false);
checkEq("\"other\" isConstrained", pd.isConstrained(), false);
checkEq("\"other\" isExpert", pd.isExpert(), false);
checkEq("\"other\" isHidden", pd.isHidden(), false);
checkEq("\"other\" isPreferred", pd.isPreferred(), false);
checkEq("\"other\" required", pd.getValue("required"), false);
checkEq("\"other\" visualUpdate", pd.getValue("visualUpdate"), false);
checkEq("\"other\" description", pd.getShortDescription(), "user-defined-other");
checkEnumEq(pd.getName(), pd.getValue("enumerationValues"),
new Object[]{"TOP", 1, "javax.swing.SwingConstants.TOP"});
System.out.println("");
break;
default:
throw new Exception("invalid property descriptor: " + name);
}
}
}
@Override
protected BeanInfo instantiate(Class<?> type, String prefix, String name) {
if (DEFAULT.equals(prefix)) {
prefix = DEFAULT_NEW;
}
// this optimization will only use the BeanInfo search path
// if is has changed from the original
// or trying to get the ComponentBeanInfo
BeanInfo info = !DEFAULT_NEW.equals(prefix) || "ComponentBeanInfo".equals(name)
? super.instantiate(type, prefix, name)
: null;
if (info != null) {
// make sure that the returned BeanInfo matches the class
BeanDescriptor bd = info.getBeanDescriptor();
if (bd != null) {
if (type.equals(bd.getBeanClass())) {
return info;
}
}
else {
PropertyDescriptor[] pds = info.getPropertyDescriptors();
if (pds != null) {
for (PropertyDescriptor pd : pds) {
Method method = pd.getReadMethod();
if (method == null) {
method = pd.getWriteMethod();
}
if (isValid(type, method)) {
return info;
}
}
}
else {
MethodDescriptor[] mds = info.getMethodDescriptors();
if (mds != null) {
for (MethodDescriptor md : mds) {
if (isValid(type, md.getMethod())) {
return info;
}
}
}
}
}
}
return null;
}
@Override
protected BeanInfo instantiate(Class<?> type, String prefix, String name) {
if (DEFAULT.equals(prefix)) {
prefix = DEFAULT_NEW;
}
// this optimization will only use the BeanInfo search path
// if is has changed from the original
// or trying to get the ComponentBeanInfo
BeanInfo info = !DEFAULT_NEW.equals(prefix) || "ComponentBeanInfo".equals(name)
? super.instantiate(type, prefix, name)
: null;
if (info != null) {
// make sure that the returned BeanInfo matches the class
BeanDescriptor bd = info.getBeanDescriptor();
if (bd != null) {
if (type.equals(bd.getBeanClass())) {
return info;
}
}
else {
PropertyDescriptor[] pds = info.getPropertyDescriptors();
if (pds != null) {
for (PropertyDescriptor pd : pds) {
Method method = pd.getReadMethod();
if (method == null) {
method = pd.getWriteMethod();
}
if (isValid(type, method)) {
return info;
}
}
}
else {
MethodDescriptor[] mds = info.getMethodDescriptors();
if (mds != null) {
for (MethodDescriptor md : mds) {
if (isValid(type, md.getMethod())) {
return info;
}
}
}
}
}
}
return null;
}
@Override
protected BeanInfo instantiate(Class<?> type, String prefix, String name) {
if (DEFAULT.equals(prefix)) {
prefix = DEFAULT_NEW;
}
// this optimization will only use the BeanInfo search path
// if is has changed from the original
// or trying to get the ComponentBeanInfo
BeanInfo info = !DEFAULT_NEW.equals(prefix) || "ComponentBeanInfo".equals(name)
? super.instantiate(type, prefix, name)
: null;
if (info != null) {
// make sure that the returned BeanInfo matches the class
BeanDescriptor bd = info.getBeanDescriptor();
if (bd != null) {
if (type.equals(bd.getBeanClass())) {
return info;
}
}
else {
PropertyDescriptor[] pds = info.getPropertyDescriptors();
if (pds != null) {
for (PropertyDescriptor pd : pds) {
Method method = pd.getReadMethod();
if (method == null) {
method = pd.getWriteMethod();
}
if (isValid(type, method)) {
return info;
}
}
}
else {
MethodDescriptor[] mds = info.getMethodDescriptors();
if (mds != null) {
for (MethodDescriptor md : mds) {
if (isValid(type, md.getMethod())) {
return info;
}
}
}
}
}
}
return null;
}
@Override
protected BeanInfo instantiate(Class<?> type, String prefix, String name) {
if (DEFAULT.equals(prefix)) {
prefix = DEFAULT_NEW;
}
// this optimization will only use the BeanInfo search path
// if is has changed from the original
// or trying to get the ComponentBeanInfo
BeanInfo info = !DEFAULT_NEW.equals(prefix) || "ComponentBeanInfo".equals(name)
? super.instantiate(type, prefix, name)
: null;
if (info != null) {
// make sure that the returned BeanInfo matches the class
BeanDescriptor bd = info.getBeanDescriptor();
if (bd != null) {
if (type.equals(bd.getBeanClass())) {
return info;
}
}
else {
PropertyDescriptor[] pds = info.getPropertyDescriptors();
if (pds != null) {
for (PropertyDescriptor pd : pds) {
Method method = pd.getReadMethod();
if (method == null) {
method = pd.getWriteMethod();
}
if (isValid(type, method)) {
return info;
}
}
}
else {
MethodDescriptor[] mds = info.getMethodDescriptors();
if (mds != null) {
for (MethodDescriptor md : mds) {
if (isValid(type, md.getMethod())) {
return info;
}
}
}
}
}
}
return null;
}
@Override
protected BeanInfo instantiate(Class<?> type, String prefix, String name) {
if (DEFAULT.equals(prefix)) {
prefix = DEFAULT_NEW;
}
// this optimization will only use the BeanInfo search path
// if is has changed from the original
// or trying to get the ComponentBeanInfo
BeanInfo info = !DEFAULT_NEW.equals(prefix) || "ComponentBeanInfo".equals(name)
? super.instantiate(type, prefix, name)
: null;
if (info != null) {
// make sure that the returned BeanInfo matches the class
BeanDescriptor bd = info.getBeanDescriptor();
if (bd != null) {
if (type.equals(bd.getBeanClass())) {
return info;
}
}
else {
PropertyDescriptor[] pds = info.getPropertyDescriptors();
if (pds != null) {
for (PropertyDescriptor pd : pds) {
Method method = pd.getReadMethod();
if (method == null) {
method = pd.getWriteMethod();
}
if (isValid(type, method)) {
return info;
}
}
}
else {
MethodDescriptor[] mds = info.getMethodDescriptors();
if (mds != null) {
for (MethodDescriptor md : mds) {
if (isValid(type, md.getMethod())) {
return info;
}
}
}
}
}
}
return null;
}