Implementing matcher for bindListener

968 views
Skip to first unread message

Marcin Misiewicz

unread,
Nov 4, 2009, 6:31:59 PM11/4/09
to google-guice
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 ?
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

Jean-Francois Poilpret

unread,
Nov 4, 2009, 7:00:30 PM11/4/09
to google...@googlegroups.com
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

Marcin Misiewicz

unread,
Nov 5, 2009, 9:26:27 AM11/5/09
to google-guice
Thank you Jean for your advice this is what is working for me :

/**
* Our custom guice Matcher classes
* @author Marcin Misiewicz
*
*/
public final class Matchers {

public static Matcher<? super TypeLiteral<?>> subtypeOf(
final Class<?> superclass) {
return new SubtypeOf(TypeLiteral.get(superclass));
}

public static Matcher<? super TypeLiteral<?>> subtypeOf(
final TypeLiteral<?> supertype) {
return new SubtypeOf(supertype);
}

private static class SubtypeOf extends AbstractMatcher<TypeLiteral<?
>>
implements Serializable {

private static final long serialVersionUID = 1239939466206498961L;

private final TypeLiteral<?> supertype;

/**
* @param superType
*/
public SubtypeOf(TypeLiteral<?> superType) {
super();
this.supertype = checkNotNull(superType, "supertype");
}

/*
* (non-Javadoc)
*
* @see com.google.inject.matcher.Matcher#matches(java.lang.Object)
*/
@Override
public boolean matches(TypeLiteral<?> subtype) {
return (subtype.equals(supertype) ||
supertype.getRawType().isAssignableFrom(subtype.getRawType()));
}

@Override
public boolean equals(Object other) {
return other instanceof SubtypeOf
&& ((SubtypeOf) other).supertype.equals(supertype);
}

@Override
public int hashCode() {
return 37 * supertype.hashCode();
}

@Override
public String toString() {
return "subtypeOf(" + supertype.getRawType() + ".class)";
}
}
}

You have saved me a lot of time and helped to better understand
TypeLiterals ;)

Marcin Misiewicz

On 5 Lis, 01:00, Jean-Francois Poilpret <jfpoilp...@yahoo.fr> wrote:
> Marcin Misiewicz wrote:
> > Hi
>
> > I'm implementing custom injection thanks to
> >http://code.google.com/docreader/#p=google-guice&s=google-guice&t=Cus....
Reply all
Reply to author
Forward
0 new messages