You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2024/01/29 14:14:28 UTC

(commons-cli) branch master updated: Use generics to avoid defining a new method that throws Exception

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-cli.git


The following commit(s) were added to refs/heads/master by this push:
     new 9daa228  Use generics to avoid defining a new method that throws Exception
9daa228 is described below

commit 9daa228c9a87ab6e1ae705913f27bc850bd202db
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Mon Jan 29 09:14:23 2024 -0500

    Use generics to avoid defining a new method that throws Exception
    
    Javadoc
---
 .../java/org/apache/commons/cli/CommandLine.java   |  2 +-
 .../java/org/apache/commons/cli/Converter.java     | 46 ++++++++++++++--------
 src/main/java/org/apache/commons/cli/Option.java   | 10 ++---
 .../org/apache/commons/cli/ParseException.java     |  4 +-
 .../apache/commons/cli/PatternOptionBuilder.java   |  4 +-
 .../java/org/apache/commons/cli/TypeHandler.java   | 10 ++---
 6 files changed, 45 insertions(+), 31 deletions(-)

diff --git a/src/main/java/org/apache/commons/cli/CommandLine.java b/src/main/java/org/apache/commons/cli/CommandLine.java
index 0b6ff52..7f1bf48 100644
--- a/src/main/java/org/apache/commons/cli/CommandLine.java
+++ b/src/main/java/org/apache/commons/cli/CommandLine.java
@@ -446,7 +446,7 @@ public class CommandLine implements Serializable {
 
         try {
             return res == null ? defaultValue : (T) option.getConverter().apply(res);
-        } catch (final Exception e) {
+        } catch (final Throwable e) {
             throw ParseException.wrap(e);
         }
     }
diff --git a/src/main/java/org/apache/commons/cli/Converter.java b/src/main/java/org/apache/commons/cli/Converter.java
index d0d202c..b915689 100644
--- a/src/main/java/org/apache/commons/cli/Converter.java
+++ b/src/main/java/org/apache/commons/cli/Converter.java
@@ -17,7 +17,9 @@
 package org.apache.commons.cli;
 
 import java.io.File;
+import java.net.MalformedURLException;
 import java.net.URL;
+import java.nio.file.InvalidPathException;
 import java.nio.file.Path;
 import java.text.SimpleDateFormat;
 import java.util.Date;
@@ -27,28 +29,36 @@ import java.util.Date;
  * Like {@code Function<String,T>} but can throw an Exception.
  *
  * @param <T> The return type for the function.
+ * @param <E> The kind of thrown exception or error.
  * @since 1.7.0
  */
 @FunctionalInterface
