Skip to content

Commit f223d36

Browse files
authored
Merge pull request #187 from mercyblitz/dev
Release 0.1.5
2 parents 1c0340c + 1b79456 commit f223d36

6 files changed

Lines changed: 54 additions & 32 deletions

File tree

microsphere-java-core/src/main/java/io/microsphere/convert/Converter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,9 +158,9 @@ static <S, T> Converter<S, T> getConverter(Class<S> sourceType, Class<T> targetT
158158
* @return the converted object of type {@code T}, or {@code null} if no suitable converter is found
159159
*/
160160
static <T> T convertIfPossible(Object source, Class<T> targetType) {
161-
Converter converter = getConverter(source.getClass(), targetType);
161+
Converter<Object, T> converter = (Converter<Object, T>) getConverter(source.getClass(), targetType);
162162
if (converter != null) {
163-
return (T) converter.convert(source);
163+
return converter.convert(source);
164164
}
165165
return null;
166166
}

microsphere-java-core/src/main/java/io/microsphere/json/JSONUtils.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ public abstract class JSONUtils implements Utils {
128128
static {
129129
REPLACEMENT_CHARS = new String[128];
130130
for (int i = 0; i <= 0x1f; i++) {
131-
REPLACEMENT_CHARS[i] = format("\\u%04x", (int) i);
131+
REPLACEMENT_CHARS[i] = format("\\u%04x", i);
132132
}
133133
REPLACEMENT_CHARS['"'] = "\\\"";
134134
REPLACEMENT_CHARS['\\'] = "\\\\";
@@ -1350,8 +1350,9 @@ public static String escape(@Nullable String v) {
13501350
String replacement;
13511351
if (c < 0x80) {
13521352
replacement = REPLACEMENT_CHARS[c];
1353-
if (replacement == null)
1353+
if (replacement == null) {
13541354
continue;
1355+
}
13551356
} else if (c == '\u2028') {
13561357
replacement = U2028;
13571358
} else if (c == '\u2029') {
@@ -1361,12 +1362,14 @@ public static String escape(@Nullable String v) {
13611362
}
13621363
if (afterReplacement < i) { // write characters between the last replacement
13631364
// and now
1364-
if (builder == null)
1365+
if (builder == null) {
13651366
builder = new StringBuilder(length);
1367+
}
13661368
builder.append(v, afterReplacement, i);
13671369
}
1368-
if (builder == null)
1370+
if (builder == null) {
13691371
builder = new StringBuilder(length);
1372+
}
13701373
builder.append(replacement);
13711374
afterReplacement = i + 1;
13721375
}

microsphere-java-core/src/main/java/io/microsphere/reflect/MethodUtils.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,8 @@ public abstract class MethodUtils implements Utils {
183183
* Method substringMethod = MethodUtils.findMethod(String.class, "substring", int.class, int.class);
184184
* // substringMethod will be null
185185
* }</pre>
186-
*
187-
* @throws RuntimeException if any error occurs during the initialization process,
188-
* such as class not found, method not found, or other reflection-related issues.
189186
*/
190-
public static void initBannedMethods() throws RuntimeException {
187+
public static void initBannedMethods() {
191188
String bannedMethodsPropertyValue = getSystemProperty(BANNED_METHODS_PROPERTY_NAME);
192189
String[] bannedMethodsSignatures = split(bannedMethodsPropertyValue, VERTICAL_BAR_CHAR);
193190
int length = length(bannedMethodsSignatures);
@@ -204,13 +201,13 @@ public static void initBannedMethods() throws RuntimeException {
204201
for (int i = 0; i < parameterClassNames.length; i++) {
205202
parameterTypes[i] = resolveClass(parameterClassNames[i], classLoader);
206203
}
207-
addBannedMethod(declaredClass, methodName, parameterTypes);
204+
banMethod(declaredClass, methodName, parameterTypes);
208205
}
209206
}
210207
}
211208

212209
/**
213-
* Adds a banned method to the cache based on the provided class, method name, and parameter types.
210+
* Bans method to the cache based on the provided class, method name, and parameter types.
214211
* <p>
215212
* This method creates a {@link MethodKey} using the specified parameters, finds the corresponding {@link Method}
216213
* using {@link #doFindMethod(MethodKey)}, and stores it in the {@link #bannedMethodsCache}. If the method is already
@@ -220,7 +217,7 @@ public static void initBannedMethods() throws RuntimeException {
220217
* <h3>Example Usage</h3>
221218
* <pre>{@code
222219
* // Ban the String.substring(int, int) method
223-
* MethodUtils.addBannedMethod(String.class, "substring", int.class, int.class);
220+
* MethodUtils.banMethod(String.class, "substring", int.class, int.class);
224221
*
225222
* // After this call, findMethod will return null for the banned method
226223
* Method method = MethodUtils.findMethod(String.class, "substring", int.class, int.class);
@@ -235,7 +232,7 @@ public static void initBannedMethods() throws RuntimeException {
235232
* @see #initBannedMethods()
236233
* @see #bannedMethodsCache
237234
*/
238-
public static Method addBannedMethod(Class<?> declaredClass, String methodName, Class<?>... parameterTypes) {
235+
public static Method banMethod(Class<?> declaredClass, String methodName, Class<?>... parameterTypes) {
239236
MethodKey key = buildKey(declaredClass, methodName, parameterTypes);
240237
Method method = methodsCache.computeIfAbsent(key, MethodUtils::doFindMethod);
241238
bannedMethodsCache.put(key, method);

microsphere-java-core/src/main/java/io/microsphere/util/SystemUtils.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@
2020
import io.microsphere.logging.Logger;
2121

2222
import java.util.Map;
23+
import java.util.Properties;
2324

24-
import static io.microsphere.collection.MapUtils.newHashMap;
25+
import static io.microsphere.collection.MapUtils.newFixedHashMap;
2526
import static io.microsphere.logging.LoggerFactory.getLogger;
2627
import static io.microsphere.util.StringUtils.startsWith;
2728
import static java.lang.System.getProperties;
@@ -540,17 +541,19 @@ public abstract class SystemUtils implements Utils {
540541
* @see #getSystemProperty(String, String)
541542
*/
542543
public static void copySystemProperties() {
543-
Map properties = getProperties();
544+
Properties properties = getProperties();
544545
synchronized (SystemUtils.class) {
545546
Map<String, String> copy = systemPropertiesCopy;
546547
if (copy == null) {
547-
copy = newHashMap(properties.size());
548+
copy = newFixedHashMap(properties.size());
548549
systemPropertiesCopy = copy;
550+
} else {
551+
copy.clear();
549552
}
550-
copy.putAll(properties);
553+
copy.putAll((Map) properties);
551554
}
552555
if (logger.isTraceEnabled()) {
553-
logger.trace("The JDK System Properties has been copied : {}", systemPropertiesCopy);
556+
logger.trace("The JDK System Properties has been copied : {}", properties);
554557
}
555558
}
556559

microsphere-java-core/src/test/java/io/microsphere/reflect/MethodUtilsTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import static io.microsphere.reflect.MethodUtils.OBJECT_PUBLIC_METHODS;
3939
import static io.microsphere.reflect.MethodUtils.PUBLIC_METHOD_PREDICATE;
4040
import static io.microsphere.reflect.MethodUtils.STATIC_METHOD_PREDICATE;
41-
import static io.microsphere.reflect.MethodUtils.addBannedMethod;
41+
import static io.microsphere.reflect.MethodUtils.banMethod;
4242
import static io.microsphere.reflect.MethodUtils.buildKey;
4343
import static io.microsphere.reflect.MethodUtils.buildSignature;
4444
import static io.microsphere.reflect.MethodUtils.clearBannedMethods;
@@ -143,13 +143,13 @@ void testInitBannedMethodsWithoutProperty() {
143143
}
144144

145145
@Test
146-
void testAddBannedMethod() {
147-
assertAddBannedMethod(String.class, "substring", int.class);
148-
assertAddBannedMethod(String.class, "substring", int.class, int.class);
146+
void testBanMethod() {
147+
assertBanMethod(String.class, "substring", int.class);
148+
assertBanMethod(String.class, "substring", int.class, int.class);
149149
}
150150

151-
void assertAddBannedMethod(Class<?> declaringClass, String methodName, Class<?>... parameterTypes) {
152-
addBannedMethod(declaringClass, methodName, parameterTypes);
151+
void assertBanMethod(Class<?> declaringClass, String methodName, Class<?>... parameterTypes) {
152+
banMethod(declaringClass, methodName, parameterTypes);
153153
assertNull(findMethod(declaringClass, methodName, parameterTypes));
154154
}
155155

microsphere-java-core/src/test/java/io/microsphere/util/StopWatchTest.java

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import io.microsphere.logging.Logger;
2020
import io.microsphere.logging.LoggerFactory;
21+
import io.microsphere.util.StopWatch.Task;
2122
import org.junit.jupiter.api.BeforeEach;
2223
import org.junit.jupiter.api.Test;
2324

@@ -26,6 +27,7 @@
2627
import static java.util.concurrent.TimeUnit.MILLISECONDS;
2728
import static org.junit.jupiter.api.Assertions.assertEquals;
2829
import static org.junit.jupiter.api.Assertions.assertFalse;
30+
import static org.junit.jupiter.api.Assertions.assertNotEquals;
2931
import static org.junit.jupiter.api.Assertions.assertNull;
3032
import static org.junit.jupiter.api.Assertions.assertSame;
3133
import static org.junit.jupiter.api.Assertions.assertThrows;
@@ -42,11 +44,14 @@ class StopWatchTest {
4244

4345
private static final Logger logger = LoggerFactory.getLogger(StopWatchTest.class);
4446

47+
private static final String testName = "test";
48+
49+
4550
private StopWatch stopWatch;
4651

4752
@BeforeEach
4853
void setUp() {
49-
stopWatch = new StopWatch("test");
54+
stopWatch = new StopWatch(testName);
5055
}
5156

5257
@Test
@@ -57,12 +62,12 @@ void test() throws InterruptedException {
5762
Thread.sleep(10);
5863
stopWatch.stop();
5964
stopWatch.stop();
60-
StopWatch.Task currentTask = stopWatch.getCurrentTask();
65+
Task currentTask = stopWatch.getCurrentTask();
6166
assertNull(currentTask);
62-
assertEquals("test", stopWatch.getId());
67+
assertEquals(testName, stopWatch.getId());
6368
assertEquals(0, stopWatch.getRunningTasks().size());
6469
assertEquals(2, stopWatch.getCompletedTasks().size());
65-
StopWatch.Task task = stopWatch.getCompletedTasks().get(1);
70+
Task task = stopWatch.getCompletedTasks().get(1);
6671
assertEquals("1", task.getTaskName());
6772
assertFalse(task.isReentrant());
6873
assertTrue(task.getStartTimeNanos() > 0);
@@ -74,18 +79,32 @@ void test() throws InterruptedException {
7479

7580
@Test
7681
void testTask() {
77-
String testName = "test";
78-
StopWatch.Task task = start(testName);
82+
Task task = start(testName);
7983
task.stop();
8084
assertSame(testName, task.getTaskName());
8185
assertFalse(task.isReentrant());
8286
assertTrue(task.getStartTimeNanos() > 0);
8387
assertTrue(task.getElapsedNanos() > 0);
88+
}
89+
90+
@Test
91+
void testTaskOnEquals() {
92+
Task task = start(testName);
93+
assertEquals(task, task);
8494
assertEquals(task, start(testName));
85-
assertEquals(task.hashCode(), start(testName).hashCode());
95+
}
8696

97+
@Test
98+
void testTaskOnNotEquals() {
99+
Task task = start(testName);
100+
assertNotEquals(task, testName);
87101
}
88102

103+
@Test
104+
void testTaskHashCode() {
105+
Task task = start(testName);
106+
assertEquals(task.hashCode(), start(testName).hashCode());
107+
}
89108

90109
@Test
91110
void testStartOnNullTaskName() {

0 commit comments

Comments
 (0)