/// <summary> /// Using the Introspector class the method returns the property descriptors obtained through introspection. /// </summary> /// <param name="clazz">to introspect</param> /// <returns>array of property descriptors</returns> protected internal static PropertyDescriptor[] Introspect(Type clazz) { BeanInfo beanInfo; try { beanInfo = Introspector.GetBeanInfo(clazz); } catch (IntrospectionException e) { return new PropertyDescriptor[0]; } return beanInfo.PropertyDescriptors; }
private static PropertyDescriptor GetPropertyDescriptor(Class type, String property) { try { foreach (PropertyDescriptor pd in Introspector.GetBeanInfo(type).PropertyDescriptors) { if (property.Equals(pd.Name)) { return(pd); } } } catch (IntrospectionException) { } return(null); }
// Write out the properties of this instance. private void InitBean(Class type, Object oldInstance, Object newInstance, Encoder @out) { foreach (Field field in type.Fields) { if (!ReflectUtil.isPackageAccessible(field.DeclaringClass)) { continue; } int mod = field.Modifiers; if (Modifier.IsFinal(mod) || Modifier.IsStatic(mod) || Modifier.IsTransient(mod)) { continue; } try { Expression oldGetExp = new Expression(field, "get", new Object[] { oldInstance }); Expression newGetExp = new Expression(field, "get", new Object[] { newInstance }); Object oldValue = oldGetExp.Value; Object newValue = newGetExp.Value; @out.WriteExpression(oldGetExp); if (!Objects.Equals(newValue, @out.Get(oldValue))) { @out.WriteStatement(new Statement(field, "set", new Object[] { oldInstance, oldValue })); } } catch (Exception exception) { @out.ExceptionListener.ExceptionThrown(exception); } } BeanInfo info; try { info = Introspector.GetBeanInfo(type); } catch (IntrospectionException) { return; } // Properties foreach (PropertyDescriptor d in info.PropertyDescriptors) { if (d.IsTransient()) { continue; } try { DoProperty(type, d, oldInstance, newInstance, @out); } catch (Exception e) { @out.ExceptionListener.ExceptionThrown(e); } } // Listeners /* * Pending(milne). There is a general problem with the archival of * listeners which is unresolved as of 1.4. Many of the methods * which install one object inside another (typically "add" methods * or setters) automatically install a listener on the "child" object * so that its "parent" may respond to changes that are made to it. * For example the JTable:setModel() method automatically adds a * TableModelListener (the JTable itself in this case) to the supplied * table model. * * We do not need to explicitly add these listeners to the model in an * archive as they will be added automatically by, in the above case, * the JTable's "setModel" method. In some cases, we must specifically * avoid trying to do this since the listener may be an inner class * that cannot be instantiated using public API. * * No general mechanism currently * exists for differentiating between these kind of listeners and * those which were added explicitly by the user. A mechanism must * be created to provide a general means to differentiate these * special cases so as to provide reliable persistence of listeners * for the general case. */ if (!type.IsSubclassOf(typeof(java.awt.Component))) { return; // Just handle the listeners of Components for now. } foreach (EventSetDescriptor d in info.EventSetDescriptors) { if (d.IsTransient()) { continue; } Class listenerType = d.ListenerType; // The ComponentListener is added automatically, when // Contatiner:add is called on the parent. if (listenerType == typeof([email protected])) { continue; } // JMenuItems have a change listener added to them in // their "add" methods to enable accessibility support - // see the add method in JMenuItem for details. We cannot // instantiate this instance as it is a private inner class // and do not need to do this anyway since it will be created // and installed by the "add" method. Special case this for now, // ignoring all change listeners on JMenuItems. if (listenerType == typeof([email protected]) && type == typeof(javax.swing.JMenuItem)) { continue; } EventListener[] oldL = new EventListener[0]; EventListener[] newL = new EventListener[0]; try { Method m = d.GetListenerMethod; oldL = (EventListener[])MethodUtil.invoke(m, oldInstance, new Object[] {}); newL = (EventListener[])MethodUtil.invoke(m, newInstance, new Object[] {}); } catch (Exception) { try { Method m = type.GetMethod("getListeners", new Class[] { typeof(Class) }); oldL = (EventListener[])MethodUtil.invoke(m, oldInstance, new Object[] { listenerType }); newL = (EventListener[])MethodUtil.invoke(m, newInstance, new Object[] { listenerType }); } catch (Exception) { return; } } // Asssume the listeners are in the same order and that there are no gaps. // Eventually, this may need to do true differencing. String addListenerMethodName = d.AddListenerMethod.Name; for (int i = newL.Length; i < oldL.Length; i++) { // System.out.println("Adding listener: " + addListenerMethodName + oldL[i]); InvokeStatement(oldInstance, addListenerMethodName, new Object[] { oldL[i] }, @out); } String removeListenerMethodName = d.RemoveListenerMethod.Name; for (int i = oldL.Length; i < newL.Length; i++) { InvokeStatement(oldInstance, removeListenerMethodName, new Object[] { newL[i] }, @out); } } }