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 7646687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:16:19+00:00 2026-05-31T10:16:19+00:00

The following generic Guice binding method behaves correctly: <T> Key<?> bindMultibinder( ArrayList<Class<? extends T>>

  • 0

The following generic Guice binding method behaves correctly:

<T> Key<?> bindMultibinder(
    ArrayList<Class<? extends T>> contents, Class<T> superClass) {
   Named annotation = randomAnnotation();
   Multibinder<T> options = 
    Multibinder.newSetBinder(binder(), superClass, annotation);
   for (Class<? extends T> t : contents) {
      options.addBinding().to(t);
   }
   final Key<?> multibinderKey = Key.get(Types.setOf( superClass ), annotation);
   return multibinderKey;
}

And uses client code like this:

ArrayList<Class<? extends Option>> options = 
 new ArrayList<Class<? extends Option>>();
options.add(CruiseControl.class);
bindMultibinder(options, Option.class);

However, if I want to allow Option take a generic parameter like Option<Radio>, then I assume I need to pass a TypeLiteral in the bindMultibinder superClass parameter. This is my best attempt so far:

<T> Key<?> bindMultibinder(
 ArrayList<TypeLiteral<? extends T>> contents, TypeLiteral<T> superClass) {
   Named annotation = randomAnnotation();
   Multibinder<T> options = 
    Multibinder.newSetBinder(binder(), superClass, annotation);
   for (TypeLiteral<? extends T> t : contents) {
      options.addBinding().to(t);
   }
   final Key<?> multibinderKey = Key.get(Types.setOf(superClass.getRawType()), annotation);
   return multibinderKey;
}

The binding code equivalent to the prior case looks like this:

ArrayList<TypeLiteral<? extends Option>> options = 
 new ArrayList<TypeLiteral<? extends Option>>();
options.add(new TypeLiteral<CruiseControl>(){});
bindMultibinder(options, new TypeLiteral<Option>(){});

I’m almost certain that the below binding is incorrect, because Types.setOf(superClass.getRawType()) returns a ParameterizedType

final Key<?> multibinderKey = 
 Key.get(Types.setOf(superClass.getRawType()), annotation);

Any ideas how to create the set correctly?

  • 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-31T10:16:21+00:00Added an answer on May 31, 2026 at 10:16 am

    ParameterizedType is the java class that is used to represent types that in java source code you need to write with angle brackets: types like Foo<Bar> or Set<Option> or Set<Option<Radio>> or even Set<? extends Option<Radio>>. That is the return value you want.

    What you’ve done will in fact work correctly with the very minor change that you want to call superClass.getType() in your next-to-last line instead of superClass.getRawType(). That being said, while I’m here I do have a few other suggestions.

    First off, in your first method I’d change it to:

    <T> Key<Set<T>> bindMultibinder(
        Iterable<? extends Class<? extends T>> contents, Class<T> superClass) {
       Named annotation = randomAnnotation();
       Multibinder<T> options = 
         Multibinder.newSetBinder(binder(), superClass, annotation);
       for (Class<? extends T> t : contents) {
          options.addBinding().to(t);
       }
       @SuppressWarnings("unchecked")
       final Key<Set<T>> multibinderKey = (Key<Set<T>>) Key.get(Types.setOf( superClass ), annotation);
       return multibinderKey;
    }
    

    That will let you do calls like this:

    bindMultibinder(ImmutableList.of(CruiseControlSubOptOne.class,
                                     CruiseControlSubOptTwo.class),
                    Option.class);
    

    Or, if you aren’t using guava – though you should be – you can use Arrays.asList instead of ImmutableList.of. You get the same amount of type safety as before without needing all those angle bracket declarations in your binding code.

    If you don’t have many callers of bindMultibinder yet, I’d also swap the order of the arguments, but that might just be a personal style thing.

    With these same changes, your second method becomes:

    <T> Key<Set<T>> bindMultibinder(
     Iterable<? extends TypeLiteral<? extends T>> contents, TypeLiteral<T> superClass) {
       Named annotation = randomAnnotation();
       Multibinder<T> options = 
        Multibinder.newSetBinder(binder(), superClass, annotation);
       for (TypeLiteral<? extends T> t : contents) {
          options.addBinding().to(t);
       }
       @SuppressWarnings("unchecked")
       final Key<Set<T>> multibinderKey = (Key<Set<T>>) Key.get(Types.setOf(superClass.getType()), annotation);
       return multibinderKey;
    }
    

    And you can use it similarly:

    bindMultibinder(ImmutableList.of(
                        new TypeLiteral<CruiseControlSubOptOne>() {},
                        new TypeLiteral<CruiseControlSubOptTwo>() {}),
                    new TypeLiteral<Option>() {});
    

    Though thinking about it now, I wonder if you really want an overload of bindMultibinder that takes a TypeLiteral. Wouldn’t you rather have one that takes a Key instead?

    <T> Key<Set<T>> bindMultibinder(Iterable<? extends Key<? extends T>> contents, Key<T> superClass) {
      Named annotation = randomAnnotation();
      Multibinder<T> options =
          Multibinder.newSetBinder(binder(), superClass.getTypeLiteral(), annotation);
      for (Key<? extends T> t : contents) {
        options.addBinding().to(t);
      }
      @SuppressWarnings("unchecked")
      final Key<Set<T>> multibinderKey =
          (Key<Set<T>>) Key.get(Types.setOf(superClass.getTypeLiteral().getType()), annotation);
      return multibinderKey;
    }
    

    After all, you can call this method in almost the same fashion:

    bindMultibinder(ImmutableList.of(
                        new Key<CruiseControlSubOptOne>() {},
                        new Key<CruiseControlSubOptTwo>() {}),
                    new Key<Option>() {});
    

    Except that Key is easier to type than TypeLiteral, and if you need to put in something that’s identified only by its annotation, that’s trivial to do:

    bindMultibinder(ImmutableList.of(
                        new Key<CruiseControlSubOptOne>() {},
                        new Key<CruiseControlSubOptTwo>() {},
                        Key.get(CruiseControl.class, Names.named("ThirdOpt")),
                        Key.get(CruiseControl.class, Names.named("FourthOpt"))),
                    new Key<Option>() {});
    

    Now, is that @Suppress making you nervous? Good instincts.

    Unfortunately, the sad fact is that when dealing with reflection around generified types – types with angle brackets in them – you’re almost certainly going to have small unchecked bits. My suggestion is that you make the bit that needs to have untyped warnings suppressed as small as possible, and expose to the outside world as much type information as you can. If you return a Key<?> from here, you’re likely going to make the caller of this method suppress untyped warnings when they try to use your return value. Better to do it here, where you can limit the warnings suppression to a single line, and one where you can prove that the cast is safe.

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

Sidebar

Related Questions

I have an interface A , which class B implements. The following generic method
Say I have the following generic class: public class Foo<T extends Bar> { //
I've got the following Generic usercontrol declared: public partial class MessageBase<T> : UserControl {
I have the following method that is supposed to be a generic Save to
I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
I'm having some trouble with a generic method I'm writing. It has the following
I have the following generic code: public V put(K key, V value){ Object o
How come java doesn't allow the following generic return type: public <T extends Enum<T>
the following generic method gives the compile error Error 13 Value of type 'Distribution'
Having the following generic class that would contain either string, int, float, long as

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.