public Void visit(ProviderInstanceBinding<? extends T> providerInstanceBinding) { // Detect provider methods and handle them // TODO(bstoler): Update this when the SPI explicitly has a case for // provider methods Provider<? extends T> provider = providerInstanceBinding.getProviderInstance(); if (provider instanceof ProviderMethod) { Context context = Context.forElement(providerInstanceBinding); bindingsCollection.addBinding(targetKey, bindingFactory.getProviderMethodBinding((ProviderMethod<?>) provider, context)); return null; } if (provider instanceof GwtDotCreateProvider) { addImplicitBinding(providerInstanceBinding); return null; } // OTt, use the normal default handler (and error) return super.visit(providerInstanceBinding); }
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(); } }
private static String providesMethodCall(ProviderInstanceBinding<?> binding, ProviderMethod<?> provider) { Method providesMethod = provider.getMethod(); Class<?> moduleClass = providesMethod.getDeclaringClass(); if (moduleClass.isLocalClass()) { throw new DeguicifierException(); } try { moduleClass.getConstructor(); } catch (NoSuchMethodException e) { throw new DeguicifierException(e); } String statement = "new " + moduleClass.getCanonicalName() + "()." + providesMethod.getName() + "(" + argumentList(binding) + ")"; return statement; }
public void testGetInstanceName_providerMethod() throws Exception { final ProviderMethod<?>[] methodHolder = new ProviderMethod[1]; Injector injector = Guice.createInjector(new ProvidingModule()); injector .getBinding(Integer.class) .acceptTargetVisitor( new DefaultBindingTargetVisitor<Object, Void>() { @SuppressWarnings("unchecked") @Override public Void visit(ProviderInstanceBinding<?> binding) { methodHolder[0] = (ProviderMethod) binding.getUserSuppliedProvider(); return null; } }); assertEquals( "Method provider should pretty print as the method signature", "#provideInteger(String)", nameFactory.getInstanceName(methodHolder[0])); }
/** Implementation of {@link OptionalBinderBinding#containsElement}. */ boolean containsElement(Element element) { // All of our bindings are ProviderInstanceBindings whose providers extend // RealOptionalBinderProviderWithDependencies and have 'this' as its binding selection. if (element instanceof ProviderInstanceBinding) { javax.inject.Provider<?> providerInstance = ((ProviderInstanceBinding<?>) element).getUserSuppliedProvider(); if (providerInstance instanceof RealOptionalBinderProviderWithDependencies) { return ((RealOptionalBinderProviderWithDependencies<?, ?>) providerInstance) .bindingSelection.equals(this); } } if (element instanceof Binding) { Key<?> elementKey = ((Binding) element).getKey(); // if it isn't one of the things we bound directly it might be an actual or default key return elementKey.equals(getKeyForActualBinding()) || elementKey.equals(getKeyForDefaultBinding()); } return false; // cannot match; }
/** * This will get the authoritative {@link BindingSelection} from the map provider. This * guarantees that everyone has the same instance of the bindingSelection and sees consistent * state. */ @Override final void initialize(InjectorImpl injector, Errors errors) throws ErrorsException { Binding<Map<K, V>> mapBinding = injector.getExistingBinding(mapKey); ProviderInstanceBinding<Map<K, V>> providerInstanceBinding = (ProviderInstanceBinding<Map<K, V>>) mapBinding; @SuppressWarnings("unchecked") RealMapProvider<K, V> mapProvider = (RealMapProvider<K, V>) providerInstanceBinding.getUserSuppliedProvider(); this.bindingSelection = mapProvider.getBindingSelection(); if (bindingSelection.tryInitialize(injector, errors)) { doInitialize(injector, errors); } }
/** * Will find and return the {@link com.google.inject.spi.Element} that is a {@link * ProviderMapEntry} with a key that matches the one supplied by the user in {@code k}. * * <p>Will return {@code null} if it cannot be found. */ private static com.google.inject.spi.Element getProviderMapEntry( Object kToFind, Iterable<com.google.inject.spi.Element> elements) { for (com.google.inject.spi.Element element : elements) { if (element instanceof ProviderInstanceBinding) { javax.inject.Provider<?> usp = ((ProviderInstanceBinding<?>) element).getUserSuppliedProvider(); if (usp instanceof ProviderMapEntry) { ProviderMapEntry<?, ?> pme = (ProviderMapEntry<?, ?>) usp; // Check if the key from the ProviderMapEntry matches the one we're looking for if (kToFind.equals(pme.getKey())) { return element; } } } } // No matching ProviderMapEntry found return null; }
public <B, V> V acceptExtensionVisitor(BindingTargetVisitor<B, V> visitor, ProviderInstanceBinding<? extends B> binding) { if(visitor instanceof ServletModuleTargetVisitor) { if(servletInstance != null) { return ((ServletModuleTargetVisitor<B, V>)visitor).visit( new InstanceServletBindingImpl(initParams, pattern, servletInstance, patternMatcher)); } else { return ((ServletModuleTargetVisitor<B, V>)visitor).visit( new LinkedServletBindingImpl(initParams, pattern, servletKey, patternMatcher)); } } else { return visitor.visit(binding); } }
public <B, V> V acceptExtensionVisitor(BindingTargetVisitor<B, V> visitor, ProviderInstanceBinding<? extends B> binding) { if(visitor instanceof ServletModuleTargetVisitor) { if(filterInstance != null) { return ((ServletModuleTargetVisitor<B, V>)visitor).visit( new InstanceFilterBindingImpl(initParams, pattern, filterInstance, patternMatcher)); } else { return ((ServletModuleTargetVisitor<B, V>)visitor).visit( new LinkedFilterBindingImpl(initParams, pattern, filterKey, patternMatcher)); } } else { return visitor.visit(binding); } }
public void testGetInstanceName_providerMethod() throws Exception { final ProviderMethod<?>[] methodHolder = new ProviderMethod[1]; Injector injector = Guice.createInjector(new ProvidingModule()); injector.getBinding(Integer.class).acceptTargetVisitor( new DefaultBindingTargetVisitor<Object, Void>() { @SuppressWarnings("unchecked") @Override public Void visit(ProviderInstanceBinding<?> binding) { methodHolder[0] = (ProviderMethod) binding.getUserSuppliedProvider(); return null; } }); assertEquals("Method provider should pretty print as the method signature", "#provideInteger(String)", nameFactory.getInstanceName(methodHolder[0])); }
public void testGetInstanceName_providerMethod() throws Exception { final ProviderMethod<?>[] methodHolder = new ProviderMethod[1]; Injector injector = Guice.createInjector(new ProvidingModule()); injector.getBinding(Integer.class).acceptTargetVisitor( new DefaultBindingTargetVisitor<Object, Void>() { @SuppressWarnings("unchecked") @Override public Void visit(ProviderInstanceBinding<?> binding) { methodHolder[0] = (ProviderMethod) binding.getProviderInstance(); return null; } }); assertEquals("Method provider should pretty print as the method signature", "#provideInteger(String)", nameFactory.getInstanceName(methodHolder[0])); }
/** * This is needed for 'initialize(injector)' below to be called so the provider * can get the injector after it is instantiated. */ @Override public <B, V> V acceptExtensionVisitor( BindingTargetVisitor<B, V> visitor, ProviderInstanceBinding<? extends B> binding) { return visitor.visit(binding); }
private static String instantiation(ProviderInstanceBinding<?> binding) { if (binding.getProviderInstance() instanceof ProviderMethod<?>) { ProviderMethod<?> provider = (ProviderMethod<?>) binding.getProviderInstance(); return providesMethodCall(binding, provider); } else if (binding.getProviderInstance() instanceof Multibinder<?>) { return createSetCall(binding); } else { throw new DeguicifierException(); } }
@Test public void binds_provider_instance() { given(module = new AbstractModule() { @Override protected void configure() { bind(Interface.class).toProvider(new InterfaceProvider()); } }); given(injector = Guice.createInjector(module)); when(injector.getBinding(Interface.class)); thenReturned(instanceOf(ProviderInstanceBinding.class)); }
@Test public void binds_provider_instance_from_method() { given(module = new AbstractModule() { @Override protected void configure() {} @Provides public Interface provide() { return new Implementation(); } }); given(injector = Guice.createInjector(module)); when(injector.getBinding(Interface.class)); thenReturned(instanceOf(ProviderInstanceBinding.class)); }
@SuppressWarnings("unchecked") // The real type of B must be T @Override public <B, V> V acceptExtensionVisitor(BindingTargetVisitor<B, V> visitor, ProviderInstanceBinding<? extends B> binding) { if (visitor instanceof ContextSensitiveBindingVisitor) { return ((ContextSensitiveBindingVisitor<T, V>)visitor).visit(this); } else { return visitor.visit(binding); } }
@SuppressWarnings("unchecked") // B must be Lazy<T> @Override public <B, V> V acceptExtensionVisitor(BindingTargetVisitor<B, V> visitor, ProviderInstanceBinding<? extends B> binding) { if (visitor instanceof LazyBindingVisitor) { return ((LazyBindingVisitor<T, V>)visitor).visit(this); } else { return visitor.visit(binding); } }
@Override public <B, V> V acceptExtensionVisitor( BindingTargetVisitor<B, V> visitor, ProviderInstanceBinding<? extends B> binding) { if (visitor instanceof ServletModuleTargetVisitor) { if (servletInstance != null) { return ((ServletModuleTargetVisitor<B, V>) visitor) .visit(new InstanceServletBindingImpl(initParams, servletInstance, patternMatcher)); } else { return ((ServletModuleTargetVisitor<B, V>) visitor) .visit(new LinkedServletBindingImpl(initParams, servletKey, patternMatcher)); } } else { return visitor.visit(binding); } }
@Override public <B, V> V acceptExtensionVisitor( BindingTargetVisitor<B, V> visitor, ProviderInstanceBinding<? extends B> binding) { if (visitor instanceof ServletModuleTargetVisitor) { if (filterInstance != null) { return ((ServletModuleTargetVisitor<B, V>) visitor) .visit(new InstanceFilterBindingImpl(initParams, filterInstance, patternMatcher)); } else { return ((ServletModuleTargetVisitor<B, V>) visitor) .visit(new LinkedFilterBindingImpl(initParams, filterKey, patternMatcher)); } } else { return visitor.visit(binding); } }
/** * Same as {@link #visit(InstanceBinding)}, but the binding edge is {@link * BindingEdge.Type#PROVIDER}. */ @Override public Collection<Edge> visit(ProviderInstanceBinding<?> binding) { return new ImmutableList.Builder<Edge>() .add( new BindingEdge( NodeId.newTypeId(binding.getKey()), NodeId.newInstanceId(binding.getKey()), BindingEdge.Type.PROVIDER)) .addAll(newDependencyEdges(NodeId.newInstanceId(binding.getKey()), binding)) .build(); }
public void testVisitProviderInstance() { Binding<?> binding = getBinding(Key.get(ConstructedClass.class), new ProviderInstanceModule()); Collection<Key<?>> dependencies = visitor.visit((ProviderInstanceBinding<?>) binding); // Dependencies will only be on the field- and method-injected classes. assertDependencies(dependencies, Key.get(E.class), Key.get(F.class)); }
@Override @SuppressWarnings("unchecked") public <T, V> V acceptExtensionVisitor( BindingTargetVisitor<T, V> visitor, ProviderInstanceBinding<? extends T> binding) { if (visitor instanceof AssistedInjectTargetVisitor) { return ((AssistedInjectTargetVisitor<T, V>) visitor).visit((AssistedInjectBinding<T>) this); } return visitor.visit(binding); }
@SuppressWarnings("unchecked") @Override public <B, V> V acceptExtensionVisitor( BindingTargetVisitor<B, V> visitor, ProviderInstanceBinding<? extends B> binding) { if (visitor instanceof MultibindingsTargetVisitor) { return ((MultibindingsTargetVisitor<Set<T>, V>) visitor).visit(this); } else { return visitor.visit(binding); } }
@Override public String toString() { return MoreObjects.toStringHelper(ProviderInstanceBinding.class) .add("key", getKey()) .add("source", getSource()) .add("scope", getScoping()) .add("provider", providerInstance) .toString(); }
@SuppressWarnings("unchecked") @Override public Object acceptExtensionVisitor( BindingTargetVisitor visitor, ProviderInstanceBinding binding) { if (visitor instanceof MultibindingsTargetVisitor) { return ((MultibindingsTargetVisitor) visitor).visit(this); } else { return visitor.visit(binding); } }
@SuppressWarnings("unchecked") @Override public <B, R> R acceptExtensionVisitor( BindingTargetVisitor<B, R> visitor, ProviderInstanceBinding<? extends B> binding) { if (visitor instanceof MultibindingsTargetVisitor) { return ((MultibindingsTargetVisitor<Optional<T>, R>) visitor).visit(this); } else { return visitor.visit(binding); } }
@Override @SuppressWarnings("unchecked") public <B, V> V acceptExtensionVisitor( BindingTargetVisitor<B, V> visitor, ProviderInstanceBinding<? extends B> binding) { if (visitor instanceof ProvidesMethodTargetVisitor) { return ((ProvidesMethodTargetVisitor<T, V>) visitor).visit(this); } return visitor.visit(binding); }
@Override @SuppressWarnings("unchecked") public <B, W> W acceptExtensionVisitor( BindingTargetVisitor<B, W> visitor, ProviderInstanceBinding<? extends B> binding) { if (visitor instanceof MultibindingsTargetVisitor) { return ((MultibindingsTargetVisitor<Map<K, V>, W>) visitor).visit(this); } else { return visitor.visit(binding); } }
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; }
/** * Same as {@link #visit(InstanceBinding)}, but the binding edge is * {@link BindingEdge.Type#PROVIDER}. */ @Override public Collection<Edge> visit(ProviderInstanceBinding<?> binding) { return new ImmutableList.Builder<Edge>() .add(new BindingEdge(NodeId.newTypeId(binding.getKey()), NodeId.newInstanceId(binding.getKey()), BindingEdge.Type.PROVIDER)) .addAll(newDependencyEdges(NodeId.newInstanceId(binding.getKey()), binding)) .build(); }
public void testVisitProviderInstance() { Binding<?> binding = getBinding(Key.get(ConstructedClass.class), new ProviderInstanceModule()); Collection<Key<?>> dependencies = visitor.visit( (ProviderInstanceBinding<?>) binding); // Dependencies will only be on the field- and method-injected classes. assertDependencies(dependencies, Key.get(E.class), Key.get(F.class)); }
@SuppressWarnings("unchecked") public <T, V> V acceptExtensionVisitor(BindingTargetVisitor<T, V> visitor, ProviderInstanceBinding<? extends T> binding) { if (visitor instanceof AssistedInjectTargetVisitor) { return ((AssistedInjectTargetVisitor<T, V>)visitor).visit((AssistedInjectBinding<T>)this); } return visitor.visit(binding); }
@SuppressWarnings("unchecked") public <B, V> V acceptExtensionVisitor( BindingTargetVisitor<B, V> visitor, ProviderInstanceBinding<? extends B> binding) { if (visitor instanceof MultibindingsTargetVisitor) { return ((MultibindingsTargetVisitor<Set<T>, V>) visitor).visit(this); } else { return visitor.visit(binding); } }
@SuppressWarnings("unchecked") public <B, R> R acceptExtensionVisitor(BindingTargetVisitor<B, R> visitor, ProviderInstanceBinding<? extends B> binding) { if (visitor instanceof MultibindingsTargetVisitor) { return ((MultibindingsTargetVisitor<Optional<T>, R>) visitor).visit(this); } else { return visitor.visit(binding); } }
/** Returns true if the key & element indicate they were bound by this OptionalBinder. */ private boolean matchesTypeKey(com.google.inject.spi.Element element, Key<?> elementKey) { // Just doing .equals(typeKey) isn't enough, because the user can bind that themselves. return elementKey.equals(typeKey) && element instanceof ProviderInstanceBinding && (((ProviderInstanceBinding) element) .getUserSuppliedProvider() instanceof RealOptionalBinderProviderWithDependencies); }
private static boolean isSourceEntry(Binding b, Source type) { if (b instanceof ProviderInstanceBinding && b.getKey().getAnnotation() instanceof RealElement) { javax.inject.Provider provider = ((ProviderInstanceBinding) b).getUserSuppliedProvider(); if (provider instanceof Map.Entry) { Map.Entry entry = (Map.Entry) provider; if (entry.getKey() == type) { return true; } } } return false; }