package fulfillmentCenter.serviceInterface; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import com.thoughtworks.xstream.converters.SingleValueConverter; import com.thoughtworks.xstream.converters.enums.EnumConverter; public class CustomSingleValueConverter< T extends Enum<T> > extends EnumConverter implements SingleValueConverter { private Class<T> enumType; public static <V extends Enum<V>> SingleValueConverter create(Class<V> enumClass) { return new CustomSingleValueConverter<V>(enumClass); } private CustomSingleValueConverter(Class<T> newEnumType) { this.enumType = newEnumType; } @SuppressWarnings("unchecked") public boolean canConvert(Class type) { return type == enumType; } public Object fromString(String str) { Object r = null; try { Method m = enumType.getMethod("fromValue", String.class); r = m.invoke(null, str); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (NoSuchMethodException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalArgumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (InvocationTargetException e) { // TODO Auto-generated catch block e.printStackTrace(); } return r; } public String toString(Object obj) { return obj.toString(); } }
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)