protected ContainerImpl create(Map<String, Object> m){ ContainerImpl impl = new ContainerImpl(); //default constructor, instantiates internal map impl.m.putAll(m); // copy all values return impl; }
/** * Recursively adds injectors for fields and methods from the given class to the given list. Injects parent classes * before sub classes. */ void addInjectors( Class clazz, List<Injector> injectors ) { if (clazz == Object.class) { return; } // Add injectors for superclass first. addInjectors(clazz.getSuperclass(), injectors); // TODO (crazybob): Filter out overridden members. addInjectorsForFields(clazz.getDeclaredFields(), false, injectors); addInjectorsForMethods(clazz.getDeclaredMethods(), false, injectors); }
void addInjectorsForMethods( Method[] methods, boolean statics, List<Injector> injectors ) { addInjectorsForMembers(Arrays.asList(methods), statics, injectors, new InjectorFactory<Method>() { public Injector create( ContainerImpl container, Method method, String name ) throws MissingDependencyException { return new MethodInjector(container, method, name); } }); }
void injectStatics( List<Class<?>> staticInjections ) { final List<Injector> injectors = new ArrayList<Injector>(); for ( Class<?> clazz : staticInjections ) { addInjectorsForFields(clazz.getDeclaredFields(), true, injectors); addInjectorsForMethods(clazz.getDeclaredMethods(), true, injectors); } callInContext(new ContextualCallable<Void>() { public Void call( InternalContext context ) { for ( Injector injector : injectors ) { injector.inject(context, null); } return null; } }); }
/** * Creates a {@link Container} instance. Injects static members for classes * which were registered using {@link #injectStatics(Class...)}. * * @param loadSingletons If true, the container will load all singletons * now. If false, the container will lazily load singletons. Eager loading * is appropriate for production use while lazy loading can speed * development. * @throws IllegalStateException if called more than once */ public Container create(boolean loadSingletons) { ensureNotCreated(); created = true; final ContainerImpl container = new ContainerImpl( new HashMap<Key<?>, InternalFactory<?>>(factories)); if (loadSingletons) { container.callInContext(new ContainerImpl.ContextualCallable<Void>() { public Void call(InternalContext context) { for (InternalFactory<?> factory : singletonFactories) { factory.create(context); } return null; } }); } container.injectStatics(staticInjections); return 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); }
public <T> T inject( final Class<T> implementation ) { return callInContext(new ContextualCallable<T>() { public T call( InternalContext context ) { return inject(implementation, context); } }); }
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); }
<T> T inject( Class<T> implementation, InternalContext context ) { try { ConstructorInjector<T> constructor = getConstructor(implementation); return implementation.cast( constructor.construct(context, implementation)); } catch ( Exception e ) { throw new RuntimeException(e); } }
@Override protected List<Injector> create( Class<?> key ) { List<Injector> injectors = new ArrayList<Injector>(); addInjectors(key, injectors); return injectors; } };
public <T> T getInstance( final Class<T> type, final String name ) { return callInContext(new ContextualCallable<T>() { public T call( InternalContext context ) { return getInstance(type, name, context); } }); }
<T> ParameterInjector<T> createParameterInjector( Key<T> key, Member member ) throws MissingDependencyException { InternalFactory<? extends T> factory = getFactory(key); if (factory == null) { throw new MissingDependencyException( "No mapping found for dependency " + key + " in " + member + "."); } ExternalContext<T> externalContext = ExternalContext.newInstance(member, key, this); return new ParameterInjector<T>(externalContext, factory); }
@SuppressWarnings("unchecked") public T create(InternalContext context) { if (constructor == null) { this.constructor = context.getContainerImpl().getConstructor(implementation); } return (T) constructor.construct(context, type); }
/** * @param m - storage model. Should not be modified after call. */ public ContainerImpl(Map<String, Object> m){ this.m = m; } /** A new instance with default storage model */ public static ContainerImpl createDefault() { // Storage reference is isolated return new ContainerImpl(new HashMap<>()); }
public <T> T getInstance( final Class<T> type ) { return callInContext(new ContextualCallable<T>() { public T call( InternalContext context ) { return getInstance(type, context); } }); }
@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); } }
void addInjectorsForFields( Field[] fields, boolean statics, List<Injector> injectors ) { addInjectorsForMembers(Arrays.asList(fields), statics, injectors, new InjectorFactory<Field>() { public Injector create( ContainerImpl container, Field field, String name ) throws MissingDependencyException { return new FieldInjector(container, field, name); } }); }
public void inject( final Object o ) { callInContext(new ContextualCallable<Void>() { public Void call( InternalContext context ) { inject(o, context); return null; } }); }