Skip to content

Commit 7e5bb2c

Browse files
committed
Optimize Beans#getCallbacks()
1 parent 5c82b4a commit 7e5bb2c

File tree

3 files changed

+33
-8
lines changed

3 files changed

+33
-8
lines changed

independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanGenerator.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -852,8 +852,7 @@ private void generateCreateForClassBean(ClassCreator cc, BeanInfo bean,
852852
class PostConstructGenerator {
853853
void generate(BlockCreator bc, Var instance) {
854854
if (!bean.isInterceptor()) {
855-
List<MethodInfo> postConstructCallbacks = Beans.getCallbacks(bean.getTarget().get().asClass(),
856-
DotNames.POST_CONSTRUCT, bean.getDeployment().getBeanArchiveIndex());
855+
List<MethodInfo> postConstructCallbacks = bean.getPostConstructCallbacks();
857856

858857
for (MethodInfo callback : postConstructCallbacks) {
859858
if (isReflectionFallbackNeeded(callback, targetPackage)) {
@@ -1480,9 +1479,7 @@ class PreDestroyGenerator {
14801479
void generate(BlockCreator bc, Var instance) {
14811480
// PreDestroy callbacks
14821481
// possibly wrapped into Runnable so that PreDestroy interceptors can proceed() correctly
1483-
List<MethodInfo> preDestroyCallbacks = Beans.getCallbacks(
1484-
bean.getTarget().get().asClass(), DotNames.PRE_DESTROY,
1485-
bean.getDeployment().getBeanArchiveIndex());
1482+
List<MethodInfo> preDestroyCallbacks = bean.getPreDestroyCallbacks();
14861483
for (MethodInfo callback : preDestroyCallbacks) {
14871484
if (isReflectionFallbackNeeded(callback, targetPackage)) {
14881485
if (Modifier.isPrivate(callback.flags())) {

independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/BeanInfo.java

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,10 @@ public class BeanInfo implements InjectionTargetInfo {
8787

8888
private final List<MethodInfo> aroundInvokes;
8989

90+
private final List<MethodInfo> postConstructCallbacks;
91+
92+
private final List<MethodInfo> preDestroyCallbacks;
93+
9094
private final InterceptionProxyInfo interceptionProxy;
9195

9296
// Following fields are only used by synthetic beans
@@ -173,6 +177,15 @@ public class BeanInfo implements InjectionTargetInfo {
173177
this.targetPackageName = targetPackageName;
174178
this.startupPriority = startupPriority;
175179
this.aroundInvokes = isInterceptor() || isDecorator() ? List.of() : Beans.getAroundInvokes(implClazz, beanDeployment);
180+
if (isClassBean() && !isInterceptor()) {
181+
this.postConstructCallbacks = Beans.getCallbacks(target.asClass(), DotNames.POST_CONSTRUCT,
182+
beanDeployment.getBeanArchiveIndex());
183+
this.preDestroyCallbacks = Beans.getCallbacks(target.asClass(), DotNames.PRE_DESTROY,
184+
beanDeployment.getBeanArchiveIndex());
185+
} else {
186+
this.postConstructCallbacks = List.of();
187+
this.preDestroyCallbacks = List.of();
188+
}
176189
}
177190

178191
@Override
@@ -457,8 +470,7 @@ public boolean hasDestroyLogic() {
457470
}
458471
// test class bean with @PreDestroy interceptor or callback
459472
return isClassBean() && (!getLifecycleInterceptors(InterceptionType.PRE_DESTROY).isEmpty()
460-
|| !Beans.getCallbacks(target.get().asClass(), DotNames.PRE_DESTROY, beanDeployment.getBeanArchiveIndex())
461-
.isEmpty());
473+
|| !preDestroyCallbacks.isEmpty());
462474
}
463475

464476
public boolean isForceApplicationClass() {
@@ -533,6 +545,22 @@ boolean hasBoundDecoratorMatching(Predicate<DotName> predicate) {
533545
return false;
534546
}
535547

548+
/**
549+
*
550+
* @return the list of {@code @PostConstruct} callback methods declared in the hierarchy of a bean class
551+
*/
552+
List<MethodInfo> getPostConstructCallbacks() {
553+
return postConstructCallbacks;
554+
}
555+
556+
/**
557+
*
558+
* @return the list of {@code @PreDestroy} callback methods declared in the hierarchy of a bean class
559+
*/
560+
List<MethodInfo> getPreDestroyCallbacks() {
561+
return preDestroyCallbacks;
562+
}
563+
536564
/**
537565
*
538566
* @return the list of around invoke interceptor methods declared in the hierarchy of a bean class

independent-projects/arc/processor/src/main/java/io/quarkus/arc/processor/Beans.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -762,7 +762,7 @@ static List<MethodInfo> getCallbacks(ClassInfo beanClass, DotName annotation, In
762762
List<MethodInfo> callbacks = new ArrayList<>();
763763
collectCallbacks(beanClass, callbacks, annotation, index, new HashSet<>(), interceptionType);
764764
Collections.reverse(callbacks);
765-
return callbacks;
765+
return callbacks.isEmpty() ? List.of() : List.copyOf(callbacks);
766766
}
767767

768768
static List<MethodInfo> getAroundInvokes(ClassInfo beanClass, BeanDeployment deployment) {

0 commit comments

Comments
 (0)