[
https://issues.apache.org/jira/browse/BEANUTILS-481?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14995720#comment-14995720
]
Matthew P Mann commented on BEANUTILS-481:
------------------------------------------
Benedikt,
The projects for which I wanted to use beanutils2 are not compatible with Java
8. I think there's probably a way to use lambda expressions with beanutils2
without the project itself being 1.8. Perhaps that could be accomplished by a
supplementary project with conveniences for Java 1.8 features. For example:
{code}
transformerRegistry.register(new FunctionTransformer<>(Integer::parseInt,
String.class, Integer.class))
{code}
... where FunctionTransformer would reside in the supplementary project.
Incidentally, it's unfortunate that the source and target types cannot be
inferred from a method literal like {{Integer::parseInt}}.
And that reminds me: I don't think we should be assuming that a
transformer/converter has only one source type and one target type. The
performance of the lookup suffers, but I think a {{boolean canConvert(Object,
Class<?>)}} method is better than {{getSourceType()}} and {{getTargetType()}}.
For example, I might want to be able to convert any enum to a string and vice
versa using an instance of a single class.
One more thing: Why are we calling the converter interface {{Transformer}}
when it's principal method is {{convert}}? I'd either rename the class to
{{Converter}} or (second choice) rename the method to {{transform}}.
-Matt
> [beanutils2] Support for nested properties and automatic conversion.
> --------------------------------------------------------------------
>
> Key: BEANUTILS-481
> URL: https://issues.apache.org/jira/browse/BEANUTILS-481
> Project: Commons BeanUtils
> Issue Type: Improvement
> Reporter: Matthew P Mann
> Fix For: 2.0
>
> Attachments: commons-beanutils2.conversion-only.patch,
> commons-beanutils2.patch
>
>
> Please consider the attached patch for the commons-beanutils2 project. I
> added support for nested properties and automatic conversion. Excerpt from
> AutoConversionTest:
> {code}
> final DateFormat dateFormat = new SimpleDateFormat("MMMM d, yyyy");
> final TransformerRegistry transformerRegistry = new TransformerRegistry()
> .register(new StringToDate(dateFormat))
> .register(new IntegerToString())
> .register(new StringToColor())
> .register(new IntegerToColor())
> .register(new StringToURL())
> .register(new StringToPhoneNumber());
> final PhoneNumber phoneNumber = new PhoneNumber();
> phoneNumber.setAreaCode("202");
> phoneNumber.setPrefix("456");
> phoneNumber.setLineNumber("1111");
> final Address address = new Address();
> address.setStreetAddress("1600 Pennsylvania Avenue Northwest");
> address.setCity("Washington");
> address.setStateCode("DC");
> address.setPostalCode("20500");
> address.setCountryCode("US");
> final Person person = new Person();
> person.setFirstName("Barack");
> person.setLastName("Obama");
> person.setBirthDate(dateFormat.parse("August 4, 1961"));
> person.setEyeColor(Color.decode("#362819"));
> person.setHairColor(GRAY);
> person.setPhoneNumber(phoneNumber);
> person.setAddress(address);
> person.setWebsite(new URL("https://www.barackobama.com/"));
> assertEquals(person, on(new Person(), transformerRegistry)
> .set("firstName").with("Barack")
> .set("lastName").with("Obama")
> .set("birthDate").with("August 4, 1961")
> .set("hairColor").with(0x808080)
> .set("eyeColor").with("#362819")
> .set("website").with("https://www.barackobama.com/")
> .set("phoneNumber").with("202-456-1111")
> .set("address").with(new Address())
> .set("address.streetAddress").with("1600 Pennsylvania Avenue Northwest")
> .set("address.city").with("Washington")
> .set("address.stateCode").with("DC")
> .set("address.postalCode").with(20500)
> .set("address.countryCode").with("US")
> .get());
> {code}
> Thanks,
> Matt
--
This message was sent by Atlassian JIRA
(v6.3.4#6332)