Null valueFieldName for ToAttributedValueConverter with annotation XStreamConverter #41

Closed
QIvan opened this Issue Mar 4, 2016 · 6 comments

Comments

Projects
None yet
2 participants
@QIvan

QIvan commented Mar 4, 2016

Hi! I try to marshall class

    @AllArgsConstructor
    @XStreamAlias(value = "message")
    class RendezvousMessage {
        private int messageType;
        private String content;
    }

with ToAttributedValueConverter to next xml representation:

<message messageType="1" content="test"/>

If I init converter manualy:

XStream xStream = new XStream();
Converter converter = new ToAttributedValueConverter(RendezvousMessage.class,
xStream.getMapper(), xStream.getReflectionProvider(), xStream.getConverterLookup(), null);
xStream.registerConverter(converter);

RendezvousMessage msg = new RendezvousMessage(1, "test");
System.out.println(xStream.toXML(msg));

it works fine, but if I try init converter with annotation @XStreamConverter

    @XStreamConverter(value = ToAttributedValueConverter.class)
    @AllArgsConstructor
    @XStreamAlias(value = "message")
    static class RendezvousMessage {
        private int messageType;
        private String content;
    }

I get exeption

com.thoughtworks.xstream.InitializationException: Cannot instantiate converter com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter for type ru.open.haven.risk.model.forts.FutSeriesTest$RendezvousMessage : Cannot construct com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter, none of the dependencies match any constructor's parameters

    at com.thoughtworks.xstream.mapper.AnnotationMapper.cacheConverter(AnnotationMapper.java:542)
    at com.thoughtworks.xstream.mapper.AnnotationMapper.processConverterAnnotations(AnnotationMapper.java:300)
    at com.thoughtworks.xstream.mapper.AnnotationMapper.processTypes(AnnotationMapper.java:194)
    at com.thoughtworks.xstream.mapper.AnnotationMapper.processAnnotations(AnnotationMapper.java:174)
    at com.thoughtworks.xstream.mapper.AnnotationMapper.serializedClass(AnnotationMapper.java:125)
    at com.thoughtworks.xstream.mapper.MapperWrapper.serializedClass(MapperWrapper.java:26)
    at com.thoughtworks.xstream.core.TreeMarshaller.start(TreeMarshaller.java:80)
    at com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.marshal(AbstractTreeMarshallingStrategy.java:37)
    at com.thoughtworks.xstream.XStream.marshal(XStream.java:1022)
    at com.thoughtworks.xstream.XStream.marshal(XStream.java:1011)
    at com.thoughtworks.xstream.XStream.toXML(XStream.java:984)
    at com.thoughtworks.xstream.XStream.toXML(XStream.java:971)

What did I do wrong?
Thanks!

@joehni

This comment has been minimized.

Show comment
Hide comment
@joehni

joehni Mar 4, 2016

Member

Nothing, it's currently simply not possible. All constructors of ToAttributedValueConverter require a String parameter even if its value is simply null. Therefore you could have declared

@XStreamConverter(value = ToAttributedValueConverter.class, strings={"messageType"})

However, it is not possible to declare

@XStreamConverter(value = ToAttributedValueConverter.class, strings={null})

So either we add another constructor that omits the valueFielName argument or we enhance the XStreamConverter annotation for an array with typed null values:

@XStreamConverter(value = ToAttributedValueConverter.class, nulls={String.class})

Or we do both ;-)

In the meantime you could simply derive from ToAttributedValueConverter (e.g. AllAttributesConverter) and add the missing constructor yourself.

Member

joehni commented Mar 4, 2016

Nothing, it's currently simply not possible. All constructors of ToAttributedValueConverter require a String parameter even if its value is simply null. Therefore you could have declared

@XStreamConverter(value = ToAttributedValueConverter.class, strings={"messageType"})

However, it is not possible to declare

@XStreamConverter(value = ToAttributedValueConverter.class, strings={null})

So either we add another constructor that omits the valueFielName argument or we enhance the XStreamConverter annotation for an array with typed null values:

@XStreamConverter(value = ToAttributedValueConverter.class, nulls={String.class})

Or we do both ;-)

In the meantime you could simply derive from ToAttributedValueConverter (e.g. AllAttributesConverter) and add the missing constructor yourself.

@joehni joehni added the bug label Mar 4, 2016

@joehni joehni self-assigned this Mar 4, 2016

@QIvan

This comment has been minimized.

Show comment
Hide comment
@QIvan

QIvan Mar 5, 2016

Thank you a lot!

In the meantime you could simply derive from ToAttributedValueConverter (e.g. AllAttributesConverter) and add the missing constructor yourself.

Yeap, I use exactly this way now =)

QIvan commented Mar 5, 2016

Thank you a lot!

In the meantime you could simply derive from ToAttributedValueConverter (e.g. AllAttributesConverter) and add the missing constructor yourself.

Yeap, I use exactly this way now =)

QIvan pushed a commit to QIvan/xstream that referenced this issue Mar 6, 2016

@joehni joehni closed this in ad63318 Mar 6, 2016

@joehni

This comment has been minimized.

Show comment
Hide comment
@joehni

joehni Mar 6, 2016

Member

Thanks for the PR, but I had it already done locally.

Member

joehni commented Mar 6, 2016

Thanks for the PR, but I had it already done locally.

@QIvan

This comment has been minimized.

Show comment
Hide comment
@QIvan

QIvan Mar 6, 2016

@joehni thank you! But what about failed test? Is it another bug?

QIvan commented Mar 6, 2016

@joehni thank you! But what about failed test? Is it another bug?

@QIvan

This comment has been minimized.

Show comment
Hide comment
@QIvan

QIvan Mar 6, 2016

And one more question please: when will a release be with this commit?

QIvan commented Mar 6, 2016

And one more question please: when will a release be with this commit?

@joehni

This comment has been minimized.

Show comment
Hide comment
@joehni

joehni Mar 6, 2016

Member

As soon as possible. The current changes should have been released months ago.

Member

joehni commented Mar 6, 2016

As soon as possible. The current changes should have been released months ago.

joehni added a commit that referenced this issue Mar 6, 2016

Add constructor to instantiate ToAttributedValueConverter with null
value for valueFieldName using XStreamConverter annotation (fixes #41).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment