Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Dubbo-3069]Use regular expressions to judge fix #3069 #3093

Merged
merged 21 commits into from
Feb 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,26 +264,33 @@ public static boolean isPrimitive(Class<?> type) {
}

public static Object convertPrimitive(Class<?> type, String value) {
if (type == char.class || type == Character.class) {
if (value == null) {
ralf0131 marked this conversation as resolved.
Show resolved Hide resolved
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);
} else if (type == byte.class || type == Byte.class) {
return Byte.valueOf(value);
return isNumber(value) ? Byte.valueOf(value) : null;
beiwei30 marked this conversation as resolved.
Show resolved Hide resolved
} else if (type == short.class || type == Short.class) {
return Short.valueOf(value);
return isNumber(value) ? Short.valueOf(value) : null;
} else if (type == int.class || type == Integer.class) {
return Integer.valueOf(value);
return isNumber(value) ? Integer.valueOf(value) : null;
} else if (type == long.class || type == Long.class) {
return Long.valueOf(value);
return isNumber(value) ? Long.valueOf(value) : null;
} else if (type == float.class || type == Float.class) {
return Float.valueOf(value);
return isNumber(value) ? Float.valueOf(value) : null;
} else if (type == double.class || type == Double.class) {
return Double.valueOf(value);
return isNumber(value) ? Double.valueOf(value) : null;
}
return value;
}

public static boolean isNumber(String str) {
String reg = "^[0-9]+(.[0-9]+)?$";
beiwei30 marked this conversation as resolved.
Show resolved Hide resolved
return str.matches(reg);
}

/**
* We only check boolean value at this moment.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
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.apache.dubbo.common.utils.ClassHelper.isNumber;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.sameInstance;
Expand Down Expand Up @@ -118,4 +120,52 @@ public void testToShortString() throws Exception {
assertThat(toShortString(null), equalTo("null"));
assertThat(toShortString(new ClassHelperTest()), startsWith("ClassHelperTest@"));
}

@Test
public void testConvertPrimitive() throws Exception {

assertThat(convertPrimitive(char.class, ""), equalTo('\0'));
assertThat(convertPrimitive(char.class, null), equalTo(null));
assertThat(convertPrimitive(char.class, "6"), equalTo('6'));

assertThat(convertPrimitive(boolean.class, ""), equalTo(Boolean.FALSE));
assertThat(convertPrimitive(boolean.class, null), equalTo(null));
assertThat(convertPrimitive(boolean.class, "true"), equalTo(Boolean.TRUE));


assertThat(convertPrimitive(byte.class, ""), equalTo(null));
assertThat(convertPrimitive(byte.class, null), equalTo(null));
assertThat(convertPrimitive(byte.class, "127"), equalTo(Byte.MAX_VALUE));


assertThat(convertPrimitive(short.class, ""), equalTo(null));
assertThat(convertPrimitive(short.class, null), equalTo(null));
assertThat(convertPrimitive(short.class, "32767"), equalTo(Short.MAX_VALUE));

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

assertThat(convertPrimitive(long.class, ""), equalTo(null));
assertThat(convertPrimitive(long.class, null), equalTo(null));
assertThat(convertPrimitive(long.class, "6"), equalTo(new Long(6)));

assertThat(convertPrimitive(float.class, ""), equalTo(null));
assertThat(convertPrimitive(float.class, null), equalTo(null));
assertThat(convertPrimitive(float.class, "1.1"), equalTo(new Float(1.1)));

assertThat(convertPrimitive(double.class, ""), equalTo(null));
assertThat(convertPrimitive(double.class, null), equalTo(null));
assertThat(convertPrimitive(double.class, "10.1"), equalTo(new Double(10.1)));
}

@Test
public void testIsNumber() throws Exception {
assertThat(isNumber("0"), is(true));
assertThat(isNumber("0.1"), is(true));
assertThat(isNumber("DUBBO"), is(false));
assertThat(isNumber(""), is(false));
assertThat(isNumber(" "), is(false));
assertThat(isNumber(" "), is(false));
}
}