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 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 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); } }); }
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 } } }
@Override public String toString() { return injectionAnnotation + " for " + injectionType + " in " + injectedClass.getName(); }