Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -158,9 +158,9 @@ static <S, T> Converter<S, T> getConverter(Class<S> sourceType, Class<T> targetT
* @return the converted object of type {@code T}, or {@code null} if no suitable converter is found
*/
static <T> T convertIfPossible(Object source, Class<T> targetType) {
Converter converter = getConverter(source.getClass(), targetType);
Converter<Object, T> converter = (Converter<Object, T>) getConverter(source.getClass(), targetType);
if (converter != null) {
return (T) converter.convert(source);
return converter.convert(source);
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public abstract class JSONUtils implements Utils {
static {
REPLACEMENT_CHARS = new String[128];
for (int i = 0; i <= 0x1f; i++) {
REPLACEMENT_CHARS[i] = format("\\u%04x", (int) i);
REPLACEMENT_CHARS[i] = format("\\u%04x", i);
}
REPLACEMENT_CHARS['"'] = "\\\"";
REPLACEMENT_CHARS['\\'] = "\\\\";
Expand Down Expand Up @@ -1350,8 +1350,9 @@ public static String escape(@Nullable String v) {
String replacement;
if (c < 0x80) {
replacement = REPLACEMENT_CHARS[c];
if (replacement == null)
if (replacement == null) {
continue;
}
} else if (c == '\u2028') {
replacement = U2028;
} else if (c == '\u2029') {
Expand All @@ -1361,12 +1362,14 @@ public static String escape(@Nullable String v) {
}
if (afterReplacement < i) { // write characters between the last replacement
// and now
if (builder == null)
if (builder == null) {
builder = new StringBuilder(length);
}
builder.append(v, afterReplacement, i);
}
if (builder == null)
if (builder == null) {
builder = new StringBuilder(length);
}
builder.append(replacement);
afterReplacement = i + 1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,8 @@ public abstract class MethodUtils implements Utils {
* Method substringMethod = MethodUtils.findMethod(String.class, "substring", int.class, int.class);
* // substringMethod will be null
* }</pre>
*
* @throws RuntimeException if any error occurs during the initialization process,
* such as class not found, method not found, or other reflection-related issues.
*/
public static void initBannedMethods() throws RuntimeException {
public static void initBannedMethods() {
String bannedMethodsPropertyValue = getSystemProperty(BANNED_METHODS_PROPERTY_NAME);
String[] bannedMethodsSignatures = split(bannedMethodsPropertyValue, VERTICAL_BAR_CHAR);
int length = length(bannedMethodsSignatures);
Expand All @@ -204,13 +201,13 @@ public static void initBannedMethods() throws RuntimeException {
for (int i = 0; i < parameterClassNames.length; i++) {
parameterTypes[i] = resolveClass(parameterClassNames[i], classLoader);
}
addBannedMethod(declaredClass, methodName, parameterTypes);
banMethod(declaredClass, methodName, parameterTypes);
}
}
}

/**
* Adds a banned method to the cache based on the provided class, method name, and parameter types.
* Bans method to the cache based on the provided class, method name, and parameter types.
* <p>
* This method creates a {@link MethodKey} using the specified parameters, finds the corresponding {@link Method}
* using {@link #doFindMethod(MethodKey)}, and stores it in the {@link #bannedMethodsCache}. If the method is already
Expand All @@ -220,7 +217,7 @@ public static void initBannedMethods() throws RuntimeException {
* <h3>Example Usage</h3>
* <pre>{@code
* // Ban the String.substring(int, int) method
* MethodUtils.addBannedMethod(String.class, "substring", int.class, int.class);
* MethodUtils.banMethod(String.class, "substring", int.class, int.class);
*
* // After this call, findMethod will return null for the banned method
* Method method = MethodUtils.findMethod(String.class, "substring", int.class, int.class);
Expand All @@ -235,7 +232,7 @@ public static void initBannedMethods() throws RuntimeException {
* @see #initBannedMethods()
* @see #bannedMethodsCache
*/
public static Method addBannedMethod(Class<?> declaredClass, String methodName, Class<?>... parameterTypes) {
public static Method banMethod(Class<?> declaredClass, String methodName, Class<?>... parameterTypes) {
MethodKey key = buildKey(declaredClass, methodName, parameterTypes);
Method method = methodsCache.computeIfAbsent(key, MethodUtils::doFindMethod);
bannedMethodsCache.put(key, method);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@
import io.microsphere.logging.Logger;

import java.util.Map;
import java.util.Properties;

import static io.microsphere.collection.MapUtils.newHashMap;
import static io.microsphere.collection.MapUtils.newFixedHashMap;
import static io.microsphere.logging.LoggerFactory.getLogger;
import static io.microsphere.util.StringUtils.startsWith;
import static java.lang.System.getProperties;
Expand Down Expand Up @@ -540,17 +541,19 @@ public abstract class SystemUtils implements Utils {
* @see #getSystemProperty(String, String)
*/
public static void copySystemProperties() {
Map properties = getProperties();
Properties properties = getProperties();
synchronized (SystemUtils.class) {
Map<String, String> copy = systemPropertiesCopy;
if (copy == null) {
copy = newHashMap(properties.size());
copy = newFixedHashMap(properties.size());
systemPropertiesCopy = copy;
} else {
copy.clear();
}
copy.putAll(properties);
copy.putAll((Map) properties);
}
if (logger.isTraceEnabled()) {
logger.trace("The JDK System Properties has been copied : {}", systemPropertiesCopy);
logger.trace("The JDK System Properties has been copied : {}", properties);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import static io.microsphere.reflect.MethodUtils.OBJECT_PUBLIC_METHODS;
import static io.microsphere.reflect.MethodUtils.PUBLIC_METHOD_PREDICATE;
import static io.microsphere.reflect.MethodUtils.STATIC_METHOD_PREDICATE;
import static io.microsphere.reflect.MethodUtils.addBannedMethod;
import static io.microsphere.reflect.MethodUtils.banMethod;
import static io.microsphere.reflect.MethodUtils.buildKey;
import static io.microsphere.reflect.MethodUtils.buildSignature;
import static io.microsphere.reflect.MethodUtils.clearBannedMethods;
Expand Down Expand Up @@ -143,13 +143,13 @@ void testInitBannedMethodsWithoutProperty() {
}

@Test
void testAddBannedMethod() {
assertAddBannedMethod(String.class, "substring", int.class);
assertAddBannedMethod(String.class, "substring", int.class, int.class);
void testBanMethod() {
assertBanMethod(String.class, "substring", int.class);
assertBanMethod(String.class, "substring", int.class, int.class);
}

void assertAddBannedMethod(Class<?> declaringClass, String methodName, Class<?>... parameterTypes) {
addBannedMethod(declaringClass, methodName, parameterTypes);
void assertBanMethod(Class<?> declaringClass, String methodName, Class<?>... parameterTypes) {
banMethod(declaringClass, methodName, parameterTypes);
assertNull(findMethod(declaringClass, methodName, parameterTypes));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import io.microsphere.logging.Logger;
import io.microsphere.logging.LoggerFactory;
import io.microsphere.util.StopWatch.Task;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

Expand All @@ -26,6 +27,7 @@
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
Expand All @@ -42,11 +44,14 @@ class StopWatchTest {

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

private static final String testName = "test";


private StopWatch stopWatch;

@BeforeEach
void setUp() {
stopWatch = new StopWatch("test");
stopWatch = new StopWatch(testName);
}

@Test
Expand All @@ -57,12 +62,12 @@ void test() throws InterruptedException {
Thread.sleep(10);
stopWatch.stop();
stopWatch.stop();
StopWatch.Task currentTask = stopWatch.getCurrentTask();
Task currentTask = stopWatch.getCurrentTask();
assertNull(currentTask);
assertEquals("test", stopWatch.getId());
assertEquals(testName, stopWatch.getId());
assertEquals(0, stopWatch.getRunningTasks().size());
assertEquals(2, stopWatch.getCompletedTasks().size());
StopWatch.Task task = stopWatch.getCompletedTasks().get(1);
Task task = stopWatch.getCompletedTasks().get(1);
assertEquals("1", task.getTaskName());
assertFalse(task.isReentrant());
assertTrue(task.getStartTimeNanos() > 0);
Expand All @@ -74,18 +79,32 @@ void test() throws InterruptedException {

@Test
void testTask() {
String testName = "test";
StopWatch.Task task = start(testName);
Task task = start(testName);
task.stop();
assertSame(testName, task.getTaskName());
assertFalse(task.isReentrant());
assertTrue(task.getStartTimeNanos() > 0);
assertTrue(task.getElapsedNanos() > 0);
}

@Test
void testTaskOnEquals() {
Task task = start(testName);
assertEquals(task, task);
assertEquals(task, start(testName));
assertEquals(task.hashCode(), start(testName).hashCode());
}

@Test
void testTaskOnNotEquals() {
Task task = start(testName);
assertNotEquals(task, testName);
}

@Test
void testTaskHashCode() {
Task task = start(testName);
assertEquals(task.hashCode(), start(testName).hashCode());
}

@Test
void testStartOnNullTaskName() {
Expand Down