Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6888365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:00:21+00:00 2026-05-27T06:00:21+00:00

If I have the following class: public class ObjectDAOMongoDBImpl<T> extends GenericDAOMongoDBImpl<T, ObjectId> implements ObjectDAO<T>

  • 0

If I have the following class:

public class ObjectDAOMongoDBImpl<T> extends GenericDAOMongoDBImpl<T, ObjectId> implements ObjectDAO<T> {
    public ObjectDAOMongoDBImpl(Class<T> entityClass, Mongo mongo, Morphia morphia, String dbName) {
        super(entityClass, mongo, morphia, dbName);
    }
}

Where, entityClass is provided at run-time – how can I use guice to bind the said type to an interface?

public class RunnerModule extends AbstractModule {      
    @Override
    protected void configure() {
        bind(GenericDAO.class).to(ObjectDAOMongoDBImpl.class);
    }
}

public class Runner<T, V> {
    GenericDAO<T, V> dao;

    @Inject
    public Runner(GenericDAO<T, V> dao) {
        this.dao = dao;
    }

    public static void main(String[] args) {
        Injector injector = Guice.createInjector(new RunnerModule());
        injector.getInstance(Runner.class);
    }
}

It’s fine to define mongo, morphia, and dbName as literals to RunnerModule (is there a cleaner way?), but I have no way of knowing what entityClass is until runtime.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:00:22+00:00Added an answer on May 27, 2026 at 6:00 am

    This isn’t doable with Guice idiomatically, and it isn’t its primary focus either.

    jfpoilpret have said everything that can be said, but I would like to approach the problem from another direction, where you have the option to (possibly) solve your problem by losing type-safety.

    So, in your code, you ask Guice to get an instance of your Runner<T, V> class like this

    injector.getInstance(Runner.class);
    

    but this can’t be resolved by Guice, because Runner<T, V> has a dependency on GenericDAO<T, V>, but you didn’t bind an exact implementation for it. So as jfpoilpret has said, you have to bind some concrete implementations for it in your module.

    I’m guessing that you want to determine the exact GenericDAO<T, V> implementation that you pass to your Runner<T, V> based on some input data, which data’s type isn’t known at compile time. Now, let’s assume you have two implementations.

    bind(new TypeLiteral<GenericDAO<String, ObjectID>>(){}).to(StringDAO.class);
    bind(new TypeLiteral<GenericDAO<Double, ObjectID>>(){}).to(IntegerDAO.class);
    

    Based on different type of inputs you can do this

    Injector injector = Guice.createInjector(new RunnerModule());
    
    // possible input which you get from *somewhere* dynamically
    Object object = 1.0;
    
    TypeLiteral<?> matchedTypeLiteral = null;
    for (Key<?> key : injector.getAllBindings().keySet()) {
      TypeLiteral<?> typeLiteral = key.getTypeLiteral();
      Type type = typeLiteral.getType();
      if (type instanceof ParameterizedType) {
        ParameterizedType parameterizedType = (ParameterizedType) type;
          if (parameterizedType.getRawType() == GenericDAO.class) {
            List<Type> actualTypeArguments =    Arrays.asList(parameterizedType.getActualTypeArguments());
            if (actualTypeArguments.get(0) == object.getClass())
              matchedTypeLiteral = typeLiteral;
        }
      }
    };
    
    Runner<?, ?> runner = new Runner<>((GenericDAO<?, ?>) injector.getInstance(Key.get(matchedTypeLiteral)));
    System.out.println(runner.dao.getClass()); // IntegerDAO.class
    

    If Object object = "string";, then the other implementation will be found. This is of course rather ugly and can be improved with checking for sub-classes and stuff, but I think you get the idea. The bottom-line is that you can’t get around this.

    If you manage to do it (getting around it), please drop me an e-mail because I would like to know about it! I had faced the same problem as you’re facing not too long ago. I’ve written a simple BSON codec where I wanted to load specific implementations of a generic interface based on the type of some arbitrary input. This worked well with Java-to-BSON mappings, but I couldn’t do it the other way around in any sensible way, so I’ve opted for a simpler solution.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following class: public abstract class AbstractParent { static String method() {
I have the following class public class UIControl { public string FName{ get; set;
I have the following class public class CountrySpecificPIIEntity { public string Country { get;
I have the following class public class CountrySpecificPIIEntity { public string Country { get;
I have the following class: public class Category { public string Name { get;
I have the following class: public class DocketType : Enumeration<DocketType, int, string> { public
Lets say i have following class public class abc { int id; string name;
I have following class in Java code: public class CHRTreeCreator extends IndexCreator { ...
I have the following class: public class DocumentCompare { public string Customer; public string
I have this following class: public class TV { public string GroupId { get;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.