public void testFactoryBindingDependencies() { Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { bind(Double.class).toInstance(5.0d); bind(ColoredCarFactory.class) .toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class)); } }); Binding<?> binding = injector.getBinding(ColoredCarFactory.class); HasDependencies hasDependencies = (HasDependencies) binding; assertEquals( ImmutableSet.<Dependency<?>>of(Dependency.get(Key.get(double.class))), hasDependencies.getDependencies()); }
public void testGuicify330Provider() { Provider<String> jsr330Provider = new Provider<String>() { @Override public String get() { return "A"; } @Override public String toString() { return "jsr330Provider"; } }; com.google.inject.Provider<String> guicified = Providers.guicify(jsr330Provider); assertEquals("guicified(jsr330Provider)", guicified.toString()); assertEquals("A", guicified.get()); // when you guicify the Guice-friendly, it's a no-op assertSame(guicified, Providers.guicify(guicified)); assertFalse(guicified instanceof HasDependencies); }
public void testDependencies_both() { Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { OptionalBinder<String> optionalbinder = OptionalBinder.newOptionalBinder(binder(), String.class); optionalbinder.setDefault().toInstance("A"); optionalbinder.setBinding().to(Key.get(String.class, Names.named("b"))); bindConstant().annotatedWith(Names.named("b")).to("B"); } }); Binding<String> binding = injector.getBinding(Key.get(String.class)); HasDependencies withDependencies = (HasDependencies) binding; Set<String> elements = Sets.newHashSet(); elements.addAll(recurseForDependencies(injector, withDependencies)); assertEquals(ImmutableSet.of("B"), elements); }
public void testDependencies_actual() { Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { OptionalBinder<String> optionalbinder = OptionalBinder.newOptionalBinder(binder(), String.class); optionalbinder.setBinding().to(Key.get(String.class, Names.named("b"))); bindConstant().annotatedWith(Names.named("b")).to("B"); } }); Binding<String> binding = injector.getBinding(Key.get(String.class)); HasDependencies withDependencies = (HasDependencies) binding; Set<String> elements = Sets.newHashSet(); elements.addAll(recurseForDependencies(injector, withDependencies)); assertEquals(ImmutableSet.of("B"), elements); }
public void testDependencies_default() { Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { OptionalBinder<String> optionalbinder = OptionalBinder.newOptionalBinder(binder(), String.class); optionalbinder.setDefault().toInstance("A"); } }); Binding<String> binding = injector.getBinding(Key.get(String.class)); HasDependencies withDependencies = (HasDependencies) binding; Set<String> elements = Sets.newHashSet(); elements.addAll(recurseForDependencies(injector, withDependencies)); assertEquals(ImmutableSet.of("A"), elements); }
/** * We just want to make sure that multibinder's binding depends on each of its values. We don't * really care about the underlying structure of those bindings, which are implementation details. */ public void testMultibinderDependencies() { Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class); multibinder.addBinding().toInstance("A"); multibinder.addBinding().to(Key.get(String.class, Names.named("b"))); bindConstant().annotatedWith(Names.named("b")).to("B"); } }); Binding<Set<String>> binding = injector.getBinding(new Key<Set<String>>() {}); HasDependencies withDependencies = (HasDependencies) binding; Set<String> elements = Sets.newHashSet(); for (Dependency<?> dependency : withDependencies.getDependencies()) { elements.add((String) injector.getInstance(dependency.getKey())); } assertEquals(ImmutableSet.of("A", "B"), elements); }
/** We just want to make sure that mapbinder's binding depends on the underlying multibinder. */ public void testMultibinderDependencies() { Injector injector = Guice.createInjector(new AbstractModule() { protected void configure() { MapBinder<Integer, String> mapBinder = MapBinder.newMapBinder(binder(), Integer.class, String.class); mapBinder.addBinding(1).toInstance("A"); mapBinder.addBinding(2).to(Key.get(String.class, Names.named("b"))); bindConstant().annotatedWith(Names.named("b")).to("B"); } }); Binding<Map<Integer, String>> binding = injector.getBinding(new Key<Map<Integer, String>>() {}); HasDependencies withDependencies = (HasDependencies) binding; Key<?> setKey = new Key<Set<Map.Entry<Integer, Provider<String>>>>() {}; assertEquals(ImmutableSet.<Dependency<?>>of(Dependency.get(setKey)), withDependencies.getDependencies()); Set<String> elements = Sets.newHashSet(); elements.addAll(recurseForDependencies(injector, withDependencies)); assertEquals(ImmutableSet.of("A", "B"), elements); }
/** We just want to make sure that mapbinder's binding depends on the underlying multibinder. */ public void testMultibinderDependenciesInToolStage() { Injector injector = Guice.createInjector(Stage.TOOL, new AbstractModule() { protected void configure() { MapBinder<Integer, String> mapBinder = MapBinder.newMapBinder(binder(), Integer.class, String.class); mapBinder.addBinding(1).toInstance("A"); mapBinder.addBinding(2).to(Key.get(String.class, Names.named("b"))); bindConstant().annotatedWith(Names.named("b")).to("B"); }}); Binding<Map<Integer, String>> binding = injector.getBinding(new Key<Map<Integer, String>>() {}); HasDependencies withDependencies = (HasDependencies) binding; Key<?> setKey = new Key<Set<Map.Entry<Integer, Provider<String>>>>() {}; assertEquals(ImmutableSet.<Dependency<?>>of(Dependency.get(setKey)), withDependencies.getDependencies()); }
public void testDependencies() { Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { OptionalBinder<String> optionalbinder = OptionalBinder.newOptionalBinder(binder(), String.class); optionalbinder.setDefault().toInstance("A"); optionalbinder.setBinding().to(Key.get(String.class, Names.named("b"))); bindConstant().annotatedWith(Names.named("b")).to("B"); } }); Binding<String> binding = injector.getBinding(Key.get(String.class)); HasDependencies withDependencies = (HasDependencies) binding; Set<String> elements = Sets.newHashSet(); elements.addAll(recurseForDependencies(injector, withDependencies)); assertEquals(ImmutableSet.of("A", "B"), elements); }
/** * We just want to make sure that multibinder's binding depends on each of its values. We don't * really care about the underlying structure of those bindings, which are implementation details. */ public void testMultibinderDependencies() { Injector injector = Guice.createInjector(new AbstractModule() { protected void configure() { Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class); multibinder.addBinding().toInstance("A"); multibinder.addBinding().to(Key.get(String.class, Names.named("b"))); bindConstant().annotatedWith(Names.named("b")).to("B"); } }); Binding<Set<String>> binding = injector.getBinding(new Key<Set<String>>() {}); HasDependencies withDependencies = (HasDependencies) binding; Set<String> elements = Sets.newHashSet(); for (Dependency<?> dependency : withDependencies.getDependencies()) { elements.add((String) injector.getInstance(dependency.getKey())); } assertEquals(ImmutableSet.of("A", "B"), elements); }
public void testGuicify330Provider() { Provider<String> jsr330Provider = new Provider<String>() { public String get() { return "A"; } @Override public String toString() { return "jsr330Provider"; } }; com.google.inject.Provider<String> guicified = Providers.guicify(jsr330Provider); assertEquals("guicified(jsr330Provider)", guicified.toString()); assertEquals("A", guicified.get()); // when you guicify the Guice-friendly, it's a no-op assertSame(guicified, Providers.guicify(guicified)); assertFalse(guicified instanceof HasDependencies); }
/** We just want to make sure that mapbinder's binding depends on the underlying multibinder. */ public void testMultibinderDependencies() { Injector injector = Guice.createInjector(new AbstractModule() { protected void configure() { MapBinder<Integer, String> mapBinder = MapBinder.newMapBinder(binder(), Integer.class, String.class); mapBinder.addBinding(1).toInstance("A"); mapBinder.addBinding(2).to(Key.get(String.class, Names.named("b"))); bindConstant().annotatedWith(Names.named("b")).to("B"); } }); Binding<Map<Integer, String>> binding = injector.getBinding(new Key<Map<Integer, String>>() {}); HasDependencies withDependencies = (HasDependencies) binding; Key<?> setKey = new Key<Set<Map.Entry<Integer, Provider<String>>>>() {}; assertEquals(ImmutableSet.<Dependency<?>>of(Dependency.get(setKey)), withDependencies.getDependencies()); }
@Override protected Void visitOther(Binding<?> binding) { if( binding instanceof HasDependencies ) { analyzeDependencies(((HasDependencies) binding).getDependencies()); } return null; }
private static String argumentList(HasDependencies binding) { StringBuilder builder = new StringBuilder(); for (Dependency<?> dependency : binding.getDependencies()) { builder.append(providerCall(dependency.getKey()) + ","); } if (0 < binding.getDependencies().size()) { builder.deleteCharAt(builder.length() - 1); } return builder.toString(); }
private Collection<Key<?>> visitHasDependencies(HasDependencies hasDependencies) { Set<Key<?>> dependencies = Sets.newHashSet(); for (Dependency<?> dependency : hasDependencies.getDependencies()) { dependencies.add(dependency.getKey()); } return dependencies; }
/** * Returns a dependency edge for each {@link Dependency} in the binding. These will be from the * given node ID to the {@link Dependency}'s {@link Key}. * * @param nodeId ID of the node that should be the tail of the dependency edges * @param binding {@link Binding} for the dependencies */ private <T extends Binding<?> & HasDependencies> Collection<Edge> newDependencyEdges( NodeId nodeId, T binding) { ImmutableList.Builder<Edge> builder = ImmutableList.builder(); for (Dependency<?> dependency : binding.getDependencies()) { NodeId to = NodeId.newTypeId(dependency.getKey()); builder.add(new DependencyEdge(nodeId, to, dependency.getInjectionPoint())); } return builder.build(); }
/** * Returns a new instance node for the given {@link Binding}. * * @param binding binding for the node to create * @param instance value of the instance * @return instance node for the given binding */ private <T extends Binding<?> & HasDependencies> InstanceNode newInstanceNode( T binding, Object instance) { Collection<Member> members = Lists.newArrayList(); for (Dependency<?> dependency : binding.getDependencies()) { InjectionPoint injectionPoint = dependency.getInjectionPoint(); if (injectionPoint != null) { members.add(injectionPoint.getMember()); } } return new InstanceNode( NodeId.newInstanceId(binding.getKey()), binding.getSource(), instance, members); }
private void validateDependencies(Set<Key<?>> expectedKeys, Binding<?> binding) { Set<Dependency<?>> dependencies = ((HasDependencies) binding).getDependencies(); Set<Key<?>> actualKeys = new HashSet<>(); for (Dependency<?> dependency : dependencies) { actualKeys.add(dependency.getKey()); } assertEquals(expectedKeys, actualKeys); }
/** Safely gets the dependencies of possibly not initialized bindings. */ @SuppressWarnings("unchecked") private Set<Dependency<?>> getInternalDependencies(BindingImpl<?> binding) { if (binding instanceof ConstructorBindingImpl) { return ((ConstructorBindingImpl) binding).getInternalDependencies(); } else if (binding instanceof HasDependencies) { return ((HasDependencies) binding).getDependencies(); } else { return ImmutableSet.of(); } }
/** Check that the dependencies are correct. */ public void testMultibinderDependencies() { Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { MapBinder<Integer, String> mapBinder = MapBinder.newMapBinder(binder(), Integer.class, String.class); mapBinder.addBinding(1).toInstance("A"); mapBinder.addBinding(2).to(Key.get(String.class, Names.named("b"))); bindConstant().annotatedWith(Names.named("b")).to("B"); } }); Binding<Map<Integer, String>> binding = injector.getBinding(new Key<Map<Integer, String>>() {}); HasDependencies withDependencies = (HasDependencies) binding; Set<Dependency<?>> actualDependencies = withDependencies.getDependencies(); // We expect two dependencies, because the dependencies are annotated with // Element, which has a uniqueId, it's difficult to directly compare them. // Instead we will manually compare all the fields except the uniqueId assertEquals(2, actualDependencies.size()); for (Dependency<?> dependency : actualDependencies) { Key<?> key = dependency.getKey(); assertEquals(new TypeLiteral<String>() {}, key.getTypeLiteral()); Annotation annotation = dependency.getKey().getAnnotation(); assertTrue(annotation instanceof Element); Element element = (Element) annotation; assertEquals("", element.setName()); assertEquals(Element.Type.MAPBINDER, element.type()); assertEquals("java.lang.Integer", element.keyType()); } Set<String> elements = Sets.newHashSet(); elements.addAll(recurseForDependencies(injector, withDependencies)); assertEquals(ImmutableSet.of("A", "B"), elements); }
private Set<String> recurseForDependencies(Injector injector, HasDependencies hasDependencies) { Set<String> elements = Sets.newHashSet(); for (Dependency<?> dependency : hasDependencies.getDependencies()) { Binding<?> binding = injector.getBinding(dependency.getKey()); HasDependencies deps = (HasDependencies) binding; if (binding instanceof InstanceBinding) { elements.add((String) ((InstanceBinding<?>) binding).getInstance()); } else { elements.addAll(recurseForDependencies(injector, deps)); } } return elements; }
/** Check that the dependencies are correct in the Tool Stage. */ public void testMultibinderDependenciesInToolStage() { Injector injector = Guice.createInjector( Stage.TOOL, new AbstractModule() { @Override protected void configure() { MapBinder<Integer, String> mapBinder = MapBinder.newMapBinder(binder(), Integer.class, String.class); mapBinder.addBinding(1).toInstance("A"); mapBinder.addBinding(2).to(Key.get(String.class, Names.named("b"))); bindConstant().annotatedWith(Names.named("b")).to("B"); } }); Binding<Map<Integer, String>> binding = injector.getBinding(new Key<Map<Integer, String>>() {}); HasDependencies withDependencies = (HasDependencies) binding; Set<Dependency<?>> actualDependencies = withDependencies.getDependencies(); // We expect two dependencies, because the dependencies are annotated with // Element, which has a uniqueId, it's difficult to directly compare them. // Instead we will manually compare all the fields except the uniqueId assertEquals(2, actualDependencies.size()); for (Dependency<?> dependency : actualDependencies) { Key<?> key = dependency.getKey(); assertEquals(new TypeLiteral<String>() {}, key.getTypeLiteral()); Annotation annotation = dependency.getKey().getAnnotation(); assertTrue(annotation instanceof Element); Element element = (Element) annotation; assertEquals("", element.setName()); assertEquals(Element.Type.MAPBINDER, element.type()); assertEquals("java.lang.Integer", element.keyType()); } }
@SuppressWarnings("rawtypes") private Set<String> recurseForDependencies(Injector injector, HasDependencies hasDependencies) { Set<String> elements = Sets.newHashSet(); for (Dependency<?> dependency : hasDependencies.getDependencies()) { Binding<?> binding = injector.getBinding(dependency.getKey()); HasDependencies deps = (HasDependencies) binding; if (binding instanceof InstanceBinding) { elements.add((String) ((InstanceBinding) binding).getInstance()); } else { elements.addAll(recurseForDependencies(injector, deps)); } } return elements; }
public void testMultibindingProviderDependencies() { final Annotation setAnn = Names.named("foo"); Injector injector = Guice.createInjector( new AbstractModule() { @Override protected void configure() { Multibinder<String> multibinder = Multibinder.newSetBinder(binder(), String.class, setAnn); multibinder.addBinding().toInstance("a"); multibinder.addBinding().toInstance("b"); } }); HasDependencies providerBinding = (HasDependencies) injector.getBinding(new Key<Collection<Provider<String>>>(setAnn) {}); HasDependencies setBinding = (HasDependencies) injector.getBinding(new Key<Set<String>>(setAnn) {}); // sanity check the size assertEquals(setBinding.getDependencies().toString(), 2, setBinding.getDependencies().size()); Set<Dependency<?>> expected = Sets.newHashSet(); for (Dependency<?> dep : setBinding.getDependencies()) { Key key = dep.getKey(); Dependency<?> providerDependency = Dependency.get(key.ofType(Types.providerOf(key.getTypeLiteral().getType()))); expected.add(providerDependency); } assertEquals(expected, providerBinding.getDependencies()); }
/** * Returns a dependency edge for each {@link Dependency} in the binding. These will be from the * given node ID to the {@link Dependency}'s {@link Key}. * * @param nodeId ID of the node that should be the tail of the dependency * edges * @param binding {@link Binding} for the dependencies */ private <T extends Binding<?> & HasDependencies> Collection<Edge> newDependencyEdges( NodeId nodeId, T binding) { ImmutableList.Builder<Edge> builder = ImmutableList.builder(); for (Dependency<?> dependency : binding.getDependencies()) { NodeId to = NodeId.newTypeId(dependency.getKey()); builder.add(new DependencyEdge(nodeId, to, dependency.getInjectionPoint())); } return builder.build(); }
/** * Returns a new instance node for the given {@link Binding}. * * @param binding binding for the node to create * @param instance value of the instance * @return instance node for the given binding */ private <T extends Binding<?> & HasDependencies> InstanceNode newInstanceNode(T binding, Object instance) { Collection<Member> members = Lists.newArrayList(); for (Dependency<?> dependency : binding.getDependencies()) { InjectionPoint injectionPoint = dependency.getInjectionPoint(); if (injectionPoint != null) { members.add(injectionPoint.getMember()); } } return new InstanceNode(NodeId.newInstanceId(binding.getKey()), binding.getSource(), instance, members); }
private void validateDependencies(Set<Key<?>> expectedKeys, Binding<?> binding) { Set<Dependency<?>> dependencies = ((HasDependencies)binding).getDependencies(); Set<Key<?>> actualKeys = new HashSet<Key<?>>(); for(Dependency dependency : dependencies) { actualKeys.add(dependency.getKey()); } assertEquals(expectedKeys, actualKeys); }
public void testFactoryBindingDependencies() { Injector injector = Guice.createInjector(new AbstractModule() { @Override protected void configure() { bind(Double.class).toInstance(5.0d); bind(ColoredCarFactory.class) .toProvider(FactoryProvider.newFactory(ColoredCarFactory.class, Mustang.class)); } }); Binding<?> binding = injector.getBinding(ColoredCarFactory.class); HasDependencies hasDependencies = (HasDependencies) binding; assertEquals(ImmutableSet.<Dependency<?>>of(Dependency.get(Key.get(double.class))), hasDependencies.getDependencies()); }
private Set<String> recurseForDependencies(Injector injector, HasDependencies hasDependencies) { Set<String> elements = Sets.newHashSet(); for (Dependency<?> dependency : hasDependencies.getDependencies()) { Binding<?> binding = injector.getBinding(dependency.getKey()); HasDependencies deps = (HasDependencies) binding; if (binding instanceof InstanceBinding) { elements.add((String) ((InstanceBinding) binding).getInstance()); } else { elements.addAll(recurseForDependencies(injector, deps)); } } return elements; }
private static Key<?> keyFromOptionalSourceBinding(Binding<?> binding, Map<Key<?>, Binding<?>> elements) { // Flow is: // binding == ProviderInstanceBinding<Map.Entry<Source, Provider<String>> // dependency on: Provider<String> that maps to ProviderInstanceBinding<String> in MapBinder // dependency on: Provider<String> of user set value. Key<?> mapKey = genericOf(getOnlyElement(((HasDependencies) binding).getDependencies()).getKey()); Binding<?> mapBinding = elements.get(mapKey); Key<?> userKey = genericOf(getOnlyElement(((HasDependencies) mapBinding).getDependencies()).getKey()); return userKey; }
/** Safely gets the dependencies of possibly not initialized bindings. */ @SuppressWarnings("unchecked") private Set<Dependency<?>> getInternalDependencies(BindingImpl<?> binding) { if(binding instanceof ConstructorBindingImpl) { return ((ConstructorBindingImpl)binding).getInternalDependencies(); } else if(binding instanceof HasDependencies) { return ((HasDependencies)binding).getDependencies(); } else { return ImmutableSet.of(); } }