Search in sources :

Example 1 with NotationConverterToNotationParserAdapter

use of org.gradle.internal.typeconversion.NotationConverterToNotationParserAdapter in project gradle by gradle.

the class ProtocolToModelAdapter method adaptToEnum.

private static <T, S> T adaptToEnum(Class<T> targetType, S sourceObject) {
    try {
        String literal;
        if (sourceObject instanceof Enum) {
            literal = ((Enum<?>) sourceObject).name();
        } else if (sourceObject instanceof String) {
            literal = (String) sourceObject;
        } else {
            literal = sourceObject.toString();
        }
        @SuppressWarnings({ "rawtypes", "unchecked" }) NotationParser<String, T> parser = new NotationConverterToNotationParserAdapter<String, T>(new EnumFromCharSequenceNotationParser(targetType));
        T parsedLiteral = parser.parseNotation(literal);
        return targetType.cast(parsedLiteral);
    } catch (TypeConversionException e) {
        throw new IllegalArgumentException(String.format("Can't convert '%s' to enum type '%s'", sourceObject, targetType.getSimpleName()), e);
    }
}
Also used : NotationConverterToNotationParserAdapter(org.gradle.internal.typeconversion.NotationConverterToNotationParserAdapter) EnumFromCharSequenceNotationParser(org.gradle.internal.typeconversion.EnumFromCharSequenceNotationParser) TypeConversionException(org.gradle.internal.typeconversion.TypeConversionException)

Aggregations

EnumFromCharSequenceNotationParser (org.gradle.internal.typeconversion.EnumFromCharSequenceNotationParser)1 NotationConverterToNotationParserAdapter (org.gradle.internal.typeconversion.NotationConverterToNotationParserAdapter)1 TypeConversionException (org.gradle.internal.typeconversion.TypeConversionException)1