Search in sources :

Example 6 with ModelClassException

use of org.apache.sling.models.factory.ModelClassException in project sling by apache.

the class ImplementsExtendsTest method tearDown.

@After
public void tearDown() {
    // simulate bundle remove for ModelPackageBundleListener
    factory.listener.removedBundle(bundle, bundleEvent, registeredAdapterFactories);
    // make sure adaption is not longer possible: implementation class mapping is removed
    Resource res = getMockResourceWithProps();
    try {
        factory.getAdapter(res, SampleServiceInterface.class);
        Assert.fail("Getting the model for interface 'SampleServiceInterface' should fail after the accroding adapter factory has been unregistered");
    } catch (ModelClassException e) {
    }
}
Also used : ModelClassException(org.apache.sling.models.factory.ModelClassException) Resource(org.apache.sling.api.resource.Resource) After(org.junit.After)

Example 7 with ModelClassException

use of org.apache.sling.models.factory.ModelClassException in project sling by apache.

the class ModelAdapterFactory method createObject.

private <ModelType> Result<ModelType> createObject(final Object adaptable, final ModelClass<ModelType> modelClass) throws InstantiationException, InvocationTargetException, IllegalAccessException {
    DisposalCallbackRegistryImpl registry = new DisposalCallbackRegistryImpl();
    ModelClassConstructor<ModelType> constructorToUse = getBestMatchingConstructor(adaptable, modelClass);
    if (constructorToUse == null) {
        return new Result<ModelType>(new ModelClassException("Unable to find a useable constructor for model " + modelClass.getType()));
    }
    final Map<ValuePreparer, Object> preparedValues = new HashMap<ValuePreparer, Object>(VALUE_PREPARERS_COUNT);
    final ModelType object;
    if (constructorToUse.getConstructor().getParameterTypes().length == 0) {
        // no parameters for constructor injection? instantiate it right away
        object = constructorToUse.getConstructor().newInstance();
    } else {
        // if this fails, make sure resources that may be claimed by injectors are cleared up again
        try {
            Result<ModelType> result = newInstanceWithConstructorInjection(constructorToUse, adaptable, modelClass, registry, preparedValues);
            if (!result.wasSuccessful()) {
                registry.onDisposed();
                return result;
            } else {
                object = result.getValue();
            }
        } catch (InstantiationException ex) {
            registry.onDisposed();
            throw ex;
        } catch (InvocationTargetException ex) {
            registry.onDisposed();
            throw ex;
        } catch (IllegalAccessException ex) {
            registry.onDisposed();
            throw ex;
        }
    }
    registerCallbackRegistry(object, registry);
    InjectCallback callback = new SetFieldCallback(object);
    InjectableField[] injectableFields = modelClass.getInjectableFields();
    MissingElementsException missingElements = new MissingElementsException("Could not inject all required fields into " + modelClass.getType());
    for (InjectableField field : injectableFields) {
        RuntimeException t = injectElement(field, adaptable, registry, callback, preparedValues);
        if (t != null) {
            missingElements.addMissingElementExceptions(new MissingElementException(field.getAnnotatedElement(), t));
        }
    }
    registry.seal();
    if (!missingElements.isEmpty()) {
        return new Result<ModelType>(missingElements);
    }
    try {
        invokePostConstruct(object);
    } catch (InvocationTargetException e) {
        return new Result<ModelType>(new PostConstructException("Post-construct method has thrown an exception for model " + modelClass.getType(), e.getCause()));
    } catch (IllegalAccessException e) {
        new Result<ModelType>(new ModelClassException("Could not call post-construct method for model " + modelClass.getType(), e));
    }
    return new Result<ModelType>(object);
}
Also used : PostConstructException(org.apache.sling.models.factory.PostConstructException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) ValuePreparer(org.apache.sling.models.spi.ValuePreparer) MissingElementsException(org.apache.sling.models.factory.MissingElementsException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ModelClassException(org.apache.sling.models.factory.ModelClassException) InjectableField(org.apache.sling.models.impl.model.InjectableField) MissingElementException(org.apache.sling.models.factory.MissingElementException)

Aggregations

ModelClassException (org.apache.sling.models.factory.ModelClassException)7 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 CheckForNull (javax.annotation.CheckForNull)2 MissingElementException (org.apache.sling.models.factory.MissingElementException)2 MissingElementsException (org.apache.sling.models.factory.MissingElementsException)2 PostConstructException (org.apache.sling.models.factory.PostConstructException)2 ValuePreparer (org.apache.sling.models.spi.ValuePreparer)2 InvocationHandler (java.lang.reflect.InvocationHandler)1 Type (java.lang.reflect.Type)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 WeakHashMap (java.util.WeakHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Adaptable (org.apache.sling.api.adapter.Adaptable)1 Resource (org.apache.sling.api.resource.Resource)1 Model (org.apache.sling.models.annotations.Model)1 ViaProviderType (org.apache.sling.models.annotations.ViaProviderType)1 ExportException (org.apache.sling.models.factory.ExportException)1