Search in sources :

Example 1 with ConstructorParameter

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()])));
}
Also used : ConstructorParameter(org.apache.sling.models.impl.model.ConstructorParameter) ArrayList(java.util.ArrayList) MissingElementsException(org.apache.sling.models.factory.MissingElementsException) MissingElementException(org.apache.sling.models.factory.MissingElementException)

Example 2 with ConstructorParameter

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);
}
Also used : ConstructorParameter(org.apache.sling.models.impl.model.ConstructorParameter) Annotation(java.lang.annotation.Annotation) StaticInjectAnnotationProcessorFactory(org.apache.sling.models.spi.injectorspecific.StaticInjectAnnotationProcessorFactory) Before(org.junit.Before)

Aggregations

ConstructorParameter (org.apache.sling.models.impl.model.ConstructorParameter)2 Annotation (java.lang.annotation.Annotation)1 ArrayList (java.util.ArrayList)1 MissingElementException (org.apache.sling.models.factory.MissingElementException)1 MissingElementsException (org.apache.sling.models.factory.MissingElementsException)1 StaticInjectAnnotationProcessorFactory (org.apache.sling.models.spi.injectorspecific.StaticInjectAnnotationProcessorFactory)1 Before (org.junit.Before)1