public Object inject(InjectionContext context) { if (injectionProvider == null) { return null; } Object injectedValue; try { injectedValue = injectionProvider.provideInjection(context); } catch (InjectionProviderException e) { Throwable ex = e; if (ex.getCause() != null) { ex = ex.getCause(); } String message = "InjectionProvider unable to resolve @" + injectionAnnotation.annotationType().getSimpleName() + " " + injectionType.toString(); throw new ConstructionException(message, ex); } if (injectedValue == null && !optional) { String message = "Non-optional @" + injectionAnnotation.annotationType().getSimpleName() + " " + injectionType.toString() + " was null in " + injectedClass.getName(); throw new ConstructionException(message); } return getInjectedValue(injectedValue); }
public void bind(Resolution resolution) throws BindingException { InjectionProviderFactory providerFactory = resolution.application().injectionProviderFactory(); try { injectionProvider = providerFactory.newInjectionProvider(resolution, this); if (injectionProvider == null && !optional) { String message = "Non-optional @" + rawInjectionClass.getName() + " was not bound in " + injectedClass.getName(); throw new ConstructionException(message); } } catch (InvalidInjectionException e) { throw new BindingException("Could not bind dependency injection", e); } }
private Class<?> mapPrimitiveTypes(Class<?> rawInjectionType) { if (rawInjectionType == null || !rawInjectionType.isPrimitive()) { return rawInjectionType; } for (int i = 0; i < primitiveTypeMapping.length; i += 2) { if (primitiveTypeMapping[i].equals(rawInjectionType)) { return primitiveTypeMapping[i + 1]; } } return rawInjectionType; }
private Annotation[][] getConstrucxtorAnnotations(Class fragmentClass, Constructor constructor) { Annotation[][] parameterAnnotations; if (fragmentClass.getName().endsWith("_Stub")) { try { Class[] constructorParameterTypes = constructor.getParameterTypes(); Class fragmentSuperClass = fragmentClass.getSuperclass(); Constructor realConstructor = fragmentSuperClass.getDeclaredConstructor(constructorParameterTypes); parameterAnnotations = realConstructor.getParameterAnnotations(); } catch (NoSuchMethodException e) { // Shouldn't happen throw new InternalError( "Could not get real constructor of class " + fragmentClass.getName()); } } else { parameterAnnotations = constructor.getParameterAnnotations(); } return parameterAnnotations; }
public ConstructorsModel(Class fragmentClass) { this.fragmentClass = fragmentClass; constructorModels = new ArrayList<ConstructorModel>(); Constructor[] realConstructors = this.fragmentClass.getDeclaredConstructors(); Class injectionClass = fragmentClass.getName().endsWith("_Stub") ? fragmentClass.getSuperclass() : this.fragmentClass; for (int i = 0; i < realConstructors.length; i++) { Constructor constructor = realConstructors[i]; try { Constructor injectionConstructor = injectionClass.getConstructor(constructor.getParameterTypes()); ConstructorModel constructorModel = newConstructorModel(this.fragmentClass, constructor, injectionConstructor); if (constructorModel != null) { constructorModels.add(constructorModel); } } catch (NoSuchMethodException e) { // Ignore and continue } } }
private Object getInjectedValue(Object injectionResult) { if (injectionResult == null) { return null; } if (injectionResult instanceof Iterable) { if (Iterable.class.isAssignableFrom(rawInjectionClass) || rawInjectionClass.isInstance(injectionResult)) { return injectionResult; } else { return firstElementOrNull((Iterable) injectionResult); } } else { if (Iterable.class.equals(injectionType)) { return Collections.singleton(injectionResult); } } return injectionResult; }
public void bind(Resolution resolution) throws BindingException { boundConstructors = new ArrayList<ConstructorModel>(); for (ConstructorModel constructorModel : constructorModels) { try { constructorModel.bind(resolution); boundConstructors.add(constructorModel); } catch (Exception e) { // Ignore e.printStackTrace(); } } if (boundConstructors.size() == 0) { StringBuilder messageBuilder = new StringBuilder("Found no constructor that could be bound: "); if (resolution.object() instanceof AbstractCompositeDescriptor) { messageBuilder .append(fragmentClass.getName()) .append(" in ") .append(resolution.object().toString()); } else { messageBuilder.append(resolution.object().toString()); } if (messageBuilder.indexOf("$") >= 0) { messageBuilder.append("\nNon-static inner classes can not be used."); } String message = messageBuilder.toString(); throw new BindingException(message); } // Sort based on parameter count Collections.sort( boundConstructors, new Comparator<ConstructorModel>() { public int compare(ConstructorModel o1, ConstructorModel o2) { Integer model2ParametersCount = o2.constructor().getParameterTypes().length; int model1ParametersCount = o1.constructor().getParameterTypes().length; return model2ParametersCount.compareTo(model1ParametersCount); } }); }
@Override public String toString() { return injectionAnnotation + " for " + injectionType + " in " + injectedClass.getName(); }
public boolean hasScope(final Class<? extends Annotation> scope) { return scope == null || scope.equals(injectionAnnotation().annotationType()); }