On 2 Apr 2014, at 10:54, Jochen Wiedmann <[email protected]> wrote:
> Hi,
> 
> I am trying to binding a listener. My code looks like this:
> 
>         final InjectionListener<?> injectionListener = Generics.cast(new 
> InjectionListener<ILifecycleListener>(){
>             @Override
>             public void afterInjection(ILifecycleListener pInjectee) {
>                 controller.addListener(pInjectee);
>             }
>         });
>         final TypeListener typeListener = new TypeListener(){
>             @Override
>             public <I> void hear(TypeLiteral<I> pType, TypeEncounter<I> 
> pEncounter) {
>                 @SuppressWarnings("unchecked")
>                 final InjectionListener<I> listener = (InjectionListener<I>) 
> injectionListener;
>                 pEncounter.register(listener);
>             }
>         };
>         final Matcher<TypeLiteral<?>> matcher = 
> Generics.cast(Matchers.subclassesOf(ILifecycleListener.class));
>         pBinder.bindListener(matcher, typeListener);
> 
> However, I am getting the exception below. The reason seems to be with 
> Matchers.subclassesOf(), because my example works, if I use Matchers.any().
> 
> Any ideas, what might be wrong?

Matchers.subclassesOf matches instances of Class but you're using it to match 
instances of TypeLiteral, and Class != TypeLiteral

You'll need to provide your own Matcher implementation that works with 
TypeLiteral like the following example from a comment on the CustomInjections 
page of the wiki:

        https://code.google.com/p/google-guice/wiki/CustomInjections

        http://pastie.org/1064618

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import com.google.inject.TypeLiteral;
import com.google.inject.matcher.AbstractMatcher;
import com.google.inject.matcher.Matcher;

/**
 * A factory of {@link Matcher}s that can be used in 
 * {@link AbstractModule}.bindListener(matcher,listener) 
 * @author ooktay
 */
public class TypeMatchers {
   private static class SubClassesOf extends AbstractMatcher<TypeLiteral<?>> {
      private final Class<?> baseClass;

      private SubClassesOf(Class<?> baseClass) {
         this.baseClass = baseClass;
      }

      @Override
      public boolean matches(TypeLiteral<?> t) {
         return baseClass.isAssignableFrom( t.getRawType() );
      }
   }

