Skip to content

Commit 1a2033e

Browse files
committed
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)
1 parent 6162917 commit 1a2033e

File tree

2 files changed

+32
-27
lines changed

2 files changed

+32
-27
lines changed

spring-core/src/main/java/org/springframework/core/env/CommandLinePropertySource.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -193,27 +193,30 @@ public abstract class CommandLinePropertySource<T> extends PropertySource<T> {
193193
/** The default name of the property representing non-option arguments: {@value} */
194194
public static final String DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME = "nonOptionArgs";
195195

196+
196197
private String nonOptionArgsPropertyName = DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME;
197198

199+
198200
/**
199-
* Create a new {@code CommandLinePropertySource} having the default name {@value
200-
* #COMMAND_LINE_PROPERTY_SOURCE_NAME} and backed by the given source object.
201+
* Create a new {@code CommandLinePropertySource} having the default name
202+
* {@value #COMMAND_LINE_PROPERTY_SOURCE_NAME} and backed by the given source object.
201203
*/
202204
public CommandLinePropertySource(T source) {
203205
super(COMMAND_LINE_PROPERTY_SOURCE_NAME, source);
204206
}
205207

206208
/**
207-
* Create a new {@link CommandLinePropertySource} having the given name and backed by
208-
* the given source object.
209+
* Create a new {@link CommandLinePropertySource} having the given name
210+
* and backed by the given source object.
209211
*/
210212
public CommandLinePropertySource(String name, T source) {
211213
super(name, source);
212214
}
213215

216+
214217
/**
215-
* Specify the name of the special "non-option arguments" property. The default is
216-
* {@value #DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME}.
218+
* Specify the name of the special "non-option arguments" property.
219+
* The default is {@value #DEFAULT_NON_OPTION_ARGS_PROPERTY_NAME}.
217220
*/
218221
public void setNonOptionArgsPropertyName(String nonOptionArgsPropertyName) {
219222
this.nonOptionArgsPropertyName = nonOptionArgsPropertyName;
@@ -265,6 +268,7 @@ public final String getProperty(String name) {
265268
}
266269
}
267270

271+
268272
/**
269273
* Return whether the set of option arguments parsed from the command line contains
270274
* an option with the given name.
@@ -288,8 +292,8 @@ public final String getProperty(String name) {
288292
protected abstract List<String> getOptionValues(String name);
289293

290294
/**
291-
* Return the collection of non-option arguments parsed from the command line. Never
292-
* {@code null}.
295+
* Return the collection of non-option arguments parsed from the command line.
296+
* Never {@code null}.
293297
*/
294298
protected abstract List<String> getNonOptionArgs();
295299

spring-core/src/main/java/org/springframework/core/env/JOptCommandLinePropertySource.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2011 the original author or authors.
2+
* Copyright 2002-2014 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,8 @@
2222

2323
import joptsimple.OptionSet;
2424

25+
import org.springframework.util.Assert;
26+
2527
/**
2628
* {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}.
2729
*
@@ -41,12 +43,10 @@
4143
*
4244
* See {@link CommandLinePropertySource} for complete general usage examples.
4345
*
44-
* <h3>Requirements</h3>
45-
*
46-
* <p>Use of this class requires adding the jopt-simple JAR to your application classpath.
47-
* Versions 3.0 and better are supported.
46+
* <p>Requires JOpt version 3.0 or higher. Tested against JOpt up until 4.6.
4847
*
4948
* @author Chris Beams
49+
* @author Juergen Hoeller
5050
* @since 3.1
5151
* @see CommandLinePropertySource
5252
* @see joptsimple.OptionParser
@@ -72,6 +72,7 @@ public JOptCommandLinePropertySource(String name, OptionSet options) {
7272
super(name, options);
7373
}
7474

75+
7576
@Override
7677
protected boolean containsOption(String name) {
7778
return this.source.has(name);
@@ -81,26 +82,26 @@ protected boolean containsOption(String name) {
8182
public List<String> getOptionValues(String name) {
8283
List<?> argValues = this.source.valuesOf(name);
8384
List<String> stringArgValues = new ArrayList<String>();
84-
for(Object argValue : argValues) {
85-
if (!(argValue instanceof String)) {
86-
throw new IllegalArgumentException("argument values must be of type String");
87-
}
88-
stringArgValues.add((String)argValue);
85+
for (Object argValue : argValues) {
86+
Assert.isInstanceOf(String.class, argValue, "Argument values must be of type String");
87+
stringArgValues.add((String) argValue);
8988
}
90-
if (stringArgValues.size() == 0) {
91-
if (this.source.has(name)) {
92-
return Collections.emptyList();
93-
}
94-
else {
95-
return null;
96-
}
89+
if (stringArgValues.isEmpty()) {
90+
return (this.source.has(name) ? Collections.<String>emptyList() : null);
9791
}
9892
return Collections.unmodifiableList(stringArgValues);
9993
}
10094

10195
@Override
10296
protected List<String> getNonOptionArgs() {
103-
return this.source.nonOptionArguments();
97+
List<?> argValues = this.source.nonOptionArguments();
98+
List<String> stringArgValues = new ArrayList<String>();
99+
for (Object argValue : argValues) {
100+
Assert.isInstanceOf(String.class, argValue, "Argument values must be of type String");
101+
stringArgValues.add((String) argValue);
102+
}
103+
return (stringArgValues.isEmpty() ? Collections.<String>emptyList() :
104+
Collections.unmodifiableList(stringArgValues));
104105
}
105106

106107
}

0 commit comments

Comments
 (0)