List of usage examples for org.apache.commons.beanutils PropertyUtilsBean getPropertyType
public Class getPropertyType(Object bean, String name) throws IllegalAccessException, InvocationTargetException, NoSuchMethodException
null if there is no such property for the specified bean. From source file:com.xpfriend.fixture.cast.temp.PojoUtil.java
public static Class<?> getPropertyType(Object object, String propertyName, Table table, Row row) { try {/*from w w w . j a va 2 s . c om*/ String name = find(propertyName, object); PropertyUtilsBean propertyUtils = TypeConverter.getBeanUtils().getPropertyUtils(); if (object instanceof DynaBean) { return ((DynaBean) object).getDynaClass().getDynaProperty(name).getType(); } Class<?> type = propertyUtils.getPropertyType(object, name); if (type == null) { throw createNoSuchPropertyException(object, name, table, row); } return type; } catch (NoSuchMethodException e) { throw createNoSuchPropertyException(object, propertyName, table, row); } catch (InvocationTargetException e) { throw new ConfigException(e.getCause()); } catch (IllegalAccessException e) { throw new ConfigException(e); } }
From source file:com.datatorrent.stram.moduleexperiment.InjectConfigTest.java
@Test public void testBeanUtils() throws Exception { // http://www.cowtowncoder.com/blog/archives/2011/02/entry_440.html BeanUtilsTestBean testBean = new BeanUtilsTestBean(); testBean.url = new URL("http://localhost:12345/context"); ObjectMapper mapper = new ObjectMapper(); Map<String, Object> properties = mapper.convertValue(testBean, Map.class); System.out.println("testBean source: " + properties); BeanUtilsTestBean testBean2 = new BeanUtilsTestBean(); testBean2.string2 = "testBean2"; Assert.assertFalse("contains transientProperty", properties.containsKey("transientProperty")); Assert.assertTrue("contains string2", properties.containsKey("string2")); properties.remove("string2"); // remove null //properties.put("string3", ""); BeanUtilsBean bub = new BeanUtilsBean(); try {/*from w w w . ja va 2 s . c om*/ bub.getProperty(testBean, "invalidProperty"); Assert.fail("exception expected"); } catch (Exception e) { Assert.assertTrue(e.getMessage().contains("Unknown property 'invalidProperty'")); } bub.setProperty(properties, "mapProperty.someKey", "someValue"); JsonNode sourceTree = mapper.convertValue(testBean2, JsonNode.class); JsonNode updateTree = mapper.convertValue(properties, JsonNode.class); merge(sourceTree, updateTree); // mapper.readerForUpdating(testBean2).readValue(sourceTree); // Assert.assertEquals("preserve existing value", "testBean2", testBean2.string2); // Assert.assertEquals("map property", "someValue", testBean2.mapProperty.get("someKey")); // System.out.println("testBean cloned: " + mapper.convertValue(testBean2, Map.class)); PropertyUtilsBean propertyUtilsBean = BeanUtilsBean.getInstance().getPropertyUtils(); //PropertyDescriptor pd = propertyUtilsBean.getPropertyDescriptor(testBean2, "mapProperty.someKey2"); // set value on non-existing property try { propertyUtilsBean.setProperty(testBean, "nonExistingProperty.someProperty", "ddd"); Assert.fail("should throw exception"); } catch (NoSuchMethodException e) { Assert.assertTrue("" + e, e.getMessage().contains("Unknown property 'nonExistingProperty'")); } // set value on read-only property try { testBean.getMapProperty().put("s", "s1Val"); PropertyDescriptor pd = propertyUtilsBean.getPropertyDescriptor(testBean, "mapProperty"); Class<?> type = propertyUtilsBean.getPropertyType(testBean, "mapProperty.s"); propertyUtilsBean.setProperty(testBean, "mapProperty", Integer.valueOf(1)); Assert.fail("should throw exception"); } catch (Exception e) { Assert.assertTrue("" + e, e.getMessage().contains("Property 'mapProperty' has no setter method")); } // type mismatch try { propertyUtilsBean.setProperty(testBean, "intProp", "s1"); Assert.fail("should throw exception"); } catch (Exception e) { Assert.assertEquals(e.getClass(), IllegalArgumentException.class); } try { propertyUtilsBean.setProperty(testBean, "intProp", "1"); } catch (IllegalArgumentException e) { // BeanUtils does not report invalid properties, but it handles type conversion, which above doesn't Assert.assertEquals("", 0, testBean.getIntProp()); bub.setProperty(testBean, "intProp", "1"); // by default beanutils ignores conversion error Assert.assertEquals("", 1, testBean.getIntProp()); } }
From source file:org.opencms.workplace.CmsWidgetDialogParameter.java
/** * "Commits" (writes) the value of this widget back to the underlying base object.<p> * /*from w w w.j a v a 2 s . c o m*/ * @param dialog the widget dialog where the parameter is used on * * @throws CmsException in case the String value of the widget is invalid for the base Object */ @SuppressWarnings("unchecked") public void commitValue(CmsWidgetDialog dialog) throws CmsException { if (m_baseCollection == null) { PropertyUtilsBean bean = new PropertyUtilsBean(); ConvertUtilsBean converter = new ConvertUtilsBean(); Object value = null; try { Class<?> type = bean.getPropertyType(m_baseObject, m_baseObjectProperty); value = converter.convert(m_value, type); bean.setNestedProperty(m_baseObject, m_baseObjectProperty, value); setError(null); } catch (InvocationTargetException e) { setError(e.getTargetException()); throw new CmsWidgetException(Messages.get().container(Messages.ERR_PROPERTY_WRITE_3, value, dialog.keyDefault(A_CmsWidget.getLabelKey(this), getKey()), m_baseObject.getClass().getName()), e.getTargetException(), this); } catch (Exception e) { setError(e); throw new CmsWidgetException(Messages.get().container(Messages.ERR_PROPERTY_WRITE_3, value, dialog.keyDefault(A_CmsWidget.getLabelKey(this), getKey()), m_baseObject.getClass().getName()), e, this); } } else if (m_baseCollection instanceof SortedMap) { if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_value)) { int pos = m_value.indexOf('='); if ((pos > 0) && (pos < (m_value.length() - 1))) { String key = m_value.substring(0, pos); String value = m_value.substring(pos + 1); @SuppressWarnings("rawtypes") SortedMap map = (SortedMap) m_baseCollection; if (map.containsKey(key)) { Object val = map.get(key); CmsWidgetException error = new CmsWidgetException( Messages.get().container(Messages.ERR_MAP_DUPLICATE_KEY_3, dialog.keyDefault(A_CmsWidget.getLabelKey(this), getKey()), key, val), this); setError(error); throw error; } map.put(key, value); } else { CmsWidgetException error = new CmsWidgetException( Messages.get().container(Messages.ERR_MAP_PARAMETER_FORM_1, dialog.keyDefault(A_CmsWidget.getLabelKey(this), getKey())), this); setError(error); throw error; } } } else if (m_baseCollection instanceof List) { if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(m_value)) { @SuppressWarnings("rawtypes") List list = (List) m_baseCollection; list.add(m_value); } } }
From source file:org.red5.io.amf.Input.java
protected Class getPropertyType(Object instance, String propertyName) { try {/*from ww w .j a v a 2s . c om*/ if (instance != null) { Field field = instance.getClass().getField(propertyName); return field.getType(); } else { // instance is null for anonymous class, use default type } } catch (NoSuchFieldException e1) { try { BeanUtilsBean beanUtilsBean = BeanUtilsBean.getInstance(); PropertyUtilsBean propertyUtils = beanUtilsBean.getPropertyUtils(); return propertyUtils.getPropertyType(instance, propertyName); } catch (Exception e2) { // nothing } } catch (Exception e) { // ignore other exceptions } // return Object class type by default return Object.class; }