Skip to content

Commit

Permalink
Merge pull request #36556 from mkouba/issue-36545
Browse files Browse the repository at this point in the history
ArC: fix PreDestroy callback support for decorators
  • Loading branch information
mkouba authored Oct 19, 2023
2 parents 30eb8d3 + a607a67 commit cba9cd3
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 cba9cd3

Please sign in to comment.