Exemplo n.º 1
0
        private static void ParseSubHierarchy(INode parent, Node n)
        {
            INode parsed = null;
            if (n.package_node != null)
            {
                parsed = new PackageNodeImpl(parent, n.name, n.full_name);
            }
            else if (n.named_parameter_node != null)
            {
                NamedParameterNode np = n.named_parameter_node;

                if (!string.IsNullOrWhiteSpace(np.alias_name) && !string.IsNullOrWhiteSpace(np.alias_language))
                {
                    Language language;
                    try
                    {                        
                        Enum.TryParse(np.alias_language, true, out language);
                    }
                    catch (Exception)
                    {
                        string msg = string.Format(CultureInfo.CurrentCulture, "Language {0} passed in is not supported", np.alias_language);
                        throw new ArgumentException(msg);
                    }

                    parsed = new NamedParameterNodeImpl(parent, n.name,
                        n.full_name, np.full_arg_class_name, np.simple_arg_class_name,
                        np.is_set, np.is_list, np.documentation, np.short_name,
                        np.instance_default.ToArray(), np.alias_name, language);
                }
                else
                {
                    parsed = new NamedParameterNodeImpl(parent, n.name,
                       n.full_name, np.full_arg_class_name, np.simple_arg_class_name,
                       np.is_set, np.is_list, np.documentation, np.short_name,
                       np.instance_default.ToArray());
                }
            }
            else if (n.class_node != null)
            {
                ClassNode cn = n.class_node;
                IList<IConstructorDef> injectableConstructors = new List<IConstructorDef>();
                IList<IConstructorDef> allConstructors = new List<IConstructorDef>();

                foreach (ConstructorDef injectable in cn.InjectableConstructors)
                {
                    IConstructorDef def = ParseConstructorDef(injectable, true);
                    injectableConstructors.Add(def);
                    allConstructors.Add(def);
                }
                foreach (ConstructorDef other in cn.OtherConstructors)
                {
                    IConstructorDef def = ParseConstructorDef(other, false);
                    allConstructors.Add(def);
                }

                IConstructorDef[] dummy = new ConstructorDefImpl[0];
                parsed = new ClassNodeImpl(parent, n.name, n.full_name,
                cn.is_unit, cn.is_injection_candidate,
                cn.is_external_constructor, injectableConstructors,
                allConstructors, cn.default_implementation);
            }
            else
            {
                Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException("Bad protocol buffer: got abstract node" + n), LOGGER); 
            }

            foreach (Node child in n.children)
            {
                ParseSubHierarchy(parsed, child);
            }
        }
