Browse Source

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
pull/452/head
Juergen Hoeller 12 years ago
parent
commit
67e76e9a8d
  1. 4
      spring-core/src/main/java/org/springframework/core/env/CommandLinePropertySource.java
  2. 37
      spring-core/src/main/java/org/springframework/core/env/JOptCommandLinePropertySource.java

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

@ -288,8 +288,8 @@ public abstract class CommandLinePropertySource<T> extends EnumerablePropertySou
protected abstract List<String> getOptionValues(String name); protected abstract List<String> getOptionValues(String name);
/** /**
* Return the collection of non-option arguments parsed from the command line. Never * Return the collection of non-option arguments parsed from the command line.
* {@code null}. * Never {@code null}.
*/ */
protected abstract List<String> getNonOptionArgs(); protected abstract List<String> getNonOptionArgs();

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

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2014 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -23,6 +23,8 @@ import java.util.List;
import joptsimple.OptionSet; import joptsimple.OptionSet;
import joptsimple.OptionSpec; import joptsimple.OptionSpec;
import org.springframework.util.Assert;
/** /**
* {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}. * {@link CommandLinePropertySource} implementation backed by a JOpt {@link OptionSet}.
* *
@ -42,12 +44,10 @@ import joptsimple.OptionSpec;
* *
* See {@link CommandLinePropertySource} for complete general usage examples. * See {@link CommandLinePropertySource} for complete general usage examples.
* *
* <h3>Requirements</h3> * <p>Requires JOpt version 3.0 or higher. Tested against JOpt up until 4.6.
*
* <p>Use of this class requires adding the jopt-simple JAR to your application classpath.
* Versions 3.0 and better are supported.
* *
* @author Chris Beams * @author Chris Beams
* @author Juergen Hoeller
* @since 3.1 * @since 3.1
* @see CommandLinePropertySource * @see CommandLinePropertySource
* @see joptsimple.OptionParser * @see joptsimple.OptionParser
@ -73,6 +73,7 @@ public class JOptCommandLinePropertySource extends CommandLinePropertySource<Opt
super(name, options); super(name, options);
} }
@Override @Override
protected boolean containsOption(String name) { protected boolean containsOption(String name) {
return this.source.has(name); return this.source.has(name);
@ -95,26 +96,26 @@ public class JOptCommandLinePropertySource extends CommandLinePropertySource<Opt
public List<String> getOptionValues(String name) { public List<String> getOptionValues(String name) {
List<?> argValues = this.source.valuesOf(name); List<?> argValues = this.source.valuesOf(name);
List<String> stringArgValues = new ArrayList<String>(); List<String> stringArgValues = new ArrayList<String>();
for(Object argValue : argValues) { for (Object argValue : argValues) {
if (!(argValue instanceof String)) { Assert.isInstanceOf(String.class, argValue, "Argument values must be of type String");
throw new IllegalArgumentException("argument values must be of type String"); stringArgValues.add((String) argValue);
}
stringArgValues.add((String)argValue);
} }
if (stringArgValues.size() == 0) { if (stringArgValues.isEmpty()) {
if (this.source.has(name)) { return (this.source.has(name) ? Collections.<String>emptyList() : null);
return Collections.emptyList();
}
else {
return null;
}
} }
return Collections.unmodifiableList(stringArgValues); return Collections.unmodifiableList(stringArgValues);
} }
@Override @Override
protected List<String> getNonOptionArgs() { 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));
} }
} }

Loading…
Cancel
Save