From c0b0ee75a37f6f4b766bf1315d5f0e4fb90573fc Mon Sep 17 00:00:00 2001 From: altro3 Date: Fri, 8 Nov 2024 17:34:01 +0700 Subject: [PATCH] Normalization of bean class names for more correct logs Fixed #11320 --- .../context/DefaultApplicationContext.java | 7 ++-- .../micronaut/context/DefaultBeanContext.java | 9 ++--- .../io/micronaut/context/MessageUtils.java | 35 +++++++++++++++++++ .../exceptions/NonUniqueBeanException.java | 4 ++- 4 files changed, 47 insertions(+), 8 deletions(-) create mode 100644 inject/src/main/java/io/micronaut/context/MessageUtils.java diff --git a/inject/src/main/java/io/micronaut/context/DefaultApplicationContext.java b/inject/src/main/java/io/micronaut/context/DefaultApplicationContext.java index a5e735c2b2d..bf12a6f17d4 100644 --- a/inject/src/main/java/io/micronaut/context/DefaultApplicationContext.java +++ b/inject/src/main/java/io/micronaut/context/DefaultApplicationContext.java @@ -65,6 +65,7 @@ import java.util.Set; import java.util.stream.Collectors; +import static io.micronaut.context.MessageUtils.normalizeBeanClassName; import static io.micronaut.core.util.StringUtils.EMPTY_STRING_ARRAY; /** @@ -367,7 +368,7 @@ private void appendMissingEachBeanMessage(String linePrefix, messageBuilder .append(lineSeparator) .append(linePrefix) - .append("* [").append(beanType.getTypeString(true)) + .append("* [").append(normalizeBeanClassName(beanType.getTypeString(true))) .append("] requires the presence of a bean of type [") .append(dependentBean.getName()) .append("]"); @@ -428,10 +429,10 @@ private void appendEachPropertyMissingBeanMessage(String linePrefix, .append(lineSeparator) .append(linePrefix) .append("* [") - .append(definition.asArgument().getTypeString(true)); + .append(normalizeBeanClassName(definition.asArgument().getTypeString(true))); if (!definition.getBeanType().equals(beanType.getType())) { messageBuilder.append("] a candidate of [") - .append(beanType.getTypeString(true)); + .append(normalizeBeanClassName(beanType.getTypeString(true))); } messageBuilder.append("] is disabled because:") .append(lineSeparator); diff --git a/inject/src/main/java/io/micronaut/context/DefaultBeanContext.java b/inject/src/main/java/io/micronaut/context/DefaultBeanContext.java index ae0471ea4c4..acf16d5f0bb 100644 --- a/inject/src/main/java/io/micronaut/context/DefaultBeanContext.java +++ b/inject/src/main/java/io/micronaut/context/DefaultBeanContext.java @@ -147,6 +147,7 @@ import java.util.stream.Collectors; import java.util.stream.Stream; +import static io.micronaut.context.MessageUtils.normalizeBeanClassName; import static io.micronaut.core.util.StringUtils.EMPTY_STRING_ARRAY; /** @@ -848,7 +849,7 @@ public T getBean(@NonNull Argument beanType, @Nullable Qualifier quali null, beanType, qualifier, - "Bean of type [" + beanType.getTypeString(true) + "] disabled for reason: " + e.getMessage() + "Bean of type [" + normalizeBeanClassName(beanType.getTypeString(true)) + "] disabled for reason: " + e.getMessage() ); } } @@ -2840,7 +2841,7 @@ final void resolveDisabledBeanMessage(String linePrefix, messageBuilder.append(lineSeparator) .append(linePrefix) .append("* [") - .append(beanType.getTypeString(true)) + .append(normalizeBeanClassName(beanType.getTypeString(true))) .append("] is disabled because it is within the package [") .append(pkg) .append("] which is disabled due to bean requirements: ") @@ -2875,10 +2876,10 @@ final void resolveDisabledBeanMessage(String linePrefix, messageBuilder .append(lineSeparator) .append(linePrefix) - .append("* [").append(beanDefinition.asArgument().getTypeString(true)); + .append("* [").append(normalizeBeanClassName(beanDefinition.asArgument().getTypeString(true))); if (!beanDefinition.getBeanType().equals(beanType.getType())) { messageBuilder.append("] a candidate of [") - .append(beanType.getTypeString(true)); + .append(normalizeBeanClassName(beanType.getTypeString(true))); } messageBuilder.append("] is disabled because:") .append(lineSeparator); diff --git a/inject/src/main/java/io/micronaut/context/MessageUtils.java b/inject/src/main/java/io/micronaut/context/MessageUtils.java new file mode 100644 index 00000000000..6f773f57d60 --- /dev/null +++ b/inject/src/main/java/io/micronaut/context/MessageUtils.java @@ -0,0 +1,35 @@ +package io.micronaut.context; + +/** + * @since 4.8.0 + */ +public final class MessageUtils { + + private static final String POSTFIX_BEAN_DEFINITION_REFERENCE = "$Definition$Intercepted$Definition$Reference"; + private static final String POSTFIX_BEAN_DEFINITION = "$Definition$Intercepted$Definition"; + private static final String POSTFIX_BEAN_METHOD_DEFINITION = "$Definition$Intercepted"; + + private MessageUtils() { + } + + /** + * Normalization bean class names for logs. + * + * @param typeString bean class name + * + * @return normalized bean class name + */ + public static String normalizeBeanClassName(String typeString) { + if (typeString.startsWith("$")) { + typeString = typeString.substring(1); + } + if (typeString.endsWith(POSTFIX_BEAN_DEFINITION_REFERENCE)) { + typeString = typeString.substring(0, typeString.indexOf(POSTFIX_BEAN_DEFINITION_REFERENCE)); + } else if (typeString.endsWith(POSTFIX_BEAN_DEFINITION)) { + typeString = typeString.substring(0, typeString.indexOf(POSTFIX_BEAN_DEFINITION)); + } else if (typeString.endsWith(POSTFIX_BEAN_METHOD_DEFINITION)) { + typeString = typeString.substring(0, typeString.indexOf(POSTFIX_BEAN_METHOD_DEFINITION)); + } + return typeString; + } +} diff --git a/inject/src/main/java/io/micronaut/context/exceptions/NonUniqueBeanException.java b/inject/src/main/java/io/micronaut/context/exceptions/NonUniqueBeanException.java index c096d19530b..2fb28428d5c 100644 --- a/inject/src/main/java/io/micronaut/context/exceptions/NonUniqueBeanException.java +++ b/inject/src/main/java/io/micronaut/context/exceptions/NonUniqueBeanException.java @@ -21,6 +21,8 @@ import java.util.Collections; import java.util.Iterator; +import static io.micronaut.context.MessageUtils.normalizeBeanClassName; + /** * Exception thrown when a bean is not unique and has multiple possible implementations for a given bean type. @@ -70,7 +72,7 @@ private static String buildMessage(Iterator> possibleCandi final StringBuilder message = new StringBuilder("Multiple possible bean candidates found: ["); while (possibleCandidates.hasNext()) { Argument next = possibleCandidates.next().asArgument(); - message.append(next.getTypeString(true)); + message.append(normalizeBeanClassName(next.getTypeString(true))); if (possibleCandidates.hasNext()) { message.append(", "); }