diff --git a/src/main/java/org/apache/commons/beanutils2/converters/ColorConverter.java b/src/main/java/org/apache/commons/beanutils2/converters/ColorConverter.java index 7b1baffe1..39ec163cf 100644 --- a/src/main/java/org/apache/commons/beanutils2/converters/ColorConverter.java +++ b/src/main/java/org/apache/commons/beanutils2/converters/ColorConverter.java @@ -122,45 +122,49 @@ protected Class getDefaultType() { */ @Override protected T convertToType(Class type, Object value) throws Throwable { - Objects.requireNonNull(value, "Value can't be null."); - final String stringValue = value.toString(); + if (Color.class.isAssignableFrom(type)) { + Objects.requireNonNull(value, "Value can't be null."); + final String stringValue = value.toString(); - switch (stringValue.toLowerCase()) { - case "white": - return type.cast(Color.WHITE); - case "lightgray": - return type.cast(Color.LIGHT_GRAY); - case "gray": - return type.cast(Color.GRAY); - case "darkgray": - return type.cast(Color.DARK_GRAY); - case "black": - return type.cast(Color.BLACK); - case "red": - return type.cast(Color.RED); - case "pink": - return type.cast(Color.PINK); - case "orange": - return type.cast(Color.ORANGE); - case "yellow": - return type.cast(Color.YELLOW); - case "green": - return type.cast(Color.GREEN); - case "magenta": - return type.cast(Color.MAGENTA); - case "cyan": - return type.cast(Color.CYAN); - case "blue": - return type.cast(Color.BLUE); - default: - // Do nothing. - } + switch (stringValue.toLowerCase()) { + case "white": + return type.cast(Color.WHITE); + case "lightgray": + return type.cast(Color.LIGHT_GRAY); + case "gray": + return type.cast(Color.GRAY); + case "darkgray": + return type.cast(Color.DARK_GRAY); + case "black": + return type.cast(Color.BLACK); + case "red": + return type.cast(Color.RED); + case "pink": + return type.cast(Color.PINK); + case "orange": + return type.cast(Color.ORANGE); + case "yellow": + return type.cast(Color.YELLOW); + case "green": + return type.cast(Color.GREEN); + case "magenta": + return type.cast(Color.MAGENTA); + case "cyan": + return type.cast(Color.CYAN); + case "blue": + return type.cast(Color.BLUE); + default: + // Do nothing. + } + + if (stringValue.startsWith(HEX_COLOR_PREFIX)) { + return type.cast(parseWebColor(stringValue)); + } - if (stringValue.startsWith(HEX_COLOR_PREFIX)) { - return type.cast(parseWebColor(stringValue)); + return type.cast(Color.decode(stringValue)); } - return type.cast(Color.decode(stringValue)); + throw conversionException(type, value); } /** diff --git a/src/main/java/org/apache/commons/beanutils2/converters/DimensionConverter.java b/src/main/java/org/apache/commons/beanutils2/converters/DimensionConverter.java index 92421cb29..fc65a51fb 100644 --- a/src/main/java/org/apache/commons/beanutils2/converters/DimensionConverter.java +++ b/src/main/java/org/apache/commons/beanutils2/converters/DimensionConverter.java @@ -74,25 +74,30 @@ protected Class getDefaultType() { */ @Override protected T convertToType(Class type, Object value) throws Throwable { - Objects.requireNonNull(value, "Dimensions can not be null."); - final String stringValue = value.toString(); + if (Dimension.class.isAssignableFrom(type)) { + Objects.requireNonNull(value, "Dimensions can not be null."); + final String stringValue = value.toString(); - if (stringValue.isEmpty()) { - throw new IllegalArgumentException("Dimensions can not be empty."); - } + if (stringValue.isEmpty()) { + throw new IllegalArgumentException("Dimensions can not be empty."); + } - Matcher matcher = DIMENSION_PATTERN.matcher(stringValue); + Matcher matcher = DIMENSION_PATTERN.matcher(stringValue); - if (!matcher.matches()) { - throw new IllegalArgumentException("Dimension doesn't match format: {width/height} or {width}x{height}"); - } + if (!matcher.matches()) { + throw new IllegalArgumentException( + "Dimension doesn't match format: {width/height} or {width}x{height}"); + } - String x = matcher.group(1); - String y = matcher.group(2); + String x = matcher.group(1); + String y = matcher.group(2); - int xValue = Integer.parseInt(x); - int yValue = (y == null || x.equals(y)) ? xValue : Integer.parseInt(y); + int xValue = Integer.parseInt(x); + int yValue = (y == null || x.equals(y)) ? xValue : Integer.parseInt(y); + + return type.cast(new Dimension(xValue, yValue)); + } - return type.cast(new Dimension(xValue, yValue)); + throw conversionException(type, value); } } diff --git a/src/main/java/org/apache/commons/beanutils2/converters/InetAddressConverter.java b/src/main/java/org/apache/commons/beanutils2/converters/InetAddressConverter.java index 0f172270c..1721704ec 100644 --- a/src/main/java/org/apache/commons/beanutils2/converters/InetAddressConverter.java +++ b/src/main/java/org/apache/commons/beanutils2/converters/InetAddressConverter.java @@ -60,13 +60,17 @@ protected Class getDefaultType() { */ @Override protected T convertToType(Class type, Object value) throws Throwable { - Objects.requireNonNull(value, "Value can't be null."); - final String stringValue = value.toString(); + if (InetAddress.class.isAssignableFrom(type)) { + Objects.requireNonNull(value, "Value can't be null."); + final String stringValue = value.toString(); - try { - return type.cast(InetAddress.getByName(stringValue)); - } catch (UnknownHostException ex) { - throw new IllegalArgumentException("Unable to get IP address of the named host.", ex); + try { + return type.cast(InetAddress.getByName(stringValue)); + } catch (UnknownHostException ex) { + throw new IllegalArgumentException("Unable to get IP address of the named host.", ex); + } } + + throw conversionException(type, value); } } diff --git a/src/main/java/org/apache/commons/beanutils2/converters/LocaleConverter.java b/src/main/java/org/apache/commons/beanutils2/converters/LocaleConverter.java index c13d910e2..4237106db 100644 --- a/src/main/java/org/apache/commons/beanutils2/converters/LocaleConverter.java +++ b/src/main/java/org/apache/commons/beanutils2/converters/LocaleConverter.java @@ -57,9 +57,13 @@ protected Class getDefaultType() { */ @Override protected T convertToType(Class type, Object value) throws Throwable { - Objects.requireNonNull(value, "Value can't be null."); - final String stringValue = value.toString(); + if (Locale.class.isAssignableFrom(type)) { + Objects.requireNonNull(value, "Value can't be null."); + final String stringValue = value.toString(); - return type.cast(Locale.forLanguageTag(stringValue)); + return type.cast(Locale.forLanguageTag(stringValue)); + } + + throw conversionException(type, value); } } diff --git a/src/main/java/org/apache/commons/beanutils2/converters/PatternConverter.java b/src/main/java/org/apache/commons/beanutils2/converters/PatternConverter.java index 0eda6927a..03b7973cf 100644 --- a/src/main/java/org/apache/commons/beanutils2/converters/PatternConverter.java +++ b/src/main/java/org/apache/commons/beanutils2/converters/PatternConverter.java @@ -58,9 +58,13 @@ protected Class getDefaultType() { */ @Override protected T convertToType(Class type, Object value) throws Throwable { - Objects.requireNonNull(value, "Regular expression can't be null."); - final String stringValue = value.toString(); + if (Pattern.class.isAssignableFrom(type)) { + Objects.requireNonNull(value, "Regular expression can't be null."); + final String stringValue = value.toString(); - return type.cast(Pattern.compile(stringValue)); + return type.cast(Pattern.compile(stringValue)); + } + + throw conversionException(type, value); } } diff --git a/src/main/java/org/apache/commons/beanutils2/converters/PointConverter.java b/src/main/java/org/apache/commons/beanutils2/converters/PointConverter.java index 9ee4c8a48..ba901c6e9 100644 --- a/src/main/java/org/apache/commons/beanutils2/converters/PointConverter.java +++ b/src/main/java/org/apache/commons/beanutils2/converters/PointConverter.java @@ -61,29 +61,33 @@ protected Class getDefaultType() { */ @Override protected T convertToType(Class type, Object value) throws Throwable { - Objects.requireNonNull(value, "Value must not be null."); - final String stringValue = value.toString(); + if (Point.class.isAssignableFrom(type)) { + Objects.requireNonNull(value, "Value must not be null."); + final String stringValue = value.toString(); - if (stringValue.isEmpty()) { - throw new IllegalArgumentException("A point can not be empty."); - } + if (stringValue.isEmpty()) { + throw new IllegalArgumentException("A point can not be empty."); + } - final int lastCharIndex = stringValue.length() - 1; + final int lastCharIndex = stringValue.length() - 1; - if (stringValue.charAt(0) != '(' || stringValue.charAt(lastCharIndex) != ')') { - throw new IllegalArgumentException("Point coordinates must be enclosed in brackets."); - } + if (stringValue.charAt(0) != '(' || stringValue.charAt(lastCharIndex) != ')') { + throw new IllegalArgumentException("Point coordinates must be enclosed in brackets."); + } + + final String coordinates = stringValue.substring(1, lastCharIndex); + final String[] xy = POINT_SPLIT.split(coordinates); - final String coordinates = stringValue.substring(1, lastCharIndex); - final String[] xy = POINT_SPLIT.split(coordinates); + if (xy.length != 2) { + throw new IllegalArgumentException("Point must have an x coordinate, and y coordinate only, " + + "expecting the following format: (40, 200)"); + } - if (xy.length != 2) { - throw new IllegalArgumentException("Point must have an x coordinate, and y coordinate only, " + - "expecting the following format: (40, 200)"); + final int x = Integer.parseInt(xy[0]); + final int y = Integer.parseInt(xy[1]); + return type.cast(new Point(x, y)); } - final int x = Integer.parseInt(xy[0]); - final int y = Integer.parseInt(xy[1]); - return type.cast(new Point(x, y)); + throw conversionException(type, value); } }