Package org.springframework.beans

Examples of org.springframework.beans.BeanWrapper


    ApplicationEventMulticaster multicaster = getApplicationEventMulticaster();
    if(editor instanceof ApplicationListener &&  multicaster != null){
      multicaster.addApplicationListener((ApplicationListener)editor);
    }
    if(editorProperties != null){
      BeanWrapper wrapper = new BeanWrapperImpl(editor);
      wrapper.setPropertyValues(editorProperties);
    }
    if(editor instanceof InitializingBean){
       try {
         ((InitializingBean)editor).afterPropertiesSet();
           }
View Full Code Here


    tb1.setNestedIndexedBean(new IndexedTestBean());
    tb2.setNestedIndexedBean(new IndexedTestBean());
    tb3.setNestedIndexedBean(new IndexedTestBean());
    tb4.setNestedIndexedBean(new IndexedTestBean());
    tb5.setNestedIndexedBean(new IndexedTestBean());
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.registerCustomEditor(String.class, "array.nestedIndexedBean.array.name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("array" + text);
      }

      public String getAsText() {
        return ((String) getValue()).substring(5);
      }
    });
    bw.registerCustomEditor(String.class, "list.nestedIndexedBean.list.name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("list" + text);
      }

      public String getAsText() {
        return ((String) getValue()).substring(4);
      }
    });
    bw.registerCustomEditor(String.class, "map.nestedIndexedBean.map.name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("map" + text);
      }

      public String getAsText() {
        return ((String) getValue()).substring(4);
      }
    });
    assertEquals("name0", tb0.getName());
    assertEquals("name1", tb1.getName());
    assertEquals("name2", tb2.getName());
    assertEquals("name3", tb3.getName());
    assertEquals("name4", tb4.getName());
    assertEquals("name5", tb5.getName());
    assertEquals("name0", bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name"));
    assertEquals("name1", bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name"));
    assertEquals("name2", bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name"));
    assertEquals("name3", bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name"));
    assertEquals("name4", bw.getPropertyValue("map[key1].nestedIndexedBean.map[key1].name"));
    assertEquals("name5", bw.getPropertyValue("map['key2'].nestedIndexedBean.map[\"key2\"].name"));

    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue("array[0].nestedIndexedBean.array[0].name", "name5");
    pvs.addPropertyValue("array[1].nestedIndexedBean.array[1].name", "name4");
    pvs.addPropertyValue("list[0].nestedIndexedBean.list[0].name", "name3");
    pvs.addPropertyValue("list[1].nestedIndexedBean.list[1].name", "name2");
    pvs.addPropertyValue("map[key1].nestedIndexedBean.map[\"key1\"].name", "name1");
    pvs.addPropertyValue("map['key2'].nestedIndexedBean.map[key2].name", "name0");
    bw.setPropertyValues(pvs);
    assertEquals("arrayname5", tb0.getNestedIndexedBean().getArray()[0].getName());
    assertEquals("arrayname4", tb1.getNestedIndexedBean().getArray()[1].getName());
    assertEquals("listname3", ((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName());
    assertEquals("listname2", ((TestBean) tb3.getNestedIndexedBean().getList().get(1)).getName());
    assertEquals("mapname1", ((TestBean) tb4.getNestedIndexedBean().getMap().get("key1")).getName());
    assertEquals("mapname0", ((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName());
    assertEquals("arrayname5", bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name"));
    assertEquals("arrayname4", bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name"));
    assertEquals("listname3", bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name"));
    assertEquals("listname2", bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name"));
    assertEquals("mapname1", bw.getPropertyValue("map['key1'].nestedIndexedBean.map[key1].name"));
    assertEquals("mapname0", bw.getPropertyValue("map[key2].nestedIndexedBean.map[\"key2\"].name"));
  }
View Full Code Here

      if (target == null) {
        throw new IllegalStateException("Neither BindingResult nor plain target object for bean name '" +
            beanName + "' available as request attribute");
      }
      if (this.expression != null && !"*".equals(this.expression) && !this.expression.endsWith("*")) {
        BeanWrapper bw = PropertyAccessorFactory.forBeanPropertyAccess(target);
        this.valueType = bw.getPropertyType(this.expression);
        this.value = bw.getPropertyValue(this.expression);
      }
      this.errorCodes = new String[0];
      this.errorMessages = new String[0];
    }

View Full Code Here

    tb1.setNestedIndexedBean(new IndexedTestBean());
    tb2.setNestedIndexedBean(new IndexedTestBean());
    tb3.setNestedIndexedBean(new IndexedTestBean());
    tb4.setNestedIndexedBean(new IndexedTestBean());
    tb5.setNestedIndexedBean(new IndexedTestBean());
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.registerCustomEditor(String.class, "array[0].nestedIndexedBean.array[0].name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("array" + text);
      }
    });
    bw.registerCustomEditor(String.class, "list.nestedIndexedBean.list[1].name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("list" + text);
      }
    });
    bw.registerCustomEditor(String.class, "map[key1].nestedIndexedBean.map.name", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue("map" + text);
      }
    });

    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue("array[0].nestedIndexedBean.array[0].name", "name5");
    pvs.addPropertyValue("array[1].nestedIndexedBean.array[1].name", "name4");
    pvs.addPropertyValue("list[0].nestedIndexedBean.list[0].name", "name3");
    pvs.addPropertyValue("list[1].nestedIndexedBean.list[1].name", "name2");
    pvs.addPropertyValue("map[key1].nestedIndexedBean.map[\"key1\"].name", "name1");
    pvs.addPropertyValue("map['key2'].nestedIndexedBean.map[key2].name", "name0");
    bw.setPropertyValues(pvs);
    assertEquals("arrayname5", tb0.getNestedIndexedBean().getArray()[0].getName());
    assertEquals("name4", tb1.getNestedIndexedBean().getArray()[1].getName());
    assertEquals("name3", ((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName());
    assertEquals("listname2", ((TestBean) tb3.getNestedIndexedBean().getList().get(1)).getName());
    assertEquals("mapname1", ((TestBean) tb4.getNestedIndexedBean().getMap().get("key1")).getName());
View Full Code Here

    assertEquals("name0", ((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName());
  }

  public void testIndexedPropertiesWithDirectAccessAndPropertyEditors() {
    IndexedTestBean bean = new IndexedTestBean();
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.registerCustomEditor(TestBean.class, "array", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue(new TestBean("array" + text, 99));
      }

      public String getAsText() {
        return ((TestBean) getValue()).getName();
      }
    });
    bw.registerCustomEditor(TestBean.class, "list", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue(new TestBean("list" + text, 99));
      }

      public String getAsText() {
        return ((TestBean) getValue()).getName();
      }
    });
    bw.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue(new TestBean("map" + text, 99));
      }

      public String getAsText() {
        return ((TestBean) getValue()).getName();
      }
    });

    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue("array[0]", "a");
    pvs.addPropertyValue("array[1]", "b");
    pvs.addPropertyValue("list[0]", "c");
    pvs.addPropertyValue("list[1]", "d");
    pvs.addPropertyValue("map[key1]", "e");
    pvs.addPropertyValue("map['key2']", "f");
    bw.setPropertyValues(pvs);
    assertEquals("arraya", bean.getArray()[0].getName());
    assertEquals("arrayb", bean.getArray()[1].getName());
    assertEquals("listc", ((TestBean) bean.getList().get(0)).getName());
    assertEquals("listd", ((TestBean) bean.getList().get(1)).getName());
    assertEquals("mape", ((TestBean) bean.getMap().get("key1")).getName());
View Full Code Here

    assertEquals("mapf", ((TestBean) bean.getMap().get("key2")).getName());
  }

  public void testIndexedPropertiesWithDirectAccessAndSpecificPropertyEditors() {
    IndexedTestBean bean = new IndexedTestBean();
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.registerCustomEditor(TestBean.class, "array[0]", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue(new TestBean("array0" + text, 99));
      }

      public String getAsText() {
        return ((TestBean) getValue()).getName();
      }
    });
    bw.registerCustomEditor(TestBean.class, "array[1]", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue(new TestBean("array1" + text, 99));
      }

      public String getAsText() {
        return ((TestBean) getValue()).getName();
      }
    });
    bw.registerCustomEditor(TestBean.class, "list[0]", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue(new TestBean("list0" + text, 99));
      }

      public String getAsText() {
        return ((TestBean) getValue()).getName();
      }
    });
    bw.registerCustomEditor(TestBean.class, "list[1]", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue(new TestBean("list1" + text, 99));
      }

      public String getAsText() {
        return ((TestBean) getValue()).getName();
      }
    });
    bw.registerCustomEditor(TestBean.class, "map[key1]", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue(new TestBean("mapkey1" + text, 99));
      }

      public String getAsText() {
        return ((TestBean) getValue()).getName();
      }
    });
    bw.registerCustomEditor(TestBean.class, "map[key2]", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue(new TestBean("mapkey2" + text, 99));
      }

      public String getAsText() {
        return ((TestBean) getValue()).getName();
      }
    });

    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.addPropertyValue("array[0]", "a");
    pvs.addPropertyValue("array[1]", "b");
    pvs.addPropertyValue("list[0]", "c");
    pvs.addPropertyValue("list[1]", "d");
    pvs.addPropertyValue("map[key1]", "e");
    pvs.addPropertyValue("map['key2']", "f");
    bw.setPropertyValues(pvs);
    assertEquals("array0a", bean.getArray()[0].getName());
    assertEquals("array1b", bean.getArray()[1].getName());
    assertEquals("list0c", ((TestBean) bean.getList().get(0)).getName());
    assertEquals("list1d", ((TestBean) bean.getList().get(1)).getName());
    assertEquals("mapkey1e", ((TestBean) bean.getMap().get("key1")).getName());
