Hi Craig,

There is no support in this for XStream, as XStream cannot know which
package a class is in unless you tell it (for example, if it finds
<MyClass>, how would it know it's my.package.name.MyClass vs
some.other.library.MyClass?).

Your best bet is to have some code that will auto generate a list of the
full class names that you care about and call XStream.alias()
appropriately. To generate the list, you could iterate over every class in
the package. Unfortunately this is not trivial in Java, but you could use a
library like <https://github.com/ddopson/java-class-enumerator> to simplify
it for you. You would still have to provide a list of packages (so you
don't get conflict with system or third party libraries).

Like this:

  String[] packageNames = {"my.package.name", "another.package.name",
"blah.blah.blah"};
  for (String packageName : packageNames) {
    for (Class<?> cls :
ClassEnumerator.getClassesForPackage(Package.getPackage(packageName))) { //
from class-enumerator library
      xstream.alias(cls.getSimpleName(), cls);
    }
  }

Thanks
-Joe

On Wed, Aug 29, 2012 at 5:22 AM, Craig Burlock <[email protected]>wrote:

> Hello all!
>
> Does anyone know how to remove the full package name of *ALL *classes and
> only leave the "simple name" (i.e. replace my.package.name.MyClass with
> MyClass)?
>
> I've tried xstream.alias("", "*"), but that doesn't work.
>
> (BTW, I may have the parameters around the wrong way - sorry if so)
>
> My XML contains many objects with many package names and I don't want to
> create an alias for each of them.
>
> Any ideas?

Reply via email to