Search in sources :

Example 1 with BindingException

use of org.qi4j.bootstrap.BindingException in project qi4j-sdk by Qi4j.

the class MixinsModel method bind.

// Binding
@Override
public void bind(final Resolution resolution) throws BindingException {
    // Order mixins based on @This usages
    UsageGraph<MixinModel> deps = new UsageGraph<MixinModel>(mixinModels, new Uses(), true);
    mixinModels = deps.resolveOrder();
    // Populate mappings
    for (int i = 0; i < mixinModels.size(); i++) {
        MixinModel mixinModel = mixinModels.get(i);
        mixinIndex.put(mixinModel.mixinClass(), i);
    }
    for (Map.Entry<Method, MixinModel> methodClassEntry : methodImplementation.entrySet()) {
        methodIndex.put(methodClassEntry.getKey(), mixinIndex.get(methodClassEntry.getValue().mixinClass()));
    }
    for (MixinModel mixinModel : mixinModels) {
        mixinModel.accept(new HierarchicalVisitorAdapter<Object, Object, BindingException>() {

            @Override
            public boolean visitEnter(Object visited) throws BindingException {
                if (visited instanceof InjectedFieldModel) {
                    InjectedFieldModel fieldModel = (InjectedFieldModel) visited;
                    fieldModel.bind(resolution.forField(fieldModel.field()));
                    return false;
                } else if (visited instanceof Binder) {
                    Binder constructorsModel = (Binder) visited;
                    constructorsModel.bind(resolution);
                    return false;
                }
                return true;
            }

            @Override
            public boolean visit(Object visited) throws BindingException {
                if (visited instanceof Binder) {
                    ((Binder) visited).bind(resolution);
                }
                return true;
            }
        });
    }
}
Also used : Method(java.lang.reflect.Method) InjectedFieldModel(org.qi4j.runtime.injection.InjectedFieldModel) BindingException(org.qi4j.bootstrap.BindingException) Binder(org.qi4j.runtime.model.Binder) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with BindingException

use of org.qi4j.bootstrap.BindingException in project qi4j-sdk by Qi4j.

the class ApplicationModelFactoryImpl method newApplicationModel.

@Override
public ApplicationDescriptor newApplicationModel(ApplicationAssembly assembly) throws AssemblyException {
    AssemblyHelper helper = new AssemblyHelper();
    ApplicationAssemblyImpl applicationAssembly = (ApplicationAssemblyImpl) assembly;
    ActivatorsModel<Application> applicationActivators = new ActivatorsModel<>(applicationAssembly.activators());
    List<LayerModel> layerModels = new ArrayList<>();
    final ApplicationModel applicationModel = new ApplicationModel(applicationAssembly.name(), applicationAssembly.version(), applicationAssembly.mode(), applicationAssembly.metaInfo(), applicationActivators, layerModels);
    Map<LayerAssembly, LayerModel> mapAssemblyModel = new HashMap<>();
    Map<LayerAssembly, List<LayerModel>> mapUsedLayers = new HashMap<>();
    // Build all layers
    List<LayerAssemblyImpl> layerAssemblies = new ArrayList<>(applicationAssembly.layerAssemblies());
    for (LayerAssemblyImpl layerAssembly : layerAssemblies) {
        List<LayerModel> usedLayers = new ArrayList<>();
        mapUsedLayers.put(layerAssembly, usedLayers);
        UsedLayersModel usedLayersModel = new UsedLayersModel(usedLayers);
        List<ModuleModel> moduleModels = new ArrayList<>();
        String name = layerAssembly.name();
        if (name == null) {
            throw new AssemblyException("Layer must have name set");
        }
        ActivatorsModel<Layer> layerActivators = new ActivatorsModel<>(layerAssembly.activators());
        LayerModel layerModel = new LayerModel(name, layerAssembly.metaInfo(), usedLayersModel, layerActivators, moduleModels);
        for (ModuleAssemblyImpl moduleAssembly : layerAssembly.moduleAssemblies()) {
            moduleModels.add(moduleAssembly.assembleModule(helper));
        }
        mapAssemblyModel.put(layerAssembly, layerModel);
        layerModels.add(layerModel);
    }
    // Populate used layer lists
    for (LayerAssemblyImpl layerAssembly : layerAssemblies) {
        Set<LayerAssembly> usesLayers = layerAssembly.uses();
        List<LayerModel> usedLayers = mapUsedLayers.get(layerAssembly);
        for (LayerAssembly usesLayer : usesLayers) {
            LayerModel layerModel = mapAssemblyModel.get(usesLayer);
            usedLayers.add(layerModel);
        }
    }
    // This will resolve all dependencies
    try {
        //            applicationModel.bind();
        applicationModel.accept(new BindingVisitor(applicationModel));
    } catch (BindingException e) {
        throw new AssemblyException("Unable to bind: " + applicationModel, e);
    }
    return applicationModel;
}
Also used : LayerModel(org.qi4j.runtime.structure.LayerModel) UsedLayersModel(org.qi4j.runtime.structure.UsedLayersModel) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ApplicationModel(org.qi4j.runtime.structure.ApplicationModel) ModuleModel(org.qi4j.runtime.structure.ModuleModel) AssemblyException(org.qi4j.bootstrap.AssemblyException) ArrayList(java.util.ArrayList) List(java.util.List) ActivatorsModel(org.qi4j.runtime.activation.ActivatorsModel) Layer(org.qi4j.api.structure.Layer) LayerAssembly(org.qi4j.bootstrap.LayerAssembly) BindingException(org.qi4j.bootstrap.BindingException) Application(org.qi4j.api.structure.Application)

Aggregations

HashMap (java.util.HashMap)2 BindingException (org.qi4j.bootstrap.BindingException)2 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Application (org.qi4j.api.structure.Application)1 Layer (org.qi4j.api.structure.Layer)1 AssemblyException (org.qi4j.bootstrap.AssemblyException)1 LayerAssembly (org.qi4j.bootstrap.LayerAssembly)1 ActivatorsModel (org.qi4j.runtime.activation.ActivatorsModel)1 InjectedFieldModel (org.qi4j.runtime.injection.InjectedFieldModel)1 Binder (org.qi4j.runtime.model.Binder)1 ApplicationModel (org.qi4j.runtime.structure.ApplicationModel)1 LayerModel (org.qi4j.runtime.structure.LayerModel)1 ModuleModel (org.qi4j.runtime.structure.ModuleModel)1 UsedLayersModel (org.qi4j.runtime.structure.UsedLayersModel)1