Skip to content
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

Generic Syntetic Bean cause ambigious type resolution when none exists #20566

Closed
Christopher-Chianelli opened this issue Oct 6, 2021 · 2 comments · Fixed by #20582
Closed
Assignees
Labels
area/arc Issue related to ARC (dependency injection) kind/bug Something isn't working
Milestone

Comments

@Christopher-Chianelli
Copy link
Contributor

Describe the bug

When a Synthetic bean for a generic type is created like this:

    SyntheticBeanBuildItem addMyGenericInterface() {
        DotName interfaceName = DotName.createSimple(MyGenericInterface.class.getName());
        DotName numberTypeName = DotName.createSimple(AtomicInteger.class.getName());
        DotName stringTypeName = DotName.createSimple(String.class.getName());
        return SyntheticBeanBuildItem.configure(interfaceName)
                .creator(methodCreator -> {
                    FunctionCreator functionCreator = methodCreator.createFunction(MyGenericInterface.class);
                    BytecodeCreator functionBytecode = functionCreator.getBytecode();
                    functionBytecode.returnValue(functionBytecode.loadNull());
                    methodCreator.returnValue(functionCreator.getInstance());
                })
                .types(ParameterizedType.create(interfaceName,
                                                new Type[]{
                                                        Type.create(numberTypeName, Type.Kind.CLASS),
                                                        Type.create(stringTypeName, Type.Kind.CLASS)
                                                }, null))
                .done();
    }

an "ambigious type resolution" occurs within ArC, with the same synthetic bean showing up twice.

Thus, it is impossible to inject generic Syntetic beans, since with the types argument, it cause ambiguity, and without it, it cannot find an instance.

Expected behavior

@Inject
MyGenericInterface<AtomicInteger, String> myGenericInterface;

causes myGenericInterface to be injected with the synthetic bean

Actual behavior

java.lang.RuntimeException: 
java.lang.RuntimeException: io.quarkus.builder.BuildException: Build failure: Build failed due to errors
	[error]: Build step io.quarkus.arc.deployment.ArcProcessor#validate threw an exception: javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.AmbiguousResolutionException: Ambiguous dependencies for type org.acme.my.ext.MyGenericInterface<java.util.concurrent.atomic.AtomicInteger, java.lang.String> and qualifiers [@Default]
	- java member: org.acme.my.ext.test.MyExtTest#myGenericInterface
	- declared on CLASS bean [types=[org.acme.my.ext.test.MyExtTest, java.lang.Object], qualifiers=[@Default, @Any], target=org.acme.my.ext.test.MyExtTest]
	- available beans:
		- SYNTHETIC bean [types=[org.acme.my.ext.MyGenericInterface, org.acme.my.ext.MyGenericInterface<java.util.concurrent.atomic.AtomicInteger, java.lang.String>], qualifiers=[@Default, @Any], target=n/a]
		- SYNTHETIC bean [types=[org.acme.my.ext.MyGenericInterface, org.acme.my.ext.MyGenericInterface<java.util.concurrent.atomic.AtomicInteger, java.lang.String>], qualifiers=[@Default, @Any], target=n/a]
	at io.quarkus.arc.processor.BeanDeployment.processErrors(BeanDeployment.java:1190)
	at io.quarkus.arc.processor.BeanDeployment.init(BeanDeployment.java:268)
	at io.quarkus.arc.processor.BeanProcessor.initialize(BeanProcessor.java:129)
	at io.quarkus.arc.deployment.ArcProcessor.validate(ArcProcessor.java:418)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:566)
	at io.quarkus.deployment.ExtensionLoader$2.execute(ExtensionLoader.java:820)
	at io.quarkus.builder.BuildContext.run(BuildContext.java:277)
	at org.jboss.threads.ContextHandler$1.runWith(ContextHandler.java:18)
	at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2449)
	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1478)
	at java.base/java.lang.Thread.run(Thread.java:829)
	at org.jboss.threads.JBossThread.run(JBossThread.java:501)
Caused by: javax.enterprise.inject.AmbiguousResolutionException: Ambiguous dependencies for type org.acme.my.ext.MyGenericInterface<java.util.concurrent.atomic.AtomicInteger, java.lang.String> and qualifiers [@Default]
	- java member: org.acme.my.ext.test.MyExtTest#myGenericInterface
	- declared on CLASS bean [types=[org.acme.my.ext.test.MyExtTest, java.lang.Object], qualifiers=[@Default, @Any], target=org.acme.my.ext.test.MyExtTest]
	- available beans:
		- SYNTHETIC bean [types=[org.acme.my.ext.MyGenericInterface, org.acme.my.ext.MyGenericInterface<java.util.concurrent.atomic.AtomicInteger, java.lang.String>], qualifiers=[@Default, @Any], target=n/a]
		- SYNTHETIC bean [types=[org.acme.my.ext.MyGenericInterface, org.acme.my.ext.MyGenericInterface<java.util.concurrent.atomic.AtomicInteger, java.lang.String>], qualifiers=[@Default, @Any], target=n/a]
	at io.quarkus.arc.processor.Beans.resolveInjectionPoint(Beans.java:576)
	at io.quarkus.arc.processor.BeanInfo.init(BeanInfo.java:470)
	at io.quarkus.arc.processor.BeanDeployment.init(BeanDeployment.java:256)
	... 13 more

How to Reproduce?

Reproducer: https://github.com/Christopher-Chianelli/issue-reproducer/tree/quarkus-extension-syntetic-bean

Steps to reproduce:

  1. cd my-ext
  2. mvn clean install
  3. Notice MyExtTest writeYourOwnUnitTest fails because myGenericInterface cannot be injected because ambiguous resolution (despite addMyGenericInterface only returing 1 instance of it)

Output of uname -a or ver

Linux 5.14.9-200.fc34.x86_64

Output of java -version

openjdk 11.0.12 2021-07-20

GraalVM version (if different from Java)

No response

Quarkus version or git rev

fffe31b

Build tool (ie. output of mvnw --version or gradlew --version)

Apache Maven 3.6.3 (cecedd343002696d0abb50b32b541b8a6ba2883f)

Additional information

No response

@Christopher-Chianelli Christopher-Chianelli added the kind/bug Something isn't working label Oct 6, 2021
@geoand geoand added area/arc Issue related to ARC (dependency injection) and removed triage/needs-triage labels Oct 7, 2021
@quarkus-bot
Copy link

quarkus-bot bot commented Oct 7, 2021

/cc @manovotn, @mkouba

@mkouba
Copy link
Contributor

mkouba commented Oct 7, 2021

@Christopher-Chianelli thanks for the report! This is very likely a regression introduced by this optimization, i.e. it should work fine in 2.3.0. I'll send a PR shortly.

@mkouba mkouba self-assigned this Oct 7, 2021
mkouba added a commit to mkouba/quarkus that referenced this issue Oct 7, 2021
- BeanDeployment#beansByType - a bean should not be listed multiple
times even if it has multiple types with the same raw name in its set of
bean types
- fix a regression introduced in quarkusio#20470
- resolves quarkusio#20566
@quarkus-bot quarkus-bot bot added this to the 2.4 - main milestone Oct 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/arc Issue related to ARC (dependency injection) kind/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants