Skip to content

Commit

Permalink
ArC - resolve type variables for inherited observer methods
Browse files Browse the repository at this point in the history
- resolves quarkusio#25364
  • Loading branch information
mkouba committed May 4, 2022
1 parent 43070cb commit a2142d5
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
import io.quarkus.arc.processor.ObserverTransformer.TransformationContext;
import io.quarkus.gizmo.MethodCreator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import java.util.Set;
import java.util.function.Consumer;
import javax.enterprise.event.Reception;
Expand All @@ -25,6 +27,7 @@
import org.jboss.jandex.MethodInfo;
import org.jboss.jandex.MethodParameterInfo;
import org.jboss.jandex.Type;
import org.jboss.jandex.TypeVariable;
import org.jboss.logging.Logger;

/**
Expand All @@ -48,10 +51,20 @@ static ObserverInfo create(BeanInfo declaringBean, MethodInfo observerMethod, In
} else {
priority = ObserverMethod.DEFAULT_PRIORITY;
}

Type observedType = observerMethod.parameters().get(eventParameter.position());
if (Types.containsTypeVariable(observedType)) {
Map<TypeVariable, Type> resolvedTypeVariables = Types
.resolvedTypeVariables(declaringBean.getImplClazz(), declaringBean.getDeployment())
.getOrDefault(observerMethod.declaringClass(), Collections.emptyMap());
observedType = Types.resolveTypeParam(observedType, resolvedTypeVariables,
declaringBean.getDeployment().getBeanArchiveIndex());
}

return create(null, declaringBean.getDeployment(), declaringBean.getTarget().get().asClass().name(), declaringBean,
observerMethod, injection,
eventParameter,
observerMethod.parameters().get(eventParameter.position()),
observedType,
initQualifiers(declaringBean.getDeployment(), observerMethod, eventParameter),
initReception(isAsync, declaringBean.getDeployment(), observerMethod),
initTransactionPhase(isAsync, declaringBean.getDeployment(), observerMethod), isAsync, priority, transformers,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.quarkus.arc.test.observers.inheritance.typevariable;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import io.quarkus.arc.Arc;
import io.quarkus.arc.Unremovable;
import io.quarkus.arc.test.ArcTestContainer;
import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.event.Event;
import javax.enterprise.event.Observes;
import javax.inject.Inject;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

/**
* https://github.com/quarkusio/quarkus/issues/25364
*/
public class ObserverInheritanceTypeVariableTest {

@RegisterExtension
public ArcTestContainer container = new ArcTestContainer(MyEvent.class, MyAEvent.class, MyBEvent.class, MyAService.class,
MyBService.class, EventSource.class);

@Test
public void testNotification() {
Arc.container().instance(EventSource.class).get().sendA();
assertNotNull(MyAService.event);
assertNull(MyBService.event);
}

static class MyEvent {
}

static class MyAEvent extends MyEvent {
}

static class MyBEvent extends MyEvent {
}

static abstract class AbstractService<E extends MyEvent> {

void onEvent(@Observes E myEvent) {
doSomething(myEvent);
}

abstract void doSomething(E myEvent);
}

@ApplicationScoped
static class MyAService extends AbstractService<MyAEvent> {

static volatile MyAEvent event;

@Override
protected void doSomething(MyAEvent myEvent) {
MyAService.event = myEvent;
}
}

@ApplicationScoped
static class MyBService extends AbstractService<MyBEvent> {

static volatile MyBEvent event;

@Override
void doSomething(MyBEvent myEvent) {
MyBService.event = myEvent;
}
}

@Unremovable
@Dependent
static class EventSource {

@Inject
Event<MyEvent> event;

void sendA() {
event.fire(new MyAEvent());
}
}

}

0 comments on commit a2142d5

Please sign in to comment.