View Full Code Here

    assertEquals("mapkey2f", ((TestBean) bean.getMap().get("key2")).getName());
  }

  public void testIndexedPropertiesWithListPropertyEditor() {
    IndexedTestBean bean = new IndexedTestBean();
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.registerCustomEditor(List.class, "list", new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        List result = new ArrayList();
        result.add(new TestBean("list" + text, 99));
        setValue(result);
      }
    });
    bw.setPropertyValue("list", "1");
    assertEquals("list1", ((TestBean) bean.getList().get(0)).getName());
    bw.setPropertyValue("list[0]", "test");
    assertEquals("test", bean.getList().get(0));
  }
View Full Code Here

    assertEquals("test", bean.getList().get(0));
  }

  public void testConversionToOldCollections() throws PropertyVetoException {
    OldCollectionsBean tb = new OldCollectionsBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(Vector.class, new CustomCollectionEditor(Vector.class));
    bw.registerCustomEditor(Hashtable.class, new CustomMapEditor(Hashtable.class));

    bw.setPropertyValue("vector", new String[] {"a", "b"});
    assertEquals(2, tb.getVector().size());
    assertEquals("a", tb.getVector().get(0));
    assertEquals("b", tb.getVector().get(1));

    bw.setPropertyValue("hashtable", Collections.singletonMap("foo", "bar"));
    assertEquals(1, tb.getHashtable().size());
    assertEquals("bar", tb.getHashtable().get("foo"));
  }
