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; }
@Override public <T> Void visit(Binding<T> binding) { final Key<T> key = binding.getKey(); if (!keysToIntercept.contains(key)) { return super.visit(binding); } binding.acceptTargetVisitor(new DefaultBindingTargetVisitor<T, Void>() { @Override public Void visit(UntargettedBinding<? extends T> untargettedBinding) { binder.addError("Cannot intercept bare binding of %s. " + "You may only intercept bindings that bind a class to something.", key); return null; } }); Key<T> anonymousKey = Key.get(key.getTypeLiteral(), UniqueAnnotations.create()); binder.bind(key).toProvider(new InterceptingProvider<T>(key, binder.getProvider(anonymousKey))); ScopedBindingBuilder scopedBindingBuilder = bindKeyToTarget(binding, binder, anonymousKey); // we scope the user's provider, not the interceptor. This is dangerous, // but convenient. It means that although the user's provider will live // in its proper scope, the intereptor gets invoked without a scope applyScoping(binding, scopedBindingBuilder); keysIntercepted.add(key); return null; }
@Override public <T> Void visit(Binding<T> binding) { final Key<T> key = binding.getKey(); if (!keysToIntercept.contains(key)) { return super.visit(binding); } binding.acceptTargetVisitor(new DefaultBindingTargetVisitor<T, Void>() { @Override public Void visit(UntargettedBinding<? extends T> untargettedBinding) { binder.addError("Cannot intercept bare binding of %s. " + "You may only intercept bindings that bind a class to something.", key); return null; } }); Key<T> anonymousKey = Key.get(key.getTypeLiteral(), UniqueAnnotations.create()); binder.bind(key).toProvider(new InterceptingProvider<T>(key, binder.getProvider(anonymousKey))); ScopedBindingBuilder scopedBindingBuilder = bindKeyToTarget(binding, binder, anonymousKey); // we scope the user's provider, not the interceptor. This is dangerous, // but convenient. It means that although the user's provider will live // in its proper scope, the interceptor gets invoked without a scope applyScoping(binding, scopedBindingBuilder); keysIntercepted.add(key); return null; }
@Override public String toString() { return MoreObjects.toStringHelper(UntargettedBinding.class) .add("key", getKey()) .add("source", getSource()) .toString(); }
@Override public Optional<TypeLiteral<?>> visit(UntargettedBinding<?> binding) { // Return the key type return of(binding.getKey().getTypeLiteral()); }
@Override public Object visit(UntargettedBinding<? extends T> untargettedBinding) { processInjections(untargettedBinding.getKey().getTypeLiteral()); return super.visit(untargettedBinding); }
@Override public Void visit(UntargettedBinding<? extends T> binding) { scope(binding, rebind(binding)); return null; }
@Override public Void visit(UntargettedBinding<? extends Object> untargettedBinding) { analyzeImplementation(untargettedBinding.getKey().getTypeLiteral(), false); return null; }
public Void visit(UntargettedBinding<? extends T> untargettedBinding) { addImplicitBinding(untargettedBinding); return null; }
@Override public Indexer.IndexedBinding visit(UntargettedBinding<? extends Object> binding) { return new Indexer.IndexedBinding(binding, BindingType.UNTARGETTED, scope(binding), null); }
@Override public String toString() { return Objects.toStringHelper(UntargettedBinding.class) .add("key", getKey()) .add("source", getSource()) .toString(); }