users@jersey.java.net

[Jersey] Jersey attempting to load abstract class as Provider from obfuscated library

From: Richard Sand <rsand_at_idfoundry.com>
Date: Sun, 5 May 2013 14:19:17 -0400

Hi all - I hate to ask a question about a problem encountered under
obfuscation, but I'm just trying to understand what Jersey is looking
for/expecting in this circumstance. I am using Jersey 1.17, and have the
all-to-common scenario where a library is breaking when obfuscated. But
it's the actual nature of the break that is strange to me and the reason
for this message to the list. Jersey seems to be fooled into thinking that
an unannotated, abstract class is a Provider. I am using Jersey 1.17.

I have a class called GsonMessageBodyHandler, which is an abstract class
that implements MessageBodyReader<T>, and it has no annotation (the
purpose of the class is to use Gson rather than Jackson for certain
classes, which is working great). This class is then extended by a class
called JsonMessageBodyHandler. JsonMessageBodyHandler is annotated with
"@Producer" and "@Consumes(MediaType.APPLICATION_JSON)".

When I deploy my project unobfuscated, it works fine, meaning that Jersey
recognizes JsonMessageBodyHandler as a Provider for MessageBodyReader and
all is well . However when I obfuscate, even though I tell ProGuard not to
obfuscate GsonMessageBodyHandler or JsonMessageBodyHandler, Jersey for
some reason insists on attempting to instantiate GsonMessageBodyHandler as
a Provider, and then is failing because it is abstract. I cannot figure
what is causing Jersey to attempt to load this abstract class but only if
obfuscated. Can anyone help?

The relevant sections of the startup logs are given below:

2013-05-05 01:08:29.363 [localhost-startStop-1] ERROR
c.s.j.c.s.component.ProviderFactory - The provider class, class
com.rest.common.GsonMessageBodyHandler, could not be instantiated.
Processing will continue but the class will not be utilized
java.lang.InstantiationException: null
        at
sun.reflect.InstantiationExceptionConstructorAccessorImpl.newInstance(Inst
antiationExceptionConstructorAccessorImpl.java:30) ~[na:1.6.0_35]
        at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
~[na:1.6.0_35]
        at java.lang.Class.newInstance0(Class.java:355) ~[na:1.6.0_35]
        at java.lang.Class.newInstance(Class.java:308) ~[na:1.6.0_35]
        at
com.sun.jersey.core.spi.component.ComponentConstructor._getInstance(Compon
entConstructor.java:192) ~[jersey-core-1.17.jar:1.17]
        at
com.sun.jersey.core.spi.component.ComponentConstructor.getInstance(Compone
ntConstructor.java:179) ~[jersey-core-1.17.jar:1.17]
        at
com.sun.jersey.core.spi.component.ProviderFactory.__getComponentProvider(P
roviderFactory.java:166) [jersey-core-1.17.jar:1.17]
        at
com.sun.jersey.core.spi.component.ProviderFactory._getComponentProvider(Pr
oviderFactory.java:159) [jersey-core-1.17.jar:1.17]
        at
com.sun.jersey.core.spi.component.ProviderFactory.getComponentProvider(Pro
viderFactory.java:153) [jersey-core-1.17.jar:1.17]
        at
com.sun.jersey.core.spi.component.ProviderServices.getComponent(ProviderSe
rvices.java:251) [jersey-core-1.17.jar:1.17]
        at
com.sun.jersey.core.spi.component.ProviderServices.getProviders(ProviderSe
rvices.java:148) [jersey-core-1.17.jar:1.17]
        at
com.sun.jersey.core.spi.factory.MessageBodyFactory.initWriters(MessageBody
Factory.java:206) [jersey-core-1.17.jar:1.17]
        at
com.sun.jersey.core.spi.factory.MessageBodyFactory.init(MessageBodyFactory
.java:163) [jersey-core-1.17.jar:1.17]
        at
com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApp
licationImpl.java:1310) [jersey-server-1.17.jar:1.17]
        at
com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebAp
plicationImpl.java:168) [jersey-server-1.17.jar:1.17]
        at
com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicat
ionImpl.java:774) [jersey-server-1.17.jar:1.17]
        at
com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicat
ionImpl.java:770) [jersey-server-1.17.jar:1.17]
        at
com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193)
[jersey-core-1.17.jar:1.17]
        at
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebAppl
icationImpl.java:770) [jersey-server-1.17.jar:1.17]
        at
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebAppl
icationImpl.java:765) [jersey-server-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletCont
ainer.java:489) [jersey-servlet-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent
.initiate(ServletContainer.java:319) [jersey-servlet-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:6
05) [jersey-servlet-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:2
10) [jersey-servlet-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContaine
r.java:374) [jersey-servlet-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContaine
r.java:557) [jersey-servlet-1.17.jar:1.17]
        at javax.servlet.GenericServlet.init(GenericServlet.java:160)
[servlet-api.jar:3.0.FR]
....
SEVERE: The class com.rest.common.GsonMessageBodyHandler is an abstract
class and cannot be instantiated.
2013-05-05 01:08:29.675 [localhost-startStop-1] ERROR
o.a.c.c.C.[.[localhost].[/rest] - StandardWrapper.Throwable
com.sun.jersey.spi.inject.Errors$ErrorMessagesException: null
        at
com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
~[jersey-core-1.17.jar:1.17]
        at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
~[jersey-core-1.17.jar:1.17]
        at
com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
~[jersey-core-1.17.jar:1.17]
        at
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebAppl
icationImpl.java:770) ~[jersey-server-1.17.jar:1.17]
        at
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebAppl
icationImpl.java:765) ~[jersey-server-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletCont
ainer.java:489) ~[jersey-servlet-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent
.initiate(ServletContainer.java:319) ~[jersey-servlet-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:6
05) ~[jersey-servlet-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:2
10) ~[jersey-servlet-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContaine
r.java:374) ~[jersey-servlet-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContaine
r.java:557) ~[jersey-servlet-1.17.jar:1.17]
        at javax.servlet.GenericServlet.init(GenericServlet.java:160)
~[servlet-api.jar:3.0.FR]
....
2013-05-05 01:08:29.691 [localhost-startStop-1] ERROR
o.a.c.c.C.[.[localhost].[/rest] - Servlet /rest threw load() exception
com.sun.jersey.spi.inject.Errors$ErrorMessagesException: null
        at
com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
~[jersey-core-1.17.jar:1.17]
        at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
~[jersey-core-1.17.jar:1.17]
        at
com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
~[jersey-core-1.17.jar:1.17]
        at
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebAppl
icationImpl.java:770) ~[jersey-server-1.17.jar:1.17]
        at
com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebAppl
icationImpl.java:765) ~[jersey-server-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletCont
ainer.java:489) ~[jersey-servlet-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent
.initiate(ServletContainer.java:319) ~[jersey-servlet-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:6
05) ~[jersey-servlet-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:2
10) ~[jersey-servlet-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContaine
r.java:374) ~[jersey-servlet-1.17.jar:1.17]
        at
com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContaine
r.java:557) ~[jersey-servlet-1.17.jar:1.17]
        at javax.servlet.GenericServlet.init(GenericServlet.java:160)
~[servlet-api.jar:3.0.FR]
...

Thanks all!

Richard Sand | Managing Director
PO Box 91824 | Austin | Texas 78709-1824 | USA
Office: +1 888 612 8820 ext 02 | Fax: +1 866 304 3754
Mobile: +1 267 984 3651