View Full Code Here

    assertEquals("bar", tb.getHashtable().get("foo"));
  }

  public void testUninitializedArrayPropertyWithCustomEditor() {
    IndexedTestBean bean = new IndexedTestBean(false);
    BeanWrapper bw = new BeanWrapperImpl(bean);
    PropertyEditor pe = new CustomNumberEditor(Integer.class, true);
    bw.registerCustomEditor(null, "list.age", pe);
    TestBean tb = new TestBean();
    bw.setPropertyValue("list", new ArrayList());
    bw.setPropertyValue("list[0]", tb);
    assertEquals(tb, bean.getList().get(0));
    assertEquals(pe, bw.findCustomEditor(int.class, "list.age"));
    assertEquals(pe, bw.findCustomEditor(null, "list.age"));
    assertEquals(pe, bw.findCustomEditor(int.class, "list[0].age"));
    assertEquals(pe, bw.findCustomEditor(null, "list[0].age"));
  }
View Full Code Here

    assertEquals(pe, bw.findCustomEditor(null, "list[0].age"));
  }

  public void testArrayToArrayConversion() throws PropertyVetoException {
    IndexedTestBean tb = new IndexedTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(TestBean.class, new PropertyEditorSupport() {
      public void setAsText(String text) throws IllegalArgumentException {
        setValue(new TestBean(text, 99));
      }
    });
    bw.setPropertyValue("array", new String[] {"a", "b"});
    assertEquals(2, tb.getArray().length);
    assertEquals("a", tb.getArray()[0].getName());
    assertEquals("b", tb.getArray()[1].getName());
  }
View Full Code Here

TOP

Related Classes of org.springframework.beans.BeanWrapper

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.