Search in sources :

Example 1 with InjectableMethod

use of org.apache.sling.models.impl.model.InjectableMethod in project sling by apache.

the class ModelAdapterFactory method createInvocationHandler.

private <ModelType> Result<InvocationHandler> createInvocationHandler(final Object adaptable, final ModelClass<ModelType> modelClass) {
    InjectableMethod[] injectableMethods = modelClass.getInjectableMethods();
    final Map<Method, Object> methods = new HashMap<Method, Object>();
    SetMethodsCallback callback = new SetMethodsCallback(methods);
    MapBackedInvocationHandler handler = new MapBackedInvocationHandler(methods);
    DisposalCallbackRegistryImpl registry = new DisposalCallbackRegistryImpl();
    registerCallbackRegistry(handler, registry);
    final Map<ValuePreparer, Object> preparedValues = new HashMap<ValuePreparer, Object>(VALUE_PREPARERS_COUNT);
    MissingElementsException missingElements = new MissingElementsException("Could not create all mandatory methods for interface of model " + modelClass);
    for (InjectableMethod method : injectableMethods) {
        RuntimeException t = injectElement(method, adaptable, registry, callback, preparedValues);
        if (t != null) {
            missingElements.addMissingElementExceptions(new MissingElementException(method.getAnnotatedElement(), t));
        }
    }
    registry.seal();
    if (!missingElements.isEmpty()) {
        return new Result<InvocationHandler>(missingElements);
    }
    return new Result<InvocationHandler>(handler);
}
Also used : 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) InjectableMethod(org.apache.sling.models.impl.model.InjectableMethod) Method(java.lang.reflect.Method) InjectableMethod(org.apache.sling.models.impl.model.InjectableMethod) MissingElementException(org.apache.sling.models.factory.MissingElementException)

Example 2 with InjectableMethod

use of org.apache.sling.models.impl.model.InjectableMethod in project sling by apache.

the class ModelAdapterFactory method setMethod.

private RuntimeException setMethod(InjectableMethod injectableMethod, Map<Method, Object> methods, Object value) {
    Method method = injectableMethod.getMethod();
    Result<Object> result = adaptIfNecessary(value, method.getReturnType(), method.getGenericReturnType());
    if (result.wasSuccessful()) {
        methods.put(method, result.getValue());
        return null;
    } else {
        return result.getThrowable();
    }
}
Also used : InjectableMethod(org.apache.sling.models.impl.model.InjectableMethod) Method(java.lang.reflect.Method)

Aggregations

Method (java.lang.reflect.Method)2 InjectableMethod (org.apache.sling.models.impl.model.InjectableMethod)2 HashMap (java.util.HashMap)1 WeakHashMap (java.util.WeakHashMap)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 MissingElementException (org.apache.sling.models.factory.MissingElementException)1 MissingElementsException (org.apache.sling.models.factory.MissingElementsException)1 ValuePreparer (org.apache.sling.models.spi.ValuePreparer)1