Compatibility with JOpt 4.6

JOpt 4.6 redeclared its nonOptionArguments() method from List<String> 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)
This commit is contained in:
Juergen Hoeller 2014-01-26 00:05:51 +01:00
parent 6162917b4e
commit 1a2033eb41
2 changed files with 32 additions and 27 deletions

View File

@ -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<T> extends PropertySource<T> {
/** 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<T> extends PropertySource<T> {
}
}
/**
* 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<T> extends PropertySource<T> {
protected abstract List<String> 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<String> getNonOptionArgs();

View File

@ -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.
*
* <h3>Requirements</h3>
*
* <p>Use of this class requires adding the jopt-simple JAR to your application classpath.
* Versions 3.0 and better are supported.
* <p>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<Opt
super(name, options);
}
@Override
protected boolean containsOption(String name) {
return this.source.has(name);
@ -81,26 +82,26 @@ public class JOptCommandLinePropertySource extends CommandLinePropertySource<Opt
public List<String> getOptionValues(String name) {
List<?> argValues = this.source.valuesOf(name);
List<String> stringArgValues = new ArrayList<String>();
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.<String>emptyList() : null);
}
return Collections.unmodifiableList(stringArgValues);
}
@Override
protected List<String> getNonOptionArgs() {
return this.source.nonOptionArguments();
List<?> argValues = this.source.nonOptionArguments();
List<String> stringArgValues = new ArrayList<String>();
for (Object argValue : argValues) {
Assert.isInstanceOf(String.class, argValue, "Argument values must be of type String");
stringArgValues.add((String) argValue);
}
return (stringArgValues.isEmpty() ? Collections.<String>emptyList() :
Collections.unmodifiableList(stringArgValues));
}
}