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);
}
}
Aggregations