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);
}
}
}
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;
}
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;
}
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;
}
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;
}
Aggregations