Skip to content

Commit 0882c83

Browse files
jerrick-zhuzonghaishang
authored andcommitted
support char[] for generic invoke #2003 (#2137)
* support char[] for generic invoke, #2003 * add null or empty string testcase
1 parent 5e82a5e commit 0882c83

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

dubbo-common/src/main/java/com/alibaba/dubbo/common/utils/CompatibleTypeUtils.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,19 @@ public static Object compatibleTypeConvert(Object value, Class<?> type) {
9191
} catch (ClassNotFoundException e) {
9292
throw new RuntimeException(e.getMessage(), e);
9393
}
94+
} else if (char[].class.equals(type)) {
95+
// Process string to char array for generic invoke
96+
// See
97+
// - https://github.com/apache/incubator-dubbo/issues/2003
98+
if (string == null) {
99+
return null;
100+
}
101+
else {
102+
int len = string.length();
103+
char[] chars = new char[len];
104+
string.getChars(0, len, chars, 0);
105+
return chars;
106+
}
94107
}
95108
} else if (value instanceof Number) {
96109
Number number = (Number) value;

dubbo-common/src/test/java/com/alibaba/dubbo/common/utils/CompatibleTypeUtilsTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,17 @@ public void testCompatibleTypeConvert() throws Exception {
6767

6868
result = CompatibleTypeUtils.compatibleTypeConvert("2011-12-11 12:24:12", Date.class);
6969
assertEquals(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse("2011-12-11 12:24:12"), (Date) result);
70+
71+
result = CompatibleTypeUtils.compatibleTypeConvert("ab", char[].class);
72+
assertEquals(2, ((char[]) result).length);
73+
assertEquals('a', ((char[]) result)[0]);
74+
assertEquals('b', ((char[]) result)[1]);
75+
76+
result = CompatibleTypeUtils.compatibleTypeConvert("", char[].class);
77+
assertEquals(0, ((char[]) result).length);
78+
79+
result = CompatibleTypeUtils.compatibleTypeConvert(null, char[].class);
80+
assertEquals(null, result);
7081
}
7182

7283
{

0 commit comments

Comments
 (0)