コード例 #1
0
ファイル: ClassHierarchyImpl.cs プロジェクト: kyungtaak/TANG
 public object ParseDefaultValue(INamedParameterNode name)
 {
     string[] vals = name.GetDefaultInstanceAsStrings();
     object[] ret = new Object[vals.Length];
     for (int i = 0; i < vals.Length; i++)
     {
         string val = vals[i];
         try
         {
             ret[i] = Parse(name, val);
         }
         catch (ParseException e)
         {
             throw new ClassHierarchyException("Could not parse default value", e);
         }
     }
     if (name.IsSet())
     {
         return new HashSet<object>(ret.ToList<object>());
     }
     else
     {
         if (ret.Length == 0)
         {
             return null;
         }
         else if (ret.Length == 1)
         {
             return ret[0];
         }
         else
         {
             throw new IllegalStateException("Multiple defaults for non-set named parameter! " + name.GetFullName());
         }
     }
 }
コード例 #2
0
        public void BindParameter(INamedParameterNode name, string value)
        {
            /* Parse and discard value; this is just for type checking, skip for now*/
            if (this.ClassHierarchy is ICsClassHierarchy) 
            {
                ((ICsClassHierarchy)ClassHierarchy).Parse(name, value);
            }

            if (name.IsSet()) 
            {
                BindSetEntry((INamedParameterNode)name, value);
            } 
            else 
            {
                try
                {
                    NamedParameters.Add(name, value);
                }
                catch (ArgumentException e)
                {
                    var msg = string.Format(CultureInfo.InvariantCulture, DuplicatedEntryForNamedParamater + "try to bind [{0}] to [{1}], but the configuration has been set for it.", value, name.GetFullName());
                    Utilities.Diagnostics.Exceptions.Throw(new ArgumentException(msg + e.Message), LOGGER);
                }
            }
        }
コード例 #3
0
        public void BindParameter(INamedParameterNode name, string value)
        {
            /* Parse and discard value; this is just for type checking, skip for now*/
            if (this.ClassHierarchy is ICsClassHierarchy) 
            {
                ((ICsClassHierarchy)ClassHierarchy).Parse(name, value);
            }

            if (name.IsSet()) 
            {
                BindSetEntry((INamedParameterNode)name, value);
            } 
            else 
            {
                NamedParameters.Add(name, value);
            }
        }
コード例 #4
0
ファイル: ClassHierarchyImpl.cs プロジェクト: beomyeol/reef
 public object ParseDefaultValue(INamedParameterNode name)
 {
     string[] vals = name.GetDefaultInstanceAsStrings();
     object[] ret = new object[vals.Length];
     for (int i = 0; i < vals.Length; i++)
     {
         string val = vals[i];
         try
         {
             ret[i] = Parse(name, val);
         }
         catch (ParseException e)
         {
             Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER);
             var ex = new ClassHierarchyException("Could not parse default value " + val, e);
             Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
         }
     }
     if (name.IsSet())
     {
         return new HashSet<object>(ret.ToList<object>());
     }
     if (name.IsList())
     {
         return new List<object>(ret.ToList<object>());
     }
     if (ret.Length == 0)
     {
         return null;
     }
     if (ret.Length == 1)
     {
         return ret[0];
     }
     var ec = new IllegalStateException("Multiple defaults for non-set named parameter! " + name.GetFullName());
     Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ec, LOGGER);
     return null; // this line would be never reached as Throw will throw an exception
 }
