Skip to content

Commit 544fcec

Browse files
Jira wdt 79 catch number format exception (#623)
* Fix java.lang.Boolean issues for ActiveDirectoryAuthenticator * convert to number catches format exception
1 parent 7a85b99 commit 544fcec

File tree

3 files changed

+47
-34
lines changed

3 files changed

+47
-34
lines changed

core/src/main/java/oracle/weblogic/deploy/aliases/TypeUtils.java

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
/*
2-
* Copyright (c) 2017, 2019, Oracle Corporation and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2017, 2020, Oracle Corporation and/or its affiliates.
33
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
44
*/
55
package oracle.weblogic.deploy.aliases;
66

77
import java.io.File;
88
import java.lang.reflect.Array;
9+
import java.lang.NumberFormatException;
910
import java.util.ArrayList;
1011
import java.util.Arrays;
1112
import java.util.HashMap;
@@ -168,7 +169,7 @@ public static Object convertToType(Class<?> targetType, Object value) throws Ali
168169
* or the requested data type is not supported.
169170
* @throws AliasException if classType is a primitive class type such as int, short, etc. or if a multi-valued
170171
* type is detected where the delimiter is null and a delimiter is required to handle
171-
* the conversion.
172+
* the conversion. Or if the numeric value cannot be converted to the numeric format.
172173
*/
173174
public static Object convertToType(Class<?> targetType, Object value, String delimiter) throws AliasException {
174175
final String METHOD = "convertToType";
@@ -194,38 +195,44 @@ public static Object convertToType(Class<?> targetType, Object value, String del
194195
}
195196

196197
Object result;
197-
if (targetType == String.class) {
198-
result = strValue;
199-
} else if (targetType == Boolean.class) {
200-
result = convertToBoolean(strValue);
201-
} else if (targetType == Integer.class) {
202-
result = Integer.valueOf(strValue);
203-
} else if (targetType == Short.class) {
204-
result = Short.valueOf(strValue);
205-
} else if (targetType == Long.class) {
206-
result = Long.valueOf(strValue);
207-
} else if (targetType == Float.class) {
208-
result = Float.valueOf(strValue);
209-
} else if (targetType == Double.class) {
210-
result = Double.valueOf(strValue);
211-
} else if (targetType == Character.class) {
212-
result = convertToCharacter(strValue);
213-
} else if (targetType == char[].class) {
214-
result = convertToCharArray(strValue);
215-
} else if (Object[].class.isAssignableFrom(targetType)) {
216-
result = convertToObjectArray(value, strValue, delimiter);
217-
} else if (List.class.isAssignableFrom(targetType)) {
218-
result = convertToList(value, strValue, delimiter);
219-
} else if (Properties.class.isAssignableFrom(targetType)) {
220-
result = convertToProperties(value, delimiter);
221-
} else if (PyOrderedDict.class.isAssignableFrom(targetType)) {
222-
result = convertToDictionary(value, delimiter, true);
223-
} else if (PyDictionary.class.isAssignableFrom(targetType)) {
224-
result = convertToDictionary(value, delimiter, false);
225-
} else if (Map.class.isAssignableFrom(targetType)) {
226-
result = convertToMap(value, strValue, delimiter);
227-
} else {
228-
AliasException ae = new AliasException("WLSDPLY-08502", strValue, targetType.getName());
198+
try {
199+
if (targetType == String.class) {
200+
result = strValue;
201+
} else if (targetType == Boolean.class) {
202+
result = convertToBoolean(strValue);
203+
} else if (targetType == Integer.class) {
204+
result = Integer.valueOf(strValue);
205+
} else if (targetType == Short.class) {
206+
result = Short.valueOf(strValue);
207+
} else if (targetType == Long.class) {
208+
result = Long.valueOf(strValue);
209+
} else if (targetType == Float.class) {
210+
result = Float.valueOf(strValue);
211+
} else if (targetType == Double.class) {
212+
result = Double.valueOf(strValue);
213+
} else if (targetType == Character.class) {
214+
result = convertToCharacter(strValue);
215+
} else if (targetType == char[].class) {
216+
result = convertToCharArray(strValue);
217+
} else if (Object[].class.isAssignableFrom(targetType)) {
218+
result = convertToObjectArray(value, strValue, delimiter);
219+
} else if (List.class.isAssignableFrom(targetType)) {
220+
result = convertToList(value, strValue, delimiter);
221+
} else if (Properties.class.isAssignableFrom(targetType)) {
222+
result = convertToProperties(value, delimiter);
223+
} else if (PyOrderedDict.class.isAssignableFrom(targetType)) {
224+
result = convertToDictionary(value, delimiter, true);
225+
} else if (PyDictionary.class.isAssignableFrom(targetType)) {
226+
result = convertToDictionary(value, delimiter, false);
227+
} else if (Map.class.isAssignableFrom(targetType)) {
228+
result = convertToMap(value, strValue, delimiter);
229+
} else {
230+
AliasException ae = new AliasException("WLSDPLY-08502", strValue, targetType.getName());
231+
LOGGER.throwing(CLASS, METHOD, ae);
232+
throw ae;
233+
}
234+
} catch (NumberFormatException nfe) {
235+
AliasException ae = new AliasException("WLSDPLY-08508", strValue, targetType.getSimpleName(), nfe);
229236
LOGGER.throwing(CLASS, METHOD, ae);
230237
throw ae;
231238
}

core/src/main/resources/oracle/weblogic/deploy/messages/wlsdeploy_rb.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,7 @@ WLSDPLY-08504=Unable to convert type {0} to a PyDictionary type
928928
WLSDPLY-08505=Unable to convert string {0} to a List since the delimiter is null
929929
WLSDPLY-08506=Unable to add property {0} to a PyDictionary type
930930
WLSDPLY-08507=Unable to convert type {0} to a PyObject type
931+
WLSDPLY-08508=Unable to convert String value {0} to numeric type {0}
931932

932933
###############################################################################
933934
# Deploy messages (9000 - 9999) #

core/src/test/java/oracle/weblogic/deploy/aliases/TypeUtilsTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ public void convertToTypeInvalidPrimitiveType() throws Exception {
4949
TypeUtils.convertToType( int.class, "123" );
5050
}
5151

52+
@Test(expected = AliasException.class)
53+
public void convertInvalidInteger() throws Exception {
54+
TypeUtils.convertToType(Integer.class, "this is a string");
55+
}
56+
5257
@Test
5358
public void convertToTypeNullTest() throws Exception {
5459
assertEquals("String null conversion failed", null, TypeUtils.convertToType( String.class, null ));;

0 commit comments

Comments
 (0)