Hi Roland,

Roland Roberts wrote:

> DefaultConverterLookup method lookupConverterForType(Class type) throws
> a runtime exception. This was totally unexpected since I don't see it in
> the docs. How exactly is one supposed to /safely/ look up converters?
> 
> Why do I need to do this you ask....
> 
> I have a map from name to object. Before calling toString() on the
> object, I want to see if there is a converter for the object's class and
> if so, call it. And yes, there is a reason for using that type of map
> which is what has evolved (or maybe devolved) after several rounds of
> factoring in the code.

The idea is, that a ConverterLookup actually *can* handle all types. 
Therefore XStream registers normally a ReflectionConverter with lowest 
priority that acts as catch all.

In your case you will have to register an equivalent for null i.e.:

class NoConverter implements Converter {
  static Converter INSTANCE = new NoConverter();
  boolean canConvert(Class type) {
    return true;
  }
  void marshal(...) {
    throw new ConverterException();
  }
  Object unmarshal(...) {
    throw new ConverterException();
  }
}


lookup.registerConverter(NoConverter.INSTANCE, Integer.MAX_VALUE);
Converter converter = lookup.lookupConverterForType(Void.class);
if (converter != NoConverter.INSTANCE) {
  // ...
}


- Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to