Skip to content

Commit

Permalink
fix generic handling
Browse files Browse the repository at this point in the history
  • Loading branch information
graemerocher committed May 20, 2024
1 parent 4573442 commit 0df1173
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,10 @@ public void visitClass(ClassElement element, VisitorContext context) {
}
beanElementBuilder.produceBeans(producesMethodQuery, childBuilder -> {
MethodElement methodElement = (MethodElement) childBuilder.getProducingElement();
childBuilder.typed(methodElement.getGenericReturnType());
ClassElement genericReturnType = methodElement.getGenericReturnType();
childBuilder.typed(genericReturnType);
ClassElement[] typeArguments = genericReturnType.getBoundGenericTypes().toArray(ClassElement[]::new);
childBuilder.typeArguments(typeArguments);
childBuilder.annotate(Primary.class);
AnnotationMetadata annotationMetadata = methodElement.getAnnotationMetadata();
Set<String> annotationNames = annotationMetadata.getAnnotationNames();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,30 @@ class Test {
expect:
getBean(ctx, 'test.Test').foo == 'good'
}

void "test produces with generics annotation"() {
given:
def ctx = buildContext('''
package test;
import com.google.inject.AbstractModule;
import com.google.inject.Inject;
import com.google.inject.Provides;
import io.micronaut.guice.annotation.Guice;import java.util.function.Supplier;
class SimpleModule extends AbstractModule {
@Provides
public Supplier<String> test() {
return () -> "good";
}
}
@Guice(modules= SimpleModule.class)
class Test {
@Inject public Supplier<String> foo;
}
''')
expect:
getBean(ctx, 'test.Test').foo.get() == 'good'
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.micronaut.guice.doc.examples.bindings.injector2;

public record Engine<T>(T cylinder) {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package io.micronaut.guice.doc.examples.bindings.injector2;

import com.google.inject.AbstractModule;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provides;
import com.google.inject.Singleton;
import com.google.inject.TypeLiteral;
import io.micronaut.guice.annotation.Guice;
import io.micronaut.test.extensions.junit5.annotation.MicronautTest;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

@MicronautTest(startApplication = false, environments = "generic-test")
@Guice(modules = FooModule.class, environments = "generic-test")
public class InjectorTest {
@Test
void testInjector(Injector injector) {
Foo foo = injector.getInstance(Foo.class);
Assertions.assertNotNull(foo);

Engine<V8> v8 = injector.getInstance(Key.get(new TypeLiteral<Engine<V8>>() {
}));
Engine<V6> v6 = injector.getInstance(Key.get(new TypeLiteral<Engine<V6>>() {
}));

Assertions.assertNotNull(v8);
Assertions.assertInstanceOf(V8.class, v8.cylinder());

Assertions.assertNotNull(v6);
Assertions.assertInstanceOf(V6.class, v6.cylinder());
}
}

class FooModule extends AbstractModule {

@Provides
public Engine<V8> v8() {
return new Engine<>(new V8());
}

@Provides
public Engine<V6> v6() {
return new Engine<>(new V6());
}
}
@Singleton
class Foo {}

class V8 {}
class V6 {}

0 comments on commit 0df1173

Please sign in to comment.