/** * Ensures a key isn't already mapped. */ private void checkKey(Key<?> key) { if (factories.containsKey(key) && !allowDuplicates) { throw new DependencyException( "Dependency mapping for " + key + " already exists."); } }
@SuppressWarnings("unchecked") private Constructor<T> findConstructorIn( Class<T> implementation ) { Constructor<T> found = null; Constructor<T>[] declaredConstructors = (Constructor<T>[]) implementation .getDeclaredConstructors(); for ( Constructor<T> constructor : declaredConstructors ) { if (constructor.getAnnotation(Inject.class) != null) { if (found != null) { throw new DependencyException("More than one constructor annotated" + " with @Inject found in " + implementation + "."); } found = constructor; } } if (found != null) { return found; } // If no annotated constructor is found, look for a no-arg constructor // instead. try { return implementation.getDeclaredConstructor(); } catch ( NoSuchMethodException e ) { throw new DependencyException("Could not find a suitable constructor" + " in " + implementation.getName() + "."); } }
Object createProxy(Class<? super T> expectedType) { // TODO: if I create a proxy which implements all the interfaces of // the implementation type, I'll be able to get away with one proxy // instance (as opposed to one per caller). if (!expectedType.isInterface()) { throw new DependencyException( expectedType.getName() + " is not an interface."); } if (invocationHandlers == null) { invocationHandlers = new ArrayList<DelegatingInvocationHandler<T>>(); } DelegatingInvocationHandler<T> invocationHandler = new DelegatingInvocationHandler<T>(); invocationHandlers.add(invocationHandler); return Proxy.newProxyInstance( expectedType.getClassLoader(), new Class[] { expectedType }, invocationHandler ); }
public MethodInjector( ContainerImpl container, Method method, String name ) throws MissingDependencyException { this.method = method; if (!method.isAccessible()) { SecurityManager sm = System.getSecurityManager(); try { if (sm != null) { sm.checkPermission(new ReflectPermission("suppressAccessChecks")); } method.setAccessible(true); } catch ( AccessControlException e ) { throw new DependencyException("Security manager in use, could not access method: " + name + "(" + method.getName() + ")", e); } } Class<?>[] parameterTypes = method.getParameterTypes(); if (parameterTypes.length == 0) { throw new DependencyException( method + " has no parameters to inject."); } parameterInjectors = container.getParametersInjectors( method, method.getParameterAnnotations(), parameterTypes, name); }
/** * Maps an existing dependency. All methods in this class ultimately funnel through * here. */ private <T> ContainerBuilder alias(final Key<T> key, final Key<T> aliasKey) { ensureNotCreated(); checkKey(aliasKey); final InternalFactory<? extends T> scopedFactory = (InternalFactory<? extends T>)factories.get(key); if (scopedFactory == null) { throw new DependencyException( "Dependency mapping for " + key + " doesn't exists."); } factories.put(aliasKey, scopedFactory); return this; }
throw new DependencyException("Security manager in use, could not access constructor: " + implementation.getName() + "(" + constructor.getName() + ")", e); throw new DependencyException(exception);
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); }
<M extends Member & AnnotatedElement> void addInjectorsForMembers( List<M> members, boolean statics, List<Injector> injectors, InjectorFactory<M> injectorFactory ) { for ( M member : members ) { if (isStatic(member) == statics) { Inject inject = member.getAnnotation(Inject.class); if (inject != null) { try { injectors.add(injectorFactory.create(this, member, inject.value())); } catch ( MissingDependencyException e ) { if (inject.required()) { throw new DependencyException(e); } } } } } }