ObjectPropertyUtils { public static void setPropertyValue(Object object, String propertyPath, Object propertyValue) { if (ProcessLogger.isTraceActive() && object != null) { ProcessLogger.countBegin("bean-property-write"); } try { ObjectPropertyReference.setWarning(true); ObjectPropertyReference.resolvePath(object, object.getClass(), propertyPath, true).set(propertyValue); } catch (RuntimeException e) { throw new IllegalArgumentException( "Error setting property '" + propertyPath + "' on " + object + " with " + propertyValue, e); } finally { ObjectPropertyReference.setWarning(false); if (ProcessLogger.isTraceActive() && object != null) { ProcessLogger.countEnd("bean-property-write", object.getClass().getSimpleName() + ":" + propertyPath); } } } private  ObjectPropertyUtils(); static Map<String, PropertyDescriptor> getPropertyDescriptors(Class<?> beanClass); static PropertyDescriptor getPropertyDescriptor(Class<?> beanClass, String propertyName); static void registerPropertyEditors(PropertyEditorRegistry registry); static Set<String> getReadablePropertyNames(Class<?> beanClass); static Method getReadMethod(Class<?> beanClass, String propertyName); static Method getWriteMethod(Class<?> beanClass, String propertyName); static void copyPropertiesToObject(Map<String, String> properties, Object object); static Class<?> getPropertyType(Class<?> beanClass, String propertyPath); static Class<?> getPropertyType(Object object, String propertyPath); static Set<String> getReadablePropertyNamesByType(Object bean, Class<?> propertyType); static Set<String> getReadablePropertyNamesByType(Class<?> beanClass, Class<?> propertyType); static Set<String> getReadablePropertyNamesByAnnotationType(Object bean,
            Class<? extends Annotation> annotationType); static Set<String> getReadablePropertyNamesByAnnotationType(
            Class<?> beanClass, Class<? extends Annotation> annotationType); static Set<String> getReadablePropertyNamesByCollectionType(Object bean, Class<?> collectionType); static Set<String> getWritablePropertyNames(Object bean); static Set<String> getReadablePropertyNamesByCollectionType(Class<?> beanClass, Class<?> collectionType); @SuppressWarnings("unchecked") static T getPropertyValue(Object object, String propertyPath); static String getPropertyValueAsText(Object bean, String path); static PropertyEditorRegistry getPropertyEditorRegistry(); static Class<?> getPrimitiveType(Class<?> type); static PropertyEditor getPropertyEditor(Object bean, String path); static PropertyEditor getPropertyEditor(Class<?> propertyType); static void initializeProperty(Object object, String propertyPath); static void setPropertyValue(Object object, String propertyPath, Object propertyValue); static void setPropertyValue(Object object, String propertyPath, Object propertyValue, boolean ignoreUnknown); static boolean isReadableProperty(Object object, String propertyPath); static boolean isWritableProperty(Object object, String propertyPath); static List<Field> getAllFields(List<Field> fields, Class<?> type, Class<?> stopAt); static Type getComponentType(Type type); static Class<?> getUpperBound(Type valueType); static Type findGenericType(Class<?> sourceClass, Class<?> targetClass); static String[] splitPropertyPath(String path); static String getPathTail(String path); static String removePathTail(String path); static String getCanonicalPath(String path);  }
@Test public void testSetBoolean() { TestBean tb = new TestBean(); ObjectPropertyUtils.setPropertyValue(tb, "booleanProp", "true"); assertTrue(tb.isBooleanProp()); }
@Test public void testSet() throws Throwable { TestBean tb = new TestBean(); ObjectPropertyUtils.setPropertyValue(tb, "rwProp", "foobar"); assertEquals("foobar", tb.getRwProp()); ObjectPropertyUtils.setPropertyValue(tb, "woProp", "barbaz"); assertEquals("barbaz", tb.woProp); try { ObjectPropertyUtils.setPropertyValue(tb, "roProp", "bazfoo"); fail("expected exception"); } catch (Exception E) { } long now = System.currentTimeMillis(); ObjectPropertyUtils.setPropertyValue(tb, "dateProp", new java.sql.Date(now)); assertEquals(now, tb.getDateProp().getTime()); String dateStr = "01/03/2013"; ObjectPropertyUtils.setPropertyValue(tb, "dateProp", dateStr); Date expectedDate = new SimpleDateFormat("MM/dd/yy").parse(dateStr); assertEquals(expectedDate, tb.getDateProp()); }
@Test public void testPathSet() { TestBean tb = new TestBean(); ObjectPropertyUtils.setPropertyValue(tb, "rwProp", "bar"); assertEquals("bar", tb.getRwProp()); ObjectPropertyUtils.setPropertyValue(tb, "next", new TestBean()); ObjectPropertyUtils.setPropertyValue(tb, "next.next", new TestBean()); ObjectPropertyUtils.setPropertyValue(tb, "next.next.woProp", "baz"); assertEquals("baz", tb.getNext().getNext().woProp); }
@Test public void testKradUifCollectionGroupBuilder() throws Throwable { UifUnitTestUtils.establishMockConfig(ObjectPropertyUtilsTest.class.getSimpleName()); UifUnitTestUtils.establishMockUserSession("testuser"); try { final CollectionGroupBuilder collectionGroupBuilder = new CollectionGroupBuilder(); final CollectionTestForm form = new CollectionTestForm(); CollectionTestItem item = new CollectionTestItem(); item.setFoobar("barfoo"); ObjectPropertyUtils.setPropertyValue(form, "foo.baz['foo_bar_'badquotes'_.foobar']", item); assertEquals("barfoo", form.foo.baz.get("foo_bar_'badquotes'_.foobar").foobar); final FormView view = new FormView(); view.setFormClass(CollectionTestForm.class); view.setViewHelperService(new ViewHelperServiceImpl()); view.setPresentationController(new ViewPresentationControllerBase()); view.setAuthorizer(UifUnitTestUtils.getAllowMostViewAuthorizer()); final CollectionGroup collectionGroup = new CollectionGroupBase(); collectionGroup.setCollectionObjectClass(CollectionTestItem.class); collectionGroup.setAddLinePropertyName("addLineFoo"); StackedLayoutManager layoutManager = new StackedLayoutManagerBase(); Group lineGroupPrototype = new GroupBase(); layoutManager.setLineGroupPrototype(lineGroupPrototype); collectionGroup.setLayoutManager(layoutManager); BindingInfo addLineBindingInfo = new BindingInfo(); addLineBindingInfo.setBindingPath("foo.baz['foo_bar_'badquotes'_.foobar']"); collectionGroup.setAddLineBindingInfo(addLineBindingInfo); BindingInfo collectionBindingInfo = new BindingInfo(); collectionBindingInfo.setBindingPath("foo.bar"); collectionGroup.setBindingInfo(collectionBindingInfo); ViewLifecycle.encapsulateLifecycle(view, form, null, new Runnable() { @Override public void run() { collectionGroupBuilder.build(view, form, (CollectionGroup) CopyUtils.copy(collectionGroup)); } }); } finally { GlobalVariables.setUserSession(null); GlobalVariables.clear(); GlobalResourceLoader.stop(); } }
@Test public void testSetStringMapFromInt() { Action action = new Action(); ObjectPropertyUtils.setPropertyValue(action, "actionParameters['lineIndex']", 34); assertEquals("34", action.getActionParameter("lineIndex")); }