@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; }
@SuppressWarnings("unchecked") // Delegate is of type T @Override public T get() { if (cacheValue == NOT_PRESENT) { synchronized (this) { if (cacheValue == NOT_PRESENT) { cacheValue = delegate.get(); } } } return (T) cacheValue; } };
@Override public void inject() { try { for (int f = 0; f < fields.length; f++) { fields[f].set(null, bindings[f].get()); } } catch (IllegalAccessException e) { throw new AssertionError(e); } }
@SuppressWarnings("unchecked") // Only Binding<T> and Set<T> are added to contributors. @Override public Set<T> get() { List<T> result = new ArrayList<T>(); for (SetBinding<T> setBinding = this; setBinding != null; setBinding = setBinding.parent) { for (int i = 0, size = setBinding.contributors.size(); i < size; i++) { Binding<?> contributor = setBinding.contributors.get(i); Object contribution = contributor.get(); // Let runtime exceptions through. if (contributor.provideKey.equals(provideKey)) { result.addAll((Set<T>) contribution); } else { result.add((T) contribution); } } } return Collections.unmodifiableSet(new LinkedHashSet<T>(result)); }
@Override public void injectMembers(T t) { try { for (int i = 0; i < fields.length; i++) { fields[i].set(t, fieldBindings[i].get()); } if (supertypeBinding != null) { supertypeBinding.injectMembers(t); } } catch (IllegalAccessException e) { throw new AssertionError(e); } }
@Override public T get() { if (constructor == null) { throw new UnsupportedOperationException(); } Object[] args = new Object[parameterBindings.length]; for (int i = 0; i < parameterBindings.length; i++) { args[i] = parameterBindings[i].get(); } T result; try { result = constructor.newInstance(args); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); throw cause instanceof RuntimeException ? (RuntimeException) cause : new RuntimeException(cause); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InstantiationException e) { throw new RuntimeException(e); } injectMembers(result); return result; }
@Override public <T> T get(Class<T> type) { String key = Keys.get(type); String injectableTypeKey = type.isInterface() ? key : Keys.getMembersKey(type); ClassLoader classLoader = type.getClassLoader(); @SuppressWarnings("unchecked") // The linker matches keys to bindings by their type. Binding<T> binding = (Binding<T>) getInjectableTypeBinding(classLoader, injectableTypeKey, key); return binding.get(); }
@Override public T get() { Object[] args = new Object[parameters.length]; for (int i = 0; i < parameters.length; i++) { args[i] = parameters[i].get(); } try { return (T) method.invoke(instance, args); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); throw cause instanceof RuntimeException ? (RuntimeException) cause : new RuntimeException(cause); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
@SuppressWarnings("unchecked") // Delegate is of type T @Override public T get() { return (T) ((cacheValue != NOT_PRESENT) ? cacheValue : (cacheValue = delegate.get())); } };
@SuppressWarnings("unchecked") // onlyInstance is either 'UNINITIALIZED' or a 'T'. @Override public T get() { // TODO (cgruber): Fix concurrency risk. if (onlyInstance == UNINITIALIZED) { onlyInstance = binding.get(); } return (T) onlyInstance; }
@Override public void inject() { try { for (int f = 0; f < fields.length; f++) { fields[f].set(null, bindings[f].get()); } } catch (IllegalAccessException e) { throw new AssertionError(e); } } }
@SuppressWarnings("unchecked") // Bindings<T> are the only thing added to contributors. @Override public Set<T> get() { Set<T> result = new LinkedHashSet<T>(contributors.size()); for (Binding<?> contributor : contributors) { result.add((T) contributor.get()); // Let runtime exceptions through. } return Collections.unmodifiableSet(result); }
@Override public void injectMembers(T t) { try { for (int i = 0; i < fields.length; i++) { fields[i].set(t, fieldBindings[i].get()); } if (supertypeBinding != null) { supertypeBinding.injectMembers(t); } } catch (IllegalAccessException e) { throw new AssertionError(e); } }
@SuppressWarnings("unchecked") // We defined 'T' in terms of the method's return type. @Override public T get() { Object[] args = new Object[parameters.length]; for (int i = 0; i < parameters.length; i++) { args[i] = parameters[i].get(); } try { return (T) method.invoke(instance, args); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); throw cause instanceof RuntimeException ? (RuntimeException) cause : new RuntimeException(cause); } catch (IllegalAccessException e) { throw new RuntimeException(e); } }
@Override public T get() { if (constructor == null) { throw new UnsupportedOperationException(); } Object[] args = new Object[parameterBindings.length]; for (int i = 0; i < parameterBindings.length; i++) { args[i] = parameterBindings[i].get(); } T result; try { result = constructor.newInstance(args); } catch (InvocationTargetException e) { Throwable cause = e.getCause(); throw cause instanceof RuntimeException ? (RuntimeException) cause : new RuntimeException(cause); } catch (IllegalAccessException e) { throw new AssertionError(e); } catch (InstantiationException e) { throw new RuntimeException(e); } injectMembers(result); return result; }
/** * Returns an instance of {@code type}. * * @throws IllegalArgumentException if {@code type} is not one of this object * graph's entry point types. */ public <T> T get(Class<T> type) { String key = Keys.get(type); String entryPointKey = Keys.getMembersKey(type); @SuppressWarnings("unchecked") // The linker matches keys to bindings by their type. Binding<T> binding = (Binding<T>) getEntryPointBinding(entryPointKey, key); return binding.get(); }