Exemplo n.º 2
0
        public static INode CreateClassNode(INode parent, Type clazz)
        {
            // var namedParameter = clazz.GetCustomAttribute<NamedParameterAttribute>();
            var    unit       = clazz.GetCustomAttribute <UnitAttribute>() != null;
            string simpleName = ReflectionUtilities.GetName(clazz);
            string fullName   = ReflectionUtilities.GetAssemblyQualifiedName(clazz);

            //// bool isStatic = true; // clazz.IsSealed && clazz.IsAbstract; always true in C# for Java static class
            //// bool injectable = true; // always true in C#

            bool isAssignableFromExternalConstructor = ReflectionUtilities.IsAssignableFromIgnoreGeneric(typeof(IExternalConstructor <>), clazz);

            // bool parentIsUnit = false;

            ////No such thing in C#, should be false
            ////bool foundNonStaticInnerClass = false;
            ////foreach (Type c in clazz.getNestedTypes()) {
            ////  if (!Modifier.isStatic(c.getModifiers())) {
            ////    foundNonStaticInnerClass = true;
            ////  }
            ////}

            var injectableConstructors = new List <IConstructorDef>();
            var allConstructors        = new List <IConstructorDef>();

            foreach (ConstructorInfo c in clazz.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance))
            {
                var constructorAnnotatedInjectable = c.GetCustomAttribute <InjectAttribute>() != null;

                bool constructorInjectable = constructorAnnotatedInjectable;

                ConstructorDefImpl constructorDef = CreateConstructorDef(c, constructorAnnotatedInjectable);

                if (constructorInjectable)
                {
                    // if (injectableConstructors.Contains(constructorDef))
                    if (constructorDef.IsInList(injectableConstructors))
                    {
                        var e = new ClassHierarchyException(
                            "Ambiguous boundConstructors detected in class " + clazz + ": "
                            + constructorDef + " differs from some other" + " constructor only "
                            + "by parameter order.");
                        Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                    }
                    else
                    {
                        injectableConstructors.Add(constructorDef);
                    }
                }
                allConstructors.Add(constructorDef);
            }

            string defaultImplementation = null;
            DefaultImplementationAttribute defaultImpl = clazz.GetCustomAttribute <DefaultImplementationAttribute>();

            if (defaultImpl != null)
            {
                Type defaultImplementationClazz = defaultImpl.Value;

                if (defaultImplementationClazz == null)
                {
                    defaultImplementation = defaultImpl.Name;
                }
                else
                {
                    if (!ReflectionUtilities.IsAssignableFromIgnoreGeneric(clazz, defaultImplementationClazz))
                    {
                        var e = new ClassHierarchyException(clazz
                                                            + " declares its default implementation to be non-subclass "
                                                            + defaultImplementationClazz);
                        Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                    }
                    defaultImplementation = ReflectionUtilities.GetAssemblyQualifiedName(defaultImplementationClazz);
                }
            }
            else
            {
                defaultImplementation = null;
            }

            return(new ClassNodeImpl(parent, simpleName, fullName, unit, true, isAssignableFromExternalConstructor,
                                     injectableConstructors, allConstructors, defaultImplementation));
        }
        private static void ParseSubHierarchy(INode parent, Org.Apache.REEF.Tang.Protobuf.Node n)
        {
            INode parsed = null;
            if (n.package_node != null)
            {
                parsed = new PackageNodeImpl(parent, n.name, n.full_name);
            }
            else if (n.named_parameter_node != null)
            {
                Org.Apache.REEF.Tang.Protobuf.NamedParameterNode np = n.named_parameter_node;

                if (np.alias_name != null && np.alias_language != null)
                {
                    parsed = new NamedParameterNodeImpl(parent, n.name,
                        n.full_name, np.full_arg_class_name, np.simple_arg_class_name,
                        np.is_set, np.is_list, np.documentation, np.short_name,
                        np.instance_default.ToArray(), np.alias_name, np.alias_language);
                }
                else
                {
                    parsed = new NamedParameterNodeImpl(parent, n.name,
                       n.full_name, np.full_arg_class_name, np.simple_arg_class_name,
                       np.is_set, np.is_list, np.documentation, np.short_name,
                       np.instance_default.ToArray());
                }
            }
            else if (n.class_node != null)
            {
                Org.Apache.REEF.Tang.Protobuf.ClassNode cn = n.class_node;
                IList<IConstructorDef> injectableConstructors = new List<IConstructorDef>();
                IList<IConstructorDef> allConstructors = new List<IConstructorDef>();

                foreach (Org.Apache.REEF.Tang.Protobuf.ConstructorDef injectable in cn.InjectableConstructors)
                {
                    IConstructorDef def = ParseConstructorDef(injectable, true);
                    injectableConstructors.Add(def);
                    allConstructors.Add(def);
                }
                foreach (Org.Apache.REEF.Tang.Protobuf.ConstructorDef other in cn.OtherConstructors)
                {
                    IConstructorDef def = ParseConstructorDef(other, false);
                    allConstructors.Add(def);

                }

                IConstructorDef[] dummy = new ConstructorDefImpl[0];
                parsed = new ClassNodeImpl(parent, n.name, n.full_name,
                cn.is_unit, cn.is_injection_candidate,
                cn.is_external_constructor, injectableConstructors,
                allConstructors, cn.default_implementation);
            }
            else
            {
                Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException("Bad protocol buffer: got abstract node" + n), LOGGER); 
            }

            foreach (Org.Apache.REEF.Tang.Protobuf.Node child in n.children)
            {
                ParseSubHierarchy(parsed, child);
            }
        }