Skip to content

Commit

Permalink
ArC: fix some corner cases in Build Compatible Extensions
Browse files Browse the repository at this point in the history
- support observing interceptors in `@Registration`
- fix reporting priority of beans that are not alternatives (not very useful
  in standard CDI, but in ArC, we go beyond the standard)
- fix NPE in `ClassInfo.methods()` (and `fields()`) for `java.lang.Object`
  • Loading branch information
Ladicek committed Nov 14, 2023
1 parent c570268 commit c1067bd
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ BeanRegistrar.RegistrationContext registerBeans(List<BeanRegistrar> beanRegistra
buildContext.putInternal(Key.INJECTION_POINTS, Collections.unmodifiableList(this.injectionPoints));

if (buildCompatibleExtensions != null) {
buildCompatibleExtensions.runRegistration(beanArchiveComputingIndex, beans, observers);
buildCompatibleExtensions.runRegistration(beanArchiveComputingIndex, beans, interceptors, observers);
}

return registerSyntheticBeans(beanRegistrars, buildContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ public boolean isAlternative() {

@Override
public Integer priority() {
return arcBeanInfo.getAlternativePriority();
return arcBeanInfo.getPriority();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ private List<org.jboss.jandex.ClassInfo> allSupertypes() {
alreadySeen.add(clazz.name());

DotName superClassName = clazz.superName();
if (!DotNames.OBJECT.equals(superClassName)) {
if (superClassName != null && !DotNames.OBJECT.equals(superClassName)) {
org.jboss.jandex.ClassInfo superClass = jandexIndex.getClassByName(superClassName);
workQueue.add(superClass);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,30 @@
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import jakarta.enterprise.inject.build.compatible.spi.BeanInfo;
import jakarta.enterprise.inject.build.compatible.spi.ObserverInfo;
import jakarta.enterprise.inject.spi.DefinitionException;

import org.jboss.jandex.IndexView;

import io.quarkus.arc.processor.InterceptorInfo;

class ExtensionPhaseRegistration extends ExtensionPhaseBase {
private final AllAnnotationOverlays annotationOverlays;
private final Collection<io.quarkus.arc.processor.BeanInfo> allBeans;
private final Collection<io.quarkus.arc.processor.InterceptorInfo> allInterceptors;
private final Collection<io.quarkus.arc.processor.ObserverInfo> allObservers;
private final io.quarkus.arc.processor.AssignabilityCheck assignability;

ExtensionPhaseRegistration(ExtensionInvoker invoker, org.jboss.jandex.IndexView beanArchiveIndex, SharedErrors errors,
ExtensionPhaseRegistration(ExtensionInvoker invoker, IndexView beanArchiveIndex, SharedErrors errors,
AllAnnotationOverlays annotationOverlays, Collection<io.quarkus.arc.processor.BeanInfo> allBeans,
Collection<io.quarkus.arc.processor.ObserverInfo> allObservers) {
Collection<InterceptorInfo> allInterceptors, Collection<io.quarkus.arc.processor.ObserverInfo> allObservers) {
super(ExtensionPhase.REGISTRATION, invoker, beanArchiveIndex, errors);
this.annotationOverlays = annotationOverlays;
this.allBeans = allBeans;
this.allInterceptors = allInterceptors;
this.allObservers = allObservers;
this.assignability = new io.quarkus.arc.processor.AssignabilityCheck(beanArchiveIndex, null);
}
Expand Down Expand Up @@ -84,7 +91,7 @@ private Set<org.jboss.jandex.Type> expectedTypes(org.jboss.jandex.MethodInfo jan

private List<BeanInfo> matchingBeans(org.jboss.jandex.MethodInfo jandexMethod, boolean onlyInterceptors) {
Set<org.jboss.jandex.Type> expectedTypes = expectedTypes(jandexMethod);
return allBeans.stream()
return Stream.concat(allBeans.stream(), allInterceptors.stream())
.filter(bean -> {
if (onlyInterceptors && !bean.isInterceptor()) {
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ public void runEnhancement(org.jboss.jandex.IndexView beanArchiveIndex, BeanProc
*/
public void runRegistration(org.jboss.jandex.IndexView beanArchiveIndex,
Collection<io.quarkus.arc.processor.BeanInfo> allBeans,
Collection<io.quarkus.arc.processor.InterceptorInfo> allInterceptors,
Collection<io.quarkus.arc.processor.ObserverInfo> allObservers) {
if (invoker.isEmpty()) {
return;
Expand All @@ -281,7 +282,7 @@ public void runRegistration(org.jboss.jandex.IndexView beanArchiveIndex,

try {
new ExtensionPhaseRegistration(invoker, beanArchiveIndex, errors, annotationOverlays,
allBeans, allObservers).run();
allBeans, allInterceptors, allObservers).run();
} finally {
BuildServicesImpl.reset();
}
Expand Down Expand Up @@ -563,7 +564,7 @@ public void runRegistrationAgain(org.jboss.jandex.IndexView beanArchiveIndex,

try {
new ExtensionPhaseRegistration(invoker, beanArchiveIndex, errors, annotationOverlays,
syntheticBeans, syntheticObservers).run();
syntheticBeans, Collections.emptyList(), syntheticObservers).run();
} finally {
BuildServicesImpl.reset();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,29 @@

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

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.concurrent.atomic.AtomicInteger;

import jakarta.annotation.Priority;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.context.Initialized;
import jakarta.enterprise.event.Observes;
import jakarta.enterprise.inject.Produces;
import jakarta.enterprise.inject.build.compatible.spi.BeanInfo;
import jakarta.enterprise.inject.build.compatible.spi.BuildCompatibleExtension;
import jakarta.enterprise.inject.build.compatible.spi.Messages;
import jakarta.enterprise.inject.build.compatible.spi.ObserverInfo;
import jakarta.enterprise.inject.build.compatible.spi.Registration;
import jakarta.enterprise.inject.build.compatible.spi.Types;
import jakarta.inject.Qualifier;
import jakarta.inject.Singleton;
import jakarta.interceptor.AroundInvoke;
import jakarta.interceptor.Interceptor;
import jakarta.interceptor.InterceptorBinding;
import jakarta.interceptor.InvocationContext;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
Expand All @@ -26,7 +34,8 @@
public class RegistrationTest {
@RegisterExtension
public ArcTestContainer container = ArcTestContainer.builder()
.beanClasses(MyQualifier.class, MyService.class, MyFooService.class, MyBarService.class, MyBarServiceProducer.class)
.beanClasses(MyQualifier.class, MyInterceptorBinding.class, MyInterceptor.class, MyService.class,
MyFooService.class, MyBarService.class, MyBarServiceProducer.class)
.buildCompatibleExtensions(new MyExtension())
.build();

Expand All @@ -35,12 +44,14 @@ public void test() {
assertEquals(2, MyExtension.beanCounter.get());
assertEquals(1, MyExtension.beanMyQualifierCounter.get());
assertEquals(1, MyExtension.observerQualifierCounter.get());
assertEquals(1, MyExtension.interceptorCounter.get());
}

public static class MyExtension implements BuildCompatibleExtension {
static final AtomicInteger beanCounter = new AtomicInteger();
static final AtomicInteger beanMyQualifierCounter = new AtomicInteger();
static final AtomicInteger observerQualifierCounter = new AtomicInteger();
static final AtomicInteger interceptorCounter = new AtomicInteger();

@Registration(types = MyService.class)
public void beans(BeanInfo bean) {
Expand All @@ -57,6 +68,14 @@ public void observers(ObserverInfo observer, Types types) {
observerQualifierCounter.addAndGet(observer.qualifiers().size());
}
}

@Registration(types = MyInterceptor.class)
public void interceptors(BeanInfo interceptor, Messages msg) {
if (!interceptor.isInterceptor()) {
msg.error("Interceptor expected", interceptor);
}
interceptorCounter.incrementAndGet();
}
}

// ---
Expand All @@ -66,6 +85,22 @@ public void observers(ObserverInfo observer, Types types) {
public @interface MyQualifier {
}

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@InterceptorBinding
public @interface MyInterceptorBinding {
}

@MyInterceptorBinding
@Interceptor
@Priority(1)
public static class MyInterceptor {
@AroundInvoke
public Object intercept(InvocationContext ctx) throws Exception {
return ctx.proceed();
}
}

public interface MyService {
String hello();
}
Expand Down

0 comments on commit c1067bd

Please sign in to comment.