Skip to content

Commit

Permalink
Normalization of bean class names for more correct logs
Browse files Browse the repository at this point in the history
Fixed #11320
  • Loading branch information
altro3 committed Nov 8, 2024
1 parent c0b0ee7 commit 2aaaab2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
7 changes: 7 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,10 @@
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 +24,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"
}
}

0 comments on commit 2aaaab2

Please sign in to comment.