   /**
    * Matcher matches all classes that extends, implements or is the same as 
baseClass
    * @param baseClass
    * @return Matcher
    */
   public static Matcher<TypeLiteral<?>> subclassesOf(Class<?> baseClass) {
      return new SubClassesOf(baseClass);
   }
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

> Thanks,
> 
> Jochen
> 
> 
> com.google.inject.internal.util.$ComputationException: 
> com.google.inject.internal.util.$ComputationException: 
> java.lang.ClassCastException: com.google.inject.TypeLiteral cannot be cast to 
> java.lang.Class
>     at 
> com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:553)
>     at 
> com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:419)
>     at 
> com.google.inject.internal.util.$CustomConcurrentHashMap$ComputingImpl.get(CustomConcurrentHashMap.java:2041)
>     at com.google.inject.internal.FailableCache.get(FailableCache.java:50)
>     at 
> com.google.inject.internal.ConstructorInjectorStore.get(ConstructorInjectorStore.java:49)
>     at 
> com.google.inject.internal.ConstructorBindingImpl.initialize(ConstructorBindingImpl.java:125)
>     at 
> com.google.inject.internal.InjectorImpl.initializeJitBinding(InjectorImpl.java:521)
>     at 
> com.google.inject.internal.InjectorImpl.createJustInTimeBinding(InjectorImpl.java:847)
>     at 
> com.google.inject.internal.InjectorImpl.createJustInTimeBindingRecursive(InjectorImpl.java:772)
>     at 
> com.google.inject.internal.InjectorImpl.getJustInTimeBinding(InjectorImpl.java:256)
>     at 
> com.google.inject.internal.InjectorImpl.getBindingOrThrow(InjectorImpl.java:205)
>     at 
> com.google.inject.internal.InjectorImpl.getInternalFactory(InjectorImpl.java:853)
>     at com.google.inject.internal.FactoryProxy.notify(FactoryProxy.java:46)
>     at 
> com.google.inject.internal.ProcessedBindingData.runCreationListeners(ProcessedBindingData.java:50)
>     at 
> com.google.inject.internal.InternalInjectorCreator.initializeStatically(InternalInjectorCreator.java:133)
>     at 
> com.google.inject.internal.InternalInjectorCreator.build(InternalInjectorCreator.java:106)
>     at com.google.inject.Guice.createInjector(Guice.java:95)
>     at com.google.inject.Guice.createInjector(Guice.java:72)
>     at com.google.inject.Guice.createInjector(Guice.java:62)
>     at 
> com.github.jochen.afw.core.guice.GuiceComponentFactoryBuilder.build(GuiceComponentFactoryBuilder.java:144)
>     at 
> com.github.jochen.afw.core.guice.GuiceComponentFactoryBuilderTest.testSuccessfullConfiguration(GuiceComponentFactoryBuilderTest.java:20)
>     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>     at 
> sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
>     at 
> sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
>     at java.lang.reflect.Method.invoke(Method.java:606)
>     at 
> org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
>     at 
> org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
>     at 
> org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
>     at 
> org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
>     at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
>     at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
>     at 
> org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
>     at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
>     at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
>     at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
>     at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
>     at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
>     at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
>     at 
> org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
>     at 
> org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
>     at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
>     at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
>     at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
>     at 
> org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
> Caused by: com.google.inject.internal.util.$ComputationException: 
> java.lang.ClassCastException: com.google.inject.TypeLiteral cannot be cast to 
> java.lang.Class
>     at 
> com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:553)
>     at 
> com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:419)
>     at 
> com.google.inject.internal.util.$CustomConcurrentHashMap$ComputingImpl.get(CustomConcurrentHashMap.java:2041)
>     at com.google.inject.internal.FailableCache.get(FailableCache.java:50)
>     at 
> com.google.inject.internal.MembersInjectorStore.get(MembersInjectorStore.java:65)
>     at 
> com.google.inject.internal.ConstructorInjectorStore.createConstructor(ConstructorInjectorStore.java:73)
>     at 
> com.google.inject.internal.ConstructorInjectorStore.access$000(ConstructorInjectorStore.java:28)
>     at 
> com.google.inject.internal.ConstructorInjectorStore$1.create(ConstructorInjectorStore.java:36)
>     at 
> com.google.inject.internal.ConstructorInjectorStore$1.create(ConstructorInjectorStore.java:32)
>     at com.google.inject.internal.FailableCache$1.apply(FailableCache.java:39)
>     at 
> com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:549)
>     ... 43 more
> Caused by: java.lang.ClassCastException: com.google.inject.TypeLiteral cannot 
> be cast to java.lang.Class
>     at 
> com.google.inject.matcher.Matchers$SubclassesOf.matches(Matchers.java:187)
>     at 
> com.google.inject.internal.MembersInjectorStore.createWithListeners(MembersInjectorStore.java:100)
>     at 
> com.google.inject.internal.MembersInjectorStore.access$000(MembersInjectorStore.java:34)
>     at 
> com.google.inject.internal.MembersInjectorStore$1.create(MembersInjectorStore.java:42)
>     at 
> com.google.inject.internal.MembersInjectorStore$1.create(MembersInjectorStore.java:39)
>     at com.google.inject.internal.FailableCache$1.apply(FailableCache.java:39)
>     at 
> com.google.inject.internal.util.$MapMaker$StrategyImpl.compute(MapMaker.java:549)
>     ... 53 more
> 
> 
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "google-guice" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> To post to this group, send email to [email protected].
> Visit this group at http://groups.google.com/group/google-guice.
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"google-guice" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/google-guice.
For more options, visit https://groups.google.com/d/optout.

Reply via email to