Skip to content

Commit f4b8eac

Browse files
committed
Added check for #isAssignable else throw Standard ConversionException
1 parent 36d836a commit f4b8eac

File tree

6 files changed

+103
-78
lines changed

6 files changed

+103
-78
lines changed

src/main/java/org/apache/commons/beanutils2/converters/ColorConverter.java

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -122,45 +122,49 @@ protected Class<?> getDefaultType() {
122122
*/
123123
@Override
124124
protected <T> T convertToType(Class<T> type, Object value) throws Throwable {
125-
Objects.requireNonNull(value, "Value can't be null.");
126-
final String stringValue = value.toString();
125+
if (Color.class.isAssignableFrom(type)) {
126+
Objects.requireNonNull(value, "Value can't be null.");
127+
final String stringValue = value.toString();
127128

128-
switch (stringValue.toLowerCase()) {
129-
case "white":
130-
return type.cast(Color.WHITE);
131-
case "lightgray":
132-
return type.cast(Color.LIGHT_GRAY);
133-
case "gray":
134-
return type.cast(Color.GRAY);
135-
case "darkgray":
136-
return type.cast(Color.DARK_GRAY);
137-
case "black":
138-
return type.cast(Color.BLACK);
139-
case "red":
140-
return type.cast(Color.RED);
141-
case "pink":
142-
return type.cast(Color.PINK);
143-
case "orange":
144-
return type.cast(Color.ORANGE);
145-
case "yellow":
146-
return type.cast(Color.YELLOW);
147-
case "green":
148-
return type.cast(Color.GREEN);
149-
case "magenta":
150-
return type.cast(Color.MAGENTA);
151-
case "cyan":
152-
return type.cast(Color.CYAN);
153-
case "blue":
154-
return type.cast(Color.BLUE);
155-
default:
156-
// Do nothing.
157-
}
129+
switch (stringValue.toLowerCase()) {
130+
case "white":
131+
return type.cast(Color.WHITE);
132+
case "lightgray":
133+
return type.cast(Color.LIGHT_GRAY);
134+
case "gray":
135+
return type.cast(Color.GRAY);
136+
case "darkgray":
137+
return type.cast(Color.DARK_GRAY);
138+
case "black":
139+
return type.cast(Color.BLACK);
140+
case "red":
141+
return type.cast(Color.RED);
142+
case "pink":
143+
return type.cast(Color.PINK);
144+
case "orange":
145+
return type.cast(Color.ORANGE);
146+
case "yellow":
147+
return type.cast(Color.YELLOW);
148+
case "green":
149+
return type.cast(Color.GREEN);
150+
case "magenta":
151+
return type.cast(Color.MAGENTA);
152+
case "cyan":
153+
return type.cast(Color.CYAN);
154+
case "blue":
155+
return type.cast(Color.BLUE);
156+
default:
157+
// Do nothing.
158+
}
159+
160+
if (stringValue.startsWith(HEX_COLOR_PREFIX)) {
161+
return type.cast(parseWebColor(stringValue));
162+
}
158163

159-
if (stringValue.startsWith(HEX_COLOR_PREFIX)) {
160-
return type.cast(parseWebColor(stringValue));
164+
return type.cast(Color.decode(stringValue));
161165
}
162166

163-
return type.cast(Color.decode(stringValue));
167+
throw conversionException(type, value);
164168
}
165169

166170
/**

src/main/java/org/apache/commons/beanutils2/converters/DimensionConverter.java

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,30 @@ protected Class<?> getDefaultType() {
7474
*/
7575
@Override
7676
protected <T> T convertToType(Class<T> type, Object value) throws Throwable {
77-
Objects.requireNonNull(value, "Dimensions can not be null.");
78-
final String stringValue = value.toString();
77+
if (Dimension.class.isAssignableFrom(type)) {
78+
Objects.requireNonNull(value, "Dimensions can not be null.");
79+
final String stringValue = value.toString();
7980

80-
if (stringValue.isEmpty()) {
81-
throw new IllegalArgumentException("Dimensions can not be empty.");
82-
}
81+
if (stringValue.isEmpty()) {
82+
throw new IllegalArgumentException("Dimensions can not be empty.");
83+
}
8384

84-
Matcher matcher = DIMENSION_PATTERN.matcher(stringValue);
85+
Matcher matcher = DIMENSION_PATTERN.matcher(stringValue);
8586

86-
if (!matcher.matches()) {
87-
throw new IllegalArgumentException("Dimension doesn't match format: {width/height} or {width}x{height}");
88-
}
87+
if (!matcher.matches()) {
88+
throw new IllegalArgumentException(
89+
"Dimension doesn't match format: {width/height} or {width}x{height}");
90+
}
8991

90-
String x = matcher.group(1);
91-
String y = matcher.group(2);
92+
String x = matcher.group(1);
93+
String y = matcher.group(2);
9294

93-
int xValue = Integer.parseInt(x);
94-
int yValue = (y == null || x.equals(y)) ? xValue : Integer.parseInt(y);
95+
int xValue = Integer.parseInt(x);
96+
int yValue = (y == null || x.equals(y)) ? xValue : Integer.parseInt(y);
97+
98+
return type.cast(new Dimension(xValue, yValue));
99+
}
95100

96-
return type.cast(new Dimension(xValue, yValue));
101+
throw conversionException(type, value);
97102
}
98103
}

src/main/java/org/apache/commons/beanutils2/converters/InetAddressConverter.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,17 @@ protected Class<?> getDefaultType() {
6060
*/
6161
@Override
6262
protected <T> T convertToType(Class<T> type, Object value) throws Throwable {
63-
Objects.requireNonNull(value, "Value can't be null.");
64-
final String stringValue = value.toString();
63+
if (InetAddress.class.isAssignableFrom(type)) {
64+
Objects.requireNonNull(value, "Value can't be null.");
65+
final String stringValue = value.toString();
6566

66-
try {
67-
return type.cast(InetAddress.getByName(stringValue));
68-
} catch (UnknownHostException ex) {
69-
throw new IllegalArgumentException("Unable to get IP address of the named host.", ex);
67+
try {
68+
return type.cast(InetAddress.getByName(stringValue));
69+
} catch (UnknownHostException ex) {
70+
throw new IllegalArgumentException("Unable to get IP address of the named host.", ex);
71+
}
7072
}
73+
74+
throw conversionException(type, value);
7175
}
7276
}

src/main/java/org/apache/commons/beanutils2/converters/LocaleConverter.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,13 @@ protected Class<?> getDefaultType() {
5757
*/
5858
@Override
5959
protected <T> T convertToType(Class<T> type, Object value) throws Throwable {
60-
Objects.requireNonNull(value, "Value can't be null.");
61-
final String stringValue = value.toString();
60+
if (Locale.class.isAssignableFrom(type)) {
61+
Objects.requireNonNull(value, "Value can't be null.");
62+
final String stringValue = value.toString();
6263

63-
return type.cast(Locale.forLanguageTag(stringValue));
64+
return type.cast(Locale.forLanguageTag(stringValue));
65+
}
66+
67+
throw conversionException(type, value);
6468
}
6569
}

src/main/java/org/apache/commons/beanutils2/converters/PatternConverter.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,13 @@ protected Class<?> getDefaultType() {
5858
*/
5959
@Override
6060
protected <T> T convertToType(Class<T> type, Object value) throws Throwable {
61-
Objects.requireNonNull(value, "Regular expression can't be null.");
62-
final String stringValue = value.toString();
61+
if (Pattern.class.isAssignableFrom(type)) {
62+
Objects.requireNonNull(value, "Regular expression can't be null.");
63+
final String stringValue = value.toString();
6364

64-
return type.cast(Pattern.compile(stringValue));
65+
return type.cast(Pattern.compile(stringValue));
66+
}
67+
68+
throw conversionException(type, value);
6569
}
6670
}

src/main/java/org/apache/commons/beanutils2/converters/PointConverter.java

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,29 +61,33 @@ protected Class<?> getDefaultType() {
6161
*/
6262
@Override
6363
protected <T> T convertToType(Class<T> type, Object value) throws Throwable {
64-
Objects.requireNonNull(value, "Value must not be null.");
65-
final String stringValue = value.toString();
64+
if (Point.class.isAssignableFrom(type)) {
65+
Objects.requireNonNull(value, "Value must not be null.");
66+
final String stringValue = value.toString();
6667

67-
if (stringValue.isEmpty()) {
68-
throw new IllegalArgumentException("A point can not be empty.");
69-
}
68+
if (stringValue.isEmpty()) {
69+
throw new IllegalArgumentException("A point can not be empty.");
70+
}
7071

71-
final int lastCharIndex = stringValue.length() - 1;
72+
final int lastCharIndex = stringValue.length() - 1;
7273

73-
if (stringValue.charAt(0) != '(' || stringValue.charAt(lastCharIndex) != ')') {
74-
throw new IllegalArgumentException("Point coordinates must be enclosed in brackets.");
75-
}
74+
if (stringValue.charAt(0) != '(' || stringValue.charAt(lastCharIndex) != ')') {
75+
throw new IllegalArgumentException("Point coordinates must be enclosed in brackets.");
76+
}
77+
78+
final String coordinates = stringValue.substring(1, lastCharIndex);
79+
final String[] xy = POINT_SPLIT.split(coordinates);
7680

77-
final String coordinates = stringValue.substring(1, lastCharIndex);
78-
final String[] xy = POINT_SPLIT.split(coordinates);
81+
if (xy.length != 2) {
82+
throw new IllegalArgumentException("Point must have an x coordinate, and y coordinate only, " +
83+
"expecting the following format: (40, 200)");
84+
}
7985

80-
if (xy.length != 2) {
81-
throw new IllegalArgumentException("Point must have an x coordinate, and y coordinate only, " +
82-
"expecting the following format: (40, 200)");
86+
final int x = Integer.parseInt(xy[0]);
87+
final int y = Integer.parseInt(xy[1]);
88+
return type.cast(new Point(x, y));
8389
}
8490

85-
final int x = Integer.parseInt(xy[0]);
86-
final int y = Integer.parseInt(xy[1]);
87-
return type.cast(new Point(x, y));
91+
throw conversionException(type, value);
8892
}
8993
}

0 commit comments

Comments
 (0)