2727/**
2828 * The default type converter
2929 * <p>
30- * This converter supports all the basic Java types plus types. Additionally it
31- * supports any class that defines a static {@code fromString(String)} or
32- * {@code valueOf(String)} method. Finally it supports any class that defines a
30+ * This converter supports all the basic Java types plus types. Additionally it supports any class that defines a static
31+ * {@code fromString(String)} or {@code valueOf(String)} method. Finally it supports any class that defines a
3332 * constructor that takes a string.
3433 * </p>
3534 */
36- public class DefaultTypeConverter extends DefaultTypeConverterProvider implements TypeConverter {
35+ public class DefaultTypeConverter extends DefaultTypeConverterProvider implements TypeConverter {
3736
3837 private NumericTypeConverter numericConverter ;
3938
39+ /**
40+ * Creates a new instance of the default type converter with the default {@link NumericTypeConverter} configured
41+ */
4042 public DefaultTypeConverter () {
4143 this (null );
4244 }
4345
46+ /**
47+ * Creates a new instance of the default type converter
48+ *
49+ * @param numericConverter Numeric type converter to use, if {@code null} then {@link DefaultNumericConverter} is
50+ * used
51+ */
4452 public DefaultTypeConverter (NumericTypeConverter numericConverter ) {
4553 this .numericConverter = numericConverter != null ? numericConverter : new DefaultNumericConverter ();
4654 }
@@ -51,88 +59,104 @@ public Object convert(String name, Class<?> type, String value) {
5159
5260 // Firstly try the standard Java types
5361 ConvertResult result = tryConvertBasicTypes (name , type , value );
54- if (result .wasSuccessful ())
62+ if (result .wasSuccessful ()) {
5563 return result .getConvertedValue ();
64+ }
5665
5766 // Then look for a static fromString(String) method
5867 result = tryConvertFromString (name , type , value );
59- if (result .wasSuccessful ())
68+ if (result .wasSuccessful ()) {
6069 return result .getConvertedValue ();
70+ }
6171
6272 // Then look for a static valueOf(String) method
6373 // This covers enums which have a valueOf method
6474 result = tryConvertFromValueOf (name , type , value );
65- if (result .wasSuccessful ())
75+ if (result .wasSuccessful ()) {
6676 return result .getConvertedValue ();
77+ }
6778
6879 // Finally look for a constructor taking a string
6980 result = tryConvertStringConstructor (name , type , value );
70- if (result .wasSuccessful ())
81+ if (result .wasSuccessful ()) {
7182 return result .getConvertedValue ();
83+ }
7284
7385 throw new ParseOptionConversionException (name , value , type .getSimpleName ());
7486 }
7587
7688 /**
7789 * Checks that the arguments are all non-null
78- *
79- * @param name
80- * Option/Argument name
81- * @param type
82- * Target type
83- * @param value
84- * String to convert
90+ *
91+ * @param name Option/Argument name
92+ * @param type Target type
93+ * @param value String to convert
8594 */
8695 public static void checkArguments (String name , Class <?> type , String value ) {
87- if (name == null )
96+ if (name == null ) {
8897 throw new NullPointerException ("name is null" );
89- if (type == null )
98+ }
99+ if (type == null ) {
90100 throw new NullPointerException ("type is null" );
91- if (value == null )
101+ }
102+ if (value == null ) {
92103 throw new NullPointerException ("value is null" );
104+ }
93105 }
94106
95107 /**
96- * Tries to convert the value by invoking a constructor that takes a string
97- * on the type
98- *
99- * @param type
100- * Type
101- * @param value
102- * value
108+ * Tries to convert the value by invoking a constructor that takes a string on the type
109+ * <p>
110+ * Considers two variants of the constructor, one which takes {@link String} as its parameter type and if that doesn't
111+ * exist one that takes {@link CharSequence} as it's parameter type
112+ * </p>
113+ *
114+ * @param type Type
115+ * @param value value
103116 * @return Conversion result
104117 */
105118 protected final ConvertResult tryConvertStringConstructor (String name , Class <?> type , String value ) {
119+ ConvertResult result = tryConvertConstructor (name , type , value , String .class );
120+ if (result .wasSuccessful ()) {
121+ return result ;
122+ } else {
123+ return tryConvertConstructor (name , type , value , CharSequence .class );
124+ }
125+ }
126+
127+ /**
128+ * Tries to convert the value by invoking a constructor that takes a string on the type
129+ *
130+ * @param type Type
131+ * @param value value
132+ * @return Conversion result
133+ */
134+ protected final <T > ConvertResult tryConvertConstructor (String name , Class <?> type , String value ,
135+ Class <T > parameterType ) {
106136 try {
107- Constructor <?> constructor = type .getConstructor (String . class );
137+ Constructor <?> constructor = type .getConstructor (parameterType );
108138 return new ConvertResult (constructor .newInstance (value ));
109139 } catch (Throwable ignored ) {
110140 }
111141 return ConvertResult .FAILURE ;
112142 }
113143
114144 /**
115- * Tries to convert the value by invoking a static {@code valueOf(String)}
116- * method on the type
117- *
118- * @param type
119- * Type
120- * @param value
121- * Value
145+ * Tries to convert the value by invoking a static {@code valueOf(String)} method on the type
146+ *
147+ * @param type Type
148+ * @param value Value
122149 * @return Conversion result
123150 */
124151 protected final ConvertResult tryConvertFromValueOf (String name , Class <?> type , String value ) {
125152 return tryConvertStringMethod (name , type , value , "valueOf" );
126153 }
127154
128155 /**
129- * Tries to convert the value by invoking a static
130- * {@code fromString(String)} method on the type
131- *
132- * @param type
133- * Type
134- * @param value
135- * Value
156+ * Tries to convert the value by invoking a static {@code fromString(String)} method on the type
157+ *
158+ * @param type Type
159+ * @param value Value
136160 * @return Conversion result
137161 */
138162 protected final ConvertResult tryConvertFromString (String name , Class <?> type , String value ) {
@@ -141,34 +165,53 @@ protected final ConvertResult tryConvertFromString(String name, Class<?> type, S
141165 }
142166
143167 /**
144- * Tries to convert the value by invoking a static method on the type
145- *
146- * @param type
147- * Type
148- * @param value
149- * Value
150- * @param methodName
151- * Name of the method to invoke
168+ * Tries to convert the value by invoking a static method on the
169+ * <p>
170+ * Considers two variants of the method, one which takes {@link String} as its parameter type and if that doesn't
171+ * exist one that takes {@link CharSequence} as it's parameter type
172+ * </p>
173+ *
174+ * @param type Type
175+ * @param value Value
176+ * @param methodName Name of the method to invoke
152177 * @return Conversion Result
153178 */
179+ @ SuppressWarnings ("unused" )
154180 protected final ConvertResult tryConvertStringMethod (String name , Class <?> type , String value , String methodName ) {
181+ ConvertResult result = tryConvertStringMethod (type , value , methodName , String .class );
182+ if (result .wasSuccessful ()) {
183+ return result ;
184+ } else {
185+ return tryConvertStringMethod (type , value , methodName , CharSequence .class );
186+ }
187+ }
188+
189+ /**
190+ * Tries to convert the value by invoking a static method on the type
191+ *
192+ * @param type Type
193+ * @param value Value
194+ * @param methodName Name of the method to invoke
195+ * @return Conversion Result
196+ */
197+ protected final ConvertResult tryConvertStringMethod (Class <?> type , String value ,
198+ String methodName , Class <?> parameterType ) {
155199 try {
156- Method method = type .getMethod (methodName , String . class );
200+ Method method = type .getMethod (methodName , parameterType );
157201 if (method .getReturnType ().isAssignableFrom (type )) {
158202 return new ConvertResult (method .invoke (null , value ));
159203 }
160204 } catch (Throwable ignored ) {
205+
161206 }
162207 return ConvertResult .FAILURE ;
163208 }
164209
165210 /**
166211 * Tries to convert the value if it is one of the common Java types
167- *
168- * @param type
169- * Type
170- * @param value
171- * Value
212+ *
213+ * @param type Type
214+ * @param value Value
172215 * @return Conversion result
173216 */
174217 protected final ConvertResult tryConvertBasicTypes (String name , Class <?> type , String value ) {
0 commit comments