Example usage for org.springframework.beans BeanWrapper registerCustomEditor

List of usage examples for org.springframework.beans BeanWrapper registerCustomEditor

Introduction

In this page you can find the example usage for org.springframework.beans BeanWrapper registerCustomEditor.

Prototype

void registerCustomEditor(@Nullable Class<?> requiredType, @Nullable String propertyPath,
        PropertyEditor propertyEditor);

Source Link

Document

Register the given custom property editor for the given type and property, or for all properties of the given type.

Usage

From source file:org.springframework.beans.BeanWrapperTests.java

@Test
public void testLargeMatchingPrimitiveArrayWithSpecificEditor() {
    PrimitiveArrayBean tb = new PrimitiveArrayBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(int.class, "array", new PropertyEditorSupport() {
        @Override/*from  w  w  w  .ja  v a 2  s.c  om*/
        public void setValue(Object value) {
            if (value instanceof Integer) {
                super.setValue(new Integer(((Integer) value).intValue() + 1));
            }
        }
    });
    int[] input = new int[1024];
    bw.setPropertyValue("array", input);
    assertEquals(1024, tb.getArray().length);
    assertEquals(1, tb.getArray()[0]);
    assertEquals(1, tb.getArray()[1]);
}

From source file:org.springframework.beans.BeanWrapperTests.java

@Test
public void testLargeMatchingPrimitiveArrayWithIndexSpecificEditor() {
    PrimitiveArrayBean tb = new PrimitiveArrayBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(int.class, "array[1]", new PropertyEditorSupport() {
        @Override//  w ww .  jav  a 2 s.c  o m
        public void setValue(Object value) {
            if (value instanceof Integer) {
                super.setValue(new Integer(((Integer) value).intValue() + 1));
            }
        }
    });
    int[] input = new int[1024];
    bw.setPropertyValue("array", input);
    assertEquals(1024, tb.getArray().length);
    assertEquals(0, tb.getArray()[0]);
    assertEquals(1, tb.getArray()[1]);
}

From source file:org.springframework.beans.BeanWrapperTests.java

@Test
public void testCollectionsWithStringValuesAndCustomEditor() {
    IndexedTestBean tb = new IndexedTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(String.class, "set", new StringTrimmerEditor(false));
    bw.registerCustomEditor(String.class, "list", new StringTrimmerEditor(false));

    bw.setPropertyValue("set", "set1 ");
    bw.setPropertyValue("sortedSet", "sortedSet1");
    bw.setPropertyValue("list", "list1 ");
    assertEquals(1, tb.getSet().size());
    assertTrue(tb.getSet().contains("set1"));
    assertEquals(1, tb.getSortedSet().size());
    assertTrue(tb.getSortedSet().contains("sortedSet1"));
    assertEquals(1, tb.getList().size());
    assertTrue(tb.getList().contains("list1"));

    bw.setPropertyValue("list", Arrays.asList(new String[] { "list1 " }));
    assertTrue(tb.getList().contains("list1"));
}