/** * Returns a scoped binding for {@code binding}. */ static <T> Binding<T> scope(final Binding<T> binding) { if (!binding.isSingleton() || binding instanceof SingletonBinding) { return binding; // Default scoped binding or already a scoped binding. } return new SingletonBinding<T>(binding); }
protected Binding(String provideKey, String membersKey, boolean singleton, Object requiredBy) { if (singleton && provideKey == null) { throw new InvalidBindingException(Keys.getClassName(membersKey), "is exclusively members injected and therefore cannot be scoped"); } this.provideKey = provideKey; this.membersKey = membersKey; this.requiredBy = requiredBy; this.bits = (singleton ? SINGLETON : 0); }
/** * Obtains a module adapter for {@code module} from the first responding resolver. */ @SuppressWarnings("unchecked") // cache ensures types match @Override public <T> ModuleAdapter<T> getModuleAdapter(Class<T> type) { return (ModuleAdapter<T>) loadedAdapters.get(type); }
@Override public void attach(Linker linker) { bindings = new Binding<?>[fields.length]; for (int i = 0; i < fields.length; i++) { Field field = fields[i]; String key = Keys.get(field.getGenericType(), field.getAnnotations(), field); bindings[i] = linker.requestBinding(key, field, loader); } }
@SuppressWarnings("unchecked") // At runtime we know it's a Binding<Lazy<T>>. @Override public void attach(Linker linker) { delegate = (Binding<T>) linker.requestBinding(lazyKey, requiredBy, loader); }
/** * Returns a key for {@code type} annotated with {@code annotations}, * reporting failures against {@code subject}. * * @param annotations the annotations on a single method, field or parameter. * This array may contain at most one qualifier annotation. */ public static String get(Type type, Annotation[] annotations, Object subject) { return get(type, extractQualifier(annotations, subject)); }
private void handleSetBindings(BindingsGroup bindings, M module, Method method, String setKey, String providerKey, boolean library) { SetBinding.<M>add(bindings, setKey, new ReflectiveProvidesBinding<M>( method, providerKey, moduleClass.getName(), module, library)); }
@Override public String toString() { return "@Singleton/" + binding.toString(); } }
@SuppressWarnings("unchecked") // onlyInstance is either 'UNINITIALIZED' or a 'T'. @Override public T get() { if (onlyInstance == UNINITIALIZED) { synchronized (this) { if (onlyInstance == UNINITIALIZED) { onlyInstance = binding.get(); } } } return (T) onlyInstance; }
private void handleBindings(BindingsGroup bindings, M module, Method method, String key, boolean library) { bindings.contributeProvidesBinding(key, new ReflectiveProvidesBinding<M>(method, key, moduleClass.getName(), module, library)); }
@Override public void attach(Linker linker) { for (Binding<?> contributor : contributors) { contributor.attach(linker); } }
@Override public Binding<?> contributeSetBinding(String key, SetBinding<?> value) { return super.put(key, value); } };
private <T> void putBinding(final Binding<T> binding) { // At binding insertion time it's possible that another binding for the same // key to already exist. This occurs when an @Provides method returns a type T // and we also inject the members of that type. if (binding.provideKey != null) { putIfAbsent(bindings, binding.provideKey, binding); } if (binding.membersKey != null) { putIfAbsent(bindings, binding.membersKey, binding); } }
/** * Constructs an empty array queue with an initial capacity * sufficient to hold the specified number of elements. * * @param numElements lower bound on initial capacity of the queue */ public ArrayQueue(int numElements) { allocateElements(numElements); }
/** * Returns an iterator over the elements in this queue. The elements * will be ordered from first (head) to last (tail). This is the same * order that elements would be queueued (via successive calls to * {@link #remove} or popped (via successive calls to {@link #pop}). * * @return an iterator over the elements in this queue */ @Override public Iterator<E> iterator() { return new QueueIterator(); }
@Override protected Memoizer<String, Class<?>> create(final ClassLoader classLoader) { return new Memoizer<String, Class<?>>() { @Override protected Class<?> create(String className) { try { return classLoader.loadClass(className); } catch (ClassNotFoundException e) { return Void.class; // Cache the failure (negative case). } } }; } };
/** * Inserts the specified element at the end of this queue. * * @param e the element to add * @return <tt>true</tt> (as specified by {@link Queue#offer}) * @throws NullPointerException if the specified element is null */ @Override public boolean offer(E e) { return add(e); }
@Override public <T> ModuleAdapter<T> getModuleAdapter(Class<T> type) { ModuleAdapter<T> adapter = TestingModuleAdapter.create(type); return adapter; }
/** * Returns the binding if it exists immediately. Otherwise this returns * null. If the returned binding didn't exist or was unlinked, it will be * enqueued to be linked. */ public Binding<?> requestBinding(String key, Object requiredBy, ClassLoader classLoader) { return requestBinding(key, requiredBy, classLoader, true, true); }
private String getStringForBinding(Binding<?> binding) { return binding == null ? "" : binding.toString(); } }