Skip to content

Commit 8d044b0

Browse files
committed
Add and use ConversionException.ConfigurationException(String,
Object...). Add and use ConversionException.ConfigurationException(Throwable, String, Object...)
1 parent 7d5c3a0 commit 8d044b0

4 files changed

Lines changed: 68 additions & 45 deletions

File tree

src/changes/changes.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
<action type="add" dev="ggregory" due-to="Gary Gregory">Add XMLConfiguration.read(Element).</action>
3232
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ConfigurationException.ConfigurationException(String, Object...).</action>
3333
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ConfigurationException.ConfigurationException(Throwable, String, Object...).</action>
34+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ConversionException.ConfigurationException(String, Object...).</action>
35+
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ConversionException.ConfigurationException(Throwable, String, Object...).</action>
3436
<!-- UPDATE -->
3537
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 92 to 97.</action>
3638
<action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-text from 1.14.0 to 1.15.0.</action>

src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,8 @@ private <T> T getAndConvertProperty(final Class<T> cls, final String key, final
624624
return ObjectUtils.getIfNull(getConversionHandler().to(value, cls, getInterpolator()), defaultValue);
625625
} catch (final ConversionException cex) {
626626
// improve error message
627-
throw new ConversionException(String.format("Key '%s' cannot be converted to class %s. Value is: '%s'.", key, cls.getName(), String.valueOf(value)),
628-
cex.getCause());
627+
throw new ConversionException(cex.getCause(), "Key '%s' cannot be converted to class %s. Value is: '%s'.", key, cls.getName(),
628+
String.valueOf(value));
629629
}
630630
}
631631

@@ -982,7 +982,7 @@ public List<Object> getList(final String key, final List<?> defaultValue) {
982982
} else if (isScalarValue(value)) {
983983
return Collections.singletonList((Object) value.toString());
984984
} else {
985-
throw new ConversionException('\'' + key + "' doesn't map to a List object: " + value + ", a " + value.getClass().getName());
985+
throw new ConversionException("'%s' doesn't map to a List object: %s, a %s", key, value, value.getClass().getName());
986986
}
987987
return list;
988988
}

src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java

Lines changed: 38 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,7 @@ public static Object to(final Class<?> cls, final Object value, final DefaultCon
185185
if (Duration.class.equals(cls)) {
186186
return toDuration(value);
187187
}
188-
189-
throw new ConversionException("The value '" + value + "' (" + value.getClass() + ") can't be converted to a " + cls.getName() + " object");
188+
throw new ConversionException("The value '%s' (%s) can't be converted to a %s object", value, value.getClass(), cls.getName());
190189
}
191190

