List of usage examples for org.apache.commons.beanutils PropertyUtils getSimpleProperty
public static Object getSimpleProperty(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
Return the value of the specified simple property of the specified bean, with no type conversions.
For more details see PropertyUtilsBean
.
From source file:net.sourceforge.fenixedu.presentationTier.Action.manager.personManagement.MergePersonsDA.java
public ActionForward chooseObjects(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response, String leftOid, String rightOid, String currentClass) throws FenixServiceException, IllegalAccessException, NoSuchMethodException, ClassNotFoundException { DomainObject domainObject1 = FenixFramework.getDomainObject(leftOid); DomainObject domainObject2 = FenixFramework.getDomainObject(rightOid); if (domainObject1 == null || domainObject2 == null) { request.setAttribute("mergePersonsBean", new MergePersonsBean()); return mapping.findForward("chooseObjectsToMerge"); }/* w ww . j a va 2 s.com*/ DomainClass domainClass = FenixFramework.getDomainModel().findClass(currentClass); List<MergeSlotDTO> results = new ArrayList<MergeSlotDTO>(); DomainClass domainClassOrSuperClass = domainClass; while (domainClassOrSuperClass.getSuperclass() != null) { for (Iterator<Slot> iter = domainClassOrSuperClass.getSlots(); iter.hasNext();) { Slot slot = iter.next(); String slotName = slot.getName(); Object objProperty1 = null; Object objProperty2 = null; try { objProperty1 = PropertyUtils.getSimpleProperty(domainObject1, slotName); objProperty2 = PropertyUtils.getSimpleProperty(domainObject2, slotName); } catch (InvocationTargetException e) { continue; } String property1 = (objProperty1 == null) ? "" : objProperty1.toString(); String property2 = (objProperty2 == null) ? "" : objProperty2.toString(); if (property1.equals(property2)) { continue; } results.add(new MergeSlotDTO(slotName, "Simple Primitive", property1, property2)); } domainClassOrSuperClass = (DomainClass) domainClassOrSuperClass.getSuperclass(); } domainClassOrSuperClass = domainClass; while (domainClassOrSuperClass.getSuperclass() != null) { for (Iterator<Role> iter = domainClassOrSuperClass.getRoleSlots(); iter.hasNext();) { Role roleSlot = iter.next(); String slotName = roleSlot.getName(); MergeSlotDTO mergeSlot = new MergeSlotDTO(slotName); Object property1 = null; Object property2 = null; try { property1 = PropertyUtils.getSimpleProperty(domainObject1, slotName); property2 = PropertyUtils.getSimpleProperty(domainObject2, slotName); } catch (InvocationTargetException e) { continue; } if (roleSlot.getMultiplicityUpper() == 1) { if ((property1 == null && property2 == null) || (property1 != null && property2 != null && property1.equals(property2))) { continue; } mergeSlot.setType("Reference"); fillDtoWithSimpleProperty(roleSlot, mergeSlot, property1, MergeSlotDTO.VALUE1); fillDtoWithSimpleProperty(roleSlot, mergeSlot, property2, MergeSlotDTO.VALUE2); } else { if (((Collection) property1).size() == 0 && ((Collection) property2).size() == 0) { continue; } // collection mergeSlot.setType("Collection"); fillDtoWithCollectionProperty(mergeSlot, property1, MergeSlotDTO.VALUE1, domainObject1.getExternalId(), Person.class.getName()); fillDtoWithCollectionProperty(mergeSlot, property2, MergeSlotDTO.VALUE2, domainObject2.getExternalId(), Person.class.getName()); } results.add(mergeSlot); } domainClassOrSuperClass = (DomainClass) domainClassOrSuperClass.getSuperclass(); } request.setAttribute("slots", results); request.setAttribute("classToMerge", currentClass); return mapping.findForward("displayObjects"); }
From source file:com.bstek.dorado.view.config.definition.ViewConfigDefinition.java
protected void injectResourceString(ViewConfig viewConfig, String key, String resourceString) throws Exception { Object object = viewConfig;//from ww w. j a v a 2 s . co m ResourceInjection resourceInjection = object.getClass().getAnnotation(ResourceInjection.class); String[] sections = StringUtils.split(key, "."); int len = sections.length; for (int i = 0; i < len; i++) { String section = sections[i]; boolean isObject = section.charAt(0) == '#'; if (isObject) { section = section.substring(1); } if (i == 0 && section.equals("view")) { object = viewConfig.getView(); } else if (isObject) { if (object == viewConfig) { object = viewConfig.getDataType(section); if (object == null && viewConfig.getView() != null) { object = viewConfig.getView().getViewElement(section); } } else { if (resourceInjection == null) { throwInvalidResourceKey(key); } String methodName = resourceInjection.subObjectMethod(); if (StringUtils.isEmpty(methodName)) { throwInvalidResourceKey(key); } object = MethodUtils.invokeExactMethod(object, methodName, new String[] { section }); } if (object == null) { break; } resourceInjection = object.getClass().getAnnotation(ResourceInjection.class); if (i == len - 1) { String[] defaultProperties; if (resourceInjection == null) { defaultProperties = DEFAULT_PROPERTIES; } else { defaultProperties = resourceInjection.defaultProperty(); } boolean found = false; for (String property : defaultProperties) { if (PropertyUtils.isWriteable(object, property)) { if (PropertyUtils.getSimpleProperty(object, property) == null) { PropertyUtils.setSimpleProperty(object, property, resourceString); } found = true; break; } } if (!found) { throwInvalidResourceKey(key); } } } else { if (i == len - 1) { if (PropertyUtils.getSimpleProperty(object, section) == null) { PropertyUtils.setSimpleProperty(object, section, resourceString); } } else { object = PropertyUtils.getSimpleProperty(object, section); } } } }
From source file:com.sun.faces.mock.MockResultSet.java
public Object getObject(String columnName) throws SQLException { if ((row <= 0) || (row > beans.length)) { throw new SQLException("Invalid row number " + row); }//from w w w . ja v a 2s . co m try { if (columnName.equals("writeOnlyProperty") && (beans[row - 1] instanceof TestBean)) { return (((TestBean) beans[row - 1]).getWriteOnlyPropertyValue()); } else { return (PropertyUtils.getSimpleProperty(beans[row - 1], columnName)); } } catch (Exception e) { throw new SQLException(e.getMessage()); } }
From source file:com.fiveamsolutions.nci.commons.search.OneCriterionSpecifiedCallback.java
private Object getSimpleProperty(Object bean, String name) { Object propertyValue = null;/* w w w . jav a2 s .c o m*/ try { propertyValue = PropertyUtils.getSimpleProperty(bean, name); } catch (Exception e) { throw new IllegalArgumentException( "Unable to process property with name:" + name + " " + bean.getClass(), e); } return propertyValue; }
From source file:ca.sqlpower.matchmaker.MatchMakerTestCase.java
public void testDuplicate() throws Exception { MatchMakerObject mmo = getTarget();/*from w w w . j a v a 2 s . c om*/ 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:gov.nih.nci.caarray.application.permissions.PermissionsManagementServiceBean.java
private void convertToLikeProperty(User u, User newUser, PropertyDescriptor pd) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException { if (pd.getPropertyType().equals(String.class)) { final String value = (String) PropertyUtils.getSimpleProperty(newUser, pd.getName()); if (value != null) { PropertyUtils.setSimpleProperty(newUser, pd.getName(), value + "%"); }/*from ww w . j av a 2 s . com*/ } }
From source file:com.jgeppert.struts2.jquery.chart.components.ChartData.java
@SuppressWarnings("rawtypes") public void evaluateExtraParams() { super.evaluateExtraParams(); if (color != null) addParameter("color", findString(color)); if (label != null) addParameter("label", findString(label)); if (lines != null) addParameter("lines", findString(lines)); if (bars != null) addParameter("bars", findString(bars)); if (points != null) addParameter("points", findString(points)); if (xaxis != null) addParameter("xaxis", findValue(xaxis, Integer.class)); if (yaxis != null) addParameter("yaxis", findValue(yaxis, Integer.class)); if (clickable != null) addParameter("clickable", findValue(this.clickable, Boolean.class)); if (hoverable != null) addParameter("hoverable", findValue(this.hoverable, Boolean.class)); if (shadowSize != null) addParameter("shadowSize", findValue(shadowSize, Integer.class)); if (fillBetween != null) addParameter("fillBetween", findString(fillBetween)); if (curvedLines != null) addParameter("curvedLines", findValue(this.curvedLines, Boolean.class)); if (curvedLinesFit != null) addParameter("curvedLinesFit", findValue(this.curvedLinesFit, Boolean.class)); if (curvedLinesFill != null) addParameter("curvedLinesFill", findValue(this.curvedLinesFill, Boolean.class)); if (curvedLinesFillColor != null) addParameter("curvedLinesFillColor", findString(curvedLinesFillColor)); if (curvedLinesLineWidth != null) addParameter("curvedLinesLineWidth", findValue(curvedLinesLineWidth, Integer.class)); if (stack != null) addParameter("stack", findString(stack)); if ((this.id == null || this.id.length() == 0)) { // resolves Math.abs(Integer.MIN_VALUE) issue reported by FindBugs // http://findbugs.sourceforge.net/bugDescriptions.html#RV_ABSOLUTE_VALUE_OF_RANDOM_INT int nextInt = RANDOM.nextInt(); nextInt = nextInt == Integer.MIN_VALUE ? Integer.MAX_VALUE : Math.abs(nextInt); this.id = "chartdata" + String.valueOf(nextInt); addParameter("id", this.id); }/*from w w w . j a va2s.c o m*/ Chart chart = (Chart) findAncestor(Chart.class); if (chart != null) { addParameter("chart", chart.getId()); } if (this.href != null && !this.href.equals("#")) { if (list != null) { addParameter("remoteList", findString(list.toString())); } if (listKey != null) { addParameter("remoteListKey", findString(listKey)); } if (listValue != null) { addParameter("remoteListValue", findString(listValue)); } } else if (data != null) { addParameter("data", findString(data)); } else { if (list == null) { list = parameters.get("list"); } Object listObject = findValue(list.toString()); if (listObject instanceof String) { addParameter("data", listObject); } else if (listObject instanceof Map) { Map map = (Map) listObject; Set keySet = map.keySet(); StringBuffer data = new StringBuffer(); data.append("["); boolean setComma = false; for (Object key : keySet) { if (setComma) { data.append(","); } if (!setComma) { setComma = true; } data.append("["); if (key instanceof Date) { data.append(((Date) key).getTime()); } else { data.append(key.toString()); } data.append(","); data.append(map.get(key)); data.append("]"); } data.append("]"); addParameter("data", data.toString()); } else { Iterator iterator = null; if (listObject instanceof Collection) { iterator = ((Collection) listObject).iterator(); } else { iterator = MakeIterator.convert(listObject); } if (iterator != null) { StringBuffer data = new StringBuffer(); data.append("["); Object item = iterator.next(); boolean iterat = true; int count = 0; while (iterat) { count++; if (item == null) { data.append("null"); } else { if (item instanceof Point) { data.append("["); Point point = (Point) item; data.append(point.getX()); data.append(","); data.append(point.getY()); data.append("]"); } else { data.append("["); if (listKey != null) { String key = findString(listKey); Object itemKey = null; try { itemKey = PropertyUtils.getSimpleProperty(item, key); } catch (IllegalAccessException e) { LOG.warn("Cannot read listKey", e); } catch (InvocationTargetException e) { LOG.warn("Cannot read listKey", e); } catch (NoSuchMethodException e) { LOG.warn("Cannot read listKey", e); } if (itemKey != null) { if (itemKey instanceof Date) { data.append(((Date) itemKey).getTime()); } else { data.append(itemKey.toString()); } } else { data.append(count); } } else { data.append(count); } data.append(","); if (listValue != null) { String value = findString(listValue); Object itemValue = null; try { itemValue = PropertyUtils.getSimpleProperty(item, value); } catch (IllegalAccessException e) { LOG.warn("Cannot read listValue", e); } catch (InvocationTargetException e) { LOG.warn("Cannot read listValue", e); } catch (NoSuchMethodException e) { LOG.warn("Cannot read listValue", e); } if (itemValue != null) { if (itemValue instanceof Date) { data.append(((Date) itemValue).getTime()); } else { data.append(itemValue.toString()); } } else { data.append(item.toString()); } } else { data.append(item.toString()); } data.append("]"); } } if (iterator.hasNext()) { data.append(","); item = iterator.next(); } else { iterat = false; } } data.append("]"); addParameter("data", data.toString()); } } } }
From source file:com.myjeeva.poi.ExcelWorkSheetHandler.java
private String getPropertyValue(Object targetObj, String propertyName) { String value = ""; if (null == targetObj || StringUtils.isBlank(propertyName)) { LOG.error("targetObj or propertyName is null, both require to retrieve a value"); return value; }//from w w w. ja va2 s . c o m try { if (PropertyUtils.isReadable(targetObj, propertyName)) { Object v = PropertyUtils.getSimpleProperty(targetObj, propertyName); if (null != v && StringUtils.isNotBlank(v.toString())) { value = v.toString(); } } else { LOG.error("Given property (" + propertyName + ") is not readable!"); } } catch (IllegalAccessException iae) { LOG.error(iae.getMessage()); } catch (InvocationTargetException ite) { LOG.error(ite.getMessage()); } catch (NoSuchMethodException nsme) { LOG.error(nsme.getMessage()); } return value; }
From source file:ca.sqlpower.wabit.AbstractWabitObjectTest.java
/** * Uses reflection to find all the settable properties of the object under test, * and fails if any of them can be set without an event happening. *//*from w ww . j a v a 2 s . c o m*/ public void testSettingPropertiesFiresEvents() throws Exception { CountingWabitListener listener = new CountingWabitListener(); SPObject wo = getObjectUnderTest(); wo.addSPListener(listener); List<PropertyDescriptor> settableProperties; settableProperties = Arrays.asList(PropertyUtils.getPropertyDescriptors(wo.getClass())); Set<String> propertiesToIgnoreForEvents = getPropertiesToIgnoreForEvents(); for (PropertyDescriptor property : settableProperties) { Object oldVal; if (propertiesToIgnoreForEvents.contains(property.getName())) continue; try { oldVal = PropertyUtils.getSimpleProperty(wo, property.getName()); // check for a setter if (property.getWriteMethod() == null) continue; } catch (NoSuchMethodException e) { logger.debug( "Skipping non-settable property " + property.getName() + " on " + wo.getClass().getName()); continue; } int oldChangeCount = listener.getPropertyChangeCount(); Object newVal = valueMaker.makeNewValue(property.getPropertyType(), oldVal, property.getName()); try { logger.debug("Setting property '" + property.getName() + "' to '" + newVal + "' (" + newVal.getClass().getName() + ")"); BeanUtils.copyProperty(wo, property.getName(), newVal); // some setters fire multiple events (they change more than one property) assertTrue( "Event for set " + property.getName() + " on " + wo.getClass().getName() + " didn't fire!", listener.getPropertyChangeCount() > oldChangeCount); if (listener.getPropertyChangeCount() == oldChangeCount + 1) { assertEquals("Property name mismatch for " + property.getName() + " in " + wo.getClass(), property.getName(), listener.getLastPropertyEvent().getPropertyName()); assertEquals("New value for " + property.getName() + " was wrong", newVal, listener.getLastPropertyEvent().getNewValue()); } } catch (InvocationTargetException e) { logger.debug("(non-fatal) Failed to write property '" + property.getName() + " to type " + wo.getClass().getName()); } } }
From source file:com.bstek.dorado.data.config.definition.DataTypeDefinition.java
protected void injectResourceString(EntityDataType dataType, String key, String resourceString) throws Exception { Object object = dataType;//from www.j a va2 s .c o m ResourceInjection resourceInjection = object.getClass().getAnnotation(ResourceInjection.class); String[] sections = StringUtils.split(key, "."); int len = sections.length; for (int i = 0; i < len; i++) { String section = sections[i]; boolean isObject = section.charAt(0) == '#'; if (isObject) { section = section.substring(1); if (resourceInjection == null) { throwInvalidResourceKey(key); } String methodName = resourceInjection.subObjectMethod(); if (StringUtils.isEmpty(methodName)) { throwInvalidResourceKey(key); } object = MethodUtils.invokeExactMethod(object, methodName, new String[] { section }); if (object == null) { break; } resourceInjection = object.getClass().getAnnotation(ResourceInjection.class); if (i == len - 1) { String[] defaultProperties; if (resourceInjection == null) { defaultProperties = DEFAULT_PROPERTIES; } else { defaultProperties = resourceInjection.defaultProperty(); } boolean found = false; for (String property : defaultProperties) { if (PropertyUtils.isWriteable(object, property)) { // if (PropertyUtils.getSimpleProperty(object, // property) == null) { PropertyUtils.setSimpleProperty(object, property, resourceString); // } found = true; break; } } if (!found) { throwInvalidResourceKey(key); } } } else { if (i == len - 1) { if (PropertyUtils.getSimpleProperty(object, section) == null) { PropertyUtils.setSimpleProperty(object, section, resourceString); } } else { object = PropertyUtils.getSimpleProperty(object, section); } } } }