Exemplo n.º 1
0
 public static InjectionPlanProto.InjectionPlan Serialize(InjectionPlan ip)
 {
     if (ip is Constructor)
     {
         Constructor     cons = (Constructor)ip;
         InjectionPlan[] args = cons.GetArgs();
         InjectionPlanProto.InjectionPlan[] protoArgs = new InjectionPlanProto.InjectionPlan[args.Length];
         for (int i = 0; i < args.Length; i++)
         {
             protoArgs[i] = Serialize(args[i]);
         }
         return(NewConstructor(ip.GetNode().GetFullName(), protoArgs.ToList <InjectionPlanProto.InjectionPlan>()));
     }
     if (ip is Subplan)
     {
         Subplan         sp   = (Subplan)ip;
         InjectionPlan[] args = sp.GetPlans();
         InjectionPlanProto.InjectionPlan[] subPlans = new InjectionPlanProto.InjectionPlan[args.Length];
         for (int i = 0; i < args.Length; i++)
         {
             subPlans[i] = Serialize(args[i]);
         }
         return(NewSubplan(ip.GetNode().GetFullName(), sp.GetSelectedIndex(), subPlans.ToList <InjectionPlanProto.InjectionPlan>()));
     }
     if (ip is CsInstance)
     {
         CsInstance ji = (CsInstance)ip;
         return(NewInstance(ip.GetNode().GetFullName(), ji.GetInstanceAsString()));
     }
     Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException(
                                                                "Encountered unknown type of InjectionPlan: " + ip), LOGGER);
     return(null);
 }
Exemplo n.º 2
0
 private string ShallowArgString(InjectionPlan arg) 
 {
     if (arg is Constructor || arg is Subplan) 
     {
         return arg.GetType().Name + ": " + arg.GetNode().GetName();
     } 
     else 
     {
         return arg.ToShallowString();
     }
 }
Exemplo n.º 3
0
        public static InjectionPlanProto.InjectionPlan Serialize(InjectionPlan ip)
        {
            if (ip is Constructor)
            {
                Constructor cons = (Constructor) ip;
                InjectionPlan[] args = cons.GetArgs();
                InjectionPlanProto.InjectionPlan[] protoArgs = new InjectionPlanProto.InjectionPlan[args.Length];
                for (int i = 0; i < args.Length; i++)
                {
                    protoArgs[i] = Serialize(args[i]);
                }
                return NewConstructor(ip.GetNode().GetFullName(), protoArgs.ToList<InjectionPlanProto.InjectionPlan>());
            }
            else if (ip is Subplan)
            {
                Subplan sp = (Subplan) ip;
                InjectionPlan[] args = sp.GetPlans();
                InjectionPlanProto.InjectionPlan[] subPlans = new InjectionPlanProto.InjectionPlan[args.Length];
                for (int i = 0; i < args.Length; i++)
                {
                    subPlans[i] = Serialize(args[i]);
                }
                return NewSubplan(ip.GetNode().GetFullName(), sp.GetSelectedIndex(), subPlans.ToList<InjectionPlanProto.InjectionPlan>());

            }
            else if (ip is CsInstance)
            {
                CsInstance ji = (CsInstance) ip;
                return NewInstance(ip.GetNode().GetFullName(), ji.GetInstanceAsString());
            } else
            {
                throw new IllegalStateException(
                    "Encountered unknown type of InjectionPlan: " + ip);
            }
        }
Exemplo n.º 4
0
        public object InjectFromPlan(InjectionPlan plan)
        {
            if (!plan.IsFeasible())
            {
                throw new InjectionException("Cannot inject " + plan.GetNode().GetFullName() + ": "
                    + plan.ToCantInjectString());
            }
            if (plan.IsAmbiguous())
            {
                throw new InjectionException("Cannot inject " + plan.GetNode().GetFullName() + " "
                    + plan.ToCantInjectString());
            }

            if (plan is InjectionFuturePlan)
            {
                InjectionFuturePlan fut = (InjectionFuturePlan)plan;
                string key = fut.GetNode().GetFullName();
                try
                {
                    //we will see if we need to introduce T to replace object
                    InjectionFuture ret = new InjectionFuture(this, classHierarchy.ClassForName(fut.GetNode().GetFullName()));
                    pendingFutures.Add(ret);
                    return ret;
                }
                catch (TypeLoadException e)
                {
                    throw new InjectionException("Could not get class for " + key);
                }
            }
            else if(plan.GetNode() is IClassNode && null != GetCachedInstance((IClassNode)plan.GetNode()))
            {
                return GetCachedInstance((IClassNode)plan.GetNode());
            }
            else if (plan is CsInstance)
            {
                // TODO: Must be named parameter node.  Check.
                //      throw new IllegalStateException("Instance from plan not in Injector's set of instances?!?");
                return ((CsInstance) plan).instance;
            }
            else if (plan is Constructor)
            {
                Constructor constructor = (Constructor) plan;
                object[] args = new object[constructor.GetArgs().Length];
                InjectionPlan[] argPlans = constructor.GetArgs();

                for (int i = 0; i < argPlans.Length; i++)
                {
                    args[i] = InjectFromPlan(argPlans[i]);
                }

                try
                {
                    concurrentModificationGuard = true;
                    object ret;
                    try
                    {
                        IConstructorDef def = (IConstructorDef)constructor.GetConstructorDef();
                        ConstructorInfo c = GetConstructor(def);

                        if (aspect != null)
                        {
                            ret = aspect.Inject(def, c, args);
                        }
                        else
                        {
                            ret = c.Invoke(args);
                        }
                    }
                    catch (ArgumentException e)
                    {
                        StringBuilder sb = new StringBuilder("Internal Tang error?  Could not call constructor " + constructor.GetConstructorDef() + " with arguments [");
                        foreach (Object o in args)
                        {
                            sb.Append("\n\t" + o);
                        }
                        sb.Append("]");
                        throw new IllegalStateException(sb.ToString(), e);
                    }
                    if (ret is IExternalConstructor)
                    {
                        ret = ((IExternalConstructor)ret).NewInstance();
                    }
                    instances.Add(constructor.GetNode(), ret);
                    return ret;
                }
                catch (Exception e)
                {
                    throw new InjectionException("Could not invoke constructor: " + plan,  e); //check what exception might be got and refine it
                } finally
                {
                    concurrentModificationGuard = false;
                }
            }
            else if (plan is Subplan)
            {
                Subplan ambiguous = (Subplan) plan;
                return InjectFromPlan(ambiguous.GetDelegatedPlan());
            }
            else if (plan is SetInjectionPlan)
            {
                SetInjectionPlan setPlan = (SetInjectionPlan) plan;
                ISet<object> ret = new MonotonicSet<object>();
                foreach (InjectionPlan subplan in setPlan.GetEntryPlans())
                {
                    ret.Add(InjectFromPlan(subplan));
                }
                return ret;
            } else
            {
                throw new IllegalStateException("Unknown plan type: " + plan);
            }
        }