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

ArC: fix PreDestroy callback support for decorators #36556

Merged
merged 1 commit into from
Oct 19, 2023
Merged
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 @@ -417,7 +417,7 @@ boolean isSubclassRequired() {
* @return {@code true} if the bean requires some customized destroy logic
*/
public boolean hasDestroyLogic() {
if (isInterceptor() || isDecorator()) {
if (isInterceptor()) {
return false;
}
if (disposer != null || destroyerConsumer != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ Collection<Resource> generate(DecoratorInfo decorator) {
implementCreate(classOutput, decoratorCreator, decorator, providerType, baseName,
injectionPointToProviderField, Collections.emptyMap(), Collections.emptyMap(),
targetPackage, isApplicationClass);
if (decorator.hasDestroyLogic()) {
implementDestroy(decorator, decoratorCreator, providerType, Collections.emptyMap(), isApplicationClass, baseName,
targetPackage);
}
implementGet(decorator, decoratorCreator, providerType, baseName);
implementGetTypes(decoratorCreator, beanTypes.getFieldDescriptor());
implementGetBeanClass(decorator, decoratorCreator);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package io.quarkus.arc.test.decorators;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.concurrent.atomic.AtomicBoolean;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import jakarta.annotation.Priority;
import jakarta.decorator.Decorator;
import jakarta.decorator.Delegate;
Expand All @@ -13,6 +18,7 @@
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.arc.test.ArcTestContainer;

public class SimpleDecoratorTest {
Expand All @@ -23,9 +29,13 @@ public class SimpleDecoratorTest {

@Test
public void testDecoration() {
ToUpperCaseConverter converter = Arc.container().instance(ToUpperCaseConverter.class).get();
InstanceHandle<ToUpperCaseConverter> handle = Arc.container().instance(ToUpperCaseConverter.class);
ToUpperCaseConverter converter = handle.get();
assertEquals("HOLA!", converter.convert(" holA!"));
assertEquals(" HOLA!", converter.convertNoDelegation(" holA!"));
handle.destroy();
assertTrue(TrimConverterDecorator.CONSTRUCTED.get());
assertTrue(TrimConverterDecorator.DESTROYED.get());
}

interface Converter<T> {
Expand Down Expand Up @@ -53,6 +63,9 @@ public String convertNoDelegation(String value) {
@Decorator
static class TrimConverterDecorator implements Converter<String> {

static final AtomicBoolean CONSTRUCTED = new AtomicBoolean();
static final AtomicBoolean DESTROYED = new AtomicBoolean();

@Inject
@Delegate
Converter<String> delegate;
Expand All @@ -62,6 +75,16 @@ public String convert(String value) {
return delegate.convert(value.trim());
}

@PostConstruct
void init() {
CONSTRUCTED.set(true);
}

@PreDestroy
void destroy() {
DESTROYED.set(true);
}

}

@Priority(2)
Expand Down
Loading