Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
CrazyHZM committed Dec 27, 2018
1 parent 59cbe69 commit 683af85
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,7 @@ public static ClassLoader getClassLoader(Class<?> clazz) {
// getClassLoader() returning null indicates the bootstrap ClassLoader
try {
cl = ClassLoader.getSystemClassLoader();
}
catch (Throwable ex) {
} catch (Throwable ex) {
// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
}
}
Expand Down Expand Up @@ -265,7 +264,9 @@ public static boolean isPrimitive(Class<?> type) {
}

public static Object convertPrimitive(Class<?> type, String value) {
if (type == char.class || type == Character.class) {
if (StringUtils.isEmpty(value)) {
return null;
} else if (type == char.class || type == Character.class) {
return value.length() > 0 ? value.charAt(0) : '\0';
} else if (type == boolean.class || type == Boolean.class) {
return Boolean.valueOf(value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import static org.apache.dubbo.common.utils.ClassHelper.getClassLoader;
import static org.apache.dubbo.common.utils.ClassHelper.resolvePrimitiveClassName;
import static org.apache.dubbo.common.utils.ClassHelper.toShortString;
import static org.apache.dubbo.common.utils.ClassHelper.convertPrimitive;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
Expand Down Expand Up @@ -118,4 +119,20 @@ public void testToShortString() throws Exception {
assertThat(toShortString(null), equalTo("null"));
assertThat(toShortString(new ClassHelperTest()), startsWith("ClassHelperTest@"));
}

@Test
public void testConvertPrimitive() throws Exception {

assertThat(convertPrimitive(int.class, ""), equalTo(null));
assertThat(convertPrimitive(int.class, null), equalTo(null));

assertThat(convertPrimitive(char.class, "6"), equalTo('6'));
assertThat(convertPrimitive(boolean.class, "true"), equalTo(Boolean.TRUE));
assertThat(convertPrimitive(byte.class, "127"), equalTo(Byte.MAX_VALUE));
assertThat(convertPrimitive(short.class, "32767"), equalTo(Short.MAX_VALUE));
assertThat(convertPrimitive(int.class, "6"), equalTo(6));
assertThat(convertPrimitive(long.class, "6"), equalTo(new Long(6)));
assertThat(convertPrimitive(float.class, "6"), equalTo(new Float(6)));
assertThat(convertPrimitive(double.class, "6"), equalTo(new Double(6)));
}
}

0 comments on commit 683af85

Please sign in to comment.