下面列出了怎么用java.beans.BeanDescriptor的API类实例代码及写法,或者点击链接到github查看源代码。
public void testBeanDescriptorClassClass_CustomizerClassNull() {
String beanName = "BeanDescriptorTest.bean";
MockJavaBean bean = new MockJavaBean(beanName);
Class<? extends MockJavaBean> beanClass = bean.getClass();
Class<?> cusClass = null;
BeanDescriptor bd = new BeanDescriptor(beanClass, cusClass);
assertSame(beanClass, bd.getBeanClass());
assertNull(bd.getCustomizerClass());
String displayName = beanClass.getName().substring(
beanClass.getName().lastIndexOf('.') + 1);
assertEquals(displayName, bd.getDisplayName());
assertEquals(displayName, bd.getName());
assertEquals(displayName, bd.getShortDescription());
assertNotNull(bd.attributeNames());
assertFalse(bd.isExpert());
assertFalse(bd.isHidden());
assertFalse(bd.isPreferred());
}
public void testBeanDescriptorClass() {
String beanName = "BeanDescriptorTest.bean";
MockJavaBean bean = new MockJavaBean(beanName);
Class<? extends MockJavaBean> beanClass = bean.getClass();
BeanDescriptor bd = new BeanDescriptor(beanClass);
assertSame(beanClass, bd.getBeanClass());
String displayName = beanClass.getName().substring(
beanClass.getName().lastIndexOf('.') + 1);
assertEquals(displayName, bd.getDisplayName());
assertEquals(displayName, bd.getName());
assertEquals(displayName, bd.getShortDescription());
assertNotNull(bd.attributeNames());
assertFalse(bd.isExpert());
assertFalse(bd.isHidden());
assertFalse(bd.isPreferred());
}
private static String findDisplayNameFor(Object o) {
try {
if (o == null) {
return null;
}
if (o instanceof Node.Property) {
return ((Node.Property) o).getDisplayName();
}
BeanInfo bi = Introspector.getBeanInfo(o.getClass());
if (bi != null) {
BeanDescriptor bd = bi.getBeanDescriptor();
if (bd != null) {
return bd.getDisplayName();
}
}
} catch (Exception e) {
//okay, we did our best
}
return null;
}
/**
* Returns name of the bean.
*/
private String getNameForBean() {
if (nameGetter != null) {
try {
String name = (String) nameGetter.invoke(bean);
return (name != null) ? name : ""; // NOI18N
} catch (Exception ex) {
NodeOp.warning(ex);
}
}
BeanDescriptor descriptor = beanInfo.getBeanDescriptor();
return descriptor.getDisplayName();
}
/**
* Returns a bean descriptor for specified class.
*
* @param type the class to introspect
* @return a bean descriptor
*/
public static BeanDescriptor getBeanDescriptor(Class type) {
try {
return Introspector.getBeanInfo(type).getBeanDescriptor();
} catch (IntrospectionException exception) {
throw new Error("unexpected exception", exception);
}
}
/**
* Returns a bean descriptor for specified class.
*
* @param type the class to introspect
* @return a bean descriptor
*/
public static BeanDescriptor getBeanDescriptor(Class type) {
try {
return Introspector.getBeanInfo(type).getBeanDescriptor();
} catch (IntrospectionException exception) {
throw new Error("unexpected exception", exception);
}
}
/**
* @tests java.beans.BeanDescriptor#BeanDescriptor( java.lang.Class,
* java.lang.Class)
*/
public void test_Ctor2_NullPointerException() {
try {
// Regression for HARMONY-225
new BeanDescriptor(null, String.class);
fail("No expected NullPointerException");
} catch (NullPointerException e) {
}
}
@Override
public BeanDescriptor getBeanDescriptor() {
BeanDescriptor bd = new BeanDescriptor(TestClass.class, null);
bd.setShortDescription("user-defined-description");
bd.setValue("isContainer", true);
bd.setValue("containerDelegate", "user-defined-delegate");
return bd;
}
@Override
public BeanDescriptor getBeanDescriptor() {
BeanDescriptor descriptor = new BeanDescriptor(getBeanClass());
try {
// Attempt to load the displayName and shortDescription explicitly.
String displayName = bundle.getString("displayName");
if (displayName != null) {
descriptor.setDisplayName(displayName);
}
String shortDescription = bundle.getString("shortDescription");
if (shortDescription != null) {
descriptor.setShortDescription(shortDescription);
}
// Add any other properties that are specified.
Enumeration enumeration = bundle.getKeys();
while (enumeration.hasMoreElements()) {
String key = (String)enumeration.nextElement();
String value = bundle.getString(key);
if (value != null) {
descriptor.setValue(key, value);
}
}
}
catch (Exception e) {
// Ignore any exceptions. We may get some if we try to load a
// a property that doesn't appear in the resource bundle.
}
return descriptor;
}
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);
}
public void testBeanDescriptorClass_Null() {
try {
new BeanDescriptor(null);
fail("Should throw NullPointerException.");
} catch (NullPointerException e) {
}
}
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());
}
/**
* Returns a bean descriptor for specified class.
*
* @param type the class to introspect
* @return a bean descriptor
*/
public static BeanDescriptor getBeanDescriptor(Class type) {
try {
return Introspector.getBeanInfo(type).getBeanDescriptor();
} catch (IntrospectionException exception) {
throw new Error("unexpected exception", exception);
}
}
/**
* Returns a bean descriptor for specified class.
*
* @param type the class to introspect
* @return a bean descriptor
*/
public static BeanDescriptor getBeanDescriptor(Class type) {
try {
return Introspector.getBeanInfo(type).getBeanDescriptor();
} catch (IntrospectionException exception) {
throw new Error("unexpected exception", exception);
}
}
private Sheet.Set createPropertiesSet(BeanNode.Descriptor descr, BeanDescriptor bd) {
Sheet.Set props;
props = Sheet.createPropertiesSet();
if (descr.property != null) {
convertProps (props, descr.property, this);
}
if (bd != null) {
// #29550: help from the beaninfo on property tabs
Object helpID = bd.getValue("propertiesHelpID"); // NOI18N
if (helpID != null && helpID instanceof String) {
props.setValue("helpID", helpID); // NOI18N
}
}
return props;
}
@Override
public BeanDescriptor getBeanDescriptor() {
BeanDescriptor descr = new BeanDescriptor(MicrosoftEdgeBrowser.class);
descr.setDisplayName(NbBundle.getMessage(MicrosoftEdgeBrowserBeanInfo.class, "CTL_MicrosoftEdgeBrowserName")); // NOI18N
descr.setShortDescription(NbBundle.getMessage(MicrosoftEdgeBrowserBeanInfo.class, "HINT_MicrosoftEdgeBrowserName")); // NOI18N
descr.setValue ("helpID", "org.netbeans.modules.extbrowser.ExtWebBrowser"); // NOI18N
return descr;
}
@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() {
BeanDescriptor bd = new BeanDescriptor(Component.class);
// set a value to ensure that it's unique
bd.setValue("test", Boolean.TRUE);
return bd;
}
public BeanDescriptor getBeanDescriptor() {
BeanDescriptor bd = new BeanDescriptor(FooBar.class);
// set a value to ensure that it's unique
bd.setValue("test", Boolean.TRUE);
return bd;
}
private static BeanDescriptor getBdescriptor(){
BeanDescriptor beanDescriptor = new BeanDescriptor ( com.talent.aio.examples.im.client.ui.component.MsgTextAreaJPopupMenu.class , null ); // NOI18N//GEN-HEADEREND:BeanDescriptor
// Here you can add code for customizing the BeanDescriptor.
return beanDescriptor; }
@Override
public BeanDescriptor getBeanDescriptor() {
return new BeanDescriptor(FirstBean.class);
}
public BeanDescriptor getBeanDescriptor() {
BeanDescriptor bd = new BeanDescriptor(Foo.class);
// set a value to ensure that it's unique
bd.setValue("test", Boolean.TRUE);
return bd;
}
public BeanDescriptor getBeanDescriptor() {
return this.info.getBeanDescriptor();
}
public BeanDescriptor getBeanDescriptor() {
return new BeanDescriptor(Manual.class, AutomaticCustomizer.class);
}
public BeanDescriptor getBeanDescriptor() {
BeanDescriptor bd = new BeanDescriptor(Wombat.class);
// set a value to ensure that it's unique
bd.setValue("test", Boolean.TRUE);
return bd;
}
public BeanDescriptor getBeanDescriptor() {
BeanDescriptor bd = new BeanDescriptor(Foo.class);
// set a value to ensure that it's unique
bd.setValue("test", Boolean.TRUE);
return bd;
}
public BeanDescriptor getBeanDescriptor() {
return this.info.getBeanDescriptor();
}
public BeanDescriptor getBeanDescriptor() {
return new BeanDescriptor(Manual.class, AutomaticCustomizer.class);
}
@Override
public BeanDescriptor getBeanDescriptor() {
return new BeanDescriptor(SecondBean.class);
}
public BeanDescriptor getBeanDescriptor() {
return this.info.getBeanDescriptor();
}