use of org.apache.sling.models.impl.model.ConstructorParameter in project sling by apache.
the class ModelAdapterFactory method newInstanceWithConstructorInjection.
private <ModelType> Result<ModelType> newInstanceWithConstructorInjection(final ModelClassConstructor<ModelType> constructor, final Object adaptable, final ModelClass<ModelType> modelClass, final DisposalCallbackRegistry registry, @Nonnull final Map<ValuePreparer, Object> preparedValues) throws InstantiationException, InvocationTargetException, IllegalAccessException {
ConstructorParameter[] parameters = constructor.getConstructorParameters();
List<Object> paramValues = new ArrayList<Object>(Arrays.asList(new Object[parameters.length]));
InjectCallback callback = new SetConstructorParameterCallback(paramValues);
MissingElementsException missingElements = new MissingElementsException("Required constructor parameters were not able to be injected on model " + modelClass.getType());
for (int i = 0; i < parameters.length; i++) {
RuntimeException t = injectElement(parameters[i], adaptable, registry, callback, preparedValues);
if (t != null) {
missingElements.addMissingElementExceptions(new MissingElementException(parameters[i].getAnnotatedElement(), t));
}
}
if (!missingElements.isEmpty()) {
return new Result<ModelType>(missingElements);
}
return new Result<ModelType>(constructor.getConstructor().newInstance(paramValues.toArray(new Object[paramValues.size()])));
}
use of org.apache.sling.models.impl.model.ConstructorParameter in project sling by apache.
the class SelfInjectorTest method setup.
@Before
public void setup() {
when(modelAnnotation.defaultInjectionStrategy()).thenReturn(DefaultInjectionStrategy.REQUIRED);
firstConstructorParameter = new ConstructorParameter(new Annotation[0], Object.class, Object.class, true, 0, new StaticInjectAnnotationProcessorFactory[0], null);
secondConstructorParameter = new ConstructorParameter(new Annotation[0], Object.class, Object.class, true, 1, new StaticInjectAnnotationProcessorFactory[0], null);
}
Aggregations