From d3233d853dafb84ed4bffa2d904962f9905d2227 Mon Sep 17 00:00:00 2001 From: altro3 Date: Fri, 8 Nov 2024 20:08:25 +0700 Subject: [PATCH] Normalization of bean class names for more correct logs Fixed #11320 --- .../io/micronaut/context/MessageUtils.java | 22 +++++++++++++++++++ .../MatchesPresenceOfBeansCondition.java | 4 +++- .../exceptions/NoSuchBeanException.java | 10 +++++---- .../micronaut/context/MessageUtilsSpec.groovy | 17 ++++++++++++++ 4 files changed, 48 insertions(+), 5 deletions(-) create mode 100644 inject/src/test/groovy/io/micronaut/context/MessageUtilsSpec.groovy diff --git a/inject/src/main/java/io/micronaut/context/MessageUtils.java b/inject/src/main/java/io/micronaut/context/MessageUtils.java index 6f773f57d60..b2b8a672f4d 100644 --- a/inject/src/main/java/io/micronaut/context/MessageUtils.java +++ b/inject/src/main/java/io/micronaut/context/MessageUtils.java @@ -1,6 +1,25 @@ +/* + * Copyright 2017-2024 original authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package io.micronaut.context; +import io.micronaut.core.util.StringUtils; + /** + * Some helper method for log / exception messages. + * * @since 4.8.0 */ public final class MessageUtils { @@ -20,6 +39,9 @@ private MessageUtils() { * @return normalized bean class name */ public static String normalizeBeanClassName(String typeString) { + if (StringUtils.isEmpty(typeString) || !typeString.contains("$")) { + return typeString; + } if (typeString.startsWith("$")) { typeString = typeString.substring(1); } diff --git a/inject/src/main/java/io/micronaut/context/conditions/MatchesPresenceOfBeansCondition.java b/inject/src/main/java/io/micronaut/context/conditions/MatchesPresenceOfBeansCondition.java index 85cb9fd5e51..afbe6ad5c78 100644 --- a/inject/src/main/java/io/micronaut/context/conditions/MatchesPresenceOfBeansCondition.java +++ b/inject/src/main/java/io/micronaut/context/conditions/MatchesPresenceOfBeansCondition.java @@ -25,6 +25,8 @@ import java.util.Arrays; import java.util.Objects; +import static io.micronaut.context.MessageUtils.normalizeBeanClassName; + /** * Matches presence of beans condition. * @@ -43,7 +45,7 @@ public boolean matches(ConditionContext context) { for (AnnotationClassValue bean : beans) { Class type = bean.getType().orElse(null); if (type == null || !beanContext.containsBean(type)) { - context.fail("No bean of type [" + bean.getName() + "] present within context"); + context.fail("No bean of type [" + normalizeBeanClassName(bean.getName()) + "] present within context"); return false; } } diff --git a/inject/src/main/java/io/micronaut/context/exceptions/NoSuchBeanException.java b/inject/src/main/java/io/micronaut/context/exceptions/NoSuchBeanException.java index e5139b72610..a3336bc8c40 100644 --- a/inject/src/main/java/io/micronaut/context/exceptions/NoSuchBeanException.java +++ b/inject/src/main/java/io/micronaut/context/exceptions/NoSuchBeanException.java @@ -20,6 +20,8 @@ import io.micronaut.core.annotation.Nullable; import io.micronaut.core.type.Argument; +import static io.micronaut.context.MessageUtils.normalizeBeanClassName; + /** * Thrown when no such beans exists. * @@ -44,7 +46,7 @@ public NoSuchBeanException(@NonNull Class beanType) { * @param beanType The bean type */ public NoSuchBeanException(@NonNull Argument beanType) { - super(MESSAGE_PREFIX + beanType.getTypeName() + MESSAGE_SUFFIX + additionalMessage()); + super(MESSAGE_PREFIX + normalizeBeanClassName(beanType.getTypeName()) + MESSAGE_SUFFIX + additionalMessage()); } /** @@ -53,7 +55,7 @@ public NoSuchBeanException(@NonNull Argument beanType) { * @param The type */ public NoSuchBeanException(@NonNull Class beanType, @Nullable Qualifier qualifier) { - super(MESSAGE_PREFIX + beanType.getName() + MESSAGE_EXISTS + (qualifier != null ? MESSAGE_FOR_THE_GIVEN_QUALIFIER + qualifier : "") + "." + additionalMessage()); + super(MESSAGE_PREFIX + normalizeBeanClassName(beanType.getName()) + MESSAGE_EXISTS + (qualifier != null ? MESSAGE_FOR_THE_GIVEN_QUALIFIER + qualifier : "") + "." + additionalMessage()); } /** @@ -62,7 +64,7 @@ public NoSuchBeanException(@NonNull Class beanType, @Nullable Qualifier The type */ public NoSuchBeanException(@NonNull Argument beanType, @Nullable Qualifier qualifier) { - super(MESSAGE_PREFIX + beanType.getTypeName() + MESSAGE_EXISTS + (qualifier != null ? MESSAGE_FOR_THE_GIVEN_QUALIFIER + qualifier : "") + "." + additionalMessage()); + super(MESSAGE_PREFIX + normalizeBeanClassName(beanType.getTypeName()) + MESSAGE_EXISTS + (qualifier != null ? MESSAGE_FOR_THE_GIVEN_QUALIFIER + qualifier : "") + "." + additionalMessage()); } /** @@ -73,7 +75,7 @@ public NoSuchBeanException(@NonNull Argument beanType, @Nullable Qualifie * @since 4.0.0 */ public NoSuchBeanException(@NonNull Argument beanType, @Nullable Qualifier qualifier, String message) { - super(MESSAGE_PREFIX + beanType.getTypeName() + MESSAGE_EXISTS + (qualifier != null ? MESSAGE_FOR_THE_GIVEN_QUALIFIER + qualifier : "") + ". " + message); + super(MESSAGE_PREFIX + normalizeBeanClassName(beanType.getTypeName()) + MESSAGE_EXISTS + (qualifier != null ? MESSAGE_FOR_THE_GIVEN_QUALIFIER + qualifier : "") + ". " + message); } /** diff --git a/inject/src/test/groovy/io/micronaut/context/MessageUtilsSpec.groovy b/inject/src/test/groovy/io/micronaut/context/MessageUtilsSpec.groovy new file mode 100644 index 00000000000..bbc55fdce2f --- /dev/null +++ b/inject/src/test/groovy/io/micronaut/context/MessageUtilsSpec.groovy @@ -0,0 +1,17 @@ +package io.micronaut.context + +import spock.lang.Specification + +class MessageUtilsSpec extends Specification { + + void "test bean class name normalization"() { + expect: + MessageUtils.normalizeBeanClassName(null) == null + MessageUtils.normalizeBeanClassName("") == "" + MessageUtils.normalizeBeanClassName("ClassNameWithoutDollar") == "ClassNameWithoutDollar" + MessageUtils.normalizeBeanClassName("FooImpl\$InternalClass") == "FooImpl\$InternalClass" + MessageUtils.normalizeBeanClassName("\$FooImpl\$Definition\$Intercepted") == "FooImpl" + MessageUtils.normalizeBeanClassName("\$FooImpl\$Definition\$Intercepted\$Definition") == "FooImpl" + MessageUtils.normalizeBeanClassName("\$FooImpl\$Definition\$Intercepted\$Definition\$Reference") == "FooImpl" + } +} \ No newline at end of file