List of usage examples for com.fasterxml.jackson.databind.util EnumResolver findEnum
public T findEnum(String paramString)
From source file:org.apache.pulsar.common.util.FieldParser.java
/** * Convert the given object value to the given class. * * @param from//from w ww . j a v a2 s .c om * The object value to be converted. * @param to * The type class which the given object should be converted to. * @return The converted object value. * @throws UnsupportedOperationException * If no suitable converter can be found. * @throws RuntimeException * If conversion failed somehow. This can be caused by at least an ExceptionInInitializerError, * IllegalAccessException or InvocationTargetException. */ @SuppressWarnings("unchecked") public static <T> T convert(Object from, Class<T> to) { checkNotNull(to); if (from == null) { return null; } to = (Class<T>) wrap(to); // Can we cast? Then just do it. if (to.isAssignableFrom(from.getClass())) { return to.cast(from); } // Lookup the suitable converter. String converterId = from.getClass().getName() + "_" + to.getName(); Method converter = CONVERTERS.get(converterId); if (to.isEnum()) { // Converting string to enum EnumResolver r = EnumResolver.constructUsingToString((Class<Enum<?>>) to, null); T value = (T) r.findEnum((String) from); if (value == null) { throw new RuntimeException("Invalid value '" + from + "' for enum " + to); } return value; } if (converter == null) { throw new UnsupportedOperationException("Cannot convert from " + from.getClass().getName() + " to " + to.getName() + ". Requested converter does not exist."); } // Convert the value. try { Object val = converter.invoke(to, from); return to.cast(val); } catch (Exception e) { throw new RuntimeException("Cannot convert from " + from.getClass().getName() + " to " + to.getName() + ". Conversion failed with " + e.getMessage(), e); } }