Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Normalization of bean definition class names for more correct logs #11327

Open
wants to merge 4 commits into
base: 4.8.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Normalization of bean class names for more correct logs
Fixed #11320
  • Loading branch information
altro3 committed Nov 8, 2024
commit d3233d853dafb84ed4bffa2d904962f9905d2227
22 changes: 22 additions & 0 deletions inject/src/main/java/io/micronaut/context/MessageUtils.java
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.util.Arrays;
import java.util.Objects;

import static io.micronaut.context.MessageUtils.normalizeBeanClassName;

/**
* Matches presence of beans condition.
*
Expand All @@ -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;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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());
}

/**
Expand All @@ -53,7 +55,7 @@ public NoSuchBeanException(@NonNull Argument<?> beanType) {
* @param <T> The type
*/
public <T> NoSuchBeanException(@NonNull Class<T> beanType, @Nullable Qualifier<T> 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());
}

/**
Expand All @@ -62,7 +64,7 @@ public <T> NoSuchBeanException(@NonNull Class<T> beanType, @Nullable Qualifier<T
* @param <T> The type
*/
public <T> NoSuchBeanException(@NonNull Argument<T> beanType, @Nullable Qualifier<T> 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());
}

/**
Expand All @@ -73,7 +75,7 @@ public <T> NoSuchBeanException(@NonNull Argument<T> beanType, @Nullable Qualifie
* @since 4.0.0
*/
public <T> NoSuchBeanException(@NonNull Argument<T> beanType, @Nullable Qualifier<T> 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
}
}
Loading