Search in sources :

Example 6 with InjectionContext

use of org.qi4j.runtime.injection.InjectionContext in project qi4j-sdk by Qi4j.

the class AbstractModifierModel method newInstance.

// Context
public InvocationHandler newInstance(ModuleInstance moduleInstance, InvocationHandler next, ProxyReferenceInvocationHandler proxyHandler, Method method) {
    InjectionContext injectionContext = new InjectionContext(moduleInstance, wrapNext(next), proxyHandler);
    Object modifier = constructorsModel.newInstance(injectionContext);
    try {
        if (FragmentClassLoader.isGenerated(modifier)) {
            modifier.getClass().getField("_instance").set(modifier, proxyHandler);
        }
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    } catch (NoSuchFieldException e) {
        e.printStackTrace();
    }
    injectedFieldsModel.inject(injectionContext, modifier);
    injectedMethodsModel.inject(injectionContext, modifier);
    if (isGeneric()) {
        return (InvocationHandler) modifier;
    } else {
        try {
            Method invocationMethod = modifierClass.getMethod("_" + method.getName(), method.getParameterTypes());
            TypedModifierInvocationHandler handler = new TypedModifierInvocationHandler();
            handler.setFragment(modifier);
            handler.setMethod(invocationMethod);
            return handler;
        } catch (NoSuchMethodException e) {
            throw new ConstructionException("Could not find modifier method", e);
        }
    }
}
Also used : Method(java.lang.reflect.Method) ConstructionException(org.qi4j.api.common.ConstructionException) InvocationHandler(java.lang.reflect.InvocationHandler) InjectionContext(org.qi4j.runtime.injection.InjectionContext)

Example 7 with InjectionContext

use of org.qi4j.runtime.injection.InjectionContext in project qi4j-sdk by Qi4j.

the class ServiceModel method newInstance.

public ServiceInstance newInstance(final ModuleInstance module) {
    Object[] mixins = mixinsModel.newMixinHolder();
    Map<AccessibleObject, Property<?>> properties = new HashMap<AccessibleObject, Property<?>>();
    for (PropertyModel propertyModel : stateModel.properties()) {
        Object initialValue = propertyModel.initialValue(module);
        if (propertyModel.accessor().equals(identityMethod)) {
            initialValue = identity;
        }
        Property property = new PropertyInstance<Object>(propertyModel, initialValue);
        properties.put(propertyModel.accessor(), property);
    }
    TransientStateInstance state = new TransientStateInstance(properties);
    ServiceInstance compositeInstance = new ServiceInstance(this, module, mixins, state);
    // Instantiate all mixins
    int i = 0;
    UsesInstance uses = UsesInstance.EMPTY_USES.use(this);
    InjectionContext injectionContext = new InjectionContext(compositeInstance, uses, state);
    for (MixinModel mixinModel : mixinsModel.mixinModels()) {
        mixins[i++] = mixinModel.newInstance(injectionContext);
    }
    return compositeInstance;
}
Also used : UsesInstance(org.qi4j.runtime.composite.UsesInstance) MixinModel(org.qi4j.runtime.composite.MixinModel) HashMap(java.util.HashMap) PropertyModel(org.qi4j.runtime.property.PropertyModel) PropertyInstance(org.qi4j.runtime.property.PropertyInstance) InjectionContext(org.qi4j.runtime.injection.InjectionContext) TransientStateInstance(org.qi4j.runtime.composite.TransientStateInstance) AccessibleObject(java.lang.reflect.AccessibleObject) AccessibleObject(java.lang.reflect.AccessibleObject) Property(org.qi4j.api.property.Property)

Example 8 with InjectionContext

use of org.qi4j.runtime.injection.InjectionContext in project qi4j-sdk by Qi4j.

the class ObjectModel method newInstance.

public Object newInstance(InjectionContext injectionContext) {
    Object instance;
    try {
        instance = constructorsModel.newInstance(injectionContext);
        injectionContext = new InjectionContext(injectionContext.module(), injectionContext.uses(), instance);
        injectedFieldsModel.inject(injectionContext, instance);
        injectedMethodsModel.inject(injectionContext, instance);
    } catch (Exception e) {
        throw new ConstructionException("Could not instantiate " + objectType.getName(), e);
    }
    if (instance instanceof Initializable) {
        try {
            ((Initializable) instance).initialize();
        } catch (InitializationException e) {
            throw new ConstructionException("Unable to initialize " + objectType, e);
        }
    }
    return instance;
}
Also used : Initializable(org.qi4j.api.mixin.Initializable) ConstructionException(org.qi4j.api.common.ConstructionException) InitializationException(org.qi4j.api.mixin.InitializationException) ConstructionException(org.qi4j.api.common.ConstructionException) InitializationException(org.qi4j.api.mixin.InitializationException) InjectionContext(org.qi4j.runtime.injection.InjectionContext)

Example 9 with InjectionContext

use of org.qi4j.runtime.injection.InjectionContext in project qi4j-sdk by Qi4j.

the class TransientModel method newInstance.

public TransientInstance newInstance(ModuleInstance moduleInstance, UsesInstance uses, TransientStateInstance state) {
    Object[] mixins = mixinsModel.newMixinHolder();
    TransientInstance compositeInstance = new TransientInstance(this, moduleInstance, mixins, state);
    // Instantiate all mixins
    int i = 0;
    InjectionContext injectionContext = new InjectionContext(compositeInstance, uses, state);
    for (MixinModel mixinModel : mixinsModel.mixinModels()) {
        mixins[i++] = mixinModel.newInstance(injectionContext);
    }
    // Return
    return compositeInstance;
}
Also used : InjectionContext(org.qi4j.runtime.injection.InjectionContext)

Example 10 with InjectionContext

use of org.qi4j.runtime.injection.InjectionContext in project qi4j-sdk by Qi4j.

the class ValueModel method newValueInstance.

public ValueInstance newValueInstance(ModuleInstance moduleInstance, ValueStateInstance state) {
    Object[] mixins = mixinsModel.newMixinHolder();
    ValueInstance instance = new ValueInstance(this, moduleInstance, mixins, state);
    // Instantiate all mixins
    int i = 0;
    InjectionContext injectionContext = new InjectionContext(instance, UsesInstance.EMPTY_USES, state);
    for (MixinModel mixinModel : mixinsModel.mixinModels()) {
        mixins[i++] = mixinModel.newInstance(injectionContext);
    }
    // Return
    return instance;
}
Also used : MixinModel(org.qi4j.runtime.composite.MixinModel) InjectionContext(org.qi4j.runtime.injection.InjectionContext)

Aggregations

InjectionContext (org.qi4j.runtime.injection.InjectionContext)10 ConstructionException (org.qi4j.api.common.ConstructionException)3 MixinModel (org.qi4j.runtime.composite.MixinModel)3 Activator (org.qi4j.api.activation.Activator)2 NoSuchObjectException (org.qi4j.api.object.NoSuchObjectException)2 ObjectModel (org.qi4j.runtime.object.ObjectModel)2 AccessibleObject (java.lang.reflect.AccessibleObject)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Initializable (org.qi4j.api.mixin.Initializable)1 InitializationException (org.qi4j.api.mixin.InitializationException)1 Property (org.qi4j.api.property.Property)1 TransientStateInstance (org.qi4j.runtime.composite.TransientStateInstance)1 UsesInstance (org.qi4j.runtime.composite.UsesInstance)1 PropertyInstance (org.qi4j.runtime.property.PropertyInstance)1 PropertyModel (org.qi4j.runtime.property.PropertyModel)1