Skip to content

Commit

Permalink
Fix ServiceConfig Ref unable to toString (#12511)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbumenJ committed Jun 13, 2023
1 parent ee87c8d commit 83b4114
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
Expand Up @@ -16,7 +16,11 @@
*/
package org.apache.dubbo.common.utils;

import org.apache.dubbo.config.AbstractConfig;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class ToStringUtils {

Expand All @@ -36,4 +40,63 @@ public static String printToString(Object obj) {
return obj.toString();
}
}

public static String toString(Object obj) {
if (obj == null) {
return "null";
}
if (ClassUtils.isSimpleType(obj.getClass())) {
return obj.toString();
}
if (obj.getClass().isPrimitive()) {
return obj.toString();
}
if (obj instanceof Object[]) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
Object[] objects = (Object[]) obj;
for (int i = 0; i < objects.length; i++) {
stringBuilder.append(toString(objects[i]));
if (i != objects.length - 1) {
stringBuilder.append(", ");
}
}
stringBuilder.append("]");
return stringBuilder.toString();
}
if (obj instanceof List) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("[");
List list = (List) obj;
for (int i = 0; i < list.size(); i++) {
stringBuilder.append(toString(list.get(i)));
if (i != list.size() - 1) {
stringBuilder.append(", ");
}
}
stringBuilder.append("]");
return stringBuilder.toString();
}
if (obj instanceof Map) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("{");
Map map = (Map) obj;
int i = 0;
for (Object key : map.keySet()) {
stringBuilder.append(toString(key));
stringBuilder.append("=");
stringBuilder.append(toString(map.get(key)));
if (i != map.size() - 1) {
stringBuilder.append(", ");
}
i++;
}
stringBuilder.append("}");
return stringBuilder.toString();
}
if (obj instanceof AbstractConfig) {
return obj.toString();
}
return obj.getClass() + "@" + Integer.toHexString(System.identityHashCode(obj));
}
}
Expand Up @@ -31,6 +31,7 @@
import org.apache.dubbo.common.utils.MethodUtils;
import org.apache.dubbo.common.utils.ReflectUtils;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.common.utils.ToStringUtils;
import org.apache.dubbo.config.context.ConfigManager;
import org.apache.dubbo.config.context.ConfigMode;
import org.apache.dubbo.config.support.Nested;
Expand Down Expand Up @@ -967,7 +968,7 @@ public String toString() {
buf.append(' ');
buf.append(key);
buf.append("=\"");
buf.append(key.equals("password") ? "******" : value);
buf.append(key.equals("password") ? "******" : ToStringUtils.toString(value));
buf.append('\"');
}
} catch (Exception e) {
Expand Down
Expand Up @@ -683,4 +683,20 @@ public Set<String> removeCachedMapping(String serviceKey) {

scheduledExecutorService.shutdown();
}

@Test
void testToString() {
ServiceConfig<DemoService> serviceConfig = new ServiceConfig<>();
service.setRef(new DemoServiceImpl() {
@Override
public String toString() {
throw new IllegalStateException();
}
});
try {
serviceConfig.toString();
} catch (Throwable t) {
Assertions.fail(t);
}
}
}

0 comments on commit 83b4114

Please sign in to comment.