Skip to content

Commit 9daa228

Browse files
committed
Use generics to avoid defining a new method that throws Exception
Javadoc
1 parent 935964f commit 9daa228

6 files changed

Lines changed: 45 additions & 31 deletions

File tree

src/main/java/org/apache/commons/cli/CommandLine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ public <T> T getParsedOptionValue(final Option option, final T defaultValue) thr
446446

447447
try {
448448
return res == null ? defaultValue : (T) option.getConverter().apply(res);
449-
} catch (final Exception e) {
449+
} catch (final Throwable e) {
450450
throw ParseException.wrap(e);
451451
}
452452
}

src/main/java/org/apache/commons/cli/Converter.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ Licensed to the Apache Software Foundation (ASF) under one or more
1717
package org.apache.commons.cli;
1818

1919
import java.io.File;
20+
import java.net.MalformedURLException;
2021
import java.net.URL;
22+
import java.nio.file.InvalidPathException;
2123
import java.nio.file.Path;
2224
import java.text.SimpleDateFormat;
2325
import java.util.Date;
@@ -27,28 +29,36 @@ Licensed to the Apache Software Foundation (ASF) under one or more
2729
* Like {@code Function<String,T>} but can throw an Exception.
2830
*
2931
* @param <T> The return type for the function.
32+
* @param <E> The kind of thrown exception or error.
3033
* @since 1.7.0
3134
*/
3235
@FunctionalInterface
33-
public interface Converter<T> {
36+
public interface Converter<T, E extends Throwable> {
37+
// See also Apache Commons Lang FailableFunction
3438

35-
/** The default converter. Does nothing. */
36-
Converter<?> DEFAULT = s -> s;
39+
/**
40+
* The default converter. Does nothing.
41+
*/
42+
Converter<?, RuntimeException> DEFAULT = s -> s;
3743

38-
/** Class name converter. Calls {@code Class.forName}. */
39-
Converter<Class<?>> CLASS = Class::forName;
44+
/**
45+
* Class name converter. Calls {@code Class.forName}.
46+
*/
47+
Converter<Class<?>, ClassNotFoundException> CLASS = Class::forName;
4048

41-
/** File name converter. Calls @{code new File(s)} */
42-
Converter<File> FILE = File::new;
49+
/**
50+
* File name converter. Calls @{code new File(s)}.
51+
*/
52+
Converter<File, NullPointerException> FILE = File::new;
4353

4454
/** Path converter. Calls @{code new Path(s)} */
45-
Converter<Path> PATH = s -> new File(s).toPath();
55+
Converter<Path, InvalidPathException> PATH = s -> new File(s).toPath();
4656

4757
/**
4858
* Number converter. Converts to a Double if a decimal point ('.') is in the
4959
* string or a Long otherwise.
5060
*/
51-
Converter<Number> NUMBER = s -> {
61+
Converter<Number, NumberFormatException> NUMBER = s -> {
5262
if (s.indexOf('.') != -1) {
5363
return Double.valueOf(s);
5464
}
@@ -60,19 +70,23 @@ public interface Converter<T> {
6070
* to find the class and then call the default constructor.
6171
* @see #CLASS
6272
*/
63-
Converter<Object> OBJECT = s -> CLASS.apply(s).getConstructor().newInstance();
73+
Converter<Object, ReflectiveOperationException> OBJECT = s -> CLASS.apply(s).getConstructor().newInstance();
6474

65-
/** Creates a URL. Calls {@code new URL(s)}. */
66-
Converter<URL> URL = java.net.URL::new;
75+
/**
76+
* Creates a URL. Calls {@code new URL(s)}.
77+
*/
78+
Converter<URL, MalformedURLException> URL = java.net.URL::new;
6779

68-
/** Converts to a date using the format string Form "EEE MMM dd HH:mm:ss zzz yyyy". */
69-
Converter<Date> DATE = s -> new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(s);
80+
/**
81+
* Converts to a date using the format string Form "EEE MMM dd HH:mm:ss zzz yyyy".
82+
*/
83+
Converter<Date, java.text.ParseException> DATE = s -> new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(s);
7084

7185
/**
7286
* Applies the conversion function to the String argument.
7387
* @param str the String to convert
7488
* @return the Object from the conversion.
75-
* @throws Exception on error.
89+
* @throws E on error.
7690
*/
77-
T apply(String str) throws Exception;
91+
T apply(String str) throws E;
7892
}

src/main/java/org/apache/commons/cli/Option.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static final class Builder {
8484
private char valueSeparator;
8585

8686
/** The converter to convert to type **/
87-
private Converter<?> converter;
87+
private Converter<?, ?> converter;
8888

8989
/**
9090
* Constructs a new {@code Builder} with the minimum required parameters for an {@code Option} instance.
@@ -127,7 +127,7 @@ public Option build() {
127127
* @return this builder, to allow method chaining.
128128
* @since 1.7.0
129129
*/
130-
public Builder converter(final Converter<?> converter) {
130+
public Builder converter(final Converter<?, ?> converter) {
131131
this.converter = converter;
132132
return this;
133133
}
@@ -353,7 +353,7 @@ public static Builder builder(final String option) {
353353
private char valuesep;
354354

355355
/** The explicit converter for this option. May be null */
356-
private transient Converter<?> converter;
356+
private transient Converter<?, ?> converter;
357357

358358
/**
359359
* Private constructor used by the nested Builder class.
@@ -547,7 +547,7 @@ public int getArgs() {
547547
* @return the value to type converter
548548
* @since 1.7.0
549549
*/
550-
public Converter<?> getConverter() {
550+
public Converter<?, ?> getConverter() {
551551
return converter == null ? TypeHandler.getConverter(type) : converter;
552552
}
553553

@@ -830,7 +830,7 @@ public void setArgs(final int num) {
830830
* @param converter The converter to convert the string value to the type.
831831
* @since 1.7.0
832832
*/
833-
public void setConverter(final Converter<?> converter) {
833+
public void setConverter(final Converter<?, ?> converter) {
834834
this.converter = converter;
835835
}
836836

src/main/java/org/apache/commons/cli/ParseException.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public class ParseException extends Exception {
3939
* @throws UnsupportedOperationException due to legacy expectations. Will be removed in the future.
4040
* @since 1.7.0
4141
*/
42-
public static ParseException wrap(final Exception e) throws UnsupportedOperationException {
42+
public static ParseException wrap(final Throwable e) throws UnsupportedOperationException {
4343
if (e instanceof UnsupportedOperationException) {
4444
throw (UnsupportedOperationException) e;
4545
}
@@ -53,7 +53,7 @@ public static ParseException wrap(final Exception e) throws UnsupportedOperation
5353
* Constructs a new {@code ParseException} wrapping the specified exception.
5454
* @param e the Exception to wrap.
5555
*/
56-
public ParseException(final Exception e) {
56+
public ParseException(final Throwable e) {
5757
super(e);
5858
}
5959

src/main/java/org/apache/commons/cli/PatternOptionBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public class PatternOptionBuilder {
104104
public static final Class<URL> URL_VALUE = URL.class;
105105

106106
/** The converter to use for Unimplemented data types */
107-
static final Converter<?> NOT_IMPLEMENTED = s -> {
107+
static final Converter<?, UnsupportedOperationException> NOT_IMPLEMENTED = s -> {
108108
throw new UnsupportedOperationException("Not yet implemented");
109109
};
110110

@@ -176,7 +176,7 @@ public static Options parsePattern(final String pattern) {
176176
char opt = ' ';
177177
boolean required = false;
178178
Class<?> type = null;
179-
Converter<?> converter = Converter.DEFAULT;
179+
Converter<?, ?> converter = Converter.DEFAULT;
180180

181181
final Options options = new Options();
182182

src/main/java/org/apache/commons/cli/TypeHandler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class TypeHandler {
4545
private static final int HEX_RADIX = 16;
4646

4747
/** Map of classes to converters. */
48-
private static Map<Class<?>, Converter<?>> converterMap = new HashMap<>();
48+
private static Map<Class<?>, Converter<?, ?>> converterMap = new HashMap<>();
4949

5050
static {
5151
resetConverters();
@@ -166,7 +166,7 @@ public static URL createURL(final String str) throws ParseException {
166166
public static <T> T createValue(final String str, final Class<T> clazz) throws ParseException {
167167
try {
168168
return (T) getConverter(clazz).apply(str);
169-
} catch (final Exception e) {
169+
} catch (final Throwable e) {
170170
throw ParseException.wrap(e);
171171
}
172172
}
@@ -191,8 +191,8 @@ public static Object createValue(final String str, final Object obj) throws Pars
191191
* @return the registered converter if any, {@link Converter#DEFAULT} otherwise.
192192
* @since 1.7.0
193193
*/
194-
public static Converter<?> getConverter(final Class<?> clazz) {
195-
final Converter<?> converter = converterMap.get(clazz);
194+
public static Converter<?, ?> getConverter(final Class<?> clazz) {
195+
final Converter<?, ?> converter = converterMap.get(clazz);
196196
return converter == null ? Converter.DEFAULT : converter;
197197
}
198198

@@ -225,7 +225,7 @@ public static FileInputStream openFile(final String str) throws ParseException {
225225
* @param converter The Converter to associate with Class. May be null.
226226
* @since 1.7.0
227227
*/
228-
public static void register(final Class<?> clazz, final Converter<?> converter) {
228+
public static void register(final Class<?> clazz, final Converter<?, ?> converter) {
229229
if (converter == null) {
230230
converterMap.remove(clazz);
231231
} else {

0 commit comments

Comments
 (0)