Details
-
Type: Improvement
-
Status: Closed
-
Priority: Minor
-
Resolution: Fixed
-
Affects Version/s: 1.3.1
-
Fix Version/s: 1.4
-
Component/s: Annotations
-
Labels:None
-
JDK version and platform:Sun 1.5_11 under windows
Description
@XStreamConverter currently only supports Converters which take none or a small set of arguments using DependencyInjectionFactory (AnnotationMapper, ClassLoader, ReflectionProvider, and/or JVM), but not the target type. It's fairly common that a Converter would want to know what type of value to which it's trying to convert. This is especially true for SingleValueConverters which do not have any way to get this information (full Converters can get it from the UnmarshallingContext, but then can't be used as attributes).
I've included a replacement AnnotationMapper and test case.
The changes to AnnotationMapper are fairly minor:
- Modified the cacheConverter method to take the target Class argument
- Added the target Class to the arguments when calling DependencyInjectionFactory.newInstance(...).
- Modified the internal cache to make sure that it caches both the converterType and the target class with the Converter instance rather than just the converterType with the Converter instance. This makes the caching less efficient, but makes sure that a ToStringConverter instance for Doubles doesn't try to convert a Boolean.
The test class included makes use of the ToStringConverter which is included with the standard distribution.
Hi Jason,
thanks for the code. Actually I had already some changes myself, but did not commit them, because I had no time to write the unit tests. Your tests then revealed a problem with my implementation, so it was really helpful and our implementations look very similar. The type is now supported in the head revision.
However, can you attach the next time a diff as patch? It helps a lot to see immediately the changes done and can also be applied a bit later even if the code has been evolved in the meantime.
Thanks again for taking your time,
Jörg