From b8071b528df6bfc06e0ca3b917d86270179bdcf3 Mon Sep 17 00:00:00 2001 From: Andrei Solntsev Date: Sun, 21 Jul 2024 17:04:28 +0300 Subject: [PATCH] #1306 add readable toString() methods to some objects ... to make it easier to read these values in logs --- .../java/net/datafaker/providers/base/AbstractProvider.java | 5 +++++ src/main/java/net/datafaker/providers/base/BaseFaker.java | 5 +++++ src/main/java/net/datafaker/service/FakeValuesService.java | 6 ++++++ src/main/java/net/datafaker/service/RandomService.java | 5 +++++ 4 files changed, 21 insertions(+) diff --git a/src/main/java/net/datafaker/providers/base/AbstractProvider.java b/src/main/java/net/datafaker/providers/base/AbstractProvider.java index 5663180b8..0a01ec821 100644 --- a/src/main/java/net/datafaker/providers/base/AbstractProvider.java +++ b/src/main/java/net/datafaker/providers/base/AbstractProvider.java @@ -42,4 +42,9 @@ protected List loadGenerators(Class generatorClass) { .map(ServiceLoader.Provider::get) .toList(); } + + @Override + public String toString() { + return "%s(%s)@%s".formatted(getClass().getSimpleName(), faker, Integer.toHexString(hashCode())); + } } diff --git a/src/main/java/net/datafaker/providers/base/BaseFaker.java b/src/main/java/net/datafaker/providers/base/BaseFaker.java index fc1b965b1..7236496b2 100644 --- a/src/main/java/net/datafaker/providers/base/BaseFaker.java +++ b/src/main/java/net/datafaker/providers/base/BaseFaker.java @@ -400,4 +400,9 @@ public final B getFaker() { public static Method getMethod(AbstractProvider ap, String methodName) { return ap == null ? null : ObjectMethods.getMethodByName(ap, methodName); } + + @Override + public String toString() { + return getClass().getSimpleName() + "@" + Integer.toHexString(hashCode()); + } } diff --git a/src/main/java/net/datafaker/service/FakeValuesService.java b/src/main/java/net/datafaker/service/FakeValuesService.java index 8bec26b69..375359147 100644 --- a/src/main/java/net/datafaker/service/FakeValuesService.java +++ b/src/main/java/net/datafaker/service/FakeValuesService.java @@ -1197,6 +1197,12 @@ private record MethodAndCoercedArgs(Method method, Object[] coerced) { private Object invoke(Object on) throws InvocationTargetException, IllegalAccessException { return method.invoke(on, coerced); } + + @Override + public String toString() { + return "%s[method=%s.%s(), coerced=%s]".formatted(getClass().getSimpleName(), + method.getDeclaringClass().getSimpleName(), method.getName(), Arrays.toString(coerced)); + } } private record RegExpContext(String exp, ProviderRegistration root, FakerContext context) { diff --git a/src/main/java/net/datafaker/service/RandomService.java b/src/main/java/net/datafaker/service/RandomService.java index f2f7584a2..0dc6af1f8 100644 --- a/src/main/java/net/datafaker/service/RandomService.java +++ b/src/main/java/net/datafaker/service/RandomService.java @@ -120,4 +120,9 @@ public int hashCode() { if (random == SHARED_RANDOM) return 1; return random != null ? random.hashCode() : 0; } + + @Override + public String toString() { + return getClass().getSimpleName() + "@" + random; + } }