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

NPE when using SyntheticBeanBuildItem with BeanCreator #27510

Closed
rdebusscher opened this issue Aug 25, 2022 · 10 comments · Fixed by #27537
Closed

NPE when using SyntheticBeanBuildItem with BeanCreator #27510

rdebusscher opened this issue Aug 25, 2022 · 10 comments · Fixed by #27537
Assignees
Labels
area/arc Issue related to ARC (dependency injection)
Milestone

Comments

@rdebusscher
Copy link
Contributor

Describe the bug

I'm creating a Quarkus extension and one of the things I need is to use the replacement for AfterBeanDiscovery.addBean().

But I get a NullPointerException when Quarkus tries to instantiate the bean at runtime (in generated code, so no idea what is null)

A simple reproducer of the problem can be found at https://github.com/rdebusscher/quarkus-syntheticbean

Expected behavior

Ability to instantiate an object myself in code and provide it through the 'CDI injection' system of Quarkus. No exceptions.

Actual behavior

When accessing the endpoint where the bean is injected, following NPE is thrown.

org.jboss.resteasy.spi.UnhandledException: java.lang.RuntimeException: Error injecting be.rubus.microstream.quarkus.integration.ExampleBean be.rubus.microstream.quarkus.GreetingResource.exampleBean
        at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:368)
        at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:218)
        at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:519)
...
Caused by: java.lang.RuntimeException: Error injecting be.rubus.microstream.quarkus.integration.ExampleBean be.rubus.microstream.quarkus.GreetingResource.exampleBean
        at be.rubus.microstream.quarkus.GreetingResource_Bean.create(Unknown Source)
        at be.rubus.microstream.quarkus.GreetingResource_Bean.create(Unknown Source)
        at io.quarkus.arc.impl.AbstractSharedContext.createInstanceHandle(AbstractSharedContext.java:111)
...
Caused by: java.lang.NullPointerException
        at be.rubus.microstream.quarkus.integration.ExampleBean_f7ae08679933a92c66701907ce1ebba818ff26b3_Synthetic_Bean.get(Unknown Source)
        at be.rubus.microstream.quarkus.integration.ExampleBean_f7ae08679933a92c66701907ce1ebba818ff26b3_Synthetic_Bean.get(Unknown Source)
        ... 34 more

How to Reproduce?

See https://github.com/rdebusscher/quarkus-syntheticbean

Output of uname -a or ver

Darwin 21.6.0 Darwin Kernel Version 21.6.0: Wed Aug 10 14:25:27 PDT 2022; root:xnu-8020.141.5~2/RELEASE_X86_64 x86_64

Output of java -version

openjdk version "11.0.14.1" 2022-02-08 LTS OpenJDK Runtime Environment Zulu11.54+25-CA (build 11.0.14.1+1-LTS) OpenJDK 64-Bit Server VM Zulu11.54+25-CA (build 11.0.14.1+1-LTS, mixed mode)

GraalVM version (if different from Java)

/

Quarkus version or git rev

<quarkus.version>2.11.1.Final</quarkus.version>

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

Maven 3.8.2

Additional information

No response

@rdebusscher rdebusscher added the kind/bug Something isn't working label Aug 25, 2022
@gsmet gsmet added area/arc Issue related to ARC (dependency injection) and removed triage/needs-triage labels Aug 25, 2022
@quarkus-bot
Copy link

quarkus-bot bot commented Aug 25, 2022

/cc @manovotn, @mkouba

@mkouba mkouba self-assigned this Aug 25, 2022
@manovotn
Copy link
Contributor

manovotn commented Aug 25, 2022

@rdebusscher Your singleton scope is com.google.inject.Singleton you need to use the javax.inject.Singleton variant

@mkouba
Copy link
Contributor

mkouba commented Aug 25, 2022

Matej is right. We do fail the build if a class bean is annotated with com.google.inject.Singleton but we don't check the scope class passed to the SyntheticBeanBuildItem configurator. I think that we should fail consistently...

@manovotn
Copy link
Contributor

@rdebusscher also the BeanCreator implementation needs to be in the runtime module as that is needed to instantiate the bean at runtime (when it is needed).

With that, I am getting:

[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 5.473 s - in be.rubus.quarkus.GreetingResourceTest
2022-08-25 16:19:57,034 INFO [io.quarkus] (main) app stopped in 0.040s
[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0

@mkouba mkouba removed the kind/bug Something isn't working label Aug 25, 2022
@manovotn
Copy link
Contributor

but we don't check the scope class passed to the SyntheticBeanBuildItem configurator. I think that we should fail consistently...

@mkouba yea, we could at least check if it is a scope annotation or simply check if the return value is null and throw something sensible.

@mkouba mkouba assigned manovotn and unassigned mkouba Aug 25, 2022
@rdebusscher
Copy link
Contributor Author

Thx for the quick replies and the solution.

A few related remarks (although not familiar with the entire architecture)

  • is Maven Embedder (which brings in Google Guice which was the confusion here) needed at compile time of the extension, or only at build time of the application (and thus scope/dependency should be change/moved )?
  • The ExampleBeanCreator is indeed needed in runtime part of extension. But in the view that an extension has a part that should be used by the developer (runtime) and another part by internal Quarkus stuff (deployment) it is not logic that the BeanCreator is accessible by the developer in the application code (since they should not use it directly).

@Ladicek
Copy link
Contributor

Ladicek commented Aug 25, 2022

an extension has a part that should be used by the developer (runtime) and another part by internal Quarkus stuff (deployment)

I don't think that's a helpful interpretation. It's more direct: the deployment module is supposed to contain code that is executed during build (and shall not be present at runtime), while the runtime module is supposed to contain code that is required at runtime.

@manovotn
Copy link
Contributor

is Maven Embedder (which brings in Google Guice which was the confusion here) needed at compile time of the extension, or only at build time of the application (and thus scope/dependency should be change/moved )?

I vaguely recall a discussion where @gsmet explained we cannot really get rid of that?
But that might have been just in relation to jakarta deps, not sure now :D

Tomorrow, I'll look into making the validation check the annotation so that there is sensible exception in cases such as this one.

@gsmet
Copy link
Member

gsmet commented Aug 26, 2022

I discussed with @aloubyansky to see if we could mark the bootstrap Maven and Gradle dependencies as provided (and provide them accordingly where needed) to clean up the extension deployment classpath when developing extensions.

@gsmet
Copy link
Member

gsmet commented Aug 29, 2022

@rdebusscher the PR cleaning up the classpath of the deployment artifacts has been merged: #27562

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)
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants