Search in sources :

Example 1 with TypeConversionException

use of org.gradle.internal.typeconversion.TypeConversionException 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)

Example 2 with TypeConversionException

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

the class CommandLineTaskConfigurer method configureTasksNow.

private List<String> configureTasksNow(Collection<Task> tasks, List<String> arguments) {
    List<String> remainingArguments = null;
    for (Task task : tasks) {
        CommandLineParser parser = new CommandLineParser();
        final List<OptionDescriptor> commandLineOptions = optionReader.getOptions(task);
        for (OptionDescriptor optionDescriptor : commandLineOptions) {
            String optionName = optionDescriptor.getName();
            org.gradle.cli.CommandLineOption option = parser.option(optionName);
            option.hasDescription(optionDescriptor.getDescription());
            option.hasArgument(optionDescriptor.getArgumentType());
        }
        ParsedCommandLine parsed;
        try {
            parsed = parser.parse(arguments);
        } catch (CommandLineArgumentException e) {
            // we expect that all options must be applicable for each task
            throw new TaskConfigurationException(task.getPath(), "Problem configuring task " + task.getPath() + " from command line.", e);
        }
        for (OptionDescriptor commandLineOptionDescriptor : commandLineOptions) {
            final String name = commandLineOptionDescriptor.getName();
            if (parsed.hasOption(name)) {
                ParsedCommandLineOption o = parsed.option(name);
                try {
                    commandLineOptionDescriptor.apply(task, o.getValues());
                } catch (TypeConversionException ex) {
                    throw new TaskConfigurationException(task.getPath(), String.format("Problem configuring option '%s' on task '%s' from command line.", name, task.getPath()), ex);
                }
            }
        }
        assert remainingArguments == null || remainingArguments.equals(parsed.getExtraArguments()) : "we expect all options to be consumed by each task so remainingArguments should be the same for each task";
        remainingArguments = parsed.getExtraArguments();
    }
    return remainingArguments;
}
Also used : Task(org.gradle.api.Task) OptionDescriptor(org.gradle.api.internal.tasks.options.OptionDescriptor) TypeConversionException(org.gradle.internal.typeconversion.TypeConversionException) ParsedCommandLineOption(org.gradle.cli.ParsedCommandLineOption) ParsedCommandLine(org.gradle.cli.ParsedCommandLine) CommandLineParser(org.gradle.cli.CommandLineParser) CommandLineArgumentException(org.gradle.cli.CommandLineArgumentException)

Aggregations

TypeConversionException (org.gradle.internal.typeconversion.TypeConversionException)2 Task (org.gradle.api.Task)1 OptionDescriptor (org.gradle.api.internal.tasks.options.OptionDescriptor)1 CommandLineArgumentException (org.gradle.cli.CommandLineArgumentException)1 CommandLineParser (org.gradle.cli.CommandLineParser)1 ParsedCommandLine (org.gradle.cli.ParsedCommandLine)1 ParsedCommandLineOption (org.gradle.cli.ParsedCommandLineOption)1 EnumFromCharSequenceNotationParser (org.gradle.internal.typeconversion.EnumFromCharSequenceNotationParser)1 NotationConverterToNotationParserAdapter (org.gradle.internal.typeconversion.NotationConverterToNotationParserAdapter)1