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
  • Loading branch information
altro3 committed Nov 8, 2024
1 parent 0ce7262 commit c0b0ee7
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -367,7 +368,7 @@ private <T> 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("]");
Expand Down Expand Up @@ -428,10 +429,10 @@ private <T> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -848,7 +849,7 @@ public <T> T getBean(@NonNull Argument<T> beanType, @Nullable Qualifier<T> 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()
);
}
}
Expand Down Expand Up @@ -2840,7 +2841,7 @@ final <T> 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: ")
Expand Down Expand Up @@ -2875,10 +2876,10 @@ final <T> 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);
Expand Down
35 changes: 35 additions & 0 deletions inject/src/main/java/io/micronaut/context/MessageUtils.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -70,7 +72,7 @@ private static <T> String buildMessage(Iterator<BeanDefinition<T>> possibleCandi
final StringBuilder message = new StringBuilder("Multiple possible bean candidates found: [");
while (possibleCandidates.hasNext()) {
Argument<T> next = possibleCandidates.next().asArgument();
message.append(next.getTypeString(true));
message.append(normalizeBeanClassName(next.getTypeString(true)));
if (possibleCandidates.hasNext()) {
message.append(", ");
}
Expand Down

0 comments on commit c0b0ee7

Please sign in to comment.