From 1a2033eb4185deefcf9cd9b719e33384031cb017 Mon Sep 17 00:00:00 2001 From: Juergen Hoeller Date: Sun, 26 Jan 2014 00:05:51 +0100 Subject: [PATCH] Compatibility with JOpt 4.6 JOpt 4.6 redeclared its nonOptionArguments() method from List to List, requiring us to select String arguments only as we do for regular option values already. Issue: SPR-11359 (cherry picked from commit 67e76e9) --- .../core/env/CommandLinePropertySource.java | 22 ++++++----- .../env/JOptCommandLinePropertySource.java | 37 ++++++++++--------- 2 files changed, 32 insertions(+), 27 deletions(-) diff --git a/spring-core/src/main/java/org/springframework/core/env/CommandLinePropertySource.java b/spring-core/src/main/java/org/springframework/core/env/CommandLinePropertySource.java index ba7d8c70741..29371fcc732 100644 --- a/spring-core/src/main/java/org/springframework/core/env/CommandLinePropertySource.java +++ b/spring-core/src/main/java/org/springframework/core/env/CommandLinePropertySource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -193,27 +193,30 @@ public abstract class CommandLinePropertySource extends PropertySource { /** The default name of the property representing non-option arguments: {@value} */ public static final String DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME = "nonOptionArgs"; + private String nonOptionArgsPropertyName = DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME; + /** - * Create a new {@code CommandLinePropertySource} having the default name {@value - * #COMMAND_LINE_PROPERTY_SOURCE_NAME} and backed by the given source object. + * Create a new {@code CommandLinePropertySource} having the default name + * {@value #COMMAND_LINE_PROPERTY_SOURCE_NAME} and backed by the given source object. */ public CommandLinePropertySource(T source) { super(COMMAND_LINE_PROPERTY_SOURCE_NAME, source); } /** - * Create a new {@link CommandLinePropertySource} having the given name and backed by - * the given source object. + * Create a new {@link CommandLinePropertySource} having the given name + * and backed by the given source object. */ public CommandLinePropertySource(String name, T source) { super(name, source); } + /** - * Specify the name of the special "non-option arguments" property. The default is - * {@value #DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME}. + * Specify the name of the special "non-option arguments" property. + * The default is {@value #DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME}. */ public void setNonOptionArgsPropertyName(String nonOptionArgsPropertyName) { this.nonOptionArgsPropertyName = nonOptionArgsPropertyName; @@ -265,6 +268,7 @@ public abstract class CommandLinePropertySource extends PropertySource { } } + /** * Return whether the set of option arguments parsed from the command line contains * an option with the given name. @@ -288,8 +292,8 @@ public abstract class CommandLinePropertySource extends PropertySource { protected abstract List getOptionValues(String name); /** - * Return the collection of non-option arguments parsed from the command line. Never - * {@code null}. + * Return the collection of non-option arguments parsed from the command line. + * Never {@code null}. */ protected abstract List getNonOptionArgs(); diff --git a/spring-core/src/main/java/org/springframework/core/env/JOptCommandLinePropertySource.java b/spring-core/src/main/java/org/springframework/core/env/JOptCommandLinePropertySource.java index 154c34d623d..61ed06a6d03 100644 --- a/spring-core/src/main/java/org/springframework/core/env/JOptCommandLinePropertySource.java +++ b/spring-core/src/main/java/org/springframework/core/env/JOptCommandLinePropertySource.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2011 the original author or authors. + * Copyright 2002-2014 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -22,6 +22,8 @@ import java.util.List; import joptsimple.OptionSet; +import org.springframework.util.Assert; + /** * {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}. * @@ -41,12 +43,10 @@ import joptsimple.OptionSet; * * See {@link CommandLinePropertySource} for complete general usage examples. * - *

Requirements

- * - *

Use of this class requires adding the jopt-simple JAR to your application classpath. - * Versions 3.0 and better are supported. + *

Requires JOpt version 3.0 or higher. Tested against JOpt up until 4.6. * * @author Chris Beams + * @author Juergen Hoeller * @since 3.1 * @see CommandLinePropertySource * @see joptsimple.OptionParser @@ -72,6 +72,7 @@ public class JOptCommandLinePropertySource extends CommandLinePropertySource getOptionValues(String name) { List argValues = this.source.valuesOf(name); List stringArgValues = new ArrayList(); - for(Object argValue : argValues) { - if (!(argValue instanceof String)) { - throw new IllegalArgumentException("argument values must be of type String"); - } - stringArgValues.add((String)argValue); + for (Object argValue : argValues) { + Assert.isInstanceOf(String.class, argValue, "Argument values must be of type String"); + stringArgValues.add((String) argValue); } - if (stringArgValues.size() == 0) { - if (this.source.has(name)) { - return Collections.emptyList(); - } - else { - return null; - } + if (stringArgValues.isEmpty()) { + return (this.source.has(name) ? Collections.emptyList() : null); } return Collections.unmodifiableList(stringArgValues); } @Override protected List getNonOptionArgs() { - return this.source.nonOptionArguments(); + List argValues = this.source.nonOptionArguments(); + List stringArgValues = new ArrayList(); + for (Object argValue : argValues) { + Assert.isInstanceOf(String.class, argValue, "Argument values must be of type String"); + stringArgValues.add((String) argValue); + } + return (stringArgValues.isEmpty() ? Collections.emptyList() : + Collections.unmodifiableList(stringArgValues)); } }