Bind an abstract class with its implementation using Guice.

3,654 views
Skip to first unread message

Rama Krishna Gollapudi

unread,
Feb 4, 2014, 12:38:54 PM2/4/14
to google...@googlegroups.com
Hello,
          I'm having  a problem of binding an abstract class with it's implementation using Guice . I'm getting the following exception.

com.rama.util.jaxrs.DomainResource is abstract, not a concrete class.  Unable to create AssistedInject factory.

And  i did binding like this.  bind(DomainResource.class).to(TodoResource.class);

DomainResoruce is an abstract class and TodoResource is an implementation. 

Thanks,
Rama.

Stuart McCulloch

unread,
Feb 4, 2014, 1:09:31 PM2/4/14
to google...@googlegroups.com
On 4 Feb 2014, at 17:38, Rama Krishna Gollapudi <kitt...@gmail.com> wrote:

Hello,
          I'm having  a problem of binding an abstract class with it's implementation using Guice . I'm getting the following exception.

com.rama.util.jaxrs.DomainResource is abstract, not a concrete class.  Unable to create AssistedInject factory.

^ this error message is coming from the AssistedInject extension, could you post your complete module or at least the part where you configure your factory?

And  i did binding like this.  bind(DomainResource.class).to(TodoResource.class);

DomainResoruce is an abstract class and TodoResource is an implementation. 

Thanks,
Rama.

--
You received this message because you are subscribed to the Google Groups "google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-guice...@googlegroups.com.
To post to this group, send email to google...@googlegroups.com.
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/groups/opt_out.

Sam Berlin

unread,
Feb 4, 2014, 1:09:47 PM2/4/14
to google...@googlegroups.com
The assisted inject docs describe how to do this.  The example in the doc reads:

 install(new FactoryModuleBuilder()
     .implement(Payment.class, RealPayment.class)
     .build(PaymentFactory.class));

You'd just substitute Payment for DomainResource, RealPayment for TodoResource, and PaymentFactory for your assistedinject factory.

 sam


--

Rama Krishna Gollapudi

unread,
Feb 4, 2014, 1:39:29 PM2/4/14
to google...@googlegroups.com
Here is my abstract class

public abstract class DomainResource {

    @Inject
    @Assisted
    private Domain target;

    @Inject
    private LinkHelper helper;

    /**
     * Accessor for the domain resource, will return domain information and links to domain scoped resources
     * @param info the uri information
     * @return a Map representing the returned json
     */
    @GET
    @Produces(APPLICATION_JSON)
    @SuppressWarnings("nls")
    public Map<String, Object> get(@Context final UriInfo info) {
        
        return "Implement your stuff";
    }
}


Implementaion

public class TodoResource extends DomainResource {
    @Inject
    private Domain domain;
    @Inject
    private ScopedResourceFactory factory;
    @Inject
    private LinkHelper helper;

    @GET
    @Produces(APPLICATION_JSON)
    public Map<String, Object> getTypes(@Context final UriInfo uriInfo) {
        return helper.indexMapResponse();
    }

    /**
     * @param info uriInfo object used to extract matrix params
     * @return the encounters resource
     */
    @Path("todos")
    public Object getEncounters(@Context final UriInfo info) {
        return " implement Your Stuff.";
    }
}


Factory :
public interface ScopedResourceFactory {

    DomainResource domainResource(Domain target);
}



And as per your suggestion i'm binding like this.  
install(new FactoryModuleBuilder().implement(DomainResource.class, TodoResource.class).build(
                    ScopedResourceFactory.class));

But still i'm getting exception. Please find below. 



com.rama.util.jaxrs.DomainResource is abstract, not a concrete class.  Unable to create AssistedInject factory.
  while locating com.cerner.iaware.util.jaxrs.DomainResource
  at com.rama.util.guice.ScopedResourceFactory.domainResource(ScopedResourceFactory.java:1)

1 error
at com.google.inject.internal.Errors.throwCreationExceptionIfErrorsExist(Errors.java:448)
at com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:155)
at com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:107)
at com.google.inject.Guice.createInjector(Guice.java:96)
at com.google.inject.Guice.createInjector(Guice.java:73)
at com.google.inject.Guice.createInjector(Guice.java:62)
at com.rama.todo.guice.GuiceConfigurationInitializer.getInjector(GuiceConfigurationInitializer.java:49)
at com.google.inject.servlet.GuiceServletContextListener.contextInitialized(GuiceServletContextListener.java:47)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:303)
at java.util.concurrent.FutureTask.run(FutureTask.java:138)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)




Rama Krishna Gollapudi

unread,
Feb 5, 2014, 3:36:17 PM2/5/14
to google...@googlegroups.com
Hello is there any solution to my problem ?

Sam Berlin

unread,
Feb 5, 2014, 3:44:05 PM2/5/14
to google...@googlegroups.com
Can you isolate the problem into a repeatable self-contained test-case?

 sam

Stephan Classen

unread,
Feb 5, 2014, 3:52:22 PM2/5/14
to google...@googlegroups.com
According to the documentation:

constructedType is a concrete class with an @Inject-annotated constructor. In addition to injector-supplied parameters, the constructor should have parameters that match each of the factory method's parameters. Each factory-supplied parameter requires an @Assisted annotation.


You have your @Asissted annotation on a field.
Try and add a parameter with type Domain to the constructor of TodoResource which is annotated with @Assisted and pass it to the super constructor of DomainResource

Hope this works

Rama Krishna Gollapudi

unread,
Feb 5, 2014, 3:53:47 PM2/5/14
to google...@googlegroups.com
I'm not sure. But here is my problem precisely. I'm using an abstract class inside the factory and bindind that implementation. 
Example:

public interface ResourceFactory {

    Resource domainResource(Domain target);
}

public abstract class Resource{}

public MyResource extends Resource{}.

Where Resource is an abstract. 

and  bind them with 
install(new FactoryModuleBuilder().implement(Resource.class, MyResource.class).build(
                   ResourceFactory.class));

Is it possible to bind like this?.

Sam Berlin

unread,
Feb 5, 2014, 4:02:42 PM2/5/14
to google...@googlegroups.com
Yes, this is one of the use-cases that assistedinject supports, and there's a lot of tests to make sure it's working as expected.  Since you're experiencing a problem with it, the best way to find out what's causing that problem is to isolate it into a reproducible test-case.

 sam

Rama Krishna Gollapudi

unread,
Feb 5, 2014, 4:10:34 PM2/5/14
to google...@googlegroups.com
This is not working. I'm getting the same exception(DomainResource is abstract, not a concrete class.  Unable to create AssistedInject factory).

Stuart McCulloch

unread,
Feb 5, 2014, 5:30:14 PM2/5/14
to google...@googlegroups.com
On 5 Feb 2014, at 21:10, Rama Krishna Gollapudi <kitt...@gmail.com> wrote:

This is not working. I'm getting the same exception(DomainResource is abstract, not a concrete class.  Unable to create AssistedInject factory).

Unfortunately I don’t see that exception using the snippets of code you pasted below, the factory gets built as expected - can you provide an example project that recreates the exception? (ideally something that will build locally)

Rama Krishna Gollapudi

unread,
Feb 6, 2014, 12:17:36 PM2/6/14
to google...@googlegroups.com
Thank you Guys. After isolating into test-case i  came to know that factory is already built with different implementation. It's always good to learn more. Thanks.
Reply all
Reply to author
Forward
0 new messages