Exemplo n.º 1
0
        public static INode CreateClassNode(INode parent, Type type)
        {
            var    namedParameter = type.GetCustomAttribute <NamedParameterAttribute>();
            var    isUnit         = null != type.GetCustomAttribute <UnitAttribute>();
            string simpleName     = type.Name;
            string fullName       = type.FullName;
            bool   isStatic       = type.IsSealed && type.IsAbstract;
            bool   injectable     = true;                      // TODO
            bool   isAssignableFromExternalConstructor = true; //TODO

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

            foreach (var c in type.GetConstructors())
            {
                var isConstructorInjectable = null != c.GetCustomAttribute <InjectAttribute>();

                ConstructorDefImpl constructorDef = CreateConstructorDef(injectable, c, isConstructorInjectable);

                if (isConstructorInjectable)
                {
                    injectableConstructors.Add(constructorDef);
                }
                allConstructors.Add(constructorDef);
            }

            String defaultImplementation = null;
            var    defaultImpl           = type.GetCustomAttribute <DefaultImplementationAttribute>();

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

            return(new ClassNodeImpl(parent, simpleName, fullName, isUnit, injectable, isAssignableFromExternalConstructor, injectableConstructors, allConstructors, defaultImplementation));
        }
        private static void ParseSubHierarchy(INode parent, ClassHierarchyProto.Node n)
        {
            INode parsed;
            if (n.package_node != null)
            {
                parsed = new PackageNodeImpl(parent, n.name, n.full_name);
            }
            else if (n.named_parameter_node != null)
            {
                ClassHierarchyProto.NamedParameterNode np = n.named_parameter_node;
                parsed = new NamedParameterNodeImpl(parent, n.name,
                    n.full_name, np.full_arg_class_name, np.simple_arg_class_name,
                    np.is_set, np.documentation, np.short_name,
                    np.instance_default.ToArray());
            }
            else if (n.class_node != null)
            {
                ClassHierarchyProto.ClassNode cn = n.class_node;
                IList<IConstructorDef> injectableConstructors = new List<IConstructorDef>();
                IList<IConstructorDef> allConstructors = new List<IConstructorDef>();

                foreach (ClassHierarchyProto.ConstructorDef injectable in cn.InjectableConstructors)
                {
                    IConstructorDef def = ParseConstructorDef(injectable, true);
                    injectableConstructors.Add(def);
                    allConstructors.Add(def);
                }
                foreach (ClassHierarchyProto.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
            {
                throw new IllegalStateException("Bad protocol buffer: got abstract node" + n);
            }

            foreach (ClassHierarchyProto.Node child in n.children)
            {
                ParseSubHierarchy(parsed, child);
            }
        }