public static void main(String[] args) throws IntrospectionException { BeanInfo beanInfo = Introspector.getBeanInfo(User.class); System.out.println("==>MethodDescriptor"); MethodDescriptor[] methodDescs = beanInfo.getMethodDescriptors(); for (MethodDescriptor method : methodDescs) { System.out.println(method.getName()); System.out.println(method.getDisplayName()); System.out.println(method.getShortDescription()); System.out.println(method.getValue("getName")); System.out.println("==>MethodDescriptor/ReflectionMethod"); Method reflectMethod = method.getMethod(); System.out.println(reflectMethod.getName()); System.out.println("==>MethodDescriptor/ParameterDescriptor"); ParameterDescriptor[] paramDescs = method.getParameterDescriptors(); if (paramDescs != null) { for (ParameterDescriptor paramDesc : paramDescs) { System.out.println(paramDesc.getName()); System.out.println(paramDesc.getDisplayName()); System.out.println(paramDesc.getShortDescription()); System.out.println(paramDesc.getValue("name")); } } } }
/** * have a look at the two arrays, and find the common elements (brute force) * * @param a first array * @param b second array * @return the common elements */ protected static MethodDescriptor[] getIntersectionFor( final MethodDescriptor[] a, final MethodDescriptor[] b, final MethodDescriptor[] demo) { final Vector<MethodDescriptor> res = new Vector<MethodDescriptor>(); for (int cnta = 0; cnta < a.length; cnta++) { final MethodDescriptor thisP = a[cnta]; if (b != null) { for (int cntb = 0; cntb < b.length; cntb++) { final MethodDescriptor thatP = b[cntb]; if (thisP.getDisplayName().equals(thatP.getDisplayName())) { res.add(thisP); } } } } return res.toArray(demo); }
/** * @param manager where we add our items to * @param editables the selected items * @param topLevelLayers the top-level layers that contain our elements (it's these that get * updated) * @param parentLayers the immediate parents of our items * @param theLayers the overall layers object * @param hideClipboardOperations */ public static void getDropdownListFor( final IMenuManager manager, final Editable[] editables, final Layer[] topLevelLayers, final Layer[] parentLayers, final Layers theLayers, final boolean hideClipboardOperations) { // sort out the top level layer, if we have one Layer theTopLayer = null; if (topLevelLayers != null) if (topLevelLayers.length > 0) theTopLayer = topLevelLayers[0]; // and now the edit-able bits if (editables.length > 0) { // first the parameters MenuManager subMenu = null; final PropertyDescriptor[] commonProps = getCommonPropertiesFor(editables); if (commonProps != null) { for (int i = 0; i < commonProps.length; i++) { final PropertyDescriptor thisP = commonProps[i]; // start off with the booleans if (supportsBooleanEditor(thisP)) { // generate boolean editors in the sub-menu subMenu = generateBooleanEditorFor( manager, subMenu, thisP, editables, theLayers, theTopLayer); } else { // now the drop-down lists if (supportsListEditor(thisP)) { // generate boolean editors in the sub-menu subMenu = generateListEditorFor(manager, subMenu, thisP, editables, theLayers, theTopLayer); } } } } // special case: if only one item is selected, try adding any additional // methods if (editables.length == 1) { // any additional ones? Editable theE = editables[0]; // ok, get the editor EditorType info = theE.getInfo(); if (info != null) { BeanInfo[] additional = info.getAdditionalBeanInfo(); // any there? if (additional != null) { // ok, loop through the beans for (int i = 0; i < additional.length; i++) { BeanInfo thisB = additional[i]; if (thisB instanceof EditorType) { EditorType editor = (EditorType) thisB; Editable subject = (Editable) editor.getData(); // and the properties PropertyDescriptor[] theseProps = thisB.getPropertyDescriptors(); for (int j = 0; j < theseProps.length; j++) { PropertyDescriptor thisP = theseProps[j]; // and wrap the object Editable[] holder = new Editable[] {subject}; if (supportsBooleanEditor(thisP)) { // generate boolean editors in the sub-menu subMenu = generateBooleanEditorFor( manager, subMenu, thisP, holder, theLayers, theTopLayer); } else { // now the drop-down lists if (supportsListEditor(thisP)) { // generate boolean editors in the sub-menu subMenu = generateListEditorFor( manager, subMenu, thisP, holder, theLayers, theTopLayer); } } } } } } } } // hmm, have a go at methods for this item // ok, try the methods final MethodDescriptor[] meths = getCommonMethodsFor(editables); if (meths != null) { for (int i = 0; i < meths.length; i++) { final Layer myTopLayer = theTopLayer; final MethodDescriptor thisMethD = meths[i]; if (thisMethD == null) { CorePlugin.logError( Status.ERROR, "Failed to create method, props may be wrongly named", null); } else { // create button for this method final Action doThisAction = new SubjectMethod( thisMethD.getDisplayName(), editables, thisMethD.getMethod(), myTopLayer, theLayers); // ok - add to the list. manager.add(doThisAction); } } } // hmm, now do the same for the undoable methods final MWC.GUI.Tools.SubjectAction[] actions = getUndoableActionsFor(editables); if (actions != null) { for (int i = 0; i < actions.length; i++) { final MWC.GUI.Tools.SubjectAction thisMethD = actions[i]; // create button for this method final IAction doThisAction = generateUndoableActionFor(thisMethD, editables, theLayers, theTopLayer); // ok - add to the list. manager.add(doThisAction); } } } // see if we're still looking at the parent element (we only show // clipboard // operations for item clicked on) if (!hideClipboardOperations) { final Clipboard theClipboard = CorePlugin.getDefault().getClipboard(); // hey, also see if we're going to do a cut/paste RightClickCutCopyAdaptor.getDropdownListFor( manager, editables, topLevelLayers, parentLayers, theLayers, theClipboard); // what about paste? Editable selectedItem = null; if (editables.length == 1) { selectedItem = editables[0]; } RightClickPasteAdaptor.getDropdownListFor( manager, selectedItem, topLevelLayers, parentLayers, theLayers, theClipboard); manager.add(new Separator()); } if (!_rightClickExtensionsChecked) { loadLoaderExtensions(); // ok, done _rightClickExtensionsChecked = true; } // hmm, do we have any right-click generators? if (_additionalRightClickItems != null) { for (final Iterator<RightClickContextItemGenerator> thisItem = _additionalRightClickItems.iterator(); thisItem.hasNext(); ) { final RightClickContextItemGenerator thisGen = (RightClickContextItemGenerator) thisItem.next(); try { thisGen.generate(manager, theLayers, topLevelLayers, editables); } catch (final Exception e) { // and log the error CorePlugin.logError(Status.ERROR, "failed whilst creating context menu", e); } } } }