Thanks Russ, injecting a TypeLiteral<T> then calling getType on it is
a much cleaner solution!

On Jun 15, 1:29 am, Andrew Hughes <[email protected]> wrote:
> Thanks guys...
>
> You're right Class<T> wasn't the right way to go here, Type is correct.
>
> Cheers
>
> On Wed, Jun 15, 2011 at 1:32 PM, Russ Milliken <[email protected]>wrote:
>
>
>
>
>
>
>
> > Right, you don't want to use Class<T> as a parameter because it's difficult
> > to get at that "T" at runtime.
>
> > Can you use Guice's TypeLiteral class instead?  This class provides easy
> > runtime access to the generic parameters.
>
> > -Russ
>
> > On Tue, Jun 14, 2011 at 10:58 PM, Alexandre Walter Pretyman <
> > [email protected]> wrote:
>
> >> Hi, I don't think you want to be passing the Class<T> as an argument.
>
> >> This is what I do, not sure if it is the best way, but works for me.
> >> Instead of passing the Class<T> as an argument, I get it from the
> >> Generic parameters:
>
> >>                boolean isEnhanced =
> >> getClass().getName().contains("EnhancerByGuice");
> >>                if (isEnhanced)
> >>                {
> >>                        Class<?> superClass = getClass().getSuperclass();
> >>                        this.clazz = (Class<T>) ((ParameterizedType)
> >> superClass.getGenericSuperclass()).getActualTypeArguments()[0];
> >>                }
> >>                else
> >>                {
> >>                        this.clazz = (Class<T>) ((ParameterizedType)
> >> getClass().getGenericSuperclass()).getActualTypeArguments()[0];
> >>                }
>
> >> The isEnhanced check is due to that when you use Guice's AOP on your
> >> classes, they get extended with the String "EnhancerByGuice" as part
> >> of the new name.
>
> >> Sucks having to check it by hand, would be better to have an
> >> injector.isEnhanced(instance) to check if the class is actually
> >> enhanced, but according to
> >>http://code.google.com/p/google-guice/issues/detail?id=187
> >> ; we won't be getting that anytime soon...
>
> >> Regards,
>
> >> On Jun 7, 3:06 am, ahhughes <[email protected]> wrote:
> >> > Hi All,
>
> >> > This ones a little complex (for me anyway). I have a class that
> >> > requires both a generic argument, and a Class<T> of that generic type
> >> > as a  constructor arg.
>
> >> > Easiest to explain with code:
>
> >> > //Supplier, of 'S'.... and it needs Class<S> to do its job
> >> > public abstract class Shop<S> {
> >> >     public Shop(Class<S> classOfS){ ... }
>
> >> > }
>
> >> > //FerrariShop is a Shop, but it only supplies Ferrari's...
> >> > public class FerrariShop<F extends AbstractFerrari<?>> extends Shop<F>
> >> > {
> >> >     @Inject
> >> >     public FerrariShop(Class<F> classOfF, Blah blah){  //Note blah is
> >> > injected too...
> >> >         super(classOfF);
> >> >     }
>
> >> > }
>
> >> > Then my FastDriver needs to be @Inject'd with a
> >> > 'Provider<FerrariShop<EnzoFerrari>>.....
>
> >> >     @Inject
> >> >      public FastDriver(Provider<FerrariShop<EnzoFerrari>>
> >> > enzoFerrariShopProvider) {}
>
> >> > I'm a little lost here, so THANKS for helping me if you can :)
>
> >> > p.s. if you are wondering why I am doing this, I am building a
> >> > hierarchy of http requests. Gson is de/serializing the request/
> >> > response and it  needs to know Class<F> for its serialization. For
> >> > reliability I want this to be a constructor arg. Also, Gson is
> >> > fantastic (kudos++)!
>
> >> --
> >> You received this message because you are subscribed to the Google Groups
> >> "google-guice" group.
> >> To post to this group, send email to [email protected].
> >> To unsubscribe from this group, send email to
> >> [email protected].
> >> For more options, visit this group at
> >>http://groups.google.com/group/google-guice?hl=en.
>
> >  --
> > You received this message because you are subscribed to the Google Groups
> > "google-guice" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > For more options, visit this group at
> >http://groups.google.com/group/google-guice?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-guice?hl=en.

Reply via email to