static Object toEnum(Object value, Class cls) throws ConversionException {
        if (value.getClass().equals(cls)) {
            return value;
        } else if (value instanceof String) {
            try {
                Method valueOfMethod =
                    cls.getMethod("valueOf", new Class[] { String.class });
                return valueOfMethod.invoke(null, new Object[] { value });
            } catch (Exception e) {
                throw new ConversionException("The value " + value +
                                              " can't be converted to a " +
                                              cls.getName());
            }
        } else if (value instanceof Number) {
            try {
                Method valuesMethod = cls.getMethod("values", new Class[] { });
                Object valuesArray =
                    valuesMethod.invoke(null, new Object[] { });

                return Array.get(valuesArray, ((Number)value).intValue());
            } catch (Exception e) {
                throw new ConversionException("The value " + value +
                                              " can't be converted to a " +
                                              cls.getName());
            }
        } else {
            throw new ConversionException("The value " + value +
                                          " can't be converted to a " +
                                          cls.getName());
        }
    }