コード例 #5
0
        public INode RegisterType(Type type)
        {
            if (ReflectionUtilities.IsAnonymousType(type))
            {
                // DevNote: Kinda hacky way to indicate the no-op case.
                return(rootNode);
            }

            INode n = GetAlreadyBoundNode(type);

            if (n != null)
            {
                return(n);
            }

            if (type.BaseType != null)
            {
                RegisterType(type.BaseType);
            }

            foreach (Type interf in type.GetInterfaces())
            {
                RegisterType(ReflectionUtilities.EnsureInterfaceType(interf));
            }

            Type enclosingClass = type.DeclaringType; // this.GetEnclosingClass(type);

            if (enclosingClass != null)
            {
                RegisterType(enclosingClass);
            }

            INode node = RegisterClass(type);

            foreach (Type inner in type.GetNestedTypes())
            {
                RegisterType(inner);
            }

            IClassNode classNode = node as ClassNodeImpl;

            if (classNode != null)
            {
                foreach (IConstructorDef constructorDef in classNode.GetInjectableConstructors())
                {
                    foreach (IConstructorArg constructorArg in constructorDef.GetArgs())
                    {
                        if (constructorArg.Gettype() == null)
                        {
                            Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new ArgumentException("not type in arg"), LOGGER);
                        }
                        RegisterType(constructorArg.Gettype());  // Gettype returns param's Type.fullname
                        if (constructorArg.GetNamedParameterName() != null)
                        {
                            var pn = RegisterType(constructorArg.GetNamedParameterName());

                            if (!(pn is INamedParameterNode))
                            {
                                string message = string.Format(CultureInfo.CurrentCulture,
                                                               "The class {0}, used in the constructor of {1}, should not be defined as a NamedParameter.",
                                                               constructorArg.GetNamedParameterName(),
                                                               constructorDef.GetClassName());
                                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new ArgumentException(message), LOGGER);
                            }

                            INamedParameterNode np = (INamedParameterNode)RegisterType(constructorArg.GetNamedParameterName());
                            try
                            {
                                if (np.IsSet() || np.IsList())
                                {
                                    // throw new NotImplementedException();
                                }
                                else
                                {
                                    if (!ReflectionUtilities.IsCoercable(ClassForName(constructorArg.Gettype()), ClassForName(np.GetFullArgName())))
                                    {
                                        var e = new ClassHierarchyException(
                                            "Named parameter type mismatch in " + classNode.GetFullName() + ".  Constructor expects a "
                                            + constructorArg.Gettype() + " but " + np.GetName() + " is a "
                                            + np.GetFullArgName());
                                        Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                                    }
                                }
                            }
                            catch (TypeLoadException e)
                            {
                                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER);
                                var ex = new ClassHierarchyException("Constructor refers to unknown class "
                                                                     + constructorArg.GetType(), e);
                                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
                            }
                        }
                    }
                }
            }
            else
            {
                INamedParameterNode npNode = node as INamedParameterNode;
                if (npNode != null)
                {
                    RegisterType(npNode.GetFullArgName());
                }
            }

            return(node);
        }
コード例 #6
0
        private static ClassHierarchyProto.Node SerializeNode(INode n)
        {
            IList <ClassHierarchyProto.Node> children = new List <ClassHierarchyProto.Node>();

            foreach (INode child in n.GetChildren())
            {
                children.Add(SerializeNode(child));
            }

            if (n is IClassNode)
            {
                IClassNode cn = (IClassNode)n;
                IList <IConstructorDef> injectable = cn.GetInjectableConstructors();
                IList <IConstructorDef> all        = cn.GetAllConstructors();
                IList <IConstructorDef> others     = new List <IConstructorDef>(all);

                foreach (var c in injectable)
                {
                    others.Remove(c);
                }

                IList <ClassHierarchyProto.ConstructorDef> injectableConstructors = new List <ClassHierarchyProto.ConstructorDef>();
                foreach (IConstructorDef inj in injectable)
                {
                    injectableConstructors.Add(SerializeConstructorDef(inj));
                }

                IList <ClassHierarchyProto.ConstructorDef> otherConstructors = new List <ClassHierarchyProto.ConstructorDef>();
                foreach (IConstructorDef other in others)
                {
                    otherConstructors.Add(SerializeConstructorDef(other));
                }

                List <string> implFullNames = new List <string>();
                foreach (IClassNode impl in cn.GetKnownImplementations())
                {
                    implFullNames.Add(impl.GetFullName());  //we use class fully qualifed name
                }

                return(NewClassNode(cn.GetName(), cn.GetFullName(),
                                    cn.IsInjectionCandidate(), cn.IsExternalConstructor(), cn.IsUnit(),
                                    injectableConstructors, otherConstructors, implFullNames, children));
            }
            if (n is INamedParameterNode)
            {
                INamedParameterNode np = (INamedParameterNode)n;
                return(NewNamedParameterNode(np.GetName(), np.GetFullName(),
                                             np.GetSimpleArgName(), np.GetFullArgName(), np.IsSet(), np.IsList(), np.GetDocumentation(),
                                             np.GetShortName(), np.GetDefaultInstanceAsStrings(), children));
            }
            if (n is IPackageNode)
            {
                return(NewPackageNode(n.GetName(), n.GetFullName(), children));
            }
            Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException("Encountered unknown type of Node: " + n), LOGGER);
            return(null);
        }