Marcin Misiewicz wrote:
> Hi
>
> I'm implementing custom injection thanks to
> http://code.google.com/docreader/#p=google-guice&s=google-guice&t=CustomInjections.
> I'd like to inject only into the classes which are subclasses of some
> class.
>
> AbstractModule bindListener method accepts following Matcher :
>
> Matcher<? super TypeLiteral<?>>
>
> and of course Matchers.subclassesOf(MySuperClass.class) doesn't work.
>
> How can I implement matcher with TypeLiteral ?
>
TypeLiteral.get(MySuperClass.class) converts MySuperClass.class in its
matching TypeLiteral<MySuperClass>
But I'm not sure this will work correctly with Matchers.subclassesOf().
I remember that for Guts project, I had to write the following utility
class:
public final class Matchers
{
static final public Matcher<TypeLiteral<?>> isSubtypeOf(final
Class<?> supertype)
{
return isSubtypeOf(TypeLiteral.get(supertype));
}
static final public Matcher<TypeLiteral<?>> isSubtypeOf(final
TypeLiteral<?> supertype)
{
return new AbstractMatcher<TypeLiteral<?>>()
{
@Override public boolean matches(TypeLiteral<?> type)
{
return typeIsSubtypeOf(type, supertype);
}
};
}
static public boolean typeIsSubtypeOf(
TypeLiteral<?> subtype, TypeLiteral<?> supertype)
{
// First check that raw types are compatible
// Then check that generic types are compatible! HOW????
return ( subtype.equals(supertype)
|| (
supertype.getRawType().isAssignableFrom(subtype.getRawType())
&&
supertype.equals(subtype.getSupertype(supertype.getRawType()))));
}
}
Hope this helps
Jean-Francois
> Using Matchers.any() from the example works fine but I do not want to
> hear on every class.
> I'm newbie in guice so probably the answer is simple, but right now I
> can't figure out how to do this.
>
> Thanks in advance
> Marcin
> >
>
>
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---