-public interface Converter<T> {
+public interface Converter<T, E extends Throwable> {
+    // See also Apache Commons Lang FailableFunction
 
-    /** The default converter. Does nothing. */
-    Converter<?> DEFAULT = s -> s;
+    /**
+     * The default converter. Does nothing.
+     */
+    Converter<?, RuntimeException> DEFAULT = s -> s;
 
-    /** Class name converter. Calls {@code Class.forName}. */
-    Converter<Class<?>> CLASS = Class::forName;
+    /**
+     * Class name converter. Calls {@code Class.forName}.
+     */
+    Converter<Class<?>, ClassNotFoundException> CLASS = Class::forName;
 
-    /** File name converter. Calls @{code new File(s)} */
-    Converter<File> FILE = File::new;
+    /**
+     * File name converter. Calls @{code new File(s)}.
+     */
+    Converter<File, NullPointerException> FILE = File::new;
 
     /** Path converter. Calls @{code new Path(s)} */
-    Converter<Path> PATH = s -> new File(s).toPath();
+    Converter<Path, InvalidPathException> PATH = s -> new File(s).toPath();
 
     /**
      * Number converter. Converts to a Double if a decimal point ('.') is in the
      * string or a Long otherwise.
      */
-    Converter<Number> NUMBER = s -> {
+    Converter<Number, NumberFormatException> NUMBER = s -> {
         if (s.indexOf('.') != -1) {
             return Double.valueOf(s);
         }
@@ -60,19 +70,23 @@ public interface Converter<T> {
      * to find the class and then call the default constructor.
      * @see #CLASS
      */
-    Converter<Object> OBJECT = s -> CLASS.apply(s).getConstructor().newInstance();
+    Converter<Object, ReflectiveOperationException> OBJECT = s -> CLASS.apply(s).getConstructor().newInstance();
 
-    /** Creates a URL. Calls {@code new URL(s)}. */
-    Converter<URL> URL = java.net.URL::new;
+    /** 
+     * Creates a URL. Calls {@code new URL(s)}.
+     */
+    Converter<URL, MalformedURLException> URL = java.net.URL::new;
 
-    /** Converts to a date using the format string Form "EEE MMM dd HH:mm:ss zzz yyyy". */
-    Converter<Date> DATE = s -> new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(s);
+    /**
+     * Converts to a date using the format string Form "EEE MMM dd HH:mm:ss zzz yyyy".
+     */
+    Converter<Date, java.text.ParseException> DATE = s -> new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(s);
 
     /**
      * Applies the conversion function to the String argument.
      * @param  str       the String to convert
      * @return           the Object from the conversion.
-     * @throws Exception on error.
+     * @throws E on error.
      */
-    T apply(String str) throws Exception;
+    T apply(String str) throws E;
 }
diff --git a/src/main/java/org/apache/commons/cli/Option.java b/src/main/java/org/apache/commons/cli/Option.java
index 32fa1c3..1904809 100644
--- a/src/main/java/org/apache/commons/cli/Option.java
+++ b/src/main/java/org/apache/commons/cli/Option.java
@@ -84,7 +84,7 @@ public class Option implements Cloneable, Serializable {
         private char valueSeparator;
 
         /** The converter to convert to type **/
-        private Converter<?> converter;
+        private Converter<?, ?> converter;
 
         /**
          * Constructs a new {@code Builder} with the minimum required parameters for an {@code Option} instance.
@@ -127,7 +127,7 @@ public class Option implements Cloneable, Serializable {
          * @return this builder, to allow method chaining.
          * @since 1.7.0
          */
-        public Builder converter(final Converter<?> converter) {
+        public Builder converter(final Converter<?, ?> converter) {
             this.converter = converter;
             return this;
         }
@@ -353,7 +353,7 @@ public class Option implements Cloneable, Serializable {
     private char valuesep;
 
     /** The explicit converter for this option.  May be null */
-    private transient Converter<?> converter;
+    private transient Converter<?, ?> converter;
 
     /**
      * Private constructor used by the nested Builder class.
@@ -547,7 +547,7 @@ public class Option implements Cloneable, Serializable {
      * @return the value to type converter
      * @since 1.7.0
      */
-    public Converter<?> getConverter() {
+    public Converter<?, ?> getConverter() {
         return converter == null ? TypeHandler.getConverter(type) : converter;
     }
 
@@ -830,7 +830,7 @@ public class Option implements Cloneable, Serializable {
      * @param converter The converter to convert the string value to the type.
      * @since 1.7.0
      */
-    public void setConverter(final Converter<?> converter) {
+    public void setConverter(final Converter<?, ?> converter) {
         this.converter = converter;
     }
 
diff --git a/src/main/java/org/apache/commons/cli/ParseException.java b/src/main/java/org/apache/commons/cli/ParseException.java
index 0e8d250..25e4596 100644
--- a/src/main/java/org/apache/commons/cli/ParseException.java
+++ b/src/main/java/org/apache/commons/cli/ParseException.java
@@ -39,7 +39,7 @@ public class ParseException extends Exception {
      * @throws UnsupportedOperationException due to legacy expectations.  Will be removed in the future.
      * @since 1.7.0
      */
-    public static ParseException wrap(final Exception e) throws UnsupportedOperationException {
+    public static ParseException wrap(final Throwable e) throws UnsupportedOperationException {
         if (e instanceof UnsupportedOperationException) {
             throw (UnsupportedOperationException) e;
         }
@@ -53,7 +53,7 @@ public class ParseException extends Exception {
      * Constructs a new {@code ParseException} wrapping the specified exception.
      * @param e the Exception to wrap.
      */
-    public ParseException(final Exception e) {
+    public ParseException(final Throwable e) {
         super(e);
     }
 
diff --git a/src/main/java/org/apache/commons/cli/PatternOptionBuilder.java b/src/main/java/org/apache/commons/cli/PatternOptionBuilder.java
index ca194d2..19abbd3 100644
--- a/src/main/java/org/apache/commons/cli/PatternOptionBuilder.java
+++ b/src/main/java/org/apache/commons/cli/PatternOptionBuilder.java
@@ -104,7 +104,7 @@ public class PatternOptionBuilder {
     public static final Class<URL> URL_VALUE = URL.class;
 
     /** The converter to use for Unimplemented data types */
-    static final Converter<?> NOT_IMPLEMENTED = s -> {
+    static final Converter<?, UnsupportedOperationException> NOT_IMPLEMENTED = s -> {
         throw new UnsupportedOperationException("Not yet implemented");
     };
 
@@ -176,7 +176,7 @@ public class PatternOptionBuilder {
         char opt = ' ';
         boolean required = false;
         Class<?> type = null;
-        Converter<?> converter = Converter.DEFAULT;
+        Converter<?, ?> converter = Converter.DEFAULT;
 
         final Options options = new Options();
 
diff --git a/src/main/java/org/apache/commons/cli/TypeHandler.java b/src/main/java/org/apache/commons/cli/TypeHandler.java
index 9fccffa..4184daf 100644
--- a/src/main/java/org/apache/commons/cli/TypeHandler.java
+++ b/src/main/java/org/apache/commons/cli/TypeHandler.java
@@ -45,7 +45,7 @@ public class TypeHandler {
     private static final int HEX_RADIX = 16;
 
     /** Map of classes to converters. */
-    private static Map<Class<?>, Converter<?>> converterMap = new HashMap<>();
+    private static Map<Class<?>, Converter<?, ?>> converterMap = new HashMap<>();
 
     static {
         resetConverters();
@@ -166,7 +166,7 @@ public class TypeHandler {
     public static <T> T createValue(final String str, final Class<T> clazz) throws ParseException {
         try {
             return (T) getConverter(clazz).apply(str);
-        } catch (final Exception e) {
+        } catch (final Throwable e) {
             throw ParseException.wrap(e);
         }
     }
@@ -191,8 +191,8 @@ public class TypeHandler {
      * @return the registered converter if any, {@link Converter#DEFAULT} otherwise.
      * @since 1.7.0
      */
-    public static Converter<?> getConverter(final Class<?> clazz) {
-        final Converter<?> converter = converterMap.get(clazz);
+    public static Converter<?, ?> getConverter(final Class<?> clazz) {
+        final Converter<?, ?> converter = converterMap.get(clazz);
         return converter == null ? Converter.DEFAULT : converter;
     }
 
@@ -225,7 +225,7 @@ public class TypeHandler {
      * @param converter The Converter to associate with Class.  May be null.
      * @since 1.7.0
      */
-    public static void register(final Class<?> clazz, final Converter<?> converter) {
+    public static void register(final Class<?> clazz, final Converter<?, ?> converter) {
         if (converter == null) {
             converterMap.remove(clazz);
         } else {