Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Null valueFieldName for ToAttributedValueConverter with annotation XStreamConverter #41

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

Comments

@QIvan
Copy link

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.

Copy link
Member

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.

Copy link
Author

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.

Copy link
Member

commented Mar 6, 2016

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

@QIvan

This comment has been minimized.

Copy link
Author

commented Mar 6, 2016

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

@QIvan

This comment has been minimized.

Copy link
Author

commented Mar 6, 2016

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

@joehni

This comment has been minimized.

Copy link
Member

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
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
Projects
None yet
2 participants
You can’t perform that action at this time.