Example usage for org.springframework.beans AbstractPropertyAccessor registerCustomEditor

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

Introduction

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

Prototype

@Override
    public void registerCustomEditor(Class<?> requiredType, PropertyEditor propertyEditor) 

Source Link

Usage

From source file:com.github.dbourdette.glass.job.GlassJobFactory.java

private AbstractPropertyAccessor buildAccessor(Job job) {
    AbstractPropertyAccessor accessor = null;

    if (configuration.getInjectionType() == InjectionType.FIELD) {
        accessor = new DirectFieldAccessor(job);
    } else {/*  ww  w  . ja  v  a 2 s  . c  o  m*/
        accessor = new BeanWrapperImpl(job);
    }

    SimpleDateFormat simpleDateFormat = new SimpleDateFormat(configuration.getDateFormat());
    CustomDateEditor customDateEditor = new CustomDateEditor(simpleDateFormat, true);
    accessor.registerCustomEditor(Date.class, customDateEditor);

    return accessor;
}

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

@Test
public void setPropertyWithCustomEditor() {
    MutablePropertyValues values = new MutablePropertyValues();
    values.add("name", Integer.class);
    TestBean target = new TestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(String.class, new PropertyEditorSupport() {
        @Override/*w ww  .  j a  v  a  2  s  .  co  m*/
        public void setValue(Object value) {
            super.setValue(value.toString());
        }
    });
    accessor.setPropertyValues(values);
    assertEquals(Integer.class.toString(), target.getName());
}

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

@Test
public void setIntArrayPropertyWithCustomEditor() {
    PropsTester target = new PropsTester();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(int.class, new PropertyEditorSupport() {
        @Override/*from   w w  w . j a v  a2 s.com*/
        public void setAsText(String text) {
            setValue(new Integer(Integer.parseInt(text) + 1));
        }
    });

    accessor.setPropertyValue("intArray", new int[] { 4, 5, 2, 3 });
    assertTrue("intArray length = 4", target.intArray.length == 4);
    assertTrue("correct values", target.intArray[0] == 4 && target.intArray[1] == 5 && target.intArray[2] == 2
            && target.intArray[3] == 3);

    accessor.setPropertyValue("intArray", new String[] { "3", "4", "1", "2" });
    assertTrue("intArray length = 4", target.intArray.length == 4);
    assertTrue("correct values", target.intArray[0] == 4 && target.intArray[1] == 5 && target.intArray[2] == 2
            && target.intArray[3] == 3);

    accessor.setPropertyValue("intArray", new Integer(1));
    assertTrue("intArray length = 4", target.intArray.length == 1);
    assertTrue("correct values", target.intArray[0] == 1);

    accessor.setPropertyValue("intArray", new String[] { "0" });
    assertTrue("intArray length = 4", target.intArray.length == 1);
    assertTrue("correct values", target.intArray[0] == 1);

    accessor.setPropertyValue("intArray", "0");
    assertTrue("intArray length = 4", target.intArray.length == 1);
    assertTrue("correct values", target.intArray[0] == 1);
}

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

@Test
public void setPrimitiveArrayPropertyLargeMatching() {
    Assume.group(TestGroup.PERFORMANCE);
    Assume.notLogging(LogFactory.getLog(AbstractPropertyAccessorTests.class));

    PrimitiveArrayBean target = new PrimitiveArrayBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    int[] input = new int[1024];
    StopWatch sw = new StopWatch();
    sw.start("array1");
    for (int i = 0; i < 1000; i++) {
        accessor.setPropertyValue("array", input);
    }/*from w w  w  .ja  v  a  2  s .com*/
    sw.stop();
    assertEquals(1024, target.getArray().length);
    assertEquals(0, target.getArray()[0]);
    long time1 = sw.getLastTaskTimeMillis();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);

    accessor.registerCustomEditor(String.class, new StringTrimmerEditor(false));
    sw.start("array2");
    for (int i = 0; i < 1000; i++) {
        accessor.setPropertyValue("array", input);
    }
    sw.stop();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 125);

    accessor.registerCustomEditor(int.class, "array.somePath", new CustomNumberEditor(Integer.class, false));
    sw.start("array3");
    for (int i = 0; i < 1000; i++) {
        accessor.setPropertyValue("array", input);
    }
    sw.stop();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);

    accessor.registerCustomEditor(int.class, "array[0].somePath", new CustomNumberEditor(Integer.class, false));
    sw.start("array3");
    for (int i = 0; i < 1000; i++) {
        accessor.setPropertyValue("array", input);
    }
    sw.stop();
    assertTrue("Took too long", sw.getLastTaskTimeMillis() < 100);

    accessor.registerCustomEditor(int.class, new CustomNumberEditor(Integer.class, false));
    sw.start("array4");
    for (int i = 0; i < 100; i++) {
        accessor.setPropertyValue("array", input);
    }
    sw.stop();
    assertEquals(1024, target.getArray().length);
    assertEquals(0, target.getArray()[0]);
    assertTrue("Took too long", sw.getLastTaskTimeMillis() > time1);
}

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

@Test
public void setMapPropertyWithTypeConversion() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(TestBean.class, new PropertyEditorSupport() {
        @Override//from  w w w  .  j  a v  a2s . c  o  m
        public void setAsText(String text) throws IllegalArgumentException {
            if (!StringUtils.hasLength(text)) {
                throw new IllegalArgumentException();
            }
            setValue(new TestBean(text));
        }
    });

    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("map[key1]", "rod");
    pvs.add("map[key2]", "rob");
    accessor.setPropertyValues(pvs);
    assertEquals("rod", ((TestBean) target.getMap().get("key1")).getName());
    assertEquals("rob", ((TestBean) target.getMap().get("key2")).getName());

    pvs = new MutablePropertyValues();
    pvs.add("map[key1]", "rod");
    pvs.add("map[key2]", "");
    try {
        accessor.setPropertyValues(pvs);
        fail("Should have thrown TypeMismatchException");
    } catch (PropertyBatchUpdateException ex) {
        PropertyAccessException pae = ex.getPropertyAccessException("map[key2]");
        assertTrue(pae instanceof TypeMismatchException);
    }
}