Package com.thoughtworks.xstream.converters

Examples of com.thoughtworks.xstream.converters.Converter


  public void testCanReplaceWithHigherPriority() {
   
    // this test actually depends on the keyset implementation of the corresponding cache map.
    final DefaultConverterLookup lookup = new DefaultConverterLookup();
    Converter currentConverter = new SingleValueConverterWrapper(new StringConverter());
    lookup.registerConverter(new BitSetConverter(), XStream.PRIORITY_VERY_HIGH);
    lookup.registerConverter(currentConverter, -100);
    lookup.lookupConverterForType(String.class);
    lookup.lookupConverterForType(BitSet.class);
    assertEquals(lookup.lookupConverterForType(String.class), currentConverter);
    Converter newConverter = new SingleValueConverterWrapper(new StringConverter());
    lookup.registerConverter(newConverter, 100);
    assertEquals(lookup.lookupConverterForType(String.class), newConverter);
  }
View Full Code Here


        XStream xstream = new XStream(new XppDriver());
        // using default mapper instead of XStream#buildMapper()
        Mapper mapper = new DefaultMapper(new CompositeClassLoader());
        // AttributeMapper required by ReflectionConverter
        mapper = new AttributeMapper(mapper, xstream.getConverterLookup(), xstream.getReflectionProvider());
        Converter converter = new CustomReflectionConverter(mapper, new PureJavaReflectionProvider());
        xstream.registerConverter(converter, -20);
        xstream.alias("world", World.class);
        World world = new World();

        String expected =
View Full Code Here

     */
    public DefaultConverterLookup(Mapper mapper) {
    }

    public Converter lookupConverterForType(Class type) {
        Converter cachedConverter = (Converter) typeToConverterMap.get(type);
        if (cachedConverter != null) {
            return cachedConverter;
        }
        Iterator iterator = converters.iterator();
        while (iterator.hasNext()) {
            Converter converter = (Converter) iterator.next();
            if (converter.canConvert(type)) {
                typeToConverterMap.put(type, converter);
                return converter;
            }
        }
        throw new ConversionException("No converter specified for " + type);
View Full Code Here

   
    public void flushCache() {
        typeToConverterMap.clear();
        Iterator iterator = converters.iterator();
        while (iterator.hasNext()) {
            Converter converter = (Converter) iterator.next();
            if (converter instanceof Caching) {
                ((Caching)converter).flushCache();
            }
        }
    }
View Full Code Here

     *
     * @param type the converter type to instantiate
     * @return the new instance
     */
    private Converter newInstance(Class<? extends ConverterMatcher> type) {
        Converter converter;
        // TODO: We need a separate exception for runtime initialization.
        try {
            if (SingleValueConverter.class.isAssignableFrom(type)) {
                final SingleValueConverter svc = (SingleValueConverter)type.getConstructor().newInstance();
                converter = new SingleValueConverterWrapper(svc);
View Full Code Here

                    exception.add("alias", alias);
                    exception.add("type", sourceType.getName());
                    throw exception;
                }

                Converter converter = mapper.getLocalConverter(definedIn, fieldName);
                if (converter == null) {
                    converter = lookup.lookupConverterForType(type);
                }

                if (value != null) {
View Full Code Here

                    continue;
                }

                Class type = field.getType();
                final Class declaringClass = field.getDeclaringClass();
                Converter converter = mapper.getLocalConverter(declaringClass, fieldName);
                if (converter == null) {
                    converter = lookup.lookupConverterForType(type);
                }

                if (!(converter instanceof SingleValueConverter)) {
View Full Code Here

        }
    }


    private static void registerConverter(XStream xstream, Class<? extends Converter> converterType) {
        Converter converter;
        if(configuredTypes.contains(converterType))
            return;
        if (AbstractCollectionConverter.class.isAssignableFrom(converterType)) {
            try {
                Constructor<? extends Converter> converterConstructor = converterType.getConstructor(Mapper.class);
View Full Code Here

        this(root, reader, converterLookup, (Mapper)classMapper);
    }


    public Object convertAnother(Object parent, Class type) {
        Converter converter = converterLookup.lookupConverterForType(type);
        return convert(parent, type,converter);
    }
View Full Code Here

                          ClassMapper classMapper) {
        this(writer, converterLookup, (Mapper)classMapper);
    }

    public void convertAnother(Object item) {
        Converter converter = converterLookup.lookupConverterForType(item.getClass());
        convert(item, converter);
    }
View Full Code Here

TOP

Related Classes of com.thoughtworks.xstream.converters.Converter

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.