Example #1
0
        public void TestNamedParameterWithAlias()
        {
            var ns = TangFactory.GetTang().GetDefaultClassHierarchy();
            INamedParameterNode cls = (INamedParameterNode)ns.GetNode(typeof(NamedParameterWithAlias).AssemblyQualifiedName);

            Assert.True(cls.GetAlias().Equals("org.apache.REEF.tang.tests.classHierarchy.NamedParameterWithAlias"));
            Assert.True(cls.GetAliasLanguage().ToString().Equals(Language.Java.ToString()));
        }
 private void AddAlias(INamedParameterNode np)
 {
     if (!string.IsNullOrEmpty(np.GetAlias()))
     {
         IDictionary <string, string> mapping = null;
         _aliasLookupTable.TryGetValue(np.GetAliasLanguage().ToString(), out mapping);
         if (mapping == null)
         {
             mapping = new Dictionary <string, string>();
             _aliasLookupTable.Add(np.GetAliasLanguage().ToString(), mapping);
         }
         try
         {
             mapping.Add(np.GetAlias(), np.GetFullName());
         }
         catch (Exception)
         {
             var e = new ApplicationException(string.Format(CultureInfo.CurrentCulture, "Duplicated alias {0} on named parameter {1}.", np.GetAlias(), np.GetFullName()));
             Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
         }
     }
 }
        private static Org.Apache.REEF.Tang.Protobuf.Node SerializeNode(INode n)
        {
            IList <Org.Apache.REEF.Tang.Protobuf.Node> children = new List <Org.Apache.REEF.Tang.Protobuf.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 <Org.Apache.REEF.Tang.Protobuf.ConstructorDef> injectableConstructors = new List <Org.Apache.REEF.Tang.Protobuf.ConstructorDef>();
                foreach (IConstructorDef inj in injectable)
                {
                    injectableConstructors.Add(SerializeConstructorDef(inj));
                }

                IList <Org.Apache.REEF.Tang.Protobuf.ConstructorDef> otherConstructors = new List <Org.Apache.REEF.Tang.Protobuf.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, np.GetAlias(), np.GetAliasLanguage()));
            }
            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);
        }
        private void AddAlias(INamedParameterNode np)
        {
            if (np.GetAlias() != null && !np.GetAlias().Equals(""))
            {
                IDictionary<string, string> mapping = null;
                _aliasLookupTable.TryGetValue(np.GetAliasLanguage(), out mapping);
                if (mapping == null)
                {
                    mapping = new Dictionary<string, string>();
                    _aliasLookupTable.Add(np.GetAliasLanguage(), mapping);
                }

                try
                {
                    mapping.Add(np.GetAlias(), np.GetFullName());
                }
                catch (Exception)
                {
                    var e = new ApplicationException(string.Format(CultureInfo.CurrentCulture, "Duplicated alias {0} on named parameter {1}.", np.GetAlias(), np.GetFullName()));
                    Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }
            }
        }
Example #5
0
        public INode BuildPathToNode(Type type)
        {
            INode parent = GetParentNode(type);

            Type argType = ReflectionUtilities.GetNamedParameterTargetOrNull(type);

            if (argType == null)
            {
                return(NodeFactory.CreateClassNode(parent, type));
            }
            INamedParameterNode np = NodeFactory.CreateNamedParameterNode(parent, type, argType);

            if (Parameterparser.CanParse(ReflectionUtilities.GetAssemblyQualifiedName(argType)))
            {
                if (type.GetCustomAttribute <NamedParameterAttribute>().DefaultClass != null)
                {
                    var e = new ClassHierarchyException("Named parameter " + ReflectionUtilities.GetAssemblyQualifiedName(type) + " defines default implementation for parsable type " + ReflectionUtilities.GetAssemblyQualifiedName(argType));
                    Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }
            }

            if (!string.IsNullOrEmpty(np.GetAlias()))
            {
                IDictionary <string, string> mapping = null;
                _aliasLookupTable.TryGetValue(np.GetAliasLanguage().ToString(), out mapping);
                if (null == mapping)
                {
                    mapping = new Dictionary <string, string>();
                    _aliasLookupTable.Add(np.GetAliasLanguage().ToString(), mapping);
                }
                try
                {
                    mapping.Add(np.GetAlias(), np.GetFullName());
                }
                catch (Exception)
                {
                    var e = new ApplicationException(string.Format(CultureInfo.CurrentCulture, "Duplicated alias {0} on named parameter {1}.", np.GetAlias(), np.GetFullName()));
                    Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }
            }

            string shortName = np.GetShortName();

            if (shortName != null && !shortName.Equals(string.Empty))
            {
                INamedParameterNode oldNode = null;
                shortNames.TryGetValue(shortName, out oldNode);
                if (oldNode != null)
                {
                    if (oldNode.GetFullName().Equals(np.GetFullName()))
                    {
                        var ex = new IllegalStateException("Tried to double bind "
                                                           + oldNode.GetFullName() + " to short name " + shortName);
                        Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER);
                    }
                    var e = new ClassHierarchyException("Named parameters " + oldNode.GetFullName()
                                                        + " and " + np.GetFullName() + " have the same short name: "
                                                        + shortName);
                    Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER);
                }
                shortNames.Add(shortName, np);
            }
            return(np);
        }