Skip to content

Instantly share code, notes, and snippets.

@gissuebot
Created July 7, 2014 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gissuebot/c38119b4347d5ede63ce to your computer and use it in GitHub Desktop.
Save gissuebot/c38119b4347d5ede63ce to your computer and use it in GitHub Desktop.
Migrated attachment for Guice issue 802, comment 0
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import com.google.inject.AbstractModule;
import com.google.inject.BindingAnnotation;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import com.google.inject.Stage;
import com.google.inject.TypeLiteral;
import com.google.inject.matcher.Matchers;
import com.google.inject.spi.InjectionListener;
import com.google.inject.spi.TypeEncounter;
import com.google.inject.spi.TypeListener;
public class InjectorListenerWithSingletons
{
private static final Key<IFace> KEY_1 = Key.get( IFace.class, Ann1.class );
private static final Key<IFace> KEY_2 = Key.get( IFace.class, Ann2.class );
@Retention( RetentionPolicy.RUNTIME )
@BindingAnnotation
public static @interface Ann1 {}
@Retention( RetentionPolicy.RUNTIME )
@BindingAnnotation
public static @interface Ann2 {}
public static interface IFace {}
public static final class Impl implements IFace {}
public static final class IModule extends AbstractModule
{
@Override
protected void configure()
{
bind( KEY_1 ).to( Impl.class ).in( Singleton.class );
bind( KEY_2 ).to( Impl.class ).in( Singleton.class );
}
}
public static final class ListenerModule extends AbstractModule implements TypeListener
{
@Override
protected void configure()
{
bindListener( Matchers.any(), this );
}
@Override
public <I> void hear( TypeLiteral<I> type, TypeEncounter<I> encounter )
{
final Provider<IFace> p1 = encounter.getProvider( KEY_1 );
final Provider<IFace> p2 = encounter.getProvider( KEY_2 );
encounter.register( new InjectionListener<I>()
{
@Override
public void afterInjection( I injectee )
{
p1.get(); // if either or both of these lines are commented,
p2.get(); // then the test passes.
}
} );
}
}
public static void main( String[] args )
{
Injector bindingsOnly = Guice.createInjector( Stage.PRODUCTION, new IModule() );
assertInstances( bindingsOnly ); // successful,
Injector bindingsWithListener = Guice.createInjector( Stage.PRODUCTION, new IModule(), new ListenerModule() );
assertInstances( bindingsWithListener ); // fails if both instances are retrieved within afterInjection()
}
private static void assertInstances( Injector i )
{
assert i.getInstance( KEY_1 ) == i.getInstance( KEY_1 ) : "KEY_1 should be a singleton";
assert i.getInstance( KEY_2 ) == i.getInstance( KEY_2 ) : "KEY_2 should be a singleton";
assert i.getInstance( KEY_1 ) != i.getInstance( KEY_2 ) : "KEY_1 and KEY_2 should be different";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment