Search in sources :

Example 1 with InjectionResources

use of org.apache.tapestry5.ioc.internal.util.InjectionResources in project tapestry-5 by apache.

the class RegistryImpl method autobuild.

@Override
public <T> T autobuild(final Class<T> clazz) {
    assert clazz != null;
    final Constructor constructor = InternalUtils.findAutobuildConstructor(clazz);
    if (constructor == null) {
        throw new RuntimeException(IOCMessages.noAutobuildConstructor(clazz));
    }
    Map<Class, Object> resourcesMap = CollectionFactory.newMap();
    resourcesMap.put(OperationTracker.class, RegistryImpl.this);
    InjectionResources resources = new MapInjectionResources(resourcesMap);
    ObjectCreator<T> plan = InternalUtils.createConstructorConstructionPlan(this, this, resources, null, "Invoking " + proxyFactory.getConstructorLocation(constructor).toString(), constructor);
    return plan.createObject();
}
Also used : Constructor(java.lang.reflect.Constructor) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources) InjectionResources(org.apache.tapestry5.ioc.internal.util.InjectionResources) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources)

Example 2 with InjectionResources

use of org.apache.tapestry5.ioc.internal.util.InjectionResources in project tapestry-5 by apache.

the class ServiceAdvisorImpl method advise.

/**
 * Invokes the configured method, passing the builder. The method will always take, as a parameter, a
 * MethodAdvisor.
 */
@Override
public void advise(MethodAdviceReceiver methodAdviceReceiver) {
    Map<Class, Object> resources = CollectionFactory.newMap(this.resourcesDefaults);
    resources.put(MethodAdviceReceiver.class, methodAdviceReceiver);
    InjectionResources injectionResources = new MapInjectionResources(resources);
    // By design, advise methods return void, so we know that the return value is null.
    invoke(injectionResources);
}
Also used : InjectionResources(org.apache.tapestry5.ioc.internal.util.InjectionResources) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources)

Example 3 with InjectionResources

use of org.apache.tapestry5.ioc.internal.util.InjectionResources in project tapestry-5 by apache.

the class ContributionDefImpl method invokeMethod.

private <T> void invokeMethod(ModuleBuilderSource source, ServiceResources resources, Class<T> parameterType, T parameterValue) {
    Map<Class, Object> resourceMap = CollectionFactory.newMap();
    resourceMap.put(parameterType, parameterValue);
    resourceMap.put(ObjectLocator.class, resources);
    resourceMap.put(Logger.class, resources.getLogger());
    InjectionResources injectionResources = new MapInjectionResources(resourceMap);
    for (Class t : CONFIGURATION_TYPES) {
        if (parameterType != t) {
            injectionResources = new DelegatingInjectionResources(new WrongConfigurationTypeGuard(resources.getServiceId(), t, parameterType), injectionResources);
        }
    }
    Throwable fail = null;
    Object moduleInstance = InternalUtils.isStatic(contributorMethod) ? null : source.getModuleBuilder();
    try {
        ObjectCreator[] parameters = InternalUtils.calculateParametersForMethod(contributorMethod, resources, injectionResources, resources.getTracker());
        contributorMethod.invoke(moduleInstance, InternalUtils.realizeObjects(parameters));
    } catch (InvocationTargetException ex) {
        fail = ex.getTargetException();
    } catch (Exception ex) {
        fail = ex;
    }
    if (fail != null)
        throw new RuntimeException(IOCMessages.contributionMethodError(contributorMethod, fail), fail);
}
Also used : WrongConfigurationTypeGuard(org.apache.tapestry5.ioc.internal.util.WrongConfigurationTypeGuard) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources) InjectionResources(org.apache.tapestry5.ioc.internal.util.InjectionResources) DelegatingInjectionResources(org.apache.tapestry5.ioc.internal.util.DelegatingInjectionResources) DelegatingInjectionResources(org.apache.tapestry5.ioc.internal.util.DelegatingInjectionResources) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 4 with InjectionResources

use of org.apache.tapestry5.ioc.internal.util.InjectionResources in project tapestry-5 by apache.

the class AbstractServiceCreator method createInjectionResources.

/**
 * Returns a map (based on injectionResources) that includes (possibly) an additional mapping containing the
 * collected configuration data. This involves scanning the parameters and generic types.
 */
protected final InjectionResources createInjectionResources() {
    InjectionResources core = new MapInjectionResources(injectionResources);
    InjectionResources configurations = new InjectionResources() {

        private boolean seenOne;

        @Override
        public <T> T findResource(Class<T> resourceType, Type genericType) {
            ConfigurationType thisType = PARAMETER_TYPE_TO_CONFIGURATION_TYPE.get(resourceType);
            if (thisType == null)
                return null;
            if (seenOne)
                throw new RuntimeException(IOCMessages.tooManyConfigurationParameters(creatorDescription));
            seenOne = true;
            switch(thisType) {
                case UNORDERED:
                    return resourceType.cast(getUnorderedConfiguration(genericType));
                case ORDERED:
                    return resourceType.cast(getOrderedConfiguration(genericType));
                case MAPPED:
                    return resourceType.cast(getMappedConfiguration(genericType));
            }
            return null;
        }
    };
    return new DelegatingInjectionResources(core, configurations);
}
Also used : ConfigurationType(org.apache.tapestry5.ioc.internal.ConfigurationType) ConfigurationType(org.apache.tapestry5.ioc.internal.ConfigurationType) ParameterizedType(java.lang.reflect.ParameterizedType) Type(java.lang.reflect.Type) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources) InjectionResources(org.apache.tapestry5.ioc.internal.util.InjectionResources) DelegatingInjectionResources(org.apache.tapestry5.ioc.internal.util.DelegatingInjectionResources) DelegatingInjectionResources(org.apache.tapestry5.ioc.internal.util.DelegatingInjectionResources) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources)

Example 5 with InjectionResources

use of org.apache.tapestry5.ioc.internal.util.InjectionResources in project tapestry-5 by apache.

the class ModuleImpl method instantiateModuleInstance.

private Object instantiateModuleInstance() {
    Class moduleClass = moduleDef.getBuilderClass();
    Constructor[] constructors = moduleClass.getConstructors();
    if (constructors.length == 0)
        throw new RuntimeException(IOCMessages.noPublicConstructors(moduleClass));
    if (constructors.length > 1) {
        // Sort the constructors ascending by number of parameters (descending); this is really
        // just to allow the test suite to work properly across different JVMs (which will
        // often order the constructors differently).
        Comparator<Constructor> comparator = new Comparator<Constructor>() {

            @Override
            public int compare(Constructor c1, Constructor c2) {
                return c2.getParameterTypes().length - c1.getParameterTypes().length;
            }
        };
        Arrays.sort(constructors, comparator);
        logger.warn(IOCMessages.tooManyPublicConstructors(moduleClass, constructors[0]));
    }
    Constructor constructor = constructors[0];
    if (insideConstructor)
        throw new RuntimeException(IOCMessages.recursiveModuleConstructor(moduleClass, constructor));
    ObjectLocator locator = new ObjectLocatorImpl(registry, this);
    Map<Class, Object> resourcesMap = CollectionFactory.newMap();
    resourcesMap.put(Logger.class, logger);
    resourcesMap.put(ObjectLocator.class, locator);
    resourcesMap.put(OperationTracker.class, registry);
    InjectionResources resources = new MapInjectionResources(resourcesMap);
    Throwable fail = null;
    try {
        insideConstructor = true;
        ObjectCreator[] parameterValues = InternalUtils.calculateParameters(locator, resources, constructor.getParameterTypes(), constructor.getGenericParameterTypes(), constructor.getParameterAnnotations(), registry);
        Object[] realized = InternalUtils.realizeObjects(parameterValues);
        Object result = constructor.newInstance(realized);
        InternalUtils.injectIntoFields(result, locator, resources, registry);
        return result;
    } catch (InvocationTargetException ex) {
        fail = ex.getTargetException();
    } catch (Exception ex) {
        fail = ex;
    } finally {
        insideConstructor = false;
    }
    throw new RuntimeException(IOCMessages.instantiateBuilderError(moduleClass, fail), fail);
}
Also used : Constructor(java.lang.reflect.Constructor) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources) InjectionResources(org.apache.tapestry5.ioc.internal.util.InjectionResources) MapInjectionResources(org.apache.tapestry5.ioc.internal.util.MapInjectionResources) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) ObjectStreamException(java.io.ObjectStreamException) JustInTimeObjectCreator(org.apache.tapestry5.ioc.internal.services.JustInTimeObjectCreator)

Aggregations

InjectionResources (org.apache.tapestry5.ioc.internal.util.InjectionResources)6 MapInjectionResources (org.apache.tapestry5.ioc.internal.util.MapInjectionResources)6 Constructor (java.lang.reflect.Constructor)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 DelegatingInjectionResources (org.apache.tapestry5.ioc.internal.util.DelegatingInjectionResources)2 ObjectStreamException (java.io.ObjectStreamException)1 ParameterizedType (java.lang.reflect.ParameterizedType)1 Type (java.lang.reflect.Type)1 ConfigurationType (org.apache.tapestry5.ioc.internal.ConfigurationType)1 JustInTimeObjectCreator (org.apache.tapestry5.ioc.internal.services.JustInTimeObjectCreator)1 WrongConfigurationTypeGuard (org.apache.tapestry5.ioc.internal.util.WrongConfigurationTypeGuard)1