Browse Source

toString使用大括号

zhouhao 6 years ago
parent
commit
706c9b9e3c

+ 7 - 4
hsweb-core/src/main/java/org/hswebframework/web/bean/DefaultToStringOperator.java

@@ -5,6 +5,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.hswebframework.utils.time.DateFormatter;
 import org.springframework.beans.BeanUtils;
 import org.springframework.core.annotation.AnnotationUtils;
+import org.springframework.util.ReflectionUtils;
 
 import java.beans.PropertyDescriptor;
 import java.lang.reflect.Field;
@@ -112,7 +113,10 @@ public class DefaultToStringOperator<T> implements ToStringOperator<T> {
             ToString.Ignore propertyIgnore = null;
             long propertyFeature = 0;
             try {
-                Field field = targetType.getDeclaredField(descriptor.getName());
+                Field field = ReflectionUtils.findField(targetType, descriptor.getName());
+                if (null == field) {
+                    log.warn("无法获取字段{},该字段将不会被打码!", descriptor.getName());
+                }
                 propertyIgnore = field.getAnnotation(ToString.Ignore.class);
                 features = AnnotationUtils.getAnnotation(field, ToString.Features.class);
                 if (propertyIgnore != null) {
@@ -123,9 +127,8 @@ public class DefaultToStringOperator<T> implements ToStringOperator<T> {
                 if (null != features && features.value().length > 0) {
                     propertyFeature = ToString.Feature.createFeatures(features.value());
                 }
-            } catch (NoSuchFieldException e) {
+            } catch (Exception e) {
                 log.warn("无法获取字段{},该字段将不会被打码!", descriptor.getName());
-
             }
             //是否设置了打码
             boolean cover = (propertyIgnore == null && defaultCover) || (propertyIgnore != null && propertyIgnore.cover());
@@ -335,7 +338,7 @@ public class DefaultToStringOperator<T> implements ToStringOperator<T> {
         }
         boolean writeClassName = ToString.Feature.hasFeature(features, ToString.Feature.writeClassname);
 
-        StringJoiner joiner = new StringJoiner(", ", (writeClassName ? target.getClass().getSimpleName() : "") + "(", ")");
+        StringJoiner joiner = new StringJoiner(", ", (writeClassName ? target.getClass().getSimpleName() : "") + "{", "}");
 
         mapValue.forEach((key, value) -> joiner.add(key.concat("=").concat(String.valueOf(value))));