Joanis
Joanis

Reputation: 1679

How to create an alias for a private inner class (using XStream)?

I'm creating aliases for long class names... It works perfectly fine, but one of the serialized classes is a private inner class. I can't think of a way to create an alias for it other than making it public. I don't like this solution, because it should not be public in the first place. But since making an alias for it will make it possible to change package and class names without having to modify XML files (because the first tag is the fully qualified class name).

This is how I create aliases:

xstreamInstance.alias("ClassAlias", OuterClass.InnerClassToAlias.class);

That's why I need public access to that inner class.

So, if anyone knows a trick to alias a private inner class, I would really like to hear about it.

Upvotes: 1

Views: 3046

Answers (2)

Christopher Oezbek
Christopher Oezbek

Reputation: 26393

How about using annotations for the alias?

public class Parent {
    @XStreamAlias("kiddie")
    private class Child {

    }
}

Edit: Alas, parsing of annotations of nested classes is not supported by XStream when asking to get the parent class parsed.

Upvotes: 0

Marcus Leon
Marcus Leon

Reputation: 56699

You could create a class like the following and pass your reference to the xstreamInstance to the alias method.

public class Parent {
    public void alias(XStream x) {
        x.alias("Kiddie", Parent.Child.class);
    }

    private class Child {

    }
}

Upvotes: 1

Related Questions