views:

217

answers:

2

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.

+1  A: 

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 {

    }
}
Marcus
Looks like a good idea. Thank you. So I have the choice between making the class public or adding a small method... The small method is very interesting since it doesn't allow things that shouldn't be possible (like to instantiate that inner class from elsewhere).
M. Joanis
+1  A: 

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.

Christopher Oezbek
Doesn't work for me. Only difference with the example above is that my inner class is static (nested class).
M. Joanis
I looked into the XStream code and indeed it will not parse nested classes implicitly.
Christopher Oezbek