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 all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
import io.micronaut.core.exceptions.BeanExceptionHandler;
import io.micronaut.inject.BeanDefinition;

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

/**
* An exception handler interface for task related exceptions.
*
Expand All @@ -38,7 +40,7 @@ default void handleCreationFailure(BeanDefinition<T> beanType, E throwable) {
if (DefaultTaskExceptionHandler.LOG.isErrorEnabled()) {
StringBuilder message = new StringBuilder("Error creating scheduled task ");
if (beanType != null) {
message.append("for bean [").append(beanType.asArgument()).append("] ");
message.append("for bean [").append(getNormalizedTypeString(beanType)).append("] ");
}
message.append(throwable.getMessage());
DefaultTaskExceptionHandler.LOG.error(message.toString(), throwable);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,16 @@ public void close() {
.map(bb -> AvailableByteArrayBody.create(ByteArrayBufferFactory.INSTANCE, ReactiveByteBufferByteBody.toByteArray(bb)));
}

@Override
public @NonNull CloseableByteBody move() {
BufferConsumer.Upstream upstream = this.upstream;
if (upstream == null) {
BaseSharedBuffer.failClaim();
}
this.upstream = null;
return new ReactiveByteBufferByteBody(sharedBuffer, upstream);
}

@Override
public void close() {
BufferConsumer.Upstream upstream = this.upstream;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ protected void release(ByteBuf item) {
public @NonNull CloseableByteBody move() {
BufferConsumer.Upstream upstream = this.upstream;
if (upstream == null) {
failClaim();
BaseSharedBuffer.failClaim();
}
this.upstream = null;
return new StreamingNettyByteBody(sharedBuffer, forceDelaySubscribe, upstream);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.micronaut.context

import io.micronaut.annotation.processing.test.AbstractTypeElementSpec

class MessageUtilsSpec extends AbstractTypeElementSpec {

void "test bean definition name normalization"() {

given:
def definition = buildBeanDefinition('test.Test', '''
package test;

@jakarta.inject.Singleton
class Test {

}
''')

expect:
MessageUtils.getNormalizedTypeString(definition) == "Test"
}
}
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.getNormalizedTypeString;
import static io.micronaut.core.util.StringUtils.EMPTY_STRING_ARRAY;

/**
Expand Down Expand Up @@ -428,7 +429,7 @@ private <T> void appendEachPropertyMissingBeanMessage(String linePrefix,
.append(lineSeparator)
.append(linePrefix)
.append("* [")
.append(definition.asArgument().getTypeString(true));
.append(getNormalizedTypeString(definition));
if (!definition.getBeanType().equals(beanType.getType())) {
messageBuilder.append("] a candidate of [")
.append(beanType.getTypeString(true));
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.getNormalizedTypeString;
import static io.micronaut.core.util.StringUtils.EMPTY_STRING_ARRAY;

/**
Expand Down Expand Up @@ -2875,7 +2876,7 @@ final <T> void resolveDisabledBeanMessage(String linePrefix,
messageBuilder
.append(lineSeparator)
.append(linePrefix)
.append("* [").append(beanDefinition.asArgument().getTypeString(true));
.append("* [").append(getNormalizedTypeString(beanDefinition));
if (!beanDefinition.getBeanType().equals(beanType.getType())) {
messageBuilder.append("] a candidate of [")
.append(beanType.getTypeString(true));
Expand Down
47 changes: 47 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,47 @@
/*
* 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.annotation.NonNull;
import io.micronaut.inject.BeanDefinition;
import io.micronaut.inject.ProxyBeanDefinition;

/**
* Some helper method for log / exception messages.
*
* @since 4.8.0
*/
public final class MessageUtils {

private MessageUtils() {
}

/**
* Return BeanDefinition type string or target type class name for ProxyBeanDefinition.
*
* @param beanDefinition bean definition
*
* @return normalized bean definition name
*/
public static String getNormalizedTypeString(@NonNull BeanDefinition<?> beanDefinition) {

if (beanDefinition instanceof ProxyBeanDefinition<?> proxyBeanDefinition) {
return proxyBeanDefinition.getTargetType().getSimpleName();
} else {
return beanDefinition.asArgument().getTypeString(true);
}
}
}
Loading