/** * Returns true if this builder contains a mapping for the given type and * name. */ public boolean contains(Class<?> type, String name) { return factories.containsKey(Key.newInstance(type, name)); }
/** * Constructs a new builder. */ public ContainerBuilder() { // In the current container as the default Container implementation. factories.put(Key.newInstance(Container.class, Container.DEFAULT_NAME), CONTAINER_FACTORY); // Inject the logger for the injected member's declaring class. factories.put(Key.newInstance(Logger.class, Container.DEFAULT_NAME), LOGGER_FACTORY); }
/** * Maps an existing factory to a new name. * * @param type of dependency * @param name of dependency * @param alias of to the dependency * @return this builder */ public <T> ContainerBuilder alias(Class<T> type, String name, String alias) { return alias(Key.newInstance(type, name), Key.newInstance(type, alias)); }
/** * Maps a constant value to the given type and name. */ private <T> ContainerBuilder constant(final Class<T> type, final String name, final T value) { InternalFactory<T> factory = new InternalFactory<T>() { public T create(InternalContext ignored) { return value; } @Override public String toString() { return new LinkedHashMap<String, Object>() { { put("type", type); put("name", name); put("value", value); } }.toString(); } }; return factory(Key.newInstance(type, name), factory, Scope.DEFAULT); }
return factory(Key.newInstance(type, name), factory, scope);
return factory(Key.newInstance(type, name), internalFactory, scope);
public FieldInjector( ContainerImpl container, Field field, String name ) throws MissingDependencyException { this.field = field; if (!field.isAccessible()) { SecurityManager sm = System.getSecurityManager(); try { if (sm != null) { sm.checkPermission(new ReflectPermission("suppressAccessChecks")); } field.setAccessible(true); } catch ( AccessControlException e ) { throw new DependencyException("Security manager in use, could not access field: " + field.getDeclaringClass().getName() + "(" + field.getName() + ")", e); } } Key<?> key = Key.newInstance(field.getType(), name); factory = container.getFactory(key); if (factory == null) { throw new MissingDependencyException( "No mapping found for dependency " + key + " in " + field + "."); } this.externalContext = ExternalContext.newInstance(field, key, container); }
/** * Gets parameter injectors. * * @param member to which the parameters belong * @param annotations on the parameters * @param parameterTypes parameter types * * @return injections */ <M extends AccessibleObject & Member> ParameterInjector<?>[] getParametersInjectors( M member, Annotation[][] annotations, Class[] parameterTypes, String defaultName ) throws MissingDependencyException { List<ParameterInjector<?>> parameterInjectors = new ArrayList<ParameterInjector<?>>(); Iterator<Annotation[]> annotationsIterator = Arrays.asList(annotations).iterator(); for ( Class<?> parameterType : parameterTypes ) { Inject annotation = findInject(annotationsIterator.next()); String name = annotation == null ? defaultName : annotation.value(); Key<?> key = Key.newInstance(parameterType, name); parameterInjectors.add(createParameterInjector(key, member)); } return toArray(parameterInjectors); }
@SuppressWarnings("unchecked") <T> T getInstance( Class<T> type, String name, InternalContext context ) { ExternalContext<?> previous = context.getExternalContext(); Key<T> key = Key.newInstance(type, name); context.setExternalContext(ExternalContext.newInstance(null, key, this)); try { InternalFactory o = getFactory(key); if (o != null) { return getFactory(key).create(context); } else { return null; } } finally { context.setExternalContext(previous); } }