public ResourceInfo.MethodInfo[] getMethodDescriptors() { Set<java.lang.reflect.Method> accessors = new HashSet<java.lang.reflect.Method>(); for (PropertyDescriptor p : getPropertyDescriptors()) { accessors.add(p.getReadMethod()); accessors.add(p.getWriteMethod()); } for (MethodDescriptor o : getRemoteMethodDescriptors()) { accessors.add(o.getMethod()); } TreeSet<MethodDescriptor> methods = new TreeSet<MethodDescriptor>( new Comparator<MethodDescriptor>() { public int compare(MethodDescriptor a, MethodDescriptor b) { return a.getName().compareTo(b.getName()); } }); methods.addAll(Arrays.asList(info.getMethodDescriptors())); Iterator<MethodDescriptor> iter = methods.iterator(); while (iter.hasNext()) { MethodDescriptor m = iter.next(); if (accessors.contains(m.getMethod())) { iter.remove(); } } return MethodInfo.wrap(methods); }
public static Method methodFor(String methodName) throws IntrospectionException { BeanInfo beanInfo = Introspector.getBeanInfo(SomeSteps.class); for (MethodDescriptor md : beanInfo.getMethodDescriptors()) { if (md.getMethod().getName().equals(methodName)) { return md.getMethod(); } } return null; }
static Method stepMethodFor(String methodName, Class<? extends Steps> stepsClass) throws IntrospectionException { BeanInfo beanInfo = Introspector.getBeanInfo(stepsClass); for (MethodDescriptor md : beanInfo.getMethodDescriptors()) { if (md.getMethod().getName().equals(methodName)) { return md.getMethod(); } } return null; }
/** * 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); }
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")); } } } }
public static Method findSetter(Class<?> clazz, String name) throws IntrospectionException { BeanInfo info = Introspector.getBeanInfo(clazz); Method setter = null; for (PropertyDescriptor pd : info.getPropertyDescriptors()) { if (name.equals(pd.getName())) { setter = pd.getWriteMethod(); break; } } if (setter == null) { // sometimes this happens even when there is a setter. Don't know why. String expectedSetterName = "set" + name.substring(0, 1).toUpperCase() + name.substring(1); for (MethodDescriptor md : info.getMethodDescriptors()) { if (md.getName().equals(expectedSetterName)) { setter = md.getMethod(); break; } } } return setter; }
private List<Method> findCandidateWriteMethods(MethodDescriptor[] methodDescriptors) { List<Method> matches = new ArrayList<Method>(); for (MethodDescriptor methodDescriptor : methodDescriptors) { Method method = methodDescriptor.getMethod(); if (isCandidateWriteMethod(method)) { matches.add(method); } } // Sort non-void returning write methods to guard against the ill effects of // non-deterministic sorting of methods returned from Class#getDeclaredMethods // under JDK 7. See http://bugs.sun.com/view_bug.do?bug_id=7023180 Collections.sort( matches, new Comparator<Method>() { @Override public int compare(Method m1, Method m2) { return m2.toString().compareTo(m1.toString()); } }); return matches; }
// ***** ***** public MethodDescriptor[] getMethodDescriptors() { try { MethodDescriptor md1 = new MethodDescriptor( getMethod( com.ibm.HostPublisher.IntegrationObject.HPubCommon.class, "processRequest")); String procReqDesc = res.getString("procReqDesc"); md1.setShortDescription(procReqDesc); MethodDescriptor md2 = new MethodDescriptor( getMethod( com.ibm.HostPublisher.IntegrationObject.HPubCommon.class, "doHPTransaction")); String doHPTransDesc = res.getString("doHPTransDesc"); md2.setShortDescription(doHPTransDesc); // method for receiving event MethodDescriptor md3 = new MethodDescriptor( getMethod( com.ibm.HostPublisher.IntegrationObject.HPubCommon.class, "hPubStartPerformed")); String hPubStartPerfDesc = res.getString("hPubStartPerfDesc"); md3.setShortDescription(hPubStartPerfDesc); MethodDescriptor[] arrayOfMDs = {md1, md2, md3}; return arrayOfMDs; } catch (IntrospectionException e) { e.printStackTrace(); } return null; }
public MethodDescriptor[] getMethodDescriptors() { try { String stringPackageName = new String("IntegrationObject"); MethodDescriptor mdGetHPubScreenState = new MethodDescriptor( getMethod(IntegrationObject.CountCourtDateDelete_Access.class, "getHPubScreenState")); mdGetHPubScreenState.setShortDescription("getHPubScreenState"); MethodDescriptor mdProcessRequest = new MethodDescriptor( getMethod(IntegrationObject.CountCourtDateDelete_Access.class, "processRequest")); mdProcessRequest.setShortDescription( "This method runs a Host Publisher Integration Object, Remote Int" + "egration Object, or EJB Access Bean from an application componen" + "t. This method throws a BeanException if an error occurs."); MethodDescriptor mdDoHPTransaction = new MethodDescriptor( getMethod(IntegrationObject.CountCourtDateDelete_Access.class, "doHPTransaction")); mdDoHPTransaction.setShortDescription( "This method runs a Host Publisher Integration Object or EJB Acce" + "ss Bean from a servlet or JSP. This method throws a BeanExcepti" + "on if an error occurs."); MethodDescriptor mdHPubStartPerformed = new MethodDescriptor( getMethod(IntegrationObject.CountCourtDateDelete_Access.class, "hPubStartPerformed")); mdHPubStartPerformed.setShortDescription( "When the hPubStartEvent occurs, this method should be called."); MethodDescriptor mdSetHPubScreenState = new MethodDescriptor( getMethod(IntegrationObject.CountCourtDateDelete_Access.class, "setHPubScreenState")); mdSetHPubScreenState.setShortDescription("setHPubScreenState"); MethodDescriptor mdGetHPubMacroMessage = new MethodDescriptor( getMethod( IntegrationObject.CountCourtDateDelete_Access.class, "getHPubMacroMessage")); mdGetHPubMacroMessage.setShortDescription("getHPubMacroMessage"); MethodDescriptor mdSetHPubMacroMessage = new MethodDescriptor( getMethod( IntegrationObject.CountCourtDateDelete_Access.class, "setHPubMacroMessage")); mdSetHPubMacroMessage.setShortDescription("setHPubMacroMessage"); MethodDescriptor[] arrayOfMDs = { mdGetHPubScreenState, mdProcessRequest, mdDoHPTransaction, mdHPubStartPerformed, mdSetHPubScreenState, mdGetHPubMacroMessage, mdSetHPubMacroMessage }; return arrayOfMDs; } catch (IntrospectionException e) { e.printStackTrace(); } return null; }
private MethodInfo(MethodDescriptor old) { super(old.getMethod(), old.getParameterDescriptors()); }
/** * Wrap the given delegate {@link BeanInfo} instance and find any non-void returning setter * methods, creating and adding a {@link PropertyDescriptor} for each. * * <p>The wrapped {@code BeanInfo} is not modified in any way by this process. * * @see #getPropertyDescriptors() * @throws IntrospectionException if any problems occur creating and adding new {@code * PropertyDescriptors} */ public ExtendedBeanInfo(BeanInfo delegate) throws IntrospectionException { this.delegate = delegate; // PropertyDescriptor instances from the delegate object are never added directly, but always // copied to the local collection of #propertyDescriptors and returned by calls to // #getPropertyDescriptors(). this algorithm iterates through all methods (method descriptors) // in the wrapped BeanInfo object, copying any existing PropertyDescriptor or creating a new // one for any non-standard setter methods found. ALL_METHODS: for (MethodDescriptor md : delegate.getMethodDescriptors()) { Method method = md.getMethod(); // bypass non-getter java.lang.Class methods for efficiency if (ReflectionUtils.isObjectMethod(method) && !method.getName().startsWith("get")) { continue ALL_METHODS; } // is the method a NON-INDEXED setter? ignore return type in order to capture non-void // signatures if (method.getName().startsWith("set") && method.getParameterTypes().length == 1) { String propertyName = propertyNameFor(method); if (propertyName.length() == 0) { continue ALL_METHODS; } for (PropertyDescriptor pd : delegate.getPropertyDescriptors()) { Method readMethod = pd.getReadMethod(); Method writeMethod = pd.getWriteMethod(); // has the setter already been found by the wrapped BeanInfo? if (writeMethod != null && writeMethod.getName().equals(method.getName())) { // yes -> copy it, including corresponding getter method (if any -- may be null) this.addOrUpdatePropertyDescriptor(propertyName, readMethod, writeMethod); continue ALL_METHODS; } // has a getter corresponding to this setter already been found by the wrapped BeanInfo? if (readMethod != null && readMethod.getName().equals(getterMethodNameFor(propertyName)) && readMethod.getReturnType().equals(method.getParameterTypes()[0])) { this.addOrUpdatePropertyDescriptor(propertyName, readMethod, method); continue ALL_METHODS; } } // the setter method was not found by the wrapped BeanInfo -> add a new PropertyDescriptor // for it // no corresponding getter was detected, so the 'read method' parameter is null. this.addOrUpdatePropertyDescriptor(propertyName, null, method); continue ALL_METHODS; } // is the method an INDEXED setter? ignore return type in order to capture non-void signatures if (method.getName().startsWith("set") && method.getParameterTypes().length == 2 && method.getParameterTypes()[0].equals(int.class)) { String propertyName = propertyNameFor(method); if (propertyName.length() == 0) { continue ALL_METHODS; } DELEGATE_PD: for (PropertyDescriptor pd : delegate.getPropertyDescriptors()) { if (!(pd instanceof IndexedPropertyDescriptor)) { continue DELEGATE_PD; } IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd; Method readMethod = ipd.getReadMethod(); Method writeMethod = ipd.getWriteMethod(); Method indexedReadMethod = ipd.getIndexedReadMethod(); Method indexedWriteMethod = ipd.getIndexedWriteMethod(); // has the setter already been found by the wrapped BeanInfo? if (indexedWriteMethod != null && indexedWriteMethod.getName().equals(method.getName())) { // yes -> copy it, including corresponding getter method (if any -- may be null) this.addOrUpdatePropertyDescriptor( propertyName, readMethod, writeMethod, indexedReadMethod, indexedWriteMethod); continue ALL_METHODS; } // has a getter corresponding to this setter already been found by the wrapped BeanInfo? if (indexedReadMethod != null && indexedReadMethod.getName().equals(getterMethodNameFor(propertyName)) && indexedReadMethod.getReturnType().equals(method.getParameterTypes()[1])) { this.addOrUpdatePropertyDescriptor( propertyName, readMethod, writeMethod, indexedReadMethod, method); continue ALL_METHODS; } } // the INDEXED setter method was not found by the wrapped BeanInfo -> add a new // PropertyDescriptor // for it. no corresponding INDEXED getter was detected, so the 'indexed read method' // parameter is null. this.addOrUpdatePropertyDescriptor(propertyName, null, null, null, method); continue ALL_METHODS; } // the method is not a setter, but is it a getter? for (PropertyDescriptor pd : delegate.getPropertyDescriptors()) { // have we already copied this read method to a property descriptor locally? for (PropertyDescriptor existingPD : this.propertyDescriptors) { if (method.equals(pd.getReadMethod()) && existingPD.getName().equals(pd.getName())) { if (existingPD.getReadMethod() == null) { // no -> add it now this.addOrUpdatePropertyDescriptor(pd.getName(), method, pd.getWriteMethod()); } // yes -> do not add a duplicate continue ALL_METHODS; } } if (method == pd.getReadMethod() || (pd instanceof IndexedPropertyDescriptor && method == ((IndexedPropertyDescriptor) pd).getIndexedReadMethod())) { // yes -> copy it, including corresponding setter method (if any -- may be null) if (pd instanceof IndexedPropertyDescriptor) { this.addOrUpdatePropertyDescriptor( pd.getName(), pd.getReadMethod(), pd.getWriteMethod(), ((IndexedPropertyDescriptor) pd).getIndexedReadMethod(), ((IndexedPropertyDescriptor) pd).getIndexedWriteMethod()); } else { this.addOrUpdatePropertyDescriptor( pd.getName(), pd.getReadMethod(), pd.getWriteMethod()); } continue ALL_METHODS; } } } }
/** * @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); } } } }