192191
/**
@@ -234,11 +233,11 @@ public static Boolean toBoolean(final Object value) throws ConversionException {
234233
return (Boolean) value;
235234
}
236235
if (!(value instanceof String)) {
237-
throw new ConversionException("The value " + value + " can't be converted to a Boolean object");
236+
throw new ConversionException("The value %s can't be converted to a Boolean object", value);
238237
}
239238
final Boolean b = BooleanUtils.toBooleanObject((String) value);
240239
if (b == null) {
241-
throw new ConversionException("The value " + value + " can't be converted to a Boolean object");
240+
throw new ConversionException("The value %s can't be converted to a Boolean object", value);
242241
}
243242
return b;
244243
}
@@ -276,14 +275,14 @@ public static Calendar toCalendar(final Object value, final String format) throw
276275
return calendar;
277276
}
278277
if (!(value instanceof String)) {
279-
throw new ConversionException("The value " + value + " can't be converted to a Calendar");
278+
throw new ConversionException("The value %s can't be converted to a Calendar", value);
280279
}
281280
try {
282281
final Calendar calendar = Calendar.getInstance();
283282
calendar.setTime(new SimpleDateFormat(format).parse((String) value));
284283
return calendar;
285284
} catch (final ParseException e) {
286-
throw new ConversionException("The value " + value + " can't be converted to a Calendar", e);
285+
throw new ConversionException(e, "The value %s can't be converted to a Calendar", value);
287286
}
288287
}
289288

@@ -300,7 +299,7 @@ public static Character toCharacter(final Object value) throws ConversionExcepti
300299
if (strValue.length() == 1) {
301300
return Character.valueOf(strValue.charAt(0));
302301
}
303-
throw new ConversionException(String.format("The value '%s' cannot be converted to a Character object!", strValue));
302+
throw new ConversionException("The value '%s' cannot be converted to a Character object!", strValue);
304303
}
305304

306305
/**
@@ -322,7 +321,7 @@ public static Color toColor(final Object value) throws ConversionException {
322321
return (Color) value;
323322
}
324323
if (!(value instanceof String) || StringUtils.isBlank((String) value)) {
325-
throw new ConversionException("The value " + value + " can't be converted to a Color");
324+
throw new ConversionException("The value %s can't be converted to a Color", value);
326325
}
327326
String color = ((String) value).trim();
328327

@@ -331,7 +330,7 @@ public static Color toColor(final Object value) throws ConversionException {
331330
// check the size of the string
332331
final int minlength = components.length * 2;
333332
if (color.length() < minlength) {
334-
throw new ConversionException("The value " + value + " can't be converted to a Color");
333+
throw new ConversionException("The value %s can't be converted to a Color", value);
335334
}
336335

337336
// remove the leading #
@@ -355,7 +354,7 @@ public static Color toColor(final Object value) throws ConversionException {
355354

356355
return new Color(components[0], components[1], components[2], alpha);
357356
} catch (final Exception e) {
358-
throw new ConversionException("The value " + value + " can't be converted to a Color", e);
357+
throw new ConversionException(e, "The value %s can't be converted to a Color", value);
359358
}
360359
}
361360

@@ -375,12 +374,12 @@ public static Date toDate(final Object value, final String format) throws Conver
375374
return ((Calendar) value).getTime();
376375
}
377376
if (!(value instanceof String)) {
378-
throw new ConversionException("The value " + value + " can't be converted to a Date");
377+
throw new ConversionException("The value %s can't be converted to a Date", value);
379378
}
380379
try {
381380
return new SimpleDateFormat(format).parse((String) value);
382381
} catch (final ParseException e) {
383-
throw new ConversionException("The value " + value + " can't be converted to a Date", e);
382+
throw new ConversionException(e, "The value %s can't be converted to a Date", value);
384383
}
385384
}
386385

@@ -415,10 +414,10 @@ public static Duration toDuration(final Object value) throws ConversionException
415414
try {
416415
return Duration.parse((CharSequence) value);
417416
} catch (final DateTimeParseException e) {
418-
throw new ConversionException("Could not convert " + value + " to Duration", e);
417+
throw new ConversionException(e, "Could not convert %s to Duration", value);
419418
}
420419
}
421-
throw new ConversionException("The value " + value + " can't be converted to a Duration");
420+
throw new ConversionException("The value %s can't be converted to a Duration", value);
422421
}
423422

424423
/**
@@ -438,17 +437,17 @@ static <E extends Enum<E>> E toEnum(final Object value, final Class<E> cls) thro
438437
try {
439438
return Enum.valueOf(cls, (String) value);
440439
} catch (final Exception e) {
441-
throw new ConversionException("The value " + value + " can't be converted to a " + cls.getName());
440+
throw new ConversionException(e, "The value %s can't be converted to a %s", value, cls.getName());
442441
}
443442
}
444443
if (!(value instanceof Number)) {
445-
throw new ConversionException("The value " + value + " can't be converted to a " + cls.getName());
444+
throw new ConversionException("The value %s can't be converted to a ", value, cls.getName());
446445
}
447446
try {
448447
final E[] enumConstants = cls.getEnumConstants();
449448
return enumConstants[((Number) value).intValue()];
450449
} catch (final Exception e) {
451-
throw new ConversionException("The value " + value + " can't be converted to a " + cls.getName());
450+
throw new ConversionException(e, "The value %s can't be converted to a ", value, cls.getName());
452451
}
453452
}
454453

@@ -470,7 +469,7 @@ public static File toFile(final Object value) throws ConversionException {
470469
if (value instanceof String) {
471470
return new File((String) value);
472471
}
473-
throw new ConversionException("The value " + value + " can't be converted to a File");
472+
throw new ConversionException("The value %s can't be converted to a File", value);
474473
}
475474

476475
/**
@@ -501,12 +500,12 @@ static InetAddress toInetAddress(final Object value) throws ConversionException
501500
return (InetAddress) value;
502501
}
503502
if (!(value instanceof String)) {
504-
throw new ConversionException("The value " + value + " can't be converted to a InetAddress");
503+
throw new ConversionException("The value %s can't be converted to a InetAddress", value);
505504
}
506505
try {
507506
return InetAddress.getByName((String) value);
508507
} catch (final UnknownHostException e) {
509-
throw new ConversionException("The value " + value + " can't be converted to a InetAddress", e);
508+
throw new ConversionException(e, "The value %s can't be converted to a InetAddress", value);
510509
}
511510
}
512511

@@ -540,13 +539,13 @@ static Object toInternetAddress(final Object value, final String targetClassName
540539
return value;
541540
}
542541
if (!(value instanceof String)) {
543-
throw new ConversionException("The value " + value + " can't be converted to an InternetAddress");
542+
throw new ConversionException("The value %s can't be converted to an InternetAddress", value);
544543
}
545544
try {
546545
final Constructor<?> ctor = Class.forName(targetClassName).getConstructor(String.class);
547546
return ctor.newInstance(value);
548547
} catch (final Exception e) {
549-
throw new ConversionException("The value " + value + " can't be converted to an InternetAddress", e);
548+
throw new ConversionException(e, "The value %s can't be converted to an InternetAddress", value);
550549
}
551550
}
552551

@@ -562,7 +561,7 @@ public static Locale toLocale(final Object value) throws ConversionException {
562561
return (Locale) value;
563562
}
564563
if (!(value instanceof String)) {
565-
throw new ConversionException("The value " + value + " can't be converted to a Locale");
564+
throw new ConversionException("The value %s can't be converted to a Locale", value);
566565
}
567566
final String[] elements = ((String) value).split("_");
568567
final int size = elements.length;
@@ -574,7 +573,7 @@ public static Locale toLocale(final Object value) throws ConversionException {
574573

575574
return new Locale(language, country, variant);
576575
}
577-
throw new ConversionException("The value " + value + " can't be converted to a Locale");
576+
throw new ConversionException("The value %s can't be converted to a Locale", value);
578577
}
579578

580579
/**
@@ -610,27 +609,25 @@ static Number toNumber(final Object value, final Class<?> targetClass) throws Co
610609
if (Strings.CS.startsWithAny(str, HEX_PREFIX)) {
611610
try {
612611
return new BigInteger(str.substring(HEX_PREFIX.length()), HEX_RADIX);
613-
} catch (final NumberFormatException nex) {
614-
throw new ConversionException("Could not convert " + str + " to " + targetClass.getName() + "! Invalid hex number.", nex);
612+
} catch (final NumberFormatException e) {
613+
throw new ConversionException(e, "Could not convert %s to %s! Invalid hex number.", str, targetClass.getName());
615614
}
616615
}
617-
618616
if (Strings.CS.startsWithAny(str, BIN_PREFIX)) {
619617
try {
620618
return new BigInteger(str.substring(BIN_PREFIX.length()), BIN_RADIX);
621-
} catch (final NumberFormatException nex) {
622-
throw new ConversionException("Could not convert " + str + " to " + targetClass.getName() + "! Invalid binary number.", nex);
619+
} catch (final NumberFormatException e) {
620+
throw new ConversionException(e, "Could not convert %s to %s! Invalid binary number.", str, targetClass.getName());
623621
}
624622
}
625-
626623
try {
627624
final Constructor<?> constr = targetClass.getConstructor(CONSTR_ARGS);
628625
return (Number) constr.newInstance(str);
629-
} catch (final InvocationTargetException itex) {
630-
throw new ConversionException("Could not convert " + str + " to " + targetClass.getName(), itex.getTargetException());
631-
} catch (final Exception ex) {
626+
} catch (final InvocationTargetException e) {
627+
throw new ConversionException(e.getTargetException(), "Could not convert %s to %s", str, targetClass.getName());
628+
} catch (final Exception e) {
632629
// Treat all possible exceptions the same way
633-
throw new ConversionException("Conversion error when trying to convert " + str + " to " + targetClass.getName(), ex);
630+
throw new ConversionException(e, "Conversion error when trying to convert %s to %s", str, targetClass.getName());
634631
}
635632
}
636633

@@ -652,7 +649,7 @@ public static Path toPath(final Object value) throws ConversionException {
652649
if (value instanceof String) {
653650
return Paths.get((String) value);
654651
}
655-
throw new ConversionException("The value " + value + " can't be converted to a Path");
652+
throw new ConversionException("The value %s can't be converted to a Path", value);
656653
}
657654

658655
/**
@@ -667,12 +664,12 @@ public static Pattern toPattern(final Object value) throws ConversionException {
667664
return (Pattern) value;
668665
}
669666
if (!(value instanceof String)) {
670-
throw new ConversionException("The value " + value + " can't be converted to a Pattern");
667+
throw new ConversionException("The value %s can't be converted to a Pattern", value);
671668
}
672669
try {
673670
return Pattern.compile((String) value);
674671
} catch (final PatternSyntaxException e) {
675-
throw new ConversionException("The value " + value + " can't be converted to a Pattern", e);
672+
throw new ConversionException(e, "The value %s can't be converted to a Pattern", value);
676673
}
677674
}
678675

@@ -703,12 +700,12 @@ public static URI toURI(final Object value) throws ConversionException {
703700
return (URI) value;
704701
}
705702
if (!(value instanceof String)) {
706-
throw new ConversionException("The value " + value + " can't be converted to an URI");
703+
throw new ConversionException("The value %s can't be converted to an URI", value);
707704
}
708705
try {
709706
return new URI((String) value);
710707
} catch (final URISyntaxException e) {
711-
throw new ConversionException("The value " + value + " can't be converted to an URI", e);
708+
throw new ConversionException(e, "The value %s can't be converted to an URI", value);
712709
}
713710
}
714711

@@ -724,12 +721,12 @@ public static URL toURL(final Object value) throws ConversionException {
724721
return (URL) value;
725722
}
726723
if (!(value instanceof String)) {
727-
throw new ConversionException("The value " + value + " can't be converted to an URL");
724+
throw new ConversionException("The value %s can't be converted to an URL", value);
728725
}
729726
try {
730727
return new URL((String) value);
731728
} catch (final MalformedURLException e) {
732-
throw new ConversionException("The value " + value + " can't be converted to an URL", e);
729+
throw new ConversionException(e, "The value %s can't be converted to an URL", value);
733730
}
734731
}
735732

src/main/java/org/apache/commons/configuration2/ex/ConversionException.java

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,22 @@ public ConversionException(final String message) {
4444
super(message);
4545
}
4646

47+
/**
48+
* Constructs a new {@code ConversionException} with specified detail message.
49+
*
50+
* @param format the error message for for {@link String#format(String, Object...)}.
51+
* @param params the error parameters for for {@link String#format(String, Object...)}.
52+
* @since 2.14.0
53+
*/
54+
public ConversionException(final String format, final Object... params) {
55+
super(String.format(format, params));
56+
}
57+
4758
/**
4859
* Constructs a new {@code ConversionException} with specified detail message and nested {@code Throwable}.
4960
*
5061
* @param message the error message
51-
* @param cause the exception or error that caused this exception to be thrown
62+
* @param cause the exception or error that caused this exception to be thrown
5263
*/
5364
public ConversionException(final String message, final Throwable cause) {
5465
super(message, cause);
@@ -62,4 +73,17 @@ public ConversionException(final String message, final Throwable cause) {
6273
public ConversionException(final Throwable cause) {
6374
super(cause);
6475
}
76+
77+
/**
78+
* Constructs a new {@code ConversionException} with specified detail message.
79+
*
80+
* @param format the error message for for {@link String#format(String, Object...)}.
81+
* @param params the error parameters for for {@link String#format(String, Object...)}.
82+
* @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). (A {@code null} value is permitted, and indicates that
83+
* the cause is nonexistent or unknown.)
84+
* @since 2.14.0
85+
*/
86+
public ConversionException(final Throwable cause, final String format, final Object... params) {
87+
super(String.format(format, params), cause);
88+
}
6589
}

0 commit comments

Comments
 (0)