Skip to content

Commit

Permalink
fix issue weld#149; make method parameter injection work for parametr…
Browse files Browse the repository at this point in the history
…ized types
  • Loading branch information
Johannes Hahn committed Dec 4, 2022
1 parent 6d3d037 commit 444549b
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ public Object resolveParameter(ParameterContext parameterContext, ExtensionConte
if (getContainerFromStore(extensionContext) != null) {
List<Annotation> qualifiers = resolveQualifiers(parameterContext, getContainerFromStore(extensionContext).getBeanManager());
return getContainerFromStore(extensionContext)
.select(parameterContext.getParameter().getType(), qualifiers.toArray(new Annotation[qualifiers.size()])).get();
.select(parameterContext.getParameter().getParameterizedType(),
qualifiers.toArray(new Annotation[qualifiers.size()])).get();
}
return null;
}
Expand All @@ -156,7 +157,7 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon
if ((getExplicitInjectionInfoFromStore(extensionContext) || (methodRequiresExplicitParamInjection(parameterContext))) && qualifiers.isEmpty()) {
return false;
} else {
return getContainerFromStore(extensionContext).select(parameterContext.getParameter().getType(), qualifiers.toArray(new Annotation[qualifiers.size()]))
return getContainerFromStore(extensionContext).select(parameterContext.getParameter().getParameterizedType(), qualifiers.toArray(new Annotation[qualifiers.size()]))
.isResolvable();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,17 @@
package org.jboss.weld.junit5.extensionInjection;

import javax.inject.Inject;
import javax.enterprise.event.Event;
import javax.enterprise.inject.Instance;

import org.jboss.weld.junit5.WeldJunit5Extension;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

import java.util.Map;

/**
* Basic test for JUnit 5 injection into parameter/field handled by Weld
* @author <a href="mailto:manovotn@redhat.com">Matej Novotny</a>
Expand Down Expand Up @@ -54,4 +59,26 @@ public void testparamInjectionWithQualifier(@MyQualifier BarBean bar) {
Assertions.assertNotNull(bar);
bar.ping();
}
@Nested
class TestParameterInjectionWithParametrizedTypes {

@Test
public void testparamInjectionWithEvent(Event<String> stringEvent) {
// assert param injection with built-in Event bean works
Assertions.assertNotNull(stringEvent);
}

@Test
public void testparamInjectionWithInstance(Instance<String> stringInstance) {
// assert param injection with built-in Instance works
Assertions.assertNotNull(stringInstance);
}

@Test
public void testparamInjectionWithOtherParametrizedType(Map<String, Integer> map) {
// assert param injection with ordinary bean with parametrized type works
Assertions.assertNotNull(map);
Assertions.assertTrue(map.containsKey("java.util.Map<java.lang.String, java.lang.Integer>"));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.jboss.weld.junit5.extensionInjection;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import javax.enterprise.inject.spi.InjectionPoint;
import java.util.HashMap;
import java.util.Map;

@ApplicationScoped
public class MapProducer {

@Produces
@Dependent
public <T> Map<String,T> produceMap(InjectionPoint ip) {
Map<String, T> map = new HashMap<>();
map.put(ip.getType().getTypeName(), null);
return map;
}
}

0 comments on commit 444549b

Please sign in to comment.