Skip to content

Commit

Permalink
ArC: fix PreDestroy callback support for decorators
Browse files Browse the repository at this point in the history
- fixes #36545
  • Loading branch information
mkouba committed Oct 18, 2023
1 parent 996730b commit a607a67
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
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

0 comments on commit a607a67

Please sign in to comment.