Last active
December 21, 2015 11:29
-
-
Save electrotype/6299241 to your computer and use it in GitHub Desktop.
Guice 4.0-beta and bindListener WORKING- https://groups.google.com/forum/?fromgroups#!topic/google-guice/OeEEg2ELQ0M
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import static org.junit.Assert.assertNotNull; | |
import static org.junit.Assert.assertTrue; | |
import org.junit.Test; | |
import com.google.inject.AbstractModule; | |
import com.google.inject.Binding; | |
import com.google.inject.Guice; | |
import com.google.inject.Injector; | |
import com.google.inject.assistedinject.FactoryModuleBuilder; | |
import com.google.inject.matcher.AbstractMatcher; | |
import com.google.inject.spi.ConstructorBinding; | |
import com.google.inject.spi.ProvisionListener; | |
public class TestBindListener | |
{ | |
public static interface IInitableAfterCreation | |
{ | |
void init(); | |
} | |
public static interface IClassAAA | |
{ | |
boolean isInited(); | |
} | |
public static class ClassAAA implements IClassAAA, IInitableAfterCreation | |
{ | |
boolean inited = false; | |
@Override | |
public boolean isInited() | |
{ | |
return this.inited; | |
} | |
@Override | |
public void init() | |
{ | |
this.inited = true; | |
} | |
} | |
public static interface IClassAAAFactory | |
{ | |
public IClassAAA create(); | |
} | |
@Test | |
public void testBindListenerNoAssistedFactory() | |
{ | |
Injector injector = Guice.createInjector(new AbstractModule() | |
{ | |
@Override | |
protected void configure() | |
{ | |
bind(IClassAAA.class).to(ClassAAA.class); | |
bindListener(new AbstractMatcher<Binding<?>>() | |
{ | |
@Override | |
public boolean matches(Binding<?> binding) | |
{ | |
if(binding != null && | |
binding.getKey() != null && | |
binding.getKey().getTypeLiteral() != null && | |
binding.getKey().getTypeLiteral().getRawType() != null && | |
IInitableAfterCreation.class.isAssignableFrom(binding.getKey().getTypeLiteral().getRawType())) | |
{ | |
return true; | |
} | |
return false; | |
} | |
}, | |
new ProvisionListener() | |
{ | |
@Override | |
public <T> void onProvision(ProvisionInvocation<T> provision) | |
{ | |
Object obj = provision.provision(); | |
if(obj instanceof IInitableAfterCreation) | |
{ | |
((IInitableAfterCreation)obj).init(); | |
} | |
} | |
}); | |
} | |
}); | |
IClassAAA classAAA = injector.getInstance(IClassAAA.class); | |
assertNotNull(classAAA); | |
assertTrue(classAAA.isInited()); | |
} | |
@Test | |
public void testBindListenerWithAssistedFactory() | |
{ | |
Injector injector = Guice.createInjector(new AbstractModule() | |
{ | |
@Override | |
protected void configure() | |
{ | |
install(new FactoryModuleBuilder().implement(IClassAAA.class, ClassAAA.class) | |
.build(IClassAAAFactory.class)); | |
bindListener(new AbstractMatcher<Binding<?>>() | |
{ | |
@Override | |
public boolean matches(Binding<?> binding) | |
{ | |
if(binding == null) | |
{ | |
return false; | |
} | |
if(binding.getKey() != null && | |
binding.getKey().getTypeLiteral() != null && | |
binding.getKey().getTypeLiteral().getRawType() != null && | |
IInitableAfterCreation.class.isAssignableFrom(binding.getKey().getTypeLiteral().getRawType())) | |
{ | |
return true; | |
} | |
// This is the interesting part | |
if(binding instanceof ConstructorBinding<?>) | |
{ | |
ConstructorBinding<?> constructorBinding = (ConstructorBinding<?>)binding; | |
if(constructorBinding.getConstructor() != null && | |
constructorBinding.getConstructor().getDeclaringType() != null && | |
constructorBinding.getConstructor().getDeclaringType().getRawType() != null) | |
{ | |
return IInitableAfterCreation.class.isAssignableFrom(constructorBinding.getConstructor().getDeclaringType().getRawType()); | |
} | |
} | |
return false; | |
} | |
}, | |
new ProvisionListener() | |
{ | |
@Override | |
public <T> void onProvision(ProvisionInvocation<T> provision) | |
{ | |
Object obj = provision.provision(); | |
if(obj instanceof IInitableAfterCreation) | |
{ | |
((IInitableAfterCreation)obj).init(); | |
} | |
} | |
}); | |
} | |
}); | |
IClassAAAFactory classAAAFactory = injector.getInstance(IClassAAAFactory.class); | |
assertNotNull(classAAAFactory); | |
IClassAAA classAAA = classAAAFactory.create(); | |
assertNotNull(classAAA); | |
assertTrue(classAAA.isInited()); | |
} | |
} |
For the records, the easiest way to deal with this is to always return TRUE in the matches() method.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Fix for second test at : https://gist.github.com/electrotype/6296350