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

Provider registered to Hk2InjectionManager cannot be process because of incompatible type #4092

Closed
ivanfryda opened this issue Apr 5, 2019 · 7 comments
Labels
Milestone

Comments

@ivanfryda
Copy link

ivanfryda commented Apr 5, 2019

I'm configuring Jersey via the web.xml. On startup, I'm getting the following error:

java.lang.IllegalArgumentException: Provider registered to Hk2InjectionManager cannot be process because of incompatible type: class java.lang.Class. at org.glassfish.jersey.inject.hk2.ImmediateHk2InjectionManager.register(ImmediateHk2InjectionManager.java:90)

What is going on is that CommonConfig.configureExternalObjects is getting the configured Class objects and these are sent to ImmediateHk2InjectionManager.register(Object). This method performs a providerClass.getClass(), which returns a plain Java Class object, which will not satisfy the condition for AbstractHk2InjectionManager.isRegistrable.

What is the solution to this?

@senivam
Copy link
Contributor

senivam commented Apr 8, 2019

Could you please provide following information: Jersey's version, piece of code which causes described exception (in this case reproducer would be ideal)?

@jansupol
Copy link
Contributor

jansupol commented Apr 8, 2019

Have you tried to check isRegistrable?

@Override
public boolean isRegistrable(Class<?> clazz) {
    return org.glassfish.hk2.utilities.Binder.class.isAssignableFrom(clazz);
}

@xvik
Copy link

xvik commented Apr 20, 2019

Bug appears when org.glassfish.hk2.utilities.binding.AbstractBinder used instead of org.glassfish.jersey.internal.inject.AbstractBinder. The reason is perfectly explained by @ivanfryda.

I'm sure this would be a quite common case (because org.glassfish.jersey.internal.inject.AbstractBinder is in internal package (as everything related to injection).. which is weird).

Could be reproduced with this dropwizard project (jersey 2.28): https://gitlab.com/xvik/jersey-case

INFO  [2019-04-20 09:56:11,465] org.eclipse.jetty.server.handler.ContextHandler: Stopped i.d.j.MutableServletContextHandler@566d0c69{/,null,UNAVAILABLE}
javax.servlet.ServletException: io.dropwizard.jersey.setup.JerseyServletContainer-24f360b2@d067477==io.dropwizard.jersey.setup.JerseyServletContainer,jsp=null,order=1,inst=false,async=true
	at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:692)
	at org.eclipse.jetty.servlet.ServletHolder.initialize(ServletHolder.java:428)
	at org.eclipse.jetty.servlet.ServletHandler.lambda$initialize$0(ServletHandler.java:750)
	at java.util.stream.SortedOps$SizedRefSortingSink.end(SortedOps.java:352)
	at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:482)
	at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
	at java.util.stream.StreamSpliterators$WrappingSpliterator.forEachRemaining(StreamSpliterators.java:312)
	at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:743)
	at java.util.stream.Streams$ConcatSpliterator.forEachRemaining(Streams.java:742)
	at java.util.stream.ReferencePipeline$Head.forEach(ReferencePipeline.java:580)
	at org.eclipse.jetty.servlet.ServletHandler.initialize(ServletHandler.java:744)
	at org.eclipse.jetty.servlet.ServletContextHandler.startContext(ServletContextHandler.java:369)
	at org.eclipse.jetty.server.handler.ContextHandler.doStart(ContextHandler.java:852)
	at org.eclipse.jetty.servlet.ServletContextHandler.doStart(ServletContextHandler.java:278)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:167)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:110)
	at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
	at com.codahale.metrics.jetty9.InstrumentedHandler.doStart(InstrumentedHandler.java:101)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:167)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:119)
	at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:167)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:110)
	at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
	at org.eclipse.jetty.server.handler.gzip.GzipHandler.doStart(GzipHandler.java:406)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:167)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:110)
	at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:167)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:110)
	at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
	at org.eclipse.jetty.server.handler.StatisticsHandler.doStart(StatisticsHandler.java:256)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.start(ContainerLifeCycle.java:167)
	at org.eclipse.jetty.server.Server.start(Server.java:418)
	at org.eclipse.jetty.util.component.ContainerLifeCycle.doStart(ContainerLifeCycle.java:110)
	at org.eclipse.jetty.server.handler.AbstractHandler.doStart(AbstractHandler.java:113)
	at org.eclipse.jetty.server.Server.doStart(Server.java:382)
	at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
	at io.dropwizard.cli.ServerCommand.run(ServerCommand.java:53)
	at io.dropwizard.cli.EnvironmentCommand.run(EnvironmentCommand.java:44)
	at io.dropwizard.cli.ConfiguredCommand.run(ConfiguredCommand.java:87)
	at io.dropwizard.cli.Cli.run(Cli.java:78)
	at io.dropwizard.Application.run(Application.java:93)
	at com.sample.SampleApp.main(SampleApp.java:16)
Caused by: java.lang.IllegalArgumentException: Provider registered to Hk2InjectionManager cannot be process because of incompatible type: class java.lang.Class.
	at org.glassfish.jersey.inject.hk2.ImmediateHk2InjectionManager.register(ImmediateHk2InjectionManager.java:66)
	at java.lang.Iterable.forEach(Iterable.java:75)
	at org.glassfish.jersey.model.internal.CommonConfig.configureExternalObjects(CommonConfig.java:642)
	at org.glassfish.jersey.model.internal.CommonConfig.configureMetaProviders(CommonConfig.java:609)
	at org.glassfish.jersey.server.ResourceConfig.configureMetaProviders(ResourceConfig.java:799)
	at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:327)
	at org.glassfish.jersey.server.ApplicationHandler.lambda$initialize$1(ApplicationHandler.java:292)
	at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
	at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
	at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:232)
	at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:291)
	at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:258)
	at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:311)
	at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:154)
	at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:346)
	at javax.servlet.GenericServlet.init(GenericServlet.java:244)
	at org.eclipse.jetty.servlet.ServletHolder.initServlet(ServletHolder.java:671)
	... 49 more

@jansupol
Copy link
Contributor

Unfortunately, since Jersey 2.26, org.glassfish.hk2.utilities.binding.AbstractBinder can no longer be used and org.glassfish.jersey.internal.inject.AbstractBinder is to be used instead.
This is, however, a bug:

  1. It is backward incompatible
  2. New AbstractBinder is in an internal package which won't be visible when we come up with module.info

We try to fix this in 2.29.

@jansupol jansupol added the 2.29 label Apr 23, 2019
@xvik
Copy link

xvik commented Apr 23, 2019

It would be great if injection api will go out of the intenal package (it's sad that it would mean another breaking change, but have to be done anyway).

But I (and @ivanfryda) was mainly trying to pay your attention that ImmediateHk2InjectionManager.register(Object) is not correct:

@Override
    public void register(Object provider) {
        if (isRegistrable(provider.getClass())) {
        ...
    }

it always performs provider.getClass() even if provider is already a class.

AbstractBinder was just a way to reproduce. There might be other cases when this bug will reveal again because in CommonConfig you call it with classes:

componentBag.getClasses(model -> ComponentBag.EXTERNAL_ONLY.test(model, injectionManager))
                .forEach(injectionManager::register);

register method must check if provider is already a class:

Class<?> providerClass =provider instanceof Class ? (Class) provider : provider.getClass();        
if (isRegistrable(providerClass)) {

@jansupol
Copy link
Contributor

@xvik Yes, we need to move a lot of classes out of internal packages when we define module.info, likely in 2.30

@jansupol
Copy link
Contributor

This is fixed by #4122

@senivam senivam added this to the 2.29 milestone Sep 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants