Skip to content

Fix injecting generic types extending generic types #765

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Jan 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.example.myapp.generic;

import java.util.function.BiConsumer;
import java.util.function.Consumer;

public interface Aldrich<T, T2> extends BiConsumer<T, T2>, Consumer<BiConsumer<T, T2>> {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.example.myapp.generic;

public interface MyA {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package org.example.myapp.generic;

public interface MyB {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package org.example.myapp.generic;

import java.util.function.BiConsumer;

public class MyConsumer implements BiConsumer<MyA, MyB> {

@Override
public void accept(MyA t, MyB u) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.example.myapp.generic;

import io.avaje.inject.Bean;
import io.avaje.inject.Factory;

import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;

@Factory
class MyConsumerFactory {

@Bean
MyConsumer createTest() {
return new MyConsumer();
}

@Bean
Map<String, MyConsumer> genericMap(BiConsumer<MyA, MyB> consumer) {
return new HashMap<>();
}

@Bean
Aldrich<String, String> bean() {
return new Aldrich<>() {
@Override
public void accept(String s, String s2) {

}

@Override
public void accept(BiConsumer<String, String> stringStringBiConsumer) {

}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package org.example.myapp.generic;

import jakarta.inject.Singleton;

import java.util.Map;
import java.util.function.BiConsumer;

@Singleton
class MyUseGenericDependencies {

private final Aldrich<String, String> aldrich;
private final Map<String, MyConsumer> genericMap;
private final BiConsumer<MyA, MyB> biConsumer;

MyUseGenericDependencies(Aldrich<String, String> aldrich,
Map<String, MyConsumer> genericMap,
BiConsumer<MyA, MyB> biConsumer) {
this.aldrich = aldrich;
this.genericMap = genericMap;
this.biConsumer = biConsumer;
}

Aldrich<String, String> getAldrich() {
return aldrich;
}

Map<String, MyConsumer> getGenericMap() {
return genericMap;
}

BiConsumer<MyA, MyB> getBiConsumer() {
return biConsumer;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,14 @@ class GenericFactoryTest {

@Inject Generic<Integer> intymcintface;
@Inject Generic<String> stringy;
@Inject MyUseGenericDependencies others;

@Test
void test() {
assertThat(intymcintface).isNotNull();
assertThat(stringy).isNotNull();
assertThat(others.getAldrich()).isNotNull();
assertThat(others.getGenericMap()).isNotNull();
assertThat(others.getBiConsumer()).isNotNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Stream;

import javax.lang.model.type.TypeKind;

Expand All @@ -21,22 +22,31 @@ final class TypeAppender {

void add(UType utype) {
var type = Util.unwrapProvider(utype);
var components = type.componentTypes();
if (isAddGenericType(type, components)) {
if (isAddGenericType(type)) {
addUType(type);
} else {
addSimpleType(type.mainType());
}
}

private static boolean isAddGenericType(UType type, List<UType> components) {
private static boolean isAddGenericType(UType type) {
var components = type.componentTypes();
return type.isGeneric()
&& type.kind() != TypeKind.TYPEVAR
&& (components.size() != 1 || components.get(0).kind() != TypeKind.WILDCARD)
&& components.stream()
.flatMap(TypeAppender::allComponentTypes)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Noting that this is effectively the fix here.

.noneMatch(u -> u.kind() == TypeKind.TYPEVAR || u.kind() == TypeKind.WILDCARD);
}

private static Stream<UType> allComponentTypes(UType u) {
final var componentTypes = u.componentTypes();
return componentTypes.isEmpty()
? Stream.of(u)
: Stream.concat(
Stream.of(u), componentTypes.stream().flatMap(TypeAppender::allComponentTypes));
}

void add(List<UType> sourceTypes) {
sourceTypes.forEach(this::add);
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.avaje.inject.generator.models.valid.provider;

public class BProv<T> {

final T value;

public BProv(T value) {
this.value = value;
}

public T get() {
return value;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.avaje.inject.generator.models.valid.provider;

import io.avaje.inject.Component;
import jakarta.inject.Provider;

import java.util.concurrent.atomic.AtomicInteger;

@Component
public class BProvProvider implements Provider<BProv<String>> {

AtomicInteger counter = new AtomicInteger();

@Override
public BProv<String> get() {
return new BProv<>("Hello BProv" + counter.incrementAndGet());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ void providerOfGenericType() {
BProv<String> bProv2 = beanScope.get(BProvProvider$DI.TYPE_BProvString);
assertThat(bProv).isNotSameAs(bProv2);
assertThat(bProv2.get()).isEqualTo("Hello BProv2");

BProvUser bProvUser = beanScope.get(BProvUser.class);
BProv<String> bProv3 = bProvUser.getProvider().get();
assertThat(bProv3.get()).isEqualTo("Hello BProv3");
assertThat(bProv).isNotSameAs(bProv3);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package org.example.coffee.provider;

import io.avaje.inject.Component;
import jakarta.inject.Provider;

@Component
class BProvUser {

private final Provider<BProv<String>> provider;

BProvUser(Provider<BProv<String>> provider){
this.provider = provider;
}

Provider<BProv<String>> getProvider() {
return provider;
}
}
Loading