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

org.skife.config.ExactMatchEnumCoercible Maven / Gradle / Ivy

package org.skife.config;

import java.lang.reflect.Method;

public class ExactMatchEnumCoercible implements Coercible
{
    public Coercer accept(Class clazz)
    {
        if (!clazz.isEnum()) {
            return null;
        }
        try {
            final Method m = clazz.getMethod("valueOf", String.class);
            return new Coercer()
            {
                public Object coerce(String value)
                {
                    if (value == null) {
                        return null;
                    }
                    try {
                        return m.invoke(null, value);
                    }
                    catch (Exception e) {
                        throw DefaultCoercibles.convertException(e);
                    }
                }
            };
        }
        catch (NoSuchMethodException e) {
            throw new IllegalStateException(".valueOf(String) missing! World broken!", e);
        }

    }
}