List of usage examples for java.beans PropertyDescriptor getWriteMethod
public synchronized Method getWriteMethod()
From source file:org.eclipse.wb.tests.designer.core.util.reflect.ReflectionUtilsTest.java
/** * Asserts that given {@link Class} has {@link PropertyDescriptor}'s with given names. *///from w w w. j av a 2s . c om private static void assertHasProperties(Class<?> clazz, String... expectedNames) throws Exception { List<PropertyDescriptor> descriptors = getPropertyDescriptors(clazz); // prepare names/setters of all PropertyDescriptor's List<String> propertyNames = Lists.newArrayList(); List<Method> propertySetters = Lists.newArrayList(); for (PropertyDescriptor descriptor : descriptors) { propertyNames.add(descriptor.getName()); if (descriptor.getWriteMethod() != null) { propertySetters.add(descriptor.getWriteMethod()); } } // no duplicates, please assertThat(propertyNames).doesNotHaveDuplicates(); assertThat(propertySetters).doesNotHaveDuplicates(); // assert expected names assertThat(propertyNames).contains((Object[]) expectedNames); }
From source file:ca.sqlpower.matchmaker.MatchMakerTestCase.java
public void testDuplicate() throws Exception { MatchMakerObject mmo = getTarget();/*from ww w. ja v a2 s . c o m*/ List<PropertyDescriptor> settableProperties; settableProperties = Arrays.asList(PropertyUtils.getPropertyDescriptors(mmo.getClass())); propertiesToIgnoreForDuplication.add("defaultInputClass"); propertiesToIgnoreForDuplication.add("parameters"); propertiesToIgnoreForDuplication.add("MSOInputs"); propertiesToIgnoreForDuplication.add("UUID"); propertiesToIgnoreForDuplication.add("oid"); propertiesToIgnoreForDuplication.add("parent"); propertiesToIgnoreForDuplication.add("parentProject"); propertiesToIgnoreForDuplication.add("session"); propertiesToIgnoreForDuplication.add("allowedChildTypes"); propertiesToIgnoreForDuplication.add("cachableTable"); propertiesToIgnoreForDuplication.add("cachableColumn"); propertiesToIgnoreForDuplication.add("importedCachableColumn"); propertiesToIgnoreForDuplication.add("class"); propertiesToIgnoreForDuplication.add("createDate"); propertiesToIgnoreForDuplication.add("createAppUser"); propertiesToIgnoreForDuplication.add("createOsUser"); propertiesToIgnoreForDuplication.add("dependencies"); propertiesToIgnoreForDuplication.add("children"); propertiesToIgnoreForDuplication.add("lastUpdateDate"); propertiesToIgnoreForDuplication.add("lastUpdateAppUser"); propertiesToIgnoreForDuplication.add("lastUpdateOSUser"); propertiesToIgnoreForDuplication.add("magicEnabled"); propertiesToIgnoreForDuplication.add("mergingEngine"); propertiesToIgnoreForDuplication.add("matchingEngine"); propertiesToIgnoreForDuplication.add("cleansingEngine"); propertiesToIgnoreForDuplication.add("addressCorrectionEngine"); propertiesToIgnoreForDuplication.add("addressCommittingEngine"); propertiesToIgnoreForDuplication.add("undoing"); propertiesToIgnoreForDuplication.add("results"); propertiesToIgnoreForDuplication.add("runningEngine"); propertiesToIgnoreForDuplication.add("runnableDispatcher"); propertiesToIgnoreForDuplication.add("workspaceContainer"); propertiesToIgnoreForDuplication.add("tableMergeRules"); propertiesToIgnoreForDuplication.add("resultStep"); propertiesToIgnoreForDuplication.add("inputSteps"); propertiesToIgnoreForDuplication.add("mungeSteps"); propertiesToIgnoreForDuplication.add("projects"); propertiesToIgnoreForDuplication.add("JDBCDataSource"); propertiesToIgnoreForDuplication.add("table"); propertiesToIgnoreForDuplication.add("tableIndex"); propertiesToIgnoreForDuplication.add("columnMergeRules"); propertiesToIgnoreForDuplication.add("inputs"); propertiesToIgnoreForDuplication.add("mungeStepOutputs"); propertiesToIgnoreForDuplication.add("parameterNames"); propertiesToIgnoreForDuplication.add("project"); propertiesToIgnoreForDuplication.add("addressStatus"); propertiesToIgnoreForDuplication.add("addressDB"); propertiesToIgnoreForDuplication.add("open"); propertiesToIgnoreForDuplication.add("expanded"); propertiesToIgnoreForDuplication.add("position"); propertiesToIgnoreForDuplication.add("inputCount"); propertiesToIgnoreForDuplication.add("matchPool"); propertiesToIgnoreForDuplication.add("resultTableCatalog"); propertiesToIgnoreForDuplication.add("resultTableName"); propertiesToIgnoreForDuplication.add("resultTableSchema"); propertiesToIgnoreForDuplication.add("resultTableSPDataSource"); propertiesToIgnoreForDuplication.add("sourceTableCatalog"); propertiesToIgnoreForDuplication.add("sourceTableName"); propertiesToIgnoreForDuplication.add("sourceTableSchema"); propertiesToIgnoreForDuplication.add("sourceTableSPDataSource"); propertiesToIgnoreForDuplication.add("xrefTableCatalog"); propertiesToIgnoreForDuplication.add("xrefTableName"); propertiesToIgnoreForDuplication.add("xrefTableSchema"); propertiesToIgnoreForDuplication.add("xrefTableSPDataSource"); //this throws an exception if the DS does not exist propertiesToIgnoreForDuplication.add("spDataSource"); // First pass: set all settable properties, because testing the duplication of // an object with all its properties at their defaults is not a // very convincing test of duplication! for (PropertyDescriptor property : settableProperties) { if (propertiesToIgnoreForDuplication.contains(property.getName())) continue; Object oldVal; try { oldVal = PropertyUtils.getSimpleProperty(mmo, property.getName()); // check for a setter if (property.getWriteMethod() != null && !property.getName().equals("children")) { Object newVal = getNewDifferentValue(mmo, property, oldVal); BeanUtils.copyProperty(mmo, property.getName(), newVal); } } catch (NoSuchMethodException e) { System.out.println( "Skipping non-settable property " + property.getName() + " on " + mmo.getClass().getName()); } } // Second pass get a copy make sure all of // the original mutable objects returned from getters are different // between the two objects, but have the same values. MatchMakerObject duplicate = mmo.duplicate((MatchMakerObject) mmo.getParent()); for (PropertyDescriptor property : settableProperties) { if (propertiesToIgnoreForDuplication.contains(property.getName())) continue; Object oldVal; try { oldVal = PropertyUtils.getSimpleProperty(mmo, property.getName()); /* * If this value is an unmodifiable list, it is then going to be a property * we do not wish to test duplication for, like the children lists. This * is a way to catch them all at once. */ boolean listIsModifiable = true; if (oldVal instanceof List) { List l = (List) oldVal; try { l.add("test"); l.remove("test"); } catch (UnsupportedOperationException e) { listIsModifiable = false; } } if (listIsModifiable) { Object copyVal = PropertyUtils.getSimpleProperty(duplicate, property.getName()); if (oldVal == null) { throw new NullPointerException("We forgot to set " + property.getName()); } else { if (oldVal instanceof MungeStep) { MungeStep oldStep = (MungeStep) oldVal; MungeStep copyStep = (MungeStep) copyVal; assertNotSame("The two MungeStep's share the same instance.", oldVal, copyVal); assertEquals("The two names are different.", oldStep.getName(), copyStep.getName()); assertEquals("The two visible properties are different.", oldStep.isVisible(), copyStep.isVisible()); } else { assertEquals("The two values for property " + property.getDisplayName() + " in " + mmo.getClass().getName() + " should be equal", oldVal, copyVal); if (propertiesShareInstanceForDuplication.contains(property.getName())) return; /* * Ok, the duplicate object's property value compared equal. * Now we want to make sure if we modify that property on the original, * it won't affect the copy. */ Object newCopyVal = modifyObject(mmo, property, copyVal); assertFalse( "The two values are the same mutable object for property " + property.getDisplayName() + " was " + oldVal + " and " + copyVal, oldVal.equals(newCopyVal)); } } } } catch (NoSuchMethodException e) { System.out.println( "Skipping non-settable property " + property.getName() + " on " + mmo.getClass().getName()); } } }
From source file:com.cnd.greencube.server.dao.jdbc.JdbcDAO.java
@SuppressWarnings("rawtypes") private List<Column2Property> getColumnsFromObj(Object obj, String[] columns) { Class clazz = obj.getClass(); List<Column2Property> validColumns = new ArrayList<Column2Property>(); // ???/*from w w w . j a v a 2s. c om*/ for (Field field : clazz.getDeclaredFields()) { boolean skip = true; String columnName = null; Annotation[] annotations = field.getAnnotations(); for (Annotation a : annotations) { if (a instanceof Column) { columnName = ((Column) a).name(); if (columns != null && !_inarray_(columns, columnName)) skip = true; else { skip = false; } break; } } String s = field.getName(); PropertyDescriptor pd = null; if (!skip) { // ?gettersetter try { pd = new PropertyDescriptor(s, clazz); if (pd == null || pd.getWriteMethod() == null || pd.getReadMethod() == null) { skip = true; } } catch (Exception e) { skip = true; } } if (!skip) { Column2Property c = new Column2Property(); c.propertyName = pd.getName(); c.setterMethodName = pd.getWriteMethod().getName(); c.getterMethodName = pd.getReadMethod().getName(); c.columnName = columnName; validColumns.add(c); } } Column2Property c = new Column2Property(); c.propertyName = "id"; c.setterMethodName = "setId"; c.getterMethodName = "getId"; c.columnName = "C_ID"; validColumns.add(c); return validColumns; }
From source file:com.wavemaker.runtime.data.util.QueryHandler.java
private Object cloneObject(Object oldObj, Object newObj, Class cls) { PropertyDescriptor[] beanProps; try {//from ww w. j ava 2s. com beanProps = Introspector.getBeanInfo(cls).getPropertyDescriptors(); for (PropertyDescriptor propertyDescriptor : beanProps) { if (propertyDescriptor.getName().equals("class")) { continue; } Method getter = propertyDescriptor.getReadMethod(); Method setter = propertyDescriptor.getWriteMethod(); Object val = getter.invoke(oldObj); setter.invoke(newObj, val); } } catch (Exception ex) { throw new WMRuntimeException(ex); } return newObj; }
From source file:net.sourceforge.vulcan.web.struts.forms.PluginConfigForm.java
public void introspect(HttpServletRequest request) throws IntrospectionException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, InstantiationException { Class<?> cls = null;//from w ww.j a v a 2s .co m if ("pluginConfig".equals(focus)) { cls = pluginConfig.getClass(); this.breadCrumbs.clear(); this.breadCrumbs.add("Setup"); if (isProjectPlugin()) { this.breadCrumbs.add("Projects"); this.breadCrumbs.add(projectName); this.breadCrumbs.add(this.pluginConfig.getPluginName()); } else { this.breadCrumbs.add("Plugins"); this.breadCrumbs.add(this.pluginConfig.getPluginName()); } } else { cls = PropertyUtils.getPropertyType(this, focus); if (cls.isArray()) { cls = cls.getComponentType(); } } final String prefix = focus + "."; final PropertyDescriptor[] pds; if (PluginConfigDto.class.isAssignableFrom(cls)) { final PluginConfigDto pluginConfig = (PluginConfigDto) getFocusObject(); final List<PropertyDescriptor> tmp = pluginConfig.getPropertyDescriptors(request.getLocale()); pds = tmp.toArray(new PropertyDescriptor[tmp.size()]); if (pluginConfig instanceof PluginProfileDto) { ((PluginProfileDto) pluginConfig).checkPoint(); } } else { final BeanInfo beanInfo = Introspector.getBeanInfo(cls); Introspector.flushFromCaches(cls); pds = beanInfo.getPropertyDescriptors(); } if (isNested()) { for (PropertyDescriptor pd : propertyDescriptors) { if (focus.startsWith(pd.getName())) { breadCrumbs.add(pd.getDisplayName()); } } } types.clear(); choices.clear(); propertyDescriptors.clear(); hiddenPasswords.clear(); for (PropertyDescriptor pd : pds) { final String name = prefix + pd.getName(); final PropertyDescriptor cp = new PropertyDescriptor(pd.getName(), pd.getReadMethod(), pd.getWriteMethod()); cp.setShortDescription(pd.getShortDescription()); cp.setDisplayName(pd.getDisplayName()); cp.setName(name); propertyDescriptors.add(cp); types.put(name, getTypeAndPrepare(name, pd)); } putBreadCrumbsInRequest(request); }
From source file:org.kuali.kfs.module.cam.util.AssetSeparatePaymentDistributor.java
/** * Utility method which can compute the difference between source amount and consumed amounts, then will adjust the last amount * //w w w . j a v a 2 s. c o m * @param source Source payments * @param consumedList Consumed Payments */ private void applyBalanceToPaymentAmounts(AssetPayment source, List<AssetPayment> consumedList) { try { for (PropertyDescriptor propertyDescriptor : assetPaymentProperties) { Method readMethod = propertyDescriptor.getReadMethod(); if (readMethod != null && propertyDescriptor.getPropertyType() != null && KualiDecimal.class.isAssignableFrom(propertyDescriptor.getPropertyType())) { KualiDecimal amount = (KualiDecimal) readMethod.invoke(source); if (amount != null && amount.isNonZero()) { Method writeMethod = propertyDescriptor.getWriteMethod(); KualiDecimal consumedAmount = KualiDecimal.ZERO; KualiDecimal currAmt = KualiDecimal.ZERO; if (writeMethod != null) { for (int i = 0; i < consumedList.size(); i++) { currAmt = (KualiDecimal) readMethod.invoke(consumedList.get(i)); consumedAmount = consumedAmount.add(currAmt != null ? currAmt : KualiDecimal.ZERO); } } if (!consumedAmount.equals(amount)) { AssetPayment lastPayment = consumedList.get(consumedList.size() - 1); writeMethod.invoke(lastPayment, currAmt.add(amount.subtract(consumedAmount))); } } } } } catch (Exception e) { throw new RuntimeException(e); } }
From source file:ca.sqlpower.architect.swingui.TestPlayPen.java
/** * Checks that the properties of an instance from the copy constructor are equal to the original. * In the case of a mutable property, it also checks that they don't share the same instance. * // ww w . j a v a 2s .c o m * @throws Exception */ public void testCopyConstructor() throws Exception { List<PropertyDescriptor> settableProperties = Arrays .asList(PropertyUtils.getPropertyDescriptors(pp.getClass())); Set<String> copyIgnoreProperties = new HashSet<String>(); copyIgnoreProperties.add("UI"); copyIgnoreProperties.add("UIClassID"); copyIgnoreProperties.add("accessibleContext"); copyIgnoreProperties.add("actionMap"); copyIgnoreProperties.add("alignmentX"); copyIgnoreProperties.add("alignmentY"); copyIgnoreProperties.add("ancestorListeners"); copyIgnoreProperties.add("autoscrolls"); copyIgnoreProperties.add("border"); copyIgnoreProperties.add("class"); copyIgnoreProperties.add("component"); copyIgnoreProperties.add("componentPopupMenu"); copyIgnoreProperties.add("containerListeners"); copyIgnoreProperties.add("contentPane"); copyIgnoreProperties.add("cursorManager"); copyIgnoreProperties.add("debugGraphicsOptions"); copyIgnoreProperties.add("doubleBuffered"); copyIgnoreProperties.add("enabled"); copyIgnoreProperties.add("focusCycleRoot"); copyIgnoreProperties.add("focusTraversalKeys"); copyIgnoreProperties.add("focusTraversalPolicy"); copyIgnoreProperties.add("focusTraversalPolicyProvider"); copyIgnoreProperties.add("focusTraversalPolicySet"); copyIgnoreProperties.add("focusable"); copyIgnoreProperties.add("fontRenderContext"); copyIgnoreProperties.add("graphics"); copyIgnoreProperties.add("height"); copyIgnoreProperties.add("ignoreTreeSelection"); copyIgnoreProperties.add("inheritsPopupMenu"); copyIgnoreProperties.add("inputMap"); copyIgnoreProperties.add("inputVerifier"); copyIgnoreProperties.add("insets"); copyIgnoreProperties.add("layout"); copyIgnoreProperties.add("managingFocus"); copyIgnoreProperties.add("maximumSize"); copyIgnoreProperties.add("minimumSize"); copyIgnoreProperties.add("mouseMode"); copyIgnoreProperties.add("name"); copyIgnoreProperties.add("nextFocusableComponent"); copyIgnoreProperties.add("opaque"); copyIgnoreProperties.add("optimizedDrawingEnabled"); copyIgnoreProperties.add("paintingEnabled"); copyIgnoreProperties.add("paintingTile"); copyIgnoreProperties.add("panel"); copyIgnoreProperties.add("playPenContentPane"); copyIgnoreProperties.add("preferredScrollableViewportSize"); copyIgnoreProperties.add("preferredSize"); copyIgnoreProperties.add("registeredKeyStrokes"); copyIgnoreProperties.add("requestFocusEnabled"); copyIgnoreProperties.add("rootPane"); copyIgnoreProperties.add("scrollableTracksViewportHeight"); copyIgnoreProperties.add("scrollableTracksViewportWidth"); copyIgnoreProperties.add("selectedItems"); copyIgnoreProperties.add("selectedRelationShips"); copyIgnoreProperties.add("selectedTables"); copyIgnoreProperties.add("session"); copyIgnoreProperties.add("topLevelAncestor"); copyIgnoreProperties.add("toolTipText"); copyIgnoreProperties.add("transferHandler"); copyIgnoreProperties.add("usedArea"); copyIgnoreProperties.add("validateRoot"); copyIgnoreProperties.add("verifyInputWhenFocusTarget"); copyIgnoreProperties.add("vetoableChangeListeners"); copyIgnoreProperties.add("viewPosition"); copyIgnoreProperties.add("viewportSize"); copyIgnoreProperties.add("visible"); copyIgnoreProperties.add("visibleRect"); copyIgnoreProperties.add("width"); copyIgnoreProperties.add("x"); copyIgnoreProperties.add("y"); copyIgnoreProperties.add("draggingTablePanes"); copyIgnoreProperties.add("rubberBand"); // These should not be copied because any new PlayPen needs // different values or else it will not work on the new // PlayPen. copyIgnoreProperties.add("mouseZoomInAction"); copyIgnoreProperties.add("mouseZoomOutAction"); copyIgnoreProperties.add("scrollPane"); // we're not sure if zoom should be duplicated... // it might mess up printing?!?!? copyIgnoreProperties.add("zoom"); // individual lists (e.g. tables) checked instead copyIgnoreProperties.add("components"); // The copy of the play pen is for things like print preview, so we don't want to // duplicate menus and other interactive features. (?) copyIgnoreProperties.add("popupFactory"); //This property is specific to each play pen and it's settings will be re-calculated //regularly so it does not need to be copied. copyIgnoreProperties.add("criticismBucket"); // First pass: set all settable properties, because testing the duplication of // an object with all its properties at their defaults is not a // very convincing test of duplication! for (PropertyDescriptor property : settableProperties) { if (copyIgnoreProperties.contains(property.getName())) continue; Object oldVal; try { oldVal = PropertyUtils.getSimpleProperty(pp, property.getName()); // check for a setter if (property.getWriteMethod() != null) { Object newVal = getNewDifferentValue(property, oldVal); BeanUtils.copyProperty(pp, property.getName(), newVal); } } catch (NoSuchMethodException e) { logger.warn( "Skipping non-settable property " + property.getName() + " on " + pp.getClass().getName()); } } // Second pass get a copy make sure all of // the original mutable objects returned from getters are different // between the two objects, but have the same values. PlayPen duplicate = new PlayPen(pp.getSession(), pp); for (PropertyDescriptor property : settableProperties) { logger.info(property.getName() + property.getDisplayName() + property.getShortDescription()); if (copyIgnoreProperties.contains(property.getName())) continue; Object oldVal; try { oldVal = PropertyUtils.getSimpleProperty(pp, property.getName()); Object copyVal = PropertyUtils.getSimpleProperty(duplicate, property.getName()); if (oldVal == null) { throw new NullPointerException("We forgot to set " + property.getName()); } else { assertEquals("The two values for property " + property.getDisplayName() + " in " + pp.getClass().getName() + " should be equal", oldVal, copyVal); if (isPropertyInstanceMutable(property)) { assertNotSame("Copy shares mutable property with original, property name: " + property.getDisplayName(), copyVal, oldVal); } } } catch (NoSuchMethodException e) { logger.warn( "Skipping non-settable property " + property.getName() + " on " + pp.getClass().getName()); } } }
From source file:com.sun.faces.el.impl.BeanInfoManager.java
/** * Initializes by mapping property names to BeanInfoProperties *//*from w w w. j ava 2s .c om*/ void initialize() throws ElException { try { mBeanInfo = Introspector.getBeanInfo(mBeanClass); mPropertyByName = new HashMap(); mIndexedPropertyByName = new HashMap(); PropertyDescriptor[] pds = mBeanInfo.getPropertyDescriptors(); for (int i = 0; pds != null && i < pds.length; i++) { // Treat as both an indexed property and a normal property PropertyDescriptor pd = pds[i]; if (pd instanceof IndexedPropertyDescriptor) { IndexedPropertyDescriptor ipd = (IndexedPropertyDescriptor) pd; Method readMethod = getPublicMethod(ipd.getIndexedReadMethod()); Method writeMethod = getPublicMethod(ipd.getIndexedWriteMethod()); BeanInfoIndexedProperty property = new BeanInfoIndexedProperty(readMethod, writeMethod, ipd); mIndexedPropertyByName.put(ipd.getName(), property); } Method readMethod = getPublicMethod(pd.getReadMethod()); Method writeMethod = getPublicMethod(pd.getWriteMethod()); BeanInfoProperty property = new BeanInfoProperty(readMethod, writeMethod, pd); mPropertyByName.put(pd.getName(), property); } mEventSetByName = new HashMap(); EventSetDescriptor[] esds = mBeanInfo.getEventSetDescriptors(); for (int i = 0; esds != null && i < esds.length; i++) { EventSetDescriptor esd = esds[i]; mEventSetByName.put(esd.getName(), esd); } } catch (IntrospectionException exc) { if (log.isWarnEnabled()) { log.warn(MessageUtil.getMessageWithArgs(Constants.EXCEPTION_GETTING_BEANINFO, mBeanClass.getName()), exc); } } }
From source file:org.tinygroup.weblayer.webcontext.parser.util.BeanWrapperImpl.java
public boolean isWritableProperty(String propertyName) { try {/*from ww w . ja v a 2 s.c o m*/ PropertyDescriptor pd = getPropertyDescriptorInternal(propertyName); if (pd != null) { if (pd.getWriteMethod() != null) { return true; } } else { // Maybe an indexed/mapped property... getPropertyValue(propertyName); return true; } } catch (InvalidPropertyException ex) { // Cannot be evaluated, so can't be writable. } return false; }
From source file:org.etudes.ambrosia.impl.UiPropertyReference.java
/** * Write the value for FileItem (commons file upload) values. * //ww w .ja v a 2 s . com * @param entity * The entity to write to. * @param property * The property to set. * @param value * The value to write. */ protected void setFileValue(Object entity, String property, FileItem value) { // form a "setFoo()" based setter method name StringBuffer setter = new StringBuffer("set" + property); setter.setCharAt(3, setter.substring(3, 4).toUpperCase().charAt(0)); try { // use this form, providing the setter name and no getter, so we can support properties that are write-only PropertyDescriptor pd = new PropertyDescriptor(property, entity.getClass(), null, setter.toString()); Method write = pd.getWriteMethod(); Object[] params = new Object[1]; Class[] paramTypes = write.getParameterTypes(); if ((paramTypes != null) && (paramTypes.length == 1)) { // single value boolean if (paramTypes[0] != FileItem.class) { M_log.warn( "setFileValue: target not expecting FileItem: " + entity.getClass() + " " + property); return; } params[0] = value; write.invoke(entity, params); } else { M_log.warn("setFileValue: method: " + property + " object: " + entity.getClass() + " : no one parameter setter method defined"); } } catch (IntrospectionException ie) { M_log.warn("setFileValue: method: " + property + " object: " + entity.getClass() + " : " + ie); } catch (IllegalAccessException ie) { M_log.warn("setFileValue: method: " + property + " object: " + entity.getClass() + " :" + ie); } catch (IllegalArgumentException ie) { M_log.warn("setFileValue: method: " + property + " object: " + entity.getClass() + " :" + ie); } catch (InvocationTargetException ie) { M_log.warn("setFileValue: method: " + property + " object: " + entity.getClass() + " :" + ie); } }