All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.contextfw.web.commons.cloud.internal.serializer.KeyConverter Maven / Gradle / Ivy

The newest version!
package net.contextfw.web.commons.cloud.internal.serializer;

import net.contextfw.web.application.WebApplicationException;

import com.google.inject.Key;
import com.thoughtworks.xstream.converters.SingleValueConverter;

public class KeyConverter implements SingleValueConverter {

    private ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    
    @Override
    public boolean canConvert(@SuppressWarnings("rawtypes") Class type) {
        return Key.class.isAssignableFrom(type);
    }

    @Override
    public String toString(Object obj) {
        return ((Key) obj).getTypeLiteral().getRawType().getCanonicalName();
    }

    @Override
    public Object fromString(String str) {
        try {
            return Key.get(classLoader.loadClass(str));
        } catch (ClassNotFoundException e) {
            throw new WebApplicationException(e);
        }
    }

    public void setClassLoader(ClassLoader classLoader) {
        this.classLoader = classLoader;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy