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-configuration.git
commit 8d044b00e9f1fd7450b3d9837145bebe1bac17e7 Author: Gary Gregory <[email protected]> AuthorDate: Fri Apr 3 08:45:05 2026 -0400 Add and use ConversionException.ConfigurationException(String, Object...). Add and use ConversionException.ConfigurationException(Throwable, String, Object...) --- src/changes/changes.xml | 2 + .../configuration2/AbstractConfiguration.java | 6 +- .../configuration2/convert/PropertyConverter.java | 79 +++++++++++----------- .../configuration2/ex/ConversionException.java | 26 ++++++- 4 files changed, 68 insertions(+), 45 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 6eee6fac1..604bb1ac9 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -31,6 +31,8 @@ <action type="add" dev="ggregory" due-to="Gary Gregory">Add XMLConfiguration.read(Element).</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add ConfigurationException.ConfigurationException(String, Object...).</action> <action type="add" dev="ggregory" due-to="Gary Gregory">Add ConfigurationException.ConfigurationException(Throwable, String, Object...).</action> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add ConversionException.ConfigurationException(String, Object...).</action> + <action type="add" dev="ggregory" due-to="Gary Gregory">Add ConversionException.ConfigurationException(Throwable, String, Object...).</action> <!-- UPDATE --> <action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-parent from 92 to 97.</action> <action type="update" dev="ggregory" due-to="Gary Gregory">Bump org.apache.commons:commons-text from 1.14.0 to 1.15.0.</action> diff --git a/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java b/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java index 4dd19a7ac..2cc4e7fef 100644 --- a/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java +++ b/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java @@ -624,8 +624,8 @@ public abstract class AbstractConfiguration extends BaseEventSource implements C return ObjectUtils.getIfNull(getConversionHandler().to(value, cls, getInterpolator()), defaultValue); } catch (final ConversionException cex) { // improve error message - throw new ConversionException(String.format("Key '%s' cannot be converted to class %s. Value is: '%s'.", key, cls.getName(), String.valueOf(value)), - cex.getCause()); + throw new ConversionException(cex.getCause(), "Key '%s' cannot be converted to class %s. Value is: '%s'.", key, cls.getName(), + String.valueOf(value)); } } @@ -982,7 +982,7 @@ public abstract class AbstractConfiguration extends BaseEventSource implements C } else if (isScalarValue(value)) { return Collections.singletonList((Object) value.toString()); } else { - throw new ConversionException('\'' + key + "' doesn't map to a List object: " + value + ", a " + value.getClass().getName()); + throw new ConversionException("'%s' doesn't map to a List object: %s, a %s", key, value, value.getClass().getName()); } return list; } diff --git a/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java b/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java index 7865aaff6..55e4241a1 100644 --- a/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java +++ b/src/main/java/org/apache/commons/configuration2/convert/PropertyConverter.java @@ -185,8 +185,7 @@ public final class PropertyConverter { if (Duration.class.equals(cls)) { return toDuration(value); } - - throw new ConversionException("The value '" + value + "' (" + value.getClass() + ") can't be converted to a " + cls.getName() + " object"); + throw new ConversionException("The value '%s' (%s) can't be converted to a %s object", value, value.getClass(), cls.getName()); } /** @@ -234,11 +233,11 @@ public final class PropertyConverter { return (Boolean) value; } if (!(value instanceof String)) { - throw new ConversionException("The value " + value + " can't be converted to a Boolean object"); + throw new ConversionException("The value %s can't be converted to a Boolean object", value); } final Boolean b = BooleanUtils.toBooleanObject((String) value); if (b == null) { - throw new ConversionException("The value " + value + " can't be converted to a Boolean object"); + throw new ConversionException("The value %s can't be converted to a Boolean object", value); } return b; } @@ -276,14 +275,14 @@ public final class PropertyConverter { return calendar; } if (!(value instanceof String)) { - throw new ConversionException("The value " + value + " can't be converted to a Calendar"); + throw new ConversionException("The value %s can't be converted to a Calendar", value); } try { final Calendar calendar = Calendar.getInstance(); calendar.setTime(new SimpleDateFormat(format).parse((String) value)); return calendar; } catch (final ParseException e) { - throw new ConversionException("The value " + value + " can't be converted to a Calendar", e); + throw new ConversionException(e, "The value %s can't be converted to a Calendar", value); } } @@ -300,7 +299,7 @@ public final class PropertyConverter { if (strValue.length() == 1) { return Character.valueOf(strValue.charAt(0)); } - throw new ConversionException(String.format("The value '%s' cannot be converted to a Character object!", strValue)); + throw new ConversionException("The value '%s' cannot be converted to a Character object!", strValue); } /** @@ -322,7 +321,7 @@ public final class PropertyConverter { return (Color) value; } if (!(value instanceof String) || StringUtils.isBlank((String) value)) { - throw new ConversionException("The value " + value + " can't be converted to a Color"); + throw new ConversionException("The value %s can't be converted to a Color", value); } String color = ((String) value).trim(); @@ -331,7 +330,7 @@ public final class PropertyConverter { // check the size of the string final int minlength = components.length * 2; if (color.length() < minlength) { - throw new ConversionException("The value " + value + " can't be converted to a Color"); + throw new ConversionException("The value %s can't be converted to a Color", value); } // remove the leading # @@ -355,7 +354,7 @@ public final class PropertyConverter { return new Color(components[0], components[1], components[2], alpha); } catch (final Exception e) { - throw new ConversionException("The value " + value + " can't be converted to a Color", e); + throw new ConversionException(e, "The value %s can't be converted to a Color", value); } } @@ -375,12 +374,12 @@ public final class PropertyConverter { return ((Calendar) value).getTime(); } if (!(value instanceof String)) { - throw new ConversionException("The value " + value + " can't be converted to a Date"); + throw new ConversionException("The value %s can't be converted to a Date", value); } try { return new SimpleDateFormat(format).parse((String) value); } catch (final ParseException e) { - throw new ConversionException("The value " + value + " can't be converted to a Date", e); + throw new ConversionException(e, "The value %s can't be converted to a Date", value); } } @@ -415,10 +414,10 @@ public final class PropertyConverter { try { return Duration.parse((CharSequence) value); } catch (final DateTimeParseException e) { - throw new ConversionException("Could not convert " + value + " to Duration", e); + throw new ConversionException(e, "Could not convert %s to Duration", value); } } - throw new ConversionException("The value " + value + " can't be converted to a Duration"); + throw new ConversionException("The value %s can't be converted to a Duration", value); } /** @@ -438,17 +437,17 @@ public final class PropertyConverter { try { return Enum.valueOf(cls, (String) value); } catch (final Exception e) { - throw new ConversionException("The value " + value + " can't be converted to a " + cls.getName()); + throw new ConversionException(e, "The value %s can't be converted to a %s", value, cls.getName()); } } if (!(value instanceof Number)) { - throw new ConversionException("The value " + value + " can't be converted to a " + cls.getName()); + throw new ConversionException("The value %s can't be converted to a ", value, cls.getName()); } try { final E[] enumConstants = cls.getEnumConstants(); return enumConstants[((Number) value).intValue()]; } catch (final Exception e) { - throw new ConversionException("The value " + value + " can't be converted to a " + cls.getName()); + throw new ConversionException(e, "The value %s can't be converted to a ", value, cls.getName()); } } @@ -470,7 +469,7 @@ public final class PropertyConverter { if (value instanceof String) { return new File((String) value); } - throw new ConversionException("The value " + value + " can't be converted to a File"); + throw new ConversionException("The value %s can't be converted to a File", value); } /** @@ -501,12 +500,12 @@ public final class PropertyConverter { return (InetAddress) value; } if (!(value instanceof String)) { - throw new ConversionException("The value " + value + " can't be converted to a InetAddress"); + throw new ConversionException("The value %s can't be converted to a InetAddress", value); } try { return InetAddress.getByName((String) value); } catch (final UnknownHostException e) { - throw new ConversionException("The value " + value + " can't be converted to a InetAddress", e); + throw new ConversionException(e, "The value %s can't be converted to a InetAddress", value); } } @@ -540,13 +539,13 @@ public final class PropertyConverter { return value; } if (!(value instanceof String)) { - throw new ConversionException("The value " + value + " can't be converted to an InternetAddress"); + throw new ConversionException("The value %s can't be converted to an InternetAddress", value); } try { final Constructor<?> ctor = Class.forName(targetClassName).getConstructor(String.class); return ctor.newInstance(value); } catch (final Exception e) { - throw new ConversionException("The value " + value + " can't be converted to an InternetAddress", e); + throw new ConversionException(e, "The value %s can't be converted to an InternetAddress", value); } } @@ -562,7 +561,7 @@ public final class PropertyConverter { return (Locale) value; } if (!(value instanceof String)) { - throw new ConversionException("The value " + value + " can't be converted to a Locale"); + throw new ConversionException("The value %s can't be converted to a Locale", value); } final String[] elements = ((String) value).split("_"); final int size = elements.length; @@ -574,7 +573,7 @@ public final class PropertyConverter { return new Locale(language, country, variant); } - throw new ConversionException("The value " + value + " can't be converted to a Locale"); + throw new ConversionException("The value %s can't be converted to a Locale", value); } /** @@ -610,27 +609,25 @@ public final class PropertyConverter { if (Strings.CS.startsWithAny(str, HEX_PREFIX)) { try { return new BigInteger(str.substring(HEX_PREFIX.length()), HEX_RADIX); - } catch (final NumberFormatException nex) { - throw new ConversionException("Could not convert " + str + " to " + targetClass.getName() + "! Invalid hex number.", nex); + } catch (final NumberFormatException e) { + throw new ConversionException(e, "Could not convert %s to %s! Invalid hex number.", str, targetClass.getName()); } } - if (Strings.CS.startsWithAny(str, BIN_PREFIX)) { try { return new BigInteger(str.substring(BIN_PREFIX.length()), BIN_RADIX); - } catch (final NumberFormatException nex) { - throw new ConversionException("Could not convert " + str + " to " + targetClass.getName() + "! Invalid binary number.", nex); + } catch (final NumberFormatException e) { + throw new ConversionException(e, "Could not convert %s to %s! Invalid binary number.", str, targetClass.getName()); } } - try { final Constructor<?> constr = targetClass.getConstructor(CONSTR_ARGS); return (Number) constr.newInstance(str); - } catch (final InvocationTargetException itex) { - throw new ConversionException("Could not convert " + str + " to " + targetClass.getName(), itex.getTargetException()); - } catch (final Exception ex) { + } catch (final InvocationTargetException e) { + throw new ConversionException(e.getTargetException(), "Could not convert %s to %s", str, targetClass.getName()); + } catch (final Exception e) { // Treat all possible exceptions the same way - throw new ConversionException("Conversion error when trying to convert " + str + " to " + targetClass.getName(), ex); + throw new ConversionException(e, "Conversion error when trying to convert %s to %s", str, targetClass.getName()); } } @@ -652,7 +649,7 @@ public final class PropertyConverter { if (value instanceof String) { return Paths.get((String) value); } - throw new ConversionException("The value " + value + " can't be converted to a Path"); + throw new ConversionException("The value %s can't be converted to a Path", value); } /** @@ -667,12 +664,12 @@ public final class PropertyConverter { return (Pattern) value; } if (!(value instanceof String)) { - throw new ConversionException("The value " + value + " can't be converted to a Pattern"); + throw new ConversionException("The value %s can't be converted to a Pattern", value); } try { return Pattern.compile((String) value); } catch (final PatternSyntaxException e) { - throw new ConversionException("The value " + value + " can't be converted to a Pattern", e); + throw new ConversionException(e, "The value %s can't be converted to a Pattern", value); } } @@ -703,12 +700,12 @@ public final class PropertyConverter { return (URI) value; } if (!(value instanceof String)) { - throw new ConversionException("The value " + value + " can't be converted to an URI"); + throw new ConversionException("The value %s can't be converted to an URI", value); } try { return new URI((String) value); } catch (final URISyntaxException e) { - throw new ConversionException("The value " + value + " can't be converted to an URI", e); + throw new ConversionException(e, "The value %s can't be converted to an URI", value); } } @@ -724,12 +721,12 @@ public final class PropertyConverter { return (URL) value; } if (!(value instanceof String)) { - throw new ConversionException("The value " + value + " can't be converted to an URL"); + throw new ConversionException("The value %s can't be converted to an URL", value); } try { return new URL((String) value); } catch (final MalformedURLException e) { - throw new ConversionException("The value " + value + " can't be converted to an URL", e); + throw new ConversionException(e, "The value %s can't be converted to an URL", value); } } diff --git a/src/main/java/org/apache/commons/configuration2/ex/ConversionException.java b/src/main/java/org/apache/commons/configuration2/ex/ConversionException.java index ddcd128c9..b1a6726b1 100644 --- a/src/main/java/org/apache/commons/configuration2/ex/ConversionException.java +++ b/src/main/java/org/apache/commons/configuration2/ex/ConversionException.java @@ -44,11 +44,22 @@ public class ConversionException extends ConfigurationRuntimeException { super(message); } + /** + * Constructs a new {@code ConversionException} with specified detail message. + * + * @param format the error message for for {@link String#format(String, Object...)}. + * @param params the error parameters for for {@link String#format(String, Object...)}. + * @since 2.14.0 + */ + public ConversionException(final String format, final Object... params) { + super(String.format(format, params)); + } + /** * Constructs a new {@code ConversionException} with specified detail message and nested {@code Throwable}. * * @param message the error message - * @param cause the exception or error that caused this exception to be thrown + * @param cause the exception or error that caused this exception to be thrown */ public ConversionException(final String message, final Throwable cause) { super(message, cause); @@ -62,4 +73,17 @@ public class ConversionException extends ConfigurationRuntimeException { public ConversionException(final Throwable cause) { super(cause); } + + /** + * Constructs a new {@code ConversionException} with specified detail message. + * + * @param format the error message for for {@link String#format(String, Object...)}. + * @param params the error parameters for for {@link String#format(String, Object...)}. + * @param cause the cause (which is saved for later retrieval by the {@link #getCause()} method). (A {@code null} value is permitted, and indicates that + * the cause is nonexistent or unknown.) + * @since 2.14.0 + */ + public ConversionException(final Throwable cause, final String format, final Object... params) { + super(String.format(format, params), cause); + } }
