Skip to content

Commit c2c9de9

Browse files
CrazyHZMbeiwei30
authored andcommitted
[Dubbo-3069]Use regular expressions to judge fix #3069 (#3093)
* Use regular expressions to judge fix #3069 * moved into Constants class * modify * Unused import * modify * can not put it in front * catch NumberFormatException and return 'null' if necessary * remove recursive call * support .1 and 1. * modify
1 parent 1ecf2dd commit c2c9de9

File tree

6 files changed

+136
-57
lines changed

6 files changed

+136
-57
lines changed

dubbo-common/src/main/java/org/apache/dubbo/common/Constants.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ public class Constants {
9797

9898
public static final String DEFAULT_PROXY = "javassist";
9999

100-
public static final int DEFAULT_PAYLOAD = 8 * 1024 * 1024; // 8M
100+
/**
101+
* 8M
102+
*/
103+
public static final int DEFAULT_PAYLOAD = 8 * 1024 * 1024;
101104

102105
public static final String DEFAULT_CLUSTER = "failover";
103106

@@ -155,8 +158,9 @@ public class Constants {
155158

156159
public static final int DEFAULT_CONNECT_TIMEOUT = 3000;
157160

158-
// public static final int DEFAULT_REGISTRY_CONNECT_TIMEOUT = 5000;
159-
161+
/**
162+
* public static final int DEFAULT_REGISTRY_CONNECT_TIMEOUT = 5000;
163+
*/
160164
public static final int DEFAULT_RETRIES = 2;
161165

162166
public static final int DEFAULT_FAILBACK_TASKS = 100;
@@ -165,7 +169,9 @@ public class Constants {
165169

166170
public static final int MAX_PROXY_COUNT = 65535;
167171

168-
// default buffer size is 8k.
172+
/**
173+
* default buffer size is 8k.
174+
*/
169175
public static final int DEFAULT_BUFFER_SIZE = 8 * 1024;
170176

171177
public static final Integer DEFAULT_METADATA_REPORT_RETRY_TIMES = 100;
@@ -188,7 +194,9 @@ public class Constants {
188194

189195
public static final String LOADBALANCE_KEY = "loadbalance";
190196

191-
// key for router type, for e.g., "script"/"file", corresponding to ScriptRouterFactory.NAME, FileRouterFactory.NAME
197+
/**
198+
* key for router type, for e.g., "script"/"file", corresponding to ScriptRouterFactory.NAME, FileRouterFactory.NAME
199+
*/
192200
public static final String ROUTER_KEY = "router";
193201

194202
public static final String CLUSTER_KEY = "cluster";
@@ -752,7 +760,9 @@ public class Constants {
752760
public static final String CONFIG_VERSION_KEY = "configVersion";
753761

754762
public static final String COMPATIBLE_CONFIG_KEY = "compatible_config";
755-
// package version in the manifest
763+
/**
764+
* package version in the manifest
765+
*/
756766
public static final String RELEASE_KEY = "release";
757767

758768
public static final String OVERRIDE_PROVIDERS_KEY = "providerAddresses";

dubbo-common/src/main/java/org/apache/dubbo/common/utils/ClassHelper.java

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717
package org.apache.dubbo.common.utils;
1818

19+
1920
import java.lang.reflect.Array;
2021
import java.lang.reflect.Method;
2122
import java.lang.reflect.Modifier;
@@ -104,8 +105,7 @@ public static ClassLoader getClassLoader(Class<?> clazz) {
104105
// getClassLoader() returning null indicates the bootstrap ClassLoader
105106
try {
106107
cl = ClassLoader.getSystemClassLoader();
107-
}
108-
catch (Throwable ex) {
108+
} catch (Throwable ex) {
109109
// Cannot access system ClassLoader - oh well, maybe the caller can live with null...
110110
}
111111
}
@@ -265,26 +265,34 @@ public static boolean isPrimitive(Class<?> type) {
265265
}
266266

267267
public static Object convertPrimitive(Class<?> type, String value) {
268-
if (type == char.class || type == Character.class) {
268+
if (value == null) {
269+
return null;
270+
} else if (type == char.class || type == Character.class) {
269271
return value.length() > 0 ? value.charAt(0) : '\0';
270272
} else if (type == boolean.class || type == Boolean.class) {
271273
return Boolean.valueOf(value);
272-
} else if (type == byte.class || type == Byte.class) {
273-
return Byte.valueOf(value);
274-
} else if (type == short.class || type == Short.class) {
275-
return Short.valueOf(value);
276-
} else if (type == int.class || type == Integer.class) {
277-
return Integer.valueOf(value);
278-
} else if (type == long.class || type == Long.class) {
279-
return Long.valueOf(value);
280-
} else if (type == float.class || type == Float.class) {
281-
return Float.valueOf(value);
282-
} else if (type == double.class || type == Double.class) {
283-
return Double.valueOf(value);
274+
}
275+
try {
276+
if (type == byte.class || type == Byte.class) {
277+
return Byte.valueOf(value);
278+
} else if (type == short.class || type == Short.class) {
279+
return Short.valueOf(value);
280+
} else if (type == int.class || type == Integer.class) {
281+
return Integer.valueOf(value);
282+
} else if (type == long.class || type == Long.class) {
283+
return Long.valueOf(value);
284+
} else if (type == float.class || type == Float.class) {
285+
return Float.valueOf(value);
286+
} else if (type == double.class || type == Double.class) {
287+
return Double.valueOf(value);
288+
}
289+
} catch (NumberFormatException e) {
290+
return null;
284291
}
285292
return value;
286293
}
287294

295+
288296
/**
289297
* We only check boolean value at this moment.
290298
*

dubbo-common/src/main/java/org/apache/dubbo/common/utils/StringUtils.java

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@ private StringUtils() {
5555
* Gets a CharSequence length or {@code 0} if the CharSequence is
5656
* {@code null}.
5757
*
58-
* @param cs
59-
* a CharSequence or {@code null}
58+
* @param cs a CharSequence or {@code null}
6059
* @return CharSequence length or {@code 0} if the CharSequence is
61-
* {@code null}.
60+
* {@code null}.
6261
*/
6362
public static int length(final CharSequence cs) {
6463
return cs == null ? 0 : cs.length();
@@ -77,10 +76,10 @@ public static int length(final CharSequence cs) {
7776
* StringUtils.repeat("a", -2) = ""
7877
* </pre>
7978
*
80-
* @param str the String to repeat, may be null
81-
* @param repeat number of times to repeat str, negative treated as zero
79+
* @param str the String to repeat, may be null
80+
* @param repeat number of times to repeat str, negative treated as zero
8281
* @return a new String consisting of the original String repeated,
83-
* {@code null} if null String input
82+
* {@code null} if null String input
8483
*/
8584
public static String repeat(final String str, final int repeat) {
8685
// Performance tuned for 2.0 (JDK1.4)
@@ -101,9 +100,9 @@ public static String repeat(final String str, final int repeat) {
101100

102101
final int outputLength = inputLength * repeat;
103102
switch (inputLength) {
104-
case 1 :
103+
case 1:
105104
return repeat(str.charAt(0), repeat);
106-
case 2 :
105+
case 2:
107106
final char ch0 = str.charAt(0);
108107
final char ch1 = str.charAt(1);
109108
final char[] output2 = new char[outputLength];
@@ -112,7 +111,7 @@ public static String repeat(final String str, final int repeat) {
112111
output2[i + 1] = ch1;
113112
}
114113
return new String(output2);
115-
default :
114+
default:
116115
final StringBuilder buf = new StringBuilder(outputLength);
117116
for (int i = 0; i < repeat; i++) {
118117
buf.append(str);
@@ -134,15 +133,15 @@ public static String repeat(final String str, final int repeat) {
134133
* StringUtils.repeat("?", ", ", 3) = "?, ?, ?"
135134
* </pre>
136135
*
137-
* @param str the String to repeat, may be null
138-
* @param separator the String to inject, may be null
139-
* @param repeat number of times to repeat str, negative treated as zero
136+
* @param str the String to repeat, may be null
137+
* @param separator the String to inject, may be null
138+
* @param repeat number of times to repeat str, negative treated as zero
140139
* @return a new String consisting of the original String repeated,
141-
* {@code null} if null String input
140+
* {@code null} if null String input
142141
* @since 2.5
143142
*/
144143
public static String repeat(final String str, final String separator, final int repeat) {
145-
if(str == null || separator == null) {
144+
if (str == null || separator == null) {
146145
return repeat(str, repeat);
147146
}
148147
// given that repeat(String, int) is quite optimized, better to rely on it than try and splice this into it
@@ -168,10 +167,10 @@ public static String repeat(final String str, final String separator, final int
168167
* StringUtils.removeEnd("abc", "") = "abc"
169168
* </pre>
170169
*
171-
* @param str the source String to search, may be null
172-
* @param remove the String to search for and remove, may be null
170+
* @param str the source String to search, may be null
171+
* @param remove the String to search for and remove, may be null
173172
* @return the substring with the string removed if found,
174-
* {@code null} if null String input
173+
* {@code null} if null String input
175174
*/
176175
public static String removeEnd(final String str, final String remove) {
177176
if (isAnyEmpty(str, remove)) {
@@ -200,8 +199,8 @@ public static String removeEnd(final String str, final String remove) {
200199
* consider using {@link #repeat(String, int)} instead.
201200
* </p>
202201
*
203-
* @param ch character to repeat
204-
* @param repeat number of times to repeat char, negative treated as zero
202+
* @param ch character to repeat
203+
* @param repeat number of times to repeat char, negative treated as zero
205204
* @return String with repeated character
206205
* @see #repeat(String, int)
207206
*/
@@ -234,8 +233,8 @@ public static String repeat(final char ch, final int repeat) {
234233
* StringUtils.stripEnd("120.00", ".0") = "12"
235234
* </pre>
236235
*
237-
* @param str the String to remove characters from, may be null
238-
* @param stripChars the set of characters to remove, null treated as whitespace
236+
* @param str the String to remove characters from, may be null
237+
* @param stripChars the set of characters to remove, null treated as whitespace
239238
* @return the stripped String, {@code null} if null String input
240239
*/
241240
public static String stripEnd(final String str, final String stripChars) {
@@ -274,12 +273,12 @@ public static String stripEnd(final String str, final String stripChars) {
274273
* StringUtils.replace("aba", "a", "z") = "zbz"
275274
* </pre>
276275
*
277-
* @see #replace(String text, String searchString, String replacement, int max)
278-
* @param text text to search and replace in, may be null
279-
* @param searchString the String to search for, may be null
276+
* @param text text to search and replace in, may be null
277+
* @param searchString the String to search for, may be null
280278
* @param replacement the String to replace it with, may be null
281279
* @return the text with any replacements processed,
282-
* {@code null} if null String input
280+
* {@code null} if null String input
281+
* @see #replace(String text, String searchString, String replacement, int max)
283282
*/
284283
public static String replace(final String text, final String searchString, final String replacement) {
285284
return replace(text, searchString, replacement, -1);
@@ -306,12 +305,12 @@ public static String replace(final String text, final String searchString, final
306305
* StringUtils.replace("abaa", "a", "z", -1) = "zbzz"
307306
* </pre>
308307
*
309-
* @param text text to search and replace in, may be null
310-
* @param searchString the String to search for, may be null
308+
* @param text text to search and replace in, may be null
309+
* @param searchString the String to search for, may be null
311310
* @param replacement the String to replace it with, may be null
312-
* @param max maximum number of values to replace, or {@code -1} if no maximum
311+
* @param max maximum number of values to replace, or {@code -1} if no maximum
313312
* @return the text with any replacements processed,
314-
* {@code null} if null String input
313+
* {@code null} if null String input
315314
*/
316315
public static String replace(final String text, final String searchString, final String replacement, int max) {
317316
if (isAnyEmpty(text, searchString) || replacement == null || max == 0) {
@@ -374,7 +373,7 @@ public static boolean isNoneEmpty(final String... ss) {
374373
if (ArrayUtils.isEmpty(ss)) {
375374
return false;
376375
}
377-
for (final String s : ss){
376+
for (final String s : ss) {
378377
if (isEmpty(s)) {
379378
return false;
380379
}
@@ -478,19 +477,28 @@ public static boolean isContains(String[] values, String value) {
478477
return false;
479478
}
480479

481-
public static boolean isNumeric(String str) {
482-
if (str == null) {
480+
public static boolean isNumeric(String str, boolean allowDot) {
481+
if (str == null || str.isEmpty()) {
483482
return false;
484483
}
484+
boolean hasDot = false;
485485
int sz = str.length();
486486
for (int i = 0; i < sz; i++) {
487+
if (str.charAt(i) == '.') {
488+
if (hasDot || !allowDot) {
489+
return false;
490+
}
491+
hasDot = true;
492+
continue;
493+
}
487494
if (!Character.isDigit(str.charAt(i))) {
488495
return false;
489496
}
490497
}
491498
return true;
492499
}
493500

501+
494502
/**
495503
* @param e
496504
* @return string

dubbo-common/src/test/java/org/apache/dubbo/common/utils/ClassHelperTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import static org.apache.dubbo.common.utils.ClassHelper.getClassLoader;
2626
import static org.apache.dubbo.common.utils.ClassHelper.resolvePrimitiveClassName;
2727
import static org.apache.dubbo.common.utils.ClassHelper.toShortString;
28+
import static org.apache.dubbo.common.utils.ClassHelper.convertPrimitive;
2829
import static org.hamcrest.Matchers.equalTo;
2930
import static org.hamcrest.Matchers.is;
3031
import static org.hamcrest.Matchers.sameInstance;
@@ -118,4 +119,43 @@ public void testToShortString() throws Exception {
118119
assertThat(toShortString(null), equalTo("null"));
119120
assertThat(toShortString(new ClassHelperTest()), startsWith("ClassHelperTest@"));
120121
}
122+
123+
@Test
124+
public void testConvertPrimitive() throws Exception {
125+
126+
assertThat(convertPrimitive(char.class, ""), equalTo('\0'));
127+
assertThat(convertPrimitive(char.class, null), equalTo(null));
128+
assertThat(convertPrimitive(char.class, "6"), equalTo('6'));
129+
130+
assertThat(convertPrimitive(boolean.class, ""), equalTo(Boolean.FALSE));
131+
assertThat(convertPrimitive(boolean.class, null), equalTo(null));
132+
assertThat(convertPrimitive(boolean.class, "true"), equalTo(Boolean.TRUE));
133+
134+
135+
assertThat(convertPrimitive(byte.class, ""), equalTo(null));
136+
assertThat(convertPrimitive(byte.class, null), equalTo(null));
137+
assertThat(convertPrimitive(byte.class, "127"), equalTo(Byte.MAX_VALUE));
138+
139+
140+
assertThat(convertPrimitive(short.class, ""), equalTo(null));
141+
assertThat(convertPrimitive(short.class, null), equalTo(null));
142+
assertThat(convertPrimitive(short.class, "32767"), equalTo(Short.MAX_VALUE));
143+
144+
assertThat(convertPrimitive(int.class, ""), equalTo(null));
145+
assertThat(convertPrimitive(int.class, null), equalTo(null));
146+
assertThat(convertPrimitive(int.class, "6"), equalTo(6));
147+
148+
assertThat(convertPrimitive(long.class, ""), equalTo(null));
149+
assertThat(convertPrimitive(long.class, null), equalTo(null));
150+
assertThat(convertPrimitive(long.class, "6"), equalTo(new Long(6)));
151+
152+
assertThat(convertPrimitive(float.class, ""), equalTo(null));
153+
assertThat(convertPrimitive(float.class, null), equalTo(null));
154+
assertThat(convertPrimitive(float.class, "1.1"), equalTo(new Float(1.1)));
155+
156+
assertThat(convertPrimitive(double.class, ""), equalTo(null));
157+
assertThat(convertPrimitive(double.class, null), equalTo(null));
158+
assertThat(convertPrimitive(double.class, "10.1"), equalTo(new Double(10.1)));
159+
}
160+
121161
}

dubbo-common/src/test/java/org/apache/dubbo/common/utils/StringUtilsTest.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,22 @@ public void testIsContains() throws Exception {
240240

241241
@Test
242242
public void testIsNumeric() throws Exception {
243-
assertThat(StringUtils.isNumeric("123"), is(true));
244-
assertThat(StringUtils.isNumeric("1a3"), is(false));
245-
assertThat(StringUtils.isNumeric(null), is(false));
243+
assertThat(StringUtils.isNumeric("123", false), is(true));
244+
assertThat(StringUtils.isNumeric("1a3", false), is(false));
245+
assertThat(StringUtils.isNumeric(null, false), is(false));
246+
247+
assertThat(StringUtils.isNumeric("0", true), is(true));
248+
assertThat(StringUtils.isNumeric("0.1", true), is(true));
249+
assertThat(StringUtils.isNumeric("DUBBO", true), is(false));
250+
assertThat(StringUtils.isNumeric("", true), is(false));
251+
assertThat(StringUtils.isNumeric(" ", true), is(false));
252+
assertThat(StringUtils.isNumeric(" ", true), is(false));
253+
254+
assertThat(StringUtils.isNumeric("123.3.3", true), is(false));
255+
assertThat(StringUtils.isNumeric("123.", true), is(true));
256+
assertThat(StringUtils.isNumeric(".123", true), is(true));
257+
assertThat(StringUtils.isNumeric("..123", true), is(false));
258+
246259
}
247260

248261
@Test

dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/support/MockInvoker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public static Object parseMockValue(String mock, Type[] returnTypes) throws Exce
7070
value = mock.subSequence(1, mock.length() - 1);
7171
} else if (returnTypes != null && returnTypes.length > 0 && returnTypes[0] == String.class) {
7272
value = mock;
73-
} else if (StringUtils.isNumeric(mock)) {
73+
} else if (StringUtils.isNumeric(mock, false)) {
7474
value = JSON.parse(mock);
7575
} else if (mock.startsWith("{")) {
7676
value = JSON.parseObject(mock, Map.class);

0 commit comments

Comments
 (0)