Skip to content

Commit

Permalink
ArC: optimize "List all" injection points
Browse files Browse the repository at this point in the history
- only collect InjectionPoint metadata if any of the resolved beans is
dependent
  • Loading branch information
mkouba committed Jan 13, 2025
1 parent ec6bf3c commit e42d2ed
Showing 1 changed file with 39 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Collectors;

import jakarta.enterprise.inject.spi.DefinitionException;
import jakarta.enterprise.inject.spi.InjectionPoint;
Expand Down Expand Up @@ -407,31 +408,49 @@ private static void generateListBytecode(GeneratorContext ctx) {
requiredType = Types.getTypeHandle(mc, type);
usesInstanceHandle = mc.load(false);
}

ResultHandle qualifiers = BeanGenerator.collectInjectionPointQualifiers(
ctx.beanDeployment,
ctx.constructor, ctx.injectionPoint, ctx.annotationLiterals);
ResultHandle annotationsHandle = BeanGenerator.collectInjectionPointAnnotations(
ctx.beanDeployment,
ctx.constructor, ctx.injectionPoint, ctx.annotationLiterals, ctx.injectionPointAnnotationsPredicate);
ResultHandle javaMemberHandle = BeanGenerator.getJavaMemberHandle(ctx.constructor, ctx.injectionPoint,
ctx.reflectionRegistration);

// Note that we only collect the injection point metadata if needed, i.e. if any of the resolved beans is dependent,
// and requires InjectionPoint metadata
Set<BeanInfo> beans = ctx.beanDeployment.beanResolver.resolveBeans(
type.name().equals(DotNames.INSTANCE_HANDLE) ? type.asParameterizedType().arguments().get(0) : type,
ctx.injectionPoint.getRequiredQualifiers().stream().filter(a -> !a.name().equals(DotNames.ALL))
.collect(Collectors.toSet()));
boolean collectMetadata = beans.stream()
.anyMatch(b -> BuiltinScope.DEPENDENT.isDeclaredBy(b) && b.requiresInjectionPointMetadata());

ResultHandle annotationsHandle;
ResultHandle javaMemberHandle;
ResultHandle beanHandle;
switch (ctx.targetInfo.kind()) {
case OBSERVER:
// For observers the first argument is always the declaring bean
beanHandle = ctx.constructor.invokeInterfaceMethod(
MethodDescriptors.SUPPLIER_GET, ctx.constructor.getMethodParam(0));
break;
case BEAN:
beanHandle = ctx.constructor.getThis();
break;
case INVOKER:
beanHandle = loadInvokerTargetBean(ctx.targetInfo.asInvoker(), ctx.constructor);
break;
default:
throw new IllegalStateException("Unsupported target info: " + ctx.targetInfo);
if (collectMetadata) {
annotationsHandle = BeanGenerator.collectInjectionPointAnnotations(
ctx.beanDeployment,
ctx.constructor, ctx.injectionPoint, ctx.annotationLiterals, ctx.injectionPointAnnotationsPredicate);
javaMemberHandle = BeanGenerator.getJavaMemberHandle(ctx.constructor, ctx.injectionPoint,
ctx.reflectionRegistration);
switch (ctx.targetInfo.kind()) {
case OBSERVER:
// For observers the first argument is always the declaring bean
beanHandle = ctx.constructor.invokeInterfaceMethod(
MethodDescriptors.SUPPLIER_GET, ctx.constructor.getMethodParam(0));
break;
case BEAN:
beanHandle = ctx.constructor.getThis();
break;
case INVOKER:
beanHandle = loadInvokerTargetBean(ctx.targetInfo.asInvoker(), ctx.constructor);
break;
default:
throw new IllegalStateException("Unsupported target info: " + ctx.targetInfo);
}
} else {
annotationsHandle = ctx.constructor.loadNull();
javaMemberHandle = ctx.constructor.loadNull();
beanHandle = ctx.constructor.loadNull();
}

ResultHandle listProvider = ctx.constructor.newInstance(
MethodDescriptor.ofConstructor(ListProvider.class, java.lang.reflect.Type.class, java.lang.reflect.Type.class,
Set.class,
Expand Down

0 comments on commit e42d2ed

Please sign in to comment.