Skip to content
Snippets Groups Projects
Commit 34f7badc authored by felmer's avatar felmer
Browse files

LMS-257 BeanUtils.Converter contract extended and tested

SVN: 4770
parent e03298b5
No related branches found
No related tags found
No related merge requests found
...@@ -129,9 +129,15 @@ public final class BeanUtils ...@@ -129,9 +129,15 @@ public final class BeanUtils
* Marker interface for converter classes. The real method are determined via reflection. A converter needs to match * Marker interface for converter classes. The real method are determined via reflection. A converter needs to match
* both the source and the destination bean. If the destination bean has a setter <code>setFoo(FooClass foo)</code> * both the source and the destination bean. If the destination bean has a setter <code>setFoo(FooClass foo)</code>
* and the converter has a method <code>FooClass convertToFoo(SourceBeanClass sourceBean)</code>, then this will * and the converter has a method <code>FooClass convertToFoo(SourceBeanClass sourceBean)</code>, then this will
* be called instead of any getter of the <var>sourceBean</var>. Note that if there is a matching method * be called instead of any getter of the <var>sourceBean</var>.
* <code>convertToFoo()</code>, it needs to have an appropriate return type or else an * <p>
* {@link IllegalArgumentException} will be thrown. * Note:
* <ul>
* <li>The declared <code>SourceBeanClass</code> in the converter method can also be a superclass of
* <code>sourceBean</code>or an interface implemented by <code>sourceBean</code>
* <li>If there is a matching method <code>convertToFoo()</code>, it needs to have an appropriate return type or
* else an {@link IllegalArgumentException} will be thrown.
* </ul>
*/ */
public interface Converter public interface Converter
{ {
...@@ -731,20 +737,23 @@ public final class BeanUtils ...@@ -731,20 +737,23 @@ public final class BeanUtils
{ {
if (converter != NULL_CONVERTER) if (converter != NULL_CONVERTER)
{ {
try String methodName = "convertTo" + setter.getName().substring(SETTER_PREFIX.length());
Class<? extends Converter> converterClasss = converter.getClass();
Collection<Class<?>> classes = ClassUtils.gatherAllCastableClassesAndInterfacesFor(sourceBean);
for (Class<?> clazz : classes)
{ {
String methodName = "convertTo" + setter.getName().substring(SETTER_PREFIX.length()); try
Class<? extends Converter> converterClasss = converter.getClass(); {
final Method converterMethod = converterClasss.getMethod(methodName, new Class[] final Method converterMethod = converterClasss.getMethod(methodName, new Class[] { clazz });
{ sourceBean.getClass() }); if (converterMethod.isAccessible() == false)
if (converterMethod.isAccessible() == false) {
converterMethod.setAccessible(true);
}
return converterMethod;
} catch (NoSuchMethodException e)
{ {
converterMethod.setAccessible(true); // Nothing to do here - there just isn't any converter method for this setter.
} }
return converterMethod;
} catch (NoSuchMethodException ex)
{
// Nothing to do here - there just isn't any converter method for this setter.
} }
} }
return null; return null;
......
...@@ -955,8 +955,13 @@ public final class BeanUtilsTest ...@@ -955,8 +955,13 @@ public final class BeanUtilsTest
assertEquals(msg, b2.getI(), b1.getI()); assertEquals(msg, b2.getI(), b1.getI());
assertEquals(msg, b2.getS(), b1.getS()); assertEquals(msg, b2.getS(), b1.getS());
} }
private static interface IFoo
{
public String getFoo();
}
public static class FooBean public static class FooBean implements IFoo
{ {
private String foo; private String foo;
...@@ -1001,4 +1006,20 @@ public final class BeanUtilsTest ...@@ -1001,4 +1006,20 @@ public final class BeanUtilsTest
}); });
assertEquals("some to Foo", toFooBean.getBar()); assertEquals("some to Foo", toFooBean.getBar());
} }
@Test
public void testConverterWithArgumentOfInterfaceType()
{
final FooBean tofuBean = new FooBean();
tofuBean.setFoo("some tofu");
final BarBean toFooBean = BeanUtils.createBean(BarBean.class, tofuBean, new BeanUtils.Converter()
{
@SuppressWarnings("unused")
public String convertToBar(IFoo foo)
{
return StringUtils.replace(foo.getFoo(), "tofu", "to Foo");
}
});
assertEquals("some to Foo", toFooBean.getBar());
}
} }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment