private static String instantiation(Binding<?> binding) { if (binding instanceof InstanceBinding<?>) { return instanceLiteral((InstanceBinding<?>) binding); } else if (binding instanceof ProviderKeyBinding<?>) { return providerCall(((ProviderKeyBinding<?>) binding).getProviderKey()) + ".get()"; } else if (binding instanceof LinkedKeyBinding<?>) { return providerCall(((LinkedKeyBinding<?>) binding).getLinkedKey()); } else if (binding instanceof ConstructorBinding<?>) { return constructorCall((ConstructorBinding<?>) binding); } else if (binding instanceof ProviderBinding<?>) { return provider((ProviderBinding<?>) binding); } else if (binding instanceof ProviderInstanceBinding<?>) { return instantiation((ProviderInstanceBinding<?>) binding); } else { throw new DeguicifierException(); } }
public static <T> Optional<TypeLiteral<? extends T>> targetType(Injector injector, Binding<T> binding) { if(binding instanceof UntargettedBinding) { return Optional.of(binding.getKey().getTypeLiteral()); } else if(binding instanceof ConstructorBinding) { return Optional.of((TypeLiteral<? extends T>) ((ConstructorBinding) binding).getConstructor().getDeclaringType()); } else if(binding instanceof InstanceBinding) { return Optional.of(TypeLiteral.get((Class<T>) ((InstanceBinding) binding).getInstance().getClass())); } else if(binding instanceof LinkedKeyBinding) { return targetType(injector, injector.getBinding(((LinkedKeyBinding) binding).getLinkedKey())); } else if(binding instanceof ExposedBinding) { return targetType(((ExposedBinding) binding).getPrivateElements().getInjector(), binding.getKey()); } return Optional.empty(); }
@Override public <T> Collection<T> getBeansOfType(Class<T> clazz) { Injector injector = ensureInjector(); List<T> beans = new ArrayList<T>(); Map<Key<?>, Binding<?>> bindings = injector.getBindings(); for( Binding<?> binding : bindings.values() ) { Key<?> actualKey = null; if( binding instanceof UntargettedBinding || binding instanceof ConstructorBinding ) { actualKey = binding.getKey(); } else if( binding instanceof LinkedKeyBinding ) { actualKey = ((LinkedKeyBinding<?>) binding).getLinkedKey(); } else if( binding instanceof ProviderBinding ) { actualKey = ((ProviderBinding<?>) binding).getProvidedKey(); } if( actualKey != null && clazz.isAssignableFrom(actualKey.getTypeLiteral().getRawType()) ) { beans.add(clazz.cast(binding.getProvider().get())); } } return beans; }
@Test public void binds_linked_key() { given(module = new AbstractModule() { @Override protected void configure() { bind(Interface.class).to(Implementation.class); } }); given(injector = Guice.createInjector(module)); when(injector.getBinding(Interface.class)); thenReturned(instanceOf(LinkedKeyBinding.class)); }
/** * Visitor for {@link LinkedKeyBinding}. This is the standard {@link Binding} you get from * binding an interface class to an implementation class. We draw a {@link BindingEdge} from the * interface node to the node of the implementing class. */ @Override public Collection<Edge> visit(LinkedKeyBinding<?> binding) { return ImmutableList.<Edge>of( new BindingEdge( NodeId.newTypeId(binding.getKey()), NodeId.newTypeId(binding.getLinkedKey()), BindingEdge.Type.NORMAL)); }
public void testVisitLinkedKey() { Binding<?> binding = getBinding(Key.get(Interface.class), new LinkedKeyModule()); Collection<Key<?>> dependencies = visitor.visit((LinkedKeyBinding<?>) binding); // Dependency should be to the class this interface is bound to. assertDependencies(dependencies, Key.get(ConstructedClass.class)); }
@Override public String toString() { return MoreObjects.toStringHelper(LinkedKeyBinding.class) .add("key", getKey()) .add("source", getSource()) .add("scope", getScoping()) .add("target", targetKey) .toString(); }
private static boolean matches(Binding<?> item, BindResult<?> result) { switch (result.type) { case INSTANCE: if (item instanceof InstanceBinding && ((InstanceBinding) item).getInstance().equals(result.instance)) { return true; } break; case LINKED: if (item instanceof LinkedKeyBinding && ((LinkedKeyBinding) item).getLinkedKey().equals(result.key)) { return true; } break; case PROVIDER_INSTANCE: if (item instanceof ProviderInstanceBinding && Objects.equal( ((ProviderInstanceBinding) item).getUserSuppliedProvider().get(), result.instance)) { return true; } break; case PROVIDER_KEY: if (item instanceof ProviderKeyBinding && ((ProviderKeyBinding) item).getProviderKey().equals(result.key)) { return true; } break; } return false; }
private static boolean matches(Binding<?> item, BindResult<?> result) { switch (result.type) { case INSTANCE: if (item instanceof InstanceBinding && ((InstanceBinding) item).getInstance().equals(result.instance)) { return true; } break; case LINKED: if (item instanceof LinkedKeyBinding && ((LinkedKeyBinding) item).getLinkedKey().equals(result.key)) { return true; } break; case PROVIDER_INSTANCE: if (item instanceof ProviderInstanceBinding && Objects.equal(((ProviderInstanceBinding) item).getUserSuppliedProvider().get(), result.instance)) { return true; } break; case PROVIDER_KEY: if (item instanceof ProviderKeyBinding && ((ProviderKeyBinding) item).getProviderKey().equals(result.key)) { return true; } break; } return false; }
@Override public String toString() { return Objects.toStringHelper(LinkedKeyBinding.class) .add("key", getKey()) .add("source", getSource()) .add("scope", getScoping()) .add("target", targetKey) .toString(); }
@Override public Optional<TypeLiteral<?>> visit(LinkedKeyBinding<?> binding) { // Delegate to the binding for the target type return injector.getBinding(binding.getLinkedKey()).acceptTargetVisitor(this); }
@Override public Object visit(LinkedKeyBinding<? extends T> linkedKeyBinding) { requireKey(linkedKeyBinding.getLinkedKey()); return super.visit(linkedKeyBinding); }
@Override public Void visit(LinkedKeyBinding<? extends T> binding) { scope(binding, rebind(binding).to(binding.getLinkedKey())); return null; }
@Override public Void visit(LinkedKeyBinding<? extends Object> linkedKeyBinding) { analyzeImplementation(linkedKeyBinding.getLinkedKey().getTypeLiteral(), false); return null; }
public Void visit(LinkedKeyBinding<? extends T> linkedKeyBinding) { Context context = Context.forElement(linkedKeyBinding); bindingsCollection.addBinding(targetKey, bindingFactory.getBindClassBinding(linkedKeyBinding.getLinkedKey(), targetKey, context)); return null; }
@Override public Collection<Key<?>> visit(LinkedKeyBinding<?> binding) { return ImmutableSet.<Key<?>>of(binding.getLinkedKey()); }
@Override public Indexer.IndexedBinding visit(LinkedKeyBinding<? extends Object> binding) { return new Indexer.IndexedBinding( binding, BindingType.LINKED_KEY, scope(binding), binding.getLinkedKey()); }
@Override public ResolvedBinding<T> visit(LinkedKeyBinding<? extends T> linkedKeyBinding) { return traverse(linkedKeyBinding.getLinkedKey()); }
/** * Visitor for {@link LinkedKeyBinding}. This is the standard {@link Binding} you get from * binding an interface class to an implementation class. We draw a {@link BindingEdge} from * the interface node to the node of the implementing class. */ @Override public Collection<Edge> visit(LinkedKeyBinding<?> binding) { return ImmutableList.<Edge>of(new BindingEdge(NodeId.newTypeId(binding.getKey()), NodeId.newTypeId(binding.getLinkedKey()), BindingEdge.Type.NORMAL)); }
@SuppressWarnings("unchecked") private static <T> void setInjectorTest(Key<T> setKey, TypeLiteral<?> elementType, Iterable<? extends Module> modules, boolean allowDuplicates, int otherMultibindings, BindResult... results) { Injector injector = Guice.createInjector(modules); Visitor<T> visitor = new Visitor<T>(); Binding<T> binding = injector.getBinding(setKey); MultibinderBinding<T> multibinder = (MultibinderBinding<T>)binding.acceptTargetVisitor(visitor); assertNotNull(multibinder); assertEquals(elementType, multibinder.getElementTypeLiteral()); assertEquals(allowDuplicates, multibinder.permitsDuplicates()); List<Binding<?>> elements = Lists.newArrayList(multibinder.getElements()); List<BindResult> bindResults = Lists.newArrayList(results); assertEquals("wrong bind elements, expected: " + bindResults + ", but was: " + multibinder.getElements(), bindResults.size(), elements.size()); for(BindResult result : bindResults) { Binding found = null; for(Binding item : elements) { switch (result.type) { case INSTANCE: if (item instanceof InstanceBinding && ((InstanceBinding) item).getInstance().equals(result.instance)) { found = item; } break; case LINKED: if (item instanceof LinkedKeyBinding && ((LinkedKeyBinding) item).getKey().equals(result.key)) { found = item; } break; case PROVIDER_INSTANCE: if (item instanceof ProviderInstanceBinding && ((ProviderInstanceBinding) item).getProviderInstance().get().equals( result.instance)) { found = item; } break; } } if(found == null) { fail("Could not find element: " + result + " in remaining elements: " + elements); } else { elements.remove(found); } } if(!elements.isEmpty()) { fail("Found all elements of: " + bindResults + ", but more were left over: " + elements); } Set<Binding> setOfElements = new HashSet<Binding>(multibinder.getElements()); List<Object> otherMultibinders = Lists.newArrayList(); List<Binding> otherContains = Lists.newArrayList(); for(Binding b : injector.getAllBindings().values()) { boolean contains = multibinder.containsElement(b); Object visited = b.acceptTargetVisitor(visitor); if(visited != null) { if(visited.equals(multibinder)) { assertTrue(contains); } else { otherMultibinders.add(visited); } } else if(setOfElements.contains(b)) { assertTrue(contains); } else if(contains) { otherContains.add(b); } } if(allowDuplicates) { assertEquals("contained more than it should: " + otherContains, 1, otherContains.size()); } else { assertTrue("contained more than it should: " + otherContains, otherContains.isEmpty()); } assertEquals("other multibindings found: " + otherMultibinders, otherMultibindings, otherMultibinders.size()); }