public <ThrowableType extends Throwable> void visitModel(ModelVisitor<ThrowableType> modelVisitor) throws ThrowableType { if (boundConstructors != null) { for (ConstructorModel constructorModel : boundConstructors) { constructorModel.visitModel(modelVisitor); } } else { for (ConstructorModel constructorModel : constructorModels) { constructorModel.visitModel(modelVisitor); } } }
public Object newInstance(InjectionContext injectionContext) { // Try all bound constructors, in order ConstructionException exception = null; for (ConstructorModel constructorModel : boundConstructors) { try { return constructorModel.newInstance(injectionContext); } catch (ConstructionException e) { exception = e; } } throw exception; }
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); } }); }