1 19 20 package org.netbeans.modules.apisupport.beanbrowser; 21 22 import java.beans.BeanInfo ; 23 import java.beans.IndexedPropertyDescriptor ; 24 import java.beans.Introspector ; 25 import java.beans.PropertyDescriptor ; 26 import java.lang.reflect.Method ; 27 import java.util.Collections ; 28 import org.openide.nodes.Children; 29 import org.openide.nodes.Node; 30 31 37 public class RawBeanPropKids extends Children.Keys { 38 39 protected Object thing; 40 41 public RawBeanPropKids(Object thing) { 42 this.thing = thing; 43 } 44 45 protected void addNotify() { 46 try { 47 BeanInfo bi = Introspector.getBeanInfo(thing.getClass(), Introspector.IGNORE_ALL_BEANINFO); 48 setKeys(bi.getPropertyDescriptors()); 49 } catch (Exception e) { 51 setKeys(Collections.singleton(e)); 52 } 53 } 54 55 protected void removeNotify() { 56 setKeys(Collections.EMPTY_SET); 57 } 58 59 protected Node[] createNodes(Object key) { 60 if (key instanceof PropertyDescriptor ) { 61 PropertyDescriptor pd = (PropertyDescriptor ) key; 62 if (pd instanceof IndexedPropertyDescriptor ) { 63 return null; 65 } 66 Method meth = pd.getReadMethod(); 67 if (meth == null) { 68 return new Node[] {PropSetKids.makePlainNode("[unreadable: " + pd.getName() + "]")}; 69 } 70 try { 71 Object val; 72 meth.setAccessible(true); 76 try { 77 val = meth.invoke(thing, new Object [] { }); 78 } finally { 79 meth.setAccessible(false); 80 } 81 Node n = PropSetKids.makeObjectNode(val); 82 n.setDisplayName(pd.getName() + " = " + n.getDisplayName()); 83 return new Node[] { n }; 84 } catch (Exception e) { 85 Node n = PropSetKids.makeErrorNode(e); 86 n.setDisplayName("[property: " + pd.getName() + "] " + n.getDisplayName()); 87 return new Node[] { n }; 88 } 89 } else { 90 Node n = PropSetKids.makeErrorNode((Exception ) key); 91 n.setDisplayName("[during introspection] " + n.getDisplayName()); 92 return new Node[] { n }; 93 } 94 } 95 96 } 97 | Popular Tags |