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

ArC removes indirect referenced beans #3311

Closed
dkellenb opened this issue Jul 23, 2019 · 9 comments · Fixed by #5466
Closed

ArC removes indirect referenced beans #3311

dkellenb opened this issue Jul 23, 2019 · 9 comments · Fixed by #5466
Labels
area/arc Issue related to ARC (dependency injection) kind/bug Something isn't working
Milestone

Comments

@dkellenb
Copy link

dkellenb commented Jul 23, 2019

Quarkus in version 19.1 removes indirect referenced beans.

Lets have following scenario. You define an interface for your strategies and an services that depending on any argument, returns the appropriate strategy. These strategies are defined as beans. As these beans are not referenced directly with injections, but only through the interface, the are removed by ArC. The only way to fix it, is by setting quarkus.arc.remove-unused-beans=false

public interface Exporter<T extends Exportable> {
    boolean accept(Class<T> clazz);
    String export(T object);
}

@ApplicationScoped
public class ApplicationExporter implements Exporter<Application> {
   boolean accept(Class<Application> clazz) { return clazz instanceof Application; }
   String export(Application application) {
     ...
   }
}

@ApplicationScoped
public class ExporterService {
  @Inject
  Instance<Exporter<? extends Exportable>> exporters; 
  public <T>Exporter<T> getExporter(Class<T> exporterClazz) {
    // BUG: exporters are empty!
    return StreamSupport.stream(exporters.spliterator(), false)
            .filter(exporter -> exporter.accept(exporterClazz))
            .findFirst().orElseThrow(...);
  }
}

If there is an @Inject javax.enterprise.inject.Instance<T> Quarkus (ArC) should keep all classes implementing T.

@dkellenb dkellenb changed the title ArC removes indirectly Beans ArC removes indirect referenced beans Jul 23, 2019
@geoand
Copy link
Contributor

geoand commented Jul 25, 2019

Although not a real solution, you can use quarkus.arc.remove-unused-beans=framework instead of the more invasive quarkus.arc.remove-unused-beans=false.

Furthermore you could also annotate your exporters with @Unremovable

@dkellenb
Copy link
Author

dkellenb commented Jul 25, 2019

Unfortunately we are not able to use @Unremovable or =framework, as these beans are from other libraries. But these notes may be interesting for other readers.

@geoand
Copy link
Contributor

geoand commented Jul 25, 2019

I see

@stale
Copy link

stale bot commented Nov 13, 2019

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you!
We are doing this automatically to ensure out-of-date issues does not stay around indefinitely.
If you believe this issue is still relevant please put a comment on it on why and if it truly needs to stay request or add 'pinned' label.

@stale stale bot added the stale label Nov 13, 2019
@gsmet
Copy link
Member

gsmet commented Nov 13, 2019

@mkouba should we improve the removal algorithm to deal with this case?

@maxandersen maxandersen removed the stale label Nov 13, 2019
@mkouba
Copy link
Contributor

mkouba commented Nov 13, 2019

If there is an @Inject javax.enterprise.inject.Instance Quarkus (ArC) should keep all classes implementing T.

@dkellenb We do keep those beans but there's probably a bug with matching a parameterized type with a wildcard. I'm going to verify this particular use case.

@mkouba mkouba added the area/arc Issue related to ARC (dependency injection) label Nov 13, 2019
@mkouba
Copy link
Contributor

mkouba commented Nov 13, 2019

Yep, it's a bug. I'll send a PR with a fix shortly.

@mkouba mkouba added the kind/bug Something isn't working label Nov 13, 2019
@mkouba mkouba added this to the 1.1.0 milestone Nov 13, 2019
@dkellenb
Copy link
Author

Yep, it's a bug. I'll send a PR with a fix shortly.

Great, looking forward to the fix.

@mkouba
Copy link
Contributor

mkouba commented Nov 14, 2019

@dkellenb Are you sure the injection point is Instance<Exporter<? extends Exportable>> and not something like Instance<? extends Exporter<? extends Exportable>>? It seems the bug I've found only occurs if the Instance param is wildcard or type variable... it should work if the param is a parameterized type though.

mkouba added a commit to mkouba/quarkus that referenced this issue Nov 14, 2019
mkouba added a commit to mkouba/quarkus that referenced this issue Nov 14, 2019
mkouba added a commit to mkouba/quarkus that referenced this issue Nov 15, 2019
@gsmet gsmet added backport? and removed backport? labels Nov 15, 2019
@gsmet gsmet modified the milestones: 1.1.0, 1.0.0.Final Nov 15, 2019
gsmet pushed a commit that referenced this issue Nov 15, 2019
ia3andy pushed a commit to dmlloyd/quarkus that referenced this issue Nov 19, 2019
Simulant87 pushed a commit to Simulant87/quarkus that referenced this issue Nov 23, 2019
Simulant87 pushed a commit to Simulant87/quarkus that referenced this issue Nov 23, 2019
mmusgrov pushed a commit to mmusgrov/quarkus that referenced this issue Dec 13, 2019
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.

5 participants