Ejemplo n.º 1
0
        private void BuildInjectionPlan(INode n, IDictionary <INode, InjectionPlan> memo)
        {
            if (memo.ContainsKey(n))
            {
                InjectionPlan p = null;
                memo.TryGetValue(n, out p);
                if (BUILDING == p)
                {
                    StringBuilder loopyList = new StringBuilder("[");
                    foreach (INode node in memo.Keys)
                    {
                        InjectionPlan p1 = null;
                        memo.TryGetValue(node, out p1);
                        if (p1 == BUILDING)
                        {
                            loopyList.Append(" " + node.GetFullName());
                        }
                    }
                    loopyList.Append(" ]");
                    throw new ClassHierarchyException("Detected loopy constructor involving "
                                                      + loopyList.ToString());
                }
                else
                {
                    return;
                }
            }
            memo.Add(n, BUILDING);
            InjectionPlan ip = null;

            if (n is INamedParameterNode)
            {
                INamedParameterNode np = (INamedParameterNode)n;
                object boundInstance   = ParseBoundNamedParameter(np);
                object defaultInstance = this.classHierarchy.ParseDefaultValue(np);
                object instance        = boundInstance != null ? boundInstance : defaultInstance;

                if (instance is INode)
                {
                    BuildInjectionPlan((INode)instance, memo);
                    InjectionPlan sp = null;
                    memo.TryGetValue((INode)instance, out sp);
                    if (sp != null)
                    {
                        ip = new Subplan(n, 0, new InjectionPlan[] { sp });
                    }
                }
                else if (instance is ISet <object> )
                {
                    ISet <object>        entries = (ISet <object>)instance;
                    ISet <InjectionPlan> plans   = new MonotonicSet <InjectionPlan>();
                    foreach (object entry in entries)
                    {
                        if (entry is IClassNode)
                        {
                            BuildInjectionPlan((IClassNode)entry, memo);
                            InjectionPlan p2 = null;
                            memo.TryGetValue((INode)entry, out p2);
                            if (p2 != null)
                            {
                                plans.Add(p2);
                            }
                        }
                        else
                        {
                            plans.Add(new CsInstance(n, entry));
                        }
                    }
                    ip = new SetInjectionPlan(n, plans);
                }
                else
                {
                    ip = new CsInstance(np, instance);
                }
            }
            else if (n is IClassNode)
            {
                IClassNode cn = (IClassNode)n;

                // Any (or all) of the next four values might be null; that's fine.
                object     cached      = GetCachedInstance(cn);
                IClassNode boundImpl   = this.configuration.GetBoundImplementation(cn);
                IClassNode defaultImpl = ParseDefaultImplementation(cn);
                IClassNode ec          = this.configuration.GetBoundConstructor(cn);

                ip = BuildClassNodeInjectionPlan(cn, cached, ec, boundImpl, defaultImpl, memo);
            }
            else if (n is IPackageNode)
            {
                throw new ArgumentException(
                          "Request to instantiate Java package as object");
            }
            else
            {
                throw new IllegalStateException(
                          "Type hierarchy contained unknown node type!:" + n);
            }
            memo[n] = ip;
        }
Ejemplo n.º 2
0
        private void BuildInjectionPlan(INode n, IDictionary<INode, InjectionPlan> memo)
        {
            if (memo.ContainsKey(n))
             {
                InjectionPlan p = null;
                memo.TryGetValue(n, out p);
                if (BUILDING == p)
                {
                    StringBuilder loopyList = new StringBuilder("[");
                    foreach (INode node in memo.Keys)
                    {
                        InjectionPlan p1 = null;
                        memo.TryGetValue(node, out p1);
                        if(p1 == BUILDING)
                        {
                            loopyList.Append(" " + node.GetFullName());
                        }
                    }
                    loopyList.Append(" ]");
                    throw new ClassHierarchyException("Detected loopy constructor involving "
                        + loopyList.ToString());
                }
                else
                {
                    return;
                }
            }
            memo.Add(n, BUILDING);
            InjectionPlan ip = null;
            if (n is INamedParameterNode)
            {
                INamedParameterNode np = (INamedParameterNode)n;
                object boundInstance = ParseBoundNamedParameter(np);
                object defaultInstance = this.classHierarchy.ParseDefaultValue(np);
                object instance = boundInstance != null ? boundInstance : defaultInstance;

                if (instance is INode)
                {
                    BuildInjectionPlan((INode)instance, memo);
                    InjectionPlan sp = null;
                    memo.TryGetValue((INode)instance, out sp);
                    if (sp != null)
                    {
                        ip = new Subplan(n, 0, new InjectionPlan[] {sp});
                    }
                }
                else if(instance is ISet<object>)
                {
                    ISet<object> entries = (ISet<object>) instance;
                    ISet<InjectionPlan> plans = new MonotonicSet<InjectionPlan>();
                    foreach (object entry in entries)
                    {
                        if(entry is IClassNode)
                        {
                            BuildInjectionPlan((IClassNode)entry, memo);
                            InjectionPlan p2 = null;
                            memo.TryGetValue((INode)entry, out p2);
                            if (p2 != null)
                            {
                                plans.Add(p2);
                            }
                        }
                        else
                        {
                            plans.Add(new CsInstance(n, entry));
                        }

                    }
                    ip = new SetInjectionPlan(n, plans);
                }
                else
                {
                    ip = new CsInstance(np, instance);
                }

            }
            else if (n is IClassNode)
            {
                IClassNode cn = (IClassNode) n;

                // Any (or all) of the next four values might be null; that's fine.
                object cached = GetCachedInstance(cn);
                IClassNode boundImpl = this.configuration.GetBoundImplementation(cn);
                IClassNode defaultImpl = ParseDefaultImplementation(cn);
                IClassNode ec = this.configuration.GetBoundConstructor(cn);

                ip = BuildClassNodeInjectionPlan(cn, cached, ec, boundImpl, defaultImpl, memo);
            } else if (n is IPackageNode)
            {
                throw new ArgumentException(
                    "Request to instantiate Java package as object");
            } else
            {
                throw new IllegalStateException(
                    "Type hierarchy contained unknown node type!:" + n);
            }
            memo[n] = ip;
        }