Skip to content

Commit 037e011

Browse files
authored
fix raw generic messing up tests (#861)
Fixes issue where the raw generic type would match in test scopes and cause beans not to wire
1 parent 7bb5b05 commit 037e011

File tree

5 files changed

+59
-2
lines changed

5 files changed

+59
-2
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.example.generic;
2+
3+
import java.util.function.Supplier;
4+
5+
import io.avaje.inject.test.TestScope;
6+
7+
@TestScope
8+
public class GenericBean implements Supplier<Short> {
9+
10+
@Override
11+
public Short get() {
12+
return null;
13+
}
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.example.generic;
2+
3+
import java.util.function.Supplier;
4+
5+
import jakarta.inject.Singleton;
6+
7+
@Singleton
8+
public class GenericBean2 implements Supplier<Long> {
9+
10+
@Override
11+
public Long get() {
12+
return null;
13+
}
14+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.example.generic;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import org.junit.jupiter.api.Test;
6+
7+
import io.avaje.inject.test.InjectTest;
8+
import jakarta.inject.Inject;
9+
10+
@InjectTest
11+
public class TestScopeGenericTest {
12+
13+
@Inject public GenericBean bean1;
14+
@Inject public GenericBean2 bean2;
15+
16+
@Test
17+
void getTypeName() {
18+
assertThat(bean1).isNotNull();
19+
assertThat(bean2).isNotNull();
20+
}
21+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,5 @@
11
io.avaje.inject.events.spi.ObserverManagerPlugin
2+
io.avaje.http.inject.DefaultResolverProvider
3+
io.avaje.inject.events.spi.ObserverManagerPlugin
4+
io.avaje.jsonb.inject.DefaultJsonbProvider
5+
org.example.ExampleModule

inject/src/main/java/io/avaje/inject/spi/DBuilder.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public boolean isBeanAbsent(String name, Type... types) {
6565
}
6666

6767
/**
68-
* Return the types without any annotation types.
68+
* Return the types without any annotation types or raw generics.
6969
*
7070
* <p>For the purposes of supplied beans (typically test doubles) we are not interested in
7171
* annotation types.
@@ -81,7 +81,11 @@ protected final Type[] removeAnnotations(Type[] source) {
8181
}
8282

8383
private boolean isAnnotationType(Type type) {
84-
return type instanceof Class && ((Class<?>) type).isAnnotation();
84+
if (type instanceof Class<?>) {
85+
var clazz = (Class<?>) type;
86+
return clazz.isAnnotation() || clazz.getTypeParameters().length != 0;
87+
}
88+
return false;
8589
}
8690

8791
protected final void next(String name, Type... types) {

0 commit comments

Comments
 (0)