Search in sources :

Example 1 with Initializable

use of org.qi4j.api.mixin.Initializable in project qi4j-sdk by Qi4j.

the class MixinModel method newInstance.

public Object newInstance(InjectionContext injectionContext) {
    Object mixin;
    CompositeInstance compositeInstance = injectionContext.compositeInstance();
    mixin = constructorsModel.newInstance(injectionContext);
    if (FragmentClassLoader.isGenerated(instantiationClass)) {
        try {
            instantiationClass.getDeclaredField("_instance").set(mixin, injectionContext.compositeInstance());
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
    }
    injectedFieldsModel.inject(injectionContext, mixin);
    injectedMethodsModel.inject(injectionContext, mixin);
    if (mixin instanceof Initializable) {
        try {
            ((Initializable) mixin).initialize();
        } catch (InitializationException e) {
            List<Class<?>> compositeType = toList(compositeInstance.types());
            String message = "Unable to initialize " + mixinClass + " in composite " + compositeType;
            throw new ConstructionException(message, e);
        }
    }
    return mixin;
}
Also used : Initializable(org.qi4j.api.mixin.Initializable) CompositeInstance(org.qi4j.api.composite.CompositeInstance) Iterables.toList(org.qi4j.functional.Iterables.toList) List(java.util.List) InitializationException(org.qi4j.api.mixin.InitializationException) ConstructionException(org.qi4j.api.common.ConstructionException)

Example 2 with Initializable

use of org.qi4j.api.mixin.Initializable 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)

Aggregations

ConstructionException (org.qi4j.api.common.ConstructionException)2 Initializable (org.qi4j.api.mixin.Initializable)2 InitializationException (org.qi4j.api.mixin.InitializationException)2 List (java.util.List)1 CompositeInstance (org.qi4j.api.composite.CompositeInstance)1 Iterables.toList (org.qi4j.functional.Iterables.toList)1 InjectionContext (org.qi4j.runtime.injection.InjectionContext)1