Search in sources :

Example 1 with ConstructionException

use of org.qi4j.api.common.ConstructionException in project qi4j-sdk by Qi4j.

the class ActivatorModel method newInstance.

@SuppressWarnings("unchecked")
public Activator<ActivateeType> newInstance(InjectionContext injectionContext) {
    try {
        Activator<ActivateeType> instance = (Activator<ActivateeType>) constructorsModel.newInstance(injectionContext);
        injectionContext = new InjectionContext(injectionContext.module(), injectionContext.uses(), instance);
        inject(injectionContext, instance);
        return instance;
    } catch (Exception ex) {
        throw new ConstructionException("Could not instantiate " + activatorType.getName(), ex);
    }
}
Also used : Activator(org.qi4j.api.activation.Activator) ConstructionException(org.qi4j.api.common.ConstructionException) ConstructionException(org.qi4j.api.common.ConstructionException) InjectionContext(org.qi4j.runtime.injection.InjectionContext)

Example 2 with ConstructionException

use of org.qi4j.api.common.ConstructionException 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 3 with ConstructionException

use of org.qi4j.api.common.ConstructionException 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 4 with ConstructionException

use of org.qi4j.api.common.ConstructionException in project qi4j-sdk by Qi4j.

the class Qi4jObjectFactory method createQi4jComposite.

@SuppressWarnings("unchecked")
private Object createQi4jComposite(Class aClass, boolean isAddToTypes) {
    if (compositeBuilderFactory == null) {
        return null;
    }
    Object obj = null;
    ConstructionException exception = null;
    try {
        obj = compositeBuilderFactory.newTransient(aClass);
    } catch (NoSuchTransientException e) {
        return null;
    } catch (ConstructionException e) {
        exception = e;
    }
    if (isAddToTypes) {
        addToType(aClass, qi4jComposite);
    }
    if (exception != null) {
        throw exception;
    }
    return obj;
}
Also used : NoSuchTransientException(org.qi4j.api.composite.NoSuchTransientException) ConstructionException(org.qi4j.api.common.ConstructionException)

Example 5 with ConstructionException

use of org.qi4j.api.common.ConstructionException in project qi4j-sdk by Qi4j.

the class ModuleInstance method newValueFromSerializedState.

@Override
public <T> T newValueFromSerializedState(Class<T> mixinType, String serializedState) throws NoSuchValueException, ConstructionException {
    NullArgumentException.validateNotNull("mixinType", mixinType);
    ModelModule<ValueModel> modelModule = typeLookup.lookupValueModel(mixinType);
    if (modelModule == null) {
        throw new NoSuchValueException(mixinType.getName(), name());
    }
    try {
        return valueSerialization().deserialize(modelModule.model().valueType(), serializedState);
    } catch (ValueSerializationException ex) {
        throw new ConstructionException("Could not create value from serialized state", ex);
    }
}
Also used : NoSuchValueException(org.qi4j.api.value.NoSuchValueException) ValueModel(org.qi4j.runtime.value.ValueModel) ValueSerializationException(org.qi4j.api.value.ValueSerializationException) ConstructionException(org.qi4j.api.common.ConstructionException)

Aggregations

ConstructionException (org.qi4j.api.common.ConstructionException)11 InjectionContext (org.qi4j.runtime.injection.InjectionContext)3 InvocationHandler (java.lang.reflect.InvocationHandler)2 Initializable (org.qi4j.api.mixin.Initializable)2 InitializationException (org.qi4j.api.mixin.InitializationException)2 Method (java.lang.reflect.Method)1 List (java.util.List)1 Activator (org.qi4j.api.activation.Activator)1 Composite (org.qi4j.api.composite.Composite)1 CompositeInstance (org.qi4j.api.composite.CompositeInstance)1 InvalidCompositeException (org.qi4j.api.composite.InvalidCompositeException)1 NoSuchTransientException (org.qi4j.api.composite.NoSuchTransientException)1 NoSuchObjectException (org.qi4j.api.object.NoSuchObjectException)1 PropertyDescriptor (org.qi4j.api.property.PropertyDescriptor)1 EntityCompositeAlreadyExistsException (org.qi4j.api.unitofwork.EntityCompositeAlreadyExistsException)1 NoSuchValueException (org.qi4j.api.value.NoSuchValueException)1 ValueSerializationException (org.qi4j.api.value.ValueSerializationException)1 Iterables.toList (org.qi4j.functional.Iterables.toList)1 FragmentClassLoader (org.qi4j.runtime.composite.FragmentClassLoader)1 ValueModel (org.qi4j.runtime.value.ValueModel)1