Ejemplo n.º 1
0
        private AvroNode NewAvroNode(INode n)
        {
            IList <AvroNode> children = new List <AvroNode>();

            foreach (INode child in n.GetChildren())
            {
                children.Add(NewAvroNode(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 <AvroConstructorDef> injectableConstructors = new List <AvroConstructorDef>();
                foreach (IConstructorDef inj in injectable)
                {
                    injectableConstructors.Add(NewConstructorDef(inj));
                }

                IList <AvroConstructorDef> otherConstructors = new List <AvroConstructorDef>();
                foreach (IConstructorDef other in others)
                {
                    otherConstructors.Add(NewConstructorDef(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));
            }

            Utilities.Diagnostics.Exceptions.Throw(
                new IllegalStateException("Encountered unknown type of Node: " + n), LOGGER);
            return(null);
        }
Ejemplo n.º 2
0
 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
 }
Ejemplo n.º 3
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)
                        {
                            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);
        }