3

I'm trying to use Jersey with HK2. I need to bind really weird type:

List<TransformationService<? extends Transformation, ? extends TransformationInfor>>

So I have my binder defined as follows:

resourceConfig.register(new AbstractBinder() {
        @Override
        protected void configure() {
            List<TransformationService<? extends Transformation, ? extends TransformationInfo>> transformationServices = ... ;

            bind(transformationServices)
                    .to(new TypeLiteral<List<TransformationService<? extends Transformation, ? extends TransformationInfo>>>() {});

            // This class needs the list for its construction
            bind(TransformationServiceImpl.class).to(TransformationService.class);
        }
    });

When I run the code though I get exception that my list can't be injected (packages ommitted):

[11/20/15 16:46:34] WARNING org.glassfish.jersey.internal.Errors logErrors : The following warnings have been detected: WARNING: Unknown HK2 failure detected:
MultiException stack 1 of 3
org.glassfish.hk2.api.UnsatisfiedDependencyException: There was no object available for injection at SystemInjecteeImpl(requiredType=List<TransformationService<? extends ...Transformation,? extends ...TransformationInfo>>,parent=TransformationServiceImpl,qualifiers={},position=3,optional=false,self=false,unqualified=null,334434299)
    at org.jvnet.hk2.internal.ThreeThirtyResolver.resolve(ThreeThirtyResolver.java:74)
    at org.jvnet.hk2.internal.ClazzCreator.resolve(ClazzCreator.java:214)

Any tips on how to inject such weirdo with HK2?

Jan Zyka
  • 17,460
  • 16
  • 70
  • 118
  • I think this is answered here: http://stackoverflow.com/questions/23992714/how-to-inject-an-unbound-generic-type-in-hk2 – Jan Zyka Nov 21 '15 at 19:56

1 Answers1

4

As far as I understand HK2 injection rules are the same for CDI (see spec)

At some point it mentions:

However, some Java types are not legal bean types :

  • A type variable is not a legal bean type.
  • A parameterized type that contains a wildcard type parameter is not a legal bean type.
  • An array type whose component type is not a legal bean type.

I think that in my example I'm trying to create TypeLiteral of parametrized type that contains wildcard.

Anyway, in my case I removed that unbounded type and it works. The change needed was:

bind(transformationServices)
    .to(new TypeLiteral<List<TransformationService>>() {});
Jan Zyka
  • 17,460
  • 16
  • 70
  • 118
  • It is true that hk2 uses the same rules for type safety that CDI uses. I'd have to look deeper though at why your particular use case didn't work, those rules are kind of hairy – jwells131313 Nov 23 '15 at 13:02