Skip to content

Commit

Permalink
Merge pull request quarkusio#30651 from mkouba/issue-30636
Browse files Browse the repository at this point in the history
ArC - use reflection fallback for PreDestroy callbacks if needed
  • Loading branch information
manovotn authored Jan 27, 2023
2 parents b5abe45 + 58a5197 commit 5c3776f
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,8 @@ Collection<Resource> generateSyntheticBean(BeanInfo bean) {
implementGetIdentifier(bean, beanCreator);
implementSupplierGet(beanCreator);
if (bean.hasDestroyLogic()) {
implementDestroy(bean, beanCreator, providerType, Collections.emptyMap(), isApplicationClass, baseName);
implementDestroy(bean, beanCreator, providerType, Collections.emptyMap(), isApplicationClass, baseName,
targetPackage);
}
implementCreate(classOutput, beanCreator, bean, providerType, baseName,
Collections.emptyMap(), Collections.emptyMap(),
Expand Down Expand Up @@ -363,7 +364,7 @@ Collection<Resource> generateClassBean(BeanInfo bean, ClassInfo beanClass) {
implementSupplierGet(beanCreator);
if (bean.hasDestroyLogic()) {
implementDestroy(bean, beanCreator, providerType, injectionPointToProviderSupplierField, isApplicationClass,
baseName);
baseName, targetPackage);
}
implementCreate(classOutput, beanCreator, bean, providerType, baseName,
injectionPointToProviderSupplierField,
Expand Down Expand Up @@ -448,7 +449,8 @@ Collection<Resource> generateProducerMethodBean(BeanInfo bean, MethodInfo produc
implementGetIdentifier(bean, beanCreator);
implementSupplierGet(beanCreator);
if (bean.hasDestroyLogic()) {
implementDestroy(bean, beanCreator, providerType, injectionPointToProviderField, isApplicationClass, baseName);
implementDestroy(bean, beanCreator, providerType, injectionPointToProviderField, isApplicationClass, baseName,
targetPackage);
}
implementCreate(classOutput, beanCreator, bean, providerType, baseName,
injectionPointToProviderField,
Expand Down Expand Up @@ -529,7 +531,7 @@ Collection<Resource> generateProducerFieldBean(BeanInfo bean, FieldInfo producer
implementGetIdentifier(bean, beanCreator);
implementSupplierGet(beanCreator);
if (bean.hasDestroyLogic()) {
implementDestroy(bean, beanCreator, providerType, null, isApplicationClass, baseName);
implementDestroy(bean, beanCreator, providerType, null, isApplicationClass, baseName, targetPackage);
}
implementCreate(classOutput, beanCreator, bean, providerType, baseName,
Collections.emptyMap(), Collections.emptyMap(),
Expand Down Expand Up @@ -795,7 +797,8 @@ protected MethodCreator initConstructor(ClassOutput classOutput, ClassCreator be
}

protected void implementDestroy(BeanInfo bean, ClassCreator beanCreator, ProviderType providerType,
Map<InjectionPointInfo, String> injectionPointToProviderField, boolean isApplicationClass, String baseName) {
Map<InjectionPointInfo, String> injectionPointToProviderField, boolean isApplicationClass, String baseName,
String targetPackage) {

MethodCreator destroy = beanCreator
.getMethodCreator("destroy", void.class, providerType.descriptorName(), CreationalContext.class)
Expand All @@ -817,9 +820,11 @@ protected void implementDestroy(BeanInfo bean, ClassCreator beanCreator, Provide
DotNames.PRE_DESTROY,
bean.getDeployment().getBeanArchiveIndex());
for (MethodInfo callback : preDestroyCallbacks) {
if (Modifier.isPrivate(callback.flags())) {
privateMembers.add(isApplicationClass, String.format("@PreDestroy callback %s#%s()",
callback.declaringClass().name(), callback.name()));
if (isReflectionFallbackNeeded(callback, targetPackage)) {
if (Modifier.isPrivate(callback.flags())) {
privateMembers.add(isApplicationClass, String.format("@PreDestroy callback %s#%s()",
callback.declaringClass().name(), callback.name()));
}
reflectionRegistration.registerMethod(callback);
destroy.invokeStaticMethod(MethodDescriptors.REFLECTIONS_INVOKE_METHOD,
destroy.loadClass(callback.declaringClass().name().toString()),
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.quarkus.arc.test.lifecyclecallbacks.inherited;

import java.util.concurrent.atomic.AtomicBoolean;

import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.enterprise.context.ApplicationScoped;

@ApplicationScoped
public class OriginalBean {

static final AtomicBoolean POST_CONSTRUCT = new AtomicBoolean();
static final AtomicBoolean PRE_DESTROY = new AtomicBoolean();

// package private, not visible from AlternativeBean without reflection access
@PostConstruct
void postConstruct() {
POST_CONSTRUCT.set(true);
}

@PreDestroy
void preDestroy() {
PRE_DESTROY.set(true);
}

public String ping() {
return OriginalBean.class.getSimpleName();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.quarkus.arc.test.lifecyclecallbacks.inherited;

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

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.arc.Arc;
import io.quarkus.arc.ArcContainer;
import io.quarkus.arc.InstanceHandle;
import io.quarkus.arc.test.ArcTestContainer;
import io.quarkus.arc.test.lifecyclecallbacks.inherited.subpackage.AlternativeBean;

public class PackagePrivateCallbackInheritanceTest {

@RegisterExtension
public ArcTestContainer container = new ArcTestContainer(AlternativeBean.class, OriginalBean.class);

@Test
public void testCallbacks() {
ArcContainer container = Arc.container();
InstanceHandle<OriginalBean> origBeanInstance = container.instance(OriginalBean.class);
InstanceHandle<AlternativeBean> alternativeBeanInstance = container.instance(AlternativeBean.class);

assertTrue(origBeanInstance.isAvailable());
assertTrue(alternativeBeanInstance.isAvailable());

// AlternativeBean overrides the OriginalBean
assertEquals(origBeanInstance.getBean(), alternativeBeanInstance.getBean());
assertEquals(AlternativeBean.class.getSimpleName(), alternativeBeanInstance.get().ping());
assertTrue(origBeanInstance.get().ping().equals(alternativeBeanInstance.get().ping()));

// post construct should be invoked via during creation of AlternativeBean even though it's pack-private
assertTrue(OriginalBean.POST_CONSTRUCT.get());

alternativeBeanInstance.destroy();

// pre destroy should be invoked even though it's package-private and AlternativeBean lives in a different package
assertTrue(OriginalBean.PRE_DESTROY.get());
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package io.quarkus.arc.test.interceptors.postConstruct.inherited.subpackage;
package io.quarkus.arc.test.lifecyclecallbacks.inherited.subpackage;

import javax.annotation.Priority;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Alternative;

import io.quarkus.arc.test.interceptors.postConstruct.inherited.OriginalBean;
import io.quarkus.arc.test.lifecyclecallbacks.inherited.OriginalBean;

@Alternative
@ApplicationScoped
Expand Down

0 comments on commit 5c3776f

Please sign in to comment.