http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ConstructorDefImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ConstructorDefImpl.cs b/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ConstructorDefImpl.cs deleted file mode 100644 index 4e72da7..0000000 --- a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ConstructorDefImpl.cs +++ /dev/null @@ -1,203 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using Org.Apache.Reef.Utilities.Logging; -using Org.Apache.Reef.Tang.Exceptions; -using Org.Apache.Reef.Tang.Types; - -namespace Org.Apache.Reef.Tang.Implementations -{ - public class ConstructorDefImpl : IConstructorDef - { - private static readonly Logger LOGGER = Logger.GetLogger(typeof(ConstructorDefImpl)); - - private readonly IList<IConstructorArg> args = new List<IConstructorArg>(); - private readonly String className; - - public ConstructorDefImpl(String className, IConstructorArg[] args, bool injectable) - { - this.args = args; - this.className = className; - if (injectable) - { - var duplicateItems = from x in args - group x by x into grouped - where grouped.Count() > 1 - select grouped.Key; - - if (duplicateItems.Any()) - { - var e = new ClassHierarchyException( - "Repeated constructor parameter detected. " - + "Cannot inject constructor " + ToString()); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - } - } - - public IList<IConstructorArg> GetArgs() - { - return args; - } - - public String GetClassName() - { - return className; - } - - private String Join(String sep, Object[] vals) - { - if (vals.Length != 0) - { - StringBuilder sb = new StringBuilder(vals[0].ToString()); - for (int i = 1; i < vals.Length; i++) - { - sb.Append(sep + vals[i]); - } - return sb.ToString(); - } - else - { - return ""; - } - } - - public override String ToString() - { - StringBuilder sb = new StringBuilder(className); - sb.Append("("); - sb.Append(Join(",", args.ToArray())); - sb.Append(")"); - return sb.ToString(); - } - - // Return true if our list of args is a superset of those in def. - public bool IsMoreSpecificThan(IConstructorDef def) - { - // Is everything in def also in this? - for (int i = 0; i < def.GetArgs().Count; i++) - { - bool found = false; - for (int j = 0; j < this.GetArgs().Count; j++) - { - if (GetArgs()[j].Equals(def.GetArgs()[i])) - { - found = true; - break; - } - } - // If not, then argument j from def is not in our list. Return false. - if (found == false) - return false; - } - // Everything in def's arg list is in ours. Do we have at least one extra - // argument? - return GetArgs().Count > def.GetArgs().Count; - } - - public bool TakesParameters(IList<IClassNode> paramTypes) - { - if (paramTypes.Count != args.Count) - { - return false; - } - - int i = 0; - foreach (INode t in paramTypes) - { - string s; - if (t is INamedParameterNode) - { - s = ((INamedParameterNode)paramTypes[i]).GetFullArgName(); - } - else - { - s = paramTypes[i].GetFullName(); - } - if (!args[i].Gettype().Equals(s)) - { - return false; - } - else - { - i++; - } - - } - return true; - } - - public override bool Equals(Object o) - { - return EqualsIgnoreOrder((IConstructorDef)o); - } - - public override int GetHashCode() - { - return 0; - } - - //A(int i, string j) vs. A(string i, int j) is Ambiguous in injection - private bool EqualsIgnoreOrder(IConstructorDef def) - { - if (GetArgs().Count != def.GetArgs().Count) - { - return false; - } - for (int i = 0; i < GetArgs().Count; i++) - { - bool found = false; - for (int j = 0; j < def.GetArgs().Count; j++) - { - if (GetArgs()[i].GetName().Equals(def.GetArgs()[j].GetName())) - { - found = true; - } - } - if (!found) - { - return false; - } - } - return true; - } - - public int CompareTo(object obj) - { - IConstructorDef o = (IConstructorDef)obj; - return ToString().CompareTo(o.ToString()); - } - - public bool IsInList(IList<IConstructorDef> list ) - { - foreach (IConstructorDef def in list) - { - if (CompareTo(def) == 0) - { - return true; - } - - } - return false; - } - } -}
http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/NamedParameterNodeImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/NamedParameterNodeImpl.cs b/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/NamedParameterNodeImpl.cs deleted file mode 100644 index 3bdc868..0000000 --- a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/NamedParameterNodeImpl.cs +++ /dev/null @@ -1,88 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -using System; -using Org.Apache.Reef.Tang.Types; - -namespace Org.Apache.Reef.Tang.Implementations -{ - public class NamedParameterNodeImpl : AbstractNode, INamedParameterNode - { - private readonly String fullArgName; - private readonly String simpleArgName; - private readonly String documentation; - private readonly String shortName; - private readonly String[] defaultInstanceAsStrings; - private readonly bool isSet; - private readonly bool isList; - - public NamedParameterNodeImpl(INode parent, String simpleName, - String fullName, String fullArgName, String simpleArgName, bool isSet, bool isList, - String documentation, String shortName, String[] defaultInstanceAsStrings) - : base(parent, simpleName, fullName) - { - this.fullArgName = fullArgName; - this.simpleArgName = simpleArgName; - this.isSet = isSet; - this.isList = isList; - this.documentation = documentation; - this.shortName = shortName; - this.defaultInstanceAsStrings = defaultInstanceAsStrings; - } - - public override String ToString() - { - return GetSimpleArgName() + " " + GetName(); - } - - public String GetSimpleArgName() - { - return simpleArgName; - } - - public String GetFullArgName() - { - return fullArgName; - } - - public String GetDocumentation() - { - return documentation; - } - - public String GetShortName() - { - return shortName; - } - - public String[] GetDefaultInstanceAsStrings() - { - return defaultInstanceAsStrings; - } - - public bool IsSet() - { - return isSet; - } - - public bool IsList() - { - return isList; - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/NodeFactory.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/NodeFactory.cs b/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/NodeFactory.cs deleted file mode 100644 index 474f2d5..0000000 --- a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/NodeFactory.cs +++ /dev/null @@ -1,315 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -using System; -using System.Collections.Generic; -using System.Reflection; -using Org.Apache.Reef.Utilities.Logging; -using Org.Apache.Reef.Tang.Annotations; -using Org.Apache.Reef.Tang.Types; -using Org.Apache.Reef.Tang.Util; -using Org.Apache.Reef.Tang.Exceptions; -using Org.Apache.Reef.Tang.Interface; - -namespace Org.Apache.Reef.Tang.Implementations -{ - public class NodeFactory - { - private static readonly Logger LOGGER = Logger.GetLogger(typeof(NodeFactory)); - - public static IPackageNode CreateRootPackageNode() - { - return new PackageNodeImpl(); - } - - public static INode CreateClassNode(INode parent, Type clazz) - { - //var namedParameter = clazz.GetCustomAttribute<NamedParameterAttribute>(); - var unit = null != clazz.GetCustomAttribute<UnitAttribute>(); - 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 = null != c.GetCustomAttribute<InjectAttribute>(); - - 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 (null != defaultImpl) - { - 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); - } - - //TODO - private static ConstructorDefImpl CreateConstructorDef(ConstructorInfo constructor, bool injectable) - { - var parameters = constructor.GetParameters(); - - IConstructorArg[] args = new ConstructorArgImpl[parameters.Length]; - - for (int i = 0; i < parameters.Length; i++) - { - //TODO for getInterfaceTarget() call - Type type = parameters[i].ParameterType; - type = ReflectionUtilities.EnsureInterfaceType(type); - //if (type.IsGenericType && type.FullName == null) - //{ - // type = type.GetGenericTypeDefinition(); - //} - bool isFuture; - - if(ReflectionUtilities.IsAssignableFromIgnoreGeneric(typeof(IInjectionFuture<>), type)) - { - type = ReflectionUtilities.GetInterfaceTarget(typeof(IInjectionFuture<>), type); - isFuture = true; - } - else - { - isFuture = false; - } - - ParameterAttribute named = parameters[i].GetCustomAttribute<ParameterAttribute>(); - if (named != null && !injectable) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new ClassHierarchyException(constructor + " is not injectable, but it has an @Parameter annotation."), LOGGER); - } - - if (type == null) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new ApplicationException("Exception"), LOGGER); - } - - string typename = ReflectionUtilities.GetAssemblyQualifiedName(type); - if (typename == null) - { - typename = type.Name; - } - args[i] = new ConstructorArgImpl(typename, named == null ? null : ReflectionUtilities.GetAssemblyQualifiedName(named.Value), isFuture); - } - return new ConstructorDefImpl(ReflectionUtilities.GetAssemblyQualifiedName(constructor.DeclaringType), args, injectable); - } - - public static INamedParameterNode CreateNamedParameterNode(INode parent, Type clazz, Type argClass) - { - Type setRawArgType = ReflectionUtilities.GetInterfaceTarget(typeof(ISet<>), argClass); - bool isSet = setRawArgType != null; - if(isSet) { - argClass = setRawArgType; - } - - Type listRawArgType = ReflectionUtilities.GetInterfaceTargetForType(typeof (IList<>), argClass); - bool isList = listRawArgType != null; - if (isList) - { - argClass = listRawArgType; - } - - string simpleName = ReflectionUtilities.GetName(clazz); - string fullName = ReflectionUtilities.GetAssemblyQualifiedName(clazz); - string fullArgName = ReflectionUtilities.GetAssemblyQualifiedName(argClass); - string simpleArgName = ReflectionUtilities.GetName(argClass); - - NamedParameterAttribute namedParameter = clazz.GetCustomAttribute<NamedParameterAttribute>(); - - if (namedParameter == null) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException("Got name without named parameter post-validation!"), LOGGER); - } - - bool hasStringDefault, hasClassDefault, hasStringSetDefault, hasClassSetDefault; - int default_count = 0; - //if(!namedParameter.default_value().isEmpty()) { //QUESTION: difference from below? - if (namedParameter.DefaultValue != null && namedParameter.DefaultValue.Length > 0) - { - hasStringDefault = true; - default_count++; - } - else - { - hasStringDefault = false; - } - - if (namedParameter.DefaultClass != null /*Void.class*/) - { - hasClassDefault = true; - default_count++; - } - else - { - hasClassDefault = false; - } - - if (namedParameter.DefaultValues != null && namedParameter.DefaultValues.Length > 0) - { - hasStringSetDefault = true; - default_count++; - } - else - { - hasStringSetDefault = false; - } - - if (namedParameter.DefaultClasses != null && namedParameter.DefaultClasses.Length > 0) - { - hasClassSetDefault = true; - default_count++; - } - else - { - hasClassSetDefault = false; - } - - if (default_count > 1) - { - var e = new ClassHierarchyException("Named parameter " + fullName + " defines more than one of default_value, default_class, default_values and default_classes"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - - string[] defaultInstanceAsStrings = new string[]{}; - - if (default_count == 0) - { - defaultInstanceAsStrings = new String[] { }; - } - else if (hasClassDefault) - { - Type default_class = namedParameter.DefaultClass; - AssertIsSubclassOf(clazz, default_class, argClass); - defaultInstanceAsStrings = new String[] { ReflectionUtilities.GetAssemblyQualifiedName(default_class) }; - } - else if (hasStringDefault) - { - // Don't know if the string is a class or literal here, so don't bother validating. - defaultInstanceAsStrings = new String[] { namedParameter.DefaultValue }; - } - else if (hasClassSetDefault) - { - Type[] clzs = namedParameter.DefaultClasses; - defaultInstanceAsStrings = new String[clzs.Length]; - for (int i = 0; i < clzs.Length; i++) - { - AssertIsSubclassOf(clazz, clzs[i], argClass); - defaultInstanceAsStrings[i] = ReflectionUtilities.GetAssemblyQualifiedName(clzs[i]); - } - } - else if (hasStringSetDefault) - { - defaultInstanceAsStrings = namedParameter.DefaultValues; - } - else - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException(), LOGGER); - } - - string documentation = namedParameter.Documentation; - string shortName = namedParameter.ShortName; - if (namedParameter.ShortName != null && namedParameter.ShortName.Length == 0) - { - shortName = null; - } - - return new NamedParameterNodeImpl(parent, simpleName, fullName, - fullArgName, simpleArgName, isSet, isList, documentation, shortName, defaultInstanceAsStrings); - } - - // private static void assertIsSubclassOf(Class<?> named_parameter, Class<?> default_class, Type argClass) { - private static void AssertIsSubclassOf(Type namedparameter, Type defaultclass, Type argClass) - { - bool isSubclass = false; - string argClassName = ReflectionUtilities.GetAssemblyQualifiedName(argClass); - foreach (Type t in ReflectionUtilities.ClassAndAncestors(defaultclass)) - { - if (argClassName.Equals(ReflectionUtilities.GetAssemblyQualifiedName(t))) - { - isSubclass = true; - } - } - if (!(isSubclass)) - { - var e = new ClassHierarchyException(namedparameter + " defines a default class " - + ReflectionUtilities.GetName(defaultclass) + " with a type that does not extend of its target's type " + argClass); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/PackageNodeImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/PackageNodeImpl.cs b/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/PackageNodeImpl.cs deleted file mode 100644 index 7516493..0000000 --- a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/PackageNodeImpl.cs +++ /dev/null @@ -1,49 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -using System; -using Org.Apache.Reef.Tang.Types; - -namespace Org.Apache.Reef.Tang.Implementations -{ - public class PackageNodeImpl : AbstractNode, IPackageNode - { - public PackageNodeImpl(INode parent, String name, String fullName) : - base(parent, name, fullName) - { - } - - public PackageNodeImpl() - : base(null, "", "[root node]") - { - } - - /** - * Unlike normal nodes, the root node needs to take the package name of its - * children into account. Therefore, we use the full name as the key when - * we insert nodes into the root. - */ - public override void Add(INode n) { - if (children.Count == 289) - { - Console.WriteLine("Test"); - } - children.Add(n.GetFullName(), n); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ParameterParser.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ParameterParser.cs b/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ParameterParser.cs deleted file mode 100644 index 840e447..0000000 --- a/lang/cs/Source/TANG/Tang/Implementations/ClassHierarchy/ParameterParser.cs +++ /dev/null @@ -1,199 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -using System; -using System.Collections.Generic; -using System.Reflection; -using Org.Apache.Reef.Utilities.Logging; -using Org.Apache.Reef.Tang.Exceptions; -using Org.Apache.Reef.Tang.Interface; -using Org.Apache.Reef.Tang.Util; - -namespace Org.Apache.Reef.Tang.Implementations -{ - public class ParameterParser - { - private static readonly Logger LOGGER = Logger.GetLogger(typeof(ParameterParser)); - - MonotonicTreeMap<String, ConstructorInfo> parsers = new MonotonicTreeMap<String, ConstructorInfo>(); - - //ec: ACons, tc: A - public void AddParser(Type ec) - { - Type tc = (Type)ReflectionUtilities.GetInterfaceTarget(typeof(IExternalConstructor<>), ec); - AddParser(tc, ec); - } - - //TODO - //public <T, U extends T> void AddParser(Class<U> clazz, Class<? extends ExternalConstructor<T>> ec) throws BindException { - //public void AddParser<T, U, V>(GenericType<U> clazz, GenericType<V> ec) - // where U : T - // where V: IExternalConstructor<T> - //{ - - //clazz: A, ec: ACons - private void AddParser(Type clazz, Type ec) - { - ConstructorInfo c = ec.GetConstructor(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance, null, new Type[] { typeof(string) }, null); - - if (c == null) - { - var e = new BindException("Constructor " + ReflectionUtilities.GetAssemblyQualifiedName(ec) + "(String) does not exist!"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - - //c.setAccessible(true); //set as public - parsers.Add(ReflectionUtilities.GetAssemblyQualifiedName(clazz), c); - } - - - public void MergeIn(ParameterParser p) - { - foreach (string s in p.parsers.Keys) - { - if (!parsers.ContainsKey(s)) - { - ConstructorInfo ci; - p.parsers.TryGetValue(s, out ci); - parsers.Add(s, ci); - } - else - { - ConstructorInfo oldC; - ConstructorInfo newC; - parsers.TryGetValue(s, out oldC); - p.parsers.TryGetValue(s, out newC); - if (!oldC.Equals(newC)) - { - var e = new ArgumentException( - "Conflict detected when merging parameter parsers! To parse " + s - + " I have a: " + ReflectionUtilities.GetAssemblyQualifiedName(oldC.DeclaringType) - + " the other instance has a: " + ReflectionUtilities.GetAssemblyQualifiedName(newC.DeclaringType)); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); - } - } - } - } - - //(Integer, "3") return object of new Integer(3) - public object Parse(Type c, String s) - { - Type d = ReflectionUtilities.BoxClass(c); - foreach (Type e in ReflectionUtilities.ClassAndAncestors(d)) // get all the super classes of Integer for example - { - string name = ReflectionUtilities.GetAssemblyQualifiedName(e); - if (parsers.ContainsKey(name)) - { - object ret = Parse(name, s); - if (c.IsAssignableFrom(ret.GetType())) //check if ret can be cast as c - { - return ret; - } - else - { - var ex = new InvalidCastException("Cannot cast from " + ret.GetType() + " to " + c); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - } - } - return Parse(d.Name, s); - } - - //name: "Integer", value: "12" - public object Parse(string name, string value) - { - if (parsers.ContainsKey(name)) - { - try - { - ConstructorInfo c = null; - parsers.TryGetValue(name, out c); - var o = c.Invoke(new object[] { value }); - var m = o.GetType().GetMethod("NewInstance", BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); - return m.Invoke(o, new object[] {}); - } - catch (TargetInvocationException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new ArgumentException("Error invoking constructor for " + name, e), LOGGER); - } - } - else if (name.Equals(typeof(string).Name)) - { - return (object)value; - } - if (name.Equals(typeof(Byte).Name)) - { - return (object)(Byte)Byte.Parse(value); - } - if (name.Equals(typeof(Char).Name)) - { - return (object)(Char)value[0]; - } - if (name.Equals(typeof(short).Name)) - { - return (System.Int16)System.Int16.Parse(value); - } - if (name.Equals(typeof(int).Name)) - { - return (object)(Int32)Int32.Parse(value); - } - if (name.Equals(typeof(long).Name)) - { - return (object)(Int64)Int64.Parse(value); - } - if (name.Equals(typeof(float).Name)) - { - return (object)(Single)Single.Parse(value); - } - if (name.Equals(typeof(Double).Name)) - { - return (object)(Double)Double.Parse(value); - } - if (name.Equals(typeof(Boolean).Name)) - { - return (object)(Boolean)Boolean.Parse(value); - } - if (name.Equals(typeof(byte[]).Name)) - { - byte[] bytes = new byte[value.Length * sizeof(char)]; - System.Buffer.BlockCopy(value.ToCharArray(), 0, bytes, 0, bytes.Length); - return bytes; - } - return null; - } - - private static readonly ISet<string> BUILTIN_NAMES = new HashSet<string>(new string[] - { - typeof(string).AssemblyQualifiedName, - typeof(Byte).AssemblyQualifiedName, - typeof(char).AssemblyQualifiedName, - typeof(short).AssemblyQualifiedName, - typeof(Int32).AssemblyQualifiedName, - typeof(long).AssemblyQualifiedName, - typeof(float).AssemblyQualifiedName, - typeof(double).AssemblyQualifiedName, - typeof(bool).AssemblyQualifiedName - } ); - - public bool CanParse(string name) - { - return parsers.ContainsKey(name) || BUILTIN_NAMES.Contains(name); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Implementations/Configuration/ConfigurationBuilderImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Implementations/Configuration/ConfigurationBuilderImpl.cs b/lang/cs/Source/TANG/Tang/Implementations/Configuration/ConfigurationBuilderImpl.cs deleted file mode 100644 index cd2aa30..0000000 --- a/lang/cs/Source/TANG/Tang/Implementations/Configuration/ConfigurationBuilderImpl.cs +++ /dev/null @@ -1,364 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -using System; -using System.Collections.Generic; -using System.Globalization; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Org.Apache.Reef.Utilities.Logging; -using Org.Apache.Reef.Tang.Exceptions; -using Org.Apache.Reef.Tang.Interface; -using Org.Apache.Reef.Tang.Types; -using Org.Apache.Reef.Tang.Util; - -namespace Org.Apache.Reef.Tang.Implementations -{ - public class ConfigurationBuilderImpl : IConfigurationBuilder - { - public IClassHierarchy ClassHierarchy; - - public readonly IDictionary<IClassNode, IClassNode> BoundImpls = new MonotonicTreeMap<IClassNode, IClassNode>(); - public readonly IDictionary<IClassNode, IClassNode> BoundConstructors = new MonotonicTreeMap<IClassNode, IClassNode>(); - public readonly IDictionary<INamedParameterNode, String> NamedParameters = new MonotonicTreeMap<INamedParameterNode, String>(); - public readonly IDictionary<IClassNode, IConstructorDef> LegacyConstructors = new MonotonicTreeMap<IClassNode, IConstructorDef>(); - public readonly MonotonicMultiMap<INamedParameterNode, object> BoundSetEntries = new MonotonicMultiMap<INamedParameterNode, object>(); - public readonly IDictionary<INamedParameterNode, IList<object>> BoundLists = new MonotonicTreeMap<INamedParameterNode, IList<object>>(); - - public readonly static string INIT = "<init>"; - - private static readonly Logger LOGGER = Logger.GetLogger(typeof(ConfigurationBuilderImpl)); - - protected ConfigurationBuilderImpl() - { - this.ClassHierarchy = TangFactory.GetTang().GetDefaultClassHierarchy(); - } - - public ConfigurationBuilderImpl(IClassHierarchy classHierarchy) - { - this.ClassHierarchy = classHierarchy; - } - - protected ConfigurationBuilderImpl(string[] assemblies, IConfiguration[] confs, Type[] parsers) - { - this.ClassHierarchy = TangFactory.GetTang().GetDefaultClassHierarchy(assemblies, parsers); - foreach (IConfiguration tc in confs) - { - AddConfiguration(((ConfigurationImpl) tc)); - } - } - - public ConfigurationBuilderImpl(ConfigurationBuilderImpl t) - { - this.ClassHierarchy = t.GetClassHierarchy(); - try { - AddConfiguration(t.GetClassHierarchy(), t); - } - catch (BindException e) - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException("Could not copy builder", e), LOGGER); - } - } - - protected ConfigurationBuilderImpl(string[] assemblies) : this(assemblies, new IConfiguration[0], new Type[0]) - { - } - - protected ConfigurationBuilderImpl(IConfiguration[] confs) : this(new string[0], confs, new Type[0]) - { - } - - public void AddConfiguration(IConfiguration conf) - { - AddConfiguration(conf.GetClassHierarchy(), ((ConfigurationImpl)conf).Builder); - } - - private void AddConfiguration(IClassHierarchy ns, ConfigurationBuilderImpl builder) - { - this.ClassHierarchy = this.ClassHierarchy.Merge(ns); - - if((ClassHierarchy is ClassHierarchyImpl || builder.ClassHierarchy is ClassHierarchyImpl)) - { - if((ClassHierarchy is ClassHierarchyImpl && builder.ClassHierarchy is ClassHierarchyImpl)) - { - ((ClassHierarchyImpl) ClassHierarchy).Parameterparser.MergeIn(((ClassHierarchyImpl) builder.ClassHierarchy).Parameterparser); - } - else - { - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new ArgumentException("Attempt to merge Java and non-Java class hierarchy! Not supported."), LOGGER); - } - } - - foreach (IClassNode cn in builder.BoundImpls.Keys) - { - IClassNode n = null; - builder.BoundImpls.TryGetValue(cn, out n); - if (n != null) - { - Bind(cn.GetFullName(), n.GetFullName()); - } - } - - foreach (IClassNode cn in builder.BoundConstructors.Keys) - { - IClassNode n = null; - builder.BoundConstructors.TryGetValue(cn, out n); - if (n != null) - { - Bind(cn.GetFullName(), n.GetFullName()); - } - } - - // The namedParameters set contains the strings that can be used to - // instantiate new - // named parameter instances. Create new ones where we can. - foreach (INamedParameterNode np in builder.NamedParameters.Keys) - { - string v = null; - builder.NamedParameters.TryGetValue(np, out v); - Bind(np.GetFullName(), v); - } - - foreach (IClassNode cn in builder.LegacyConstructors.Keys) - { - IConstructorDef cd = null; - builder.LegacyConstructors.TryGetValue(cn, out cd); - RegisterLegacyConstructor(cn, cd.GetArgs()); - } - - foreach (KeyValuePair<INamedParameterNode, object> e in builder.BoundSetEntries) - { - String name = ((INamedParameterNode)e.Key).GetFullName(); - if(e.Value is INode) - { - BindSetEntry(name, (INode)e.Value); - } - else if (e.Value is string) - { - BindSetEntry(name, (string)e.Value); - } else { - var ex = new IllegalStateException(string.Format(CultureInfo.CurrentCulture, "The value {0} set to the named parameter {1} is illegel.", e.Value, name)); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - } - - foreach (var p in builder.BoundLists) - { - BoundLists.Add(p.Key, p.Value); - } - } - - public IClassHierarchy GetClassHierarchy() - { - return this.ClassHierarchy; - } - - public void RegisterLegacyConstructor(IClassNode cn, IList<IClassNode> args) - { - LegacyConstructors.Add(cn, cn.GetConstructorDef(args)); - } - - public void RegisterLegacyConstructor(string s, IList<string> args) - { - IClassNode cn = (IClassNode) this.ClassHierarchy.GetNode(s); - IList<IClassNode> cnArgs = new List<IClassNode>(); - for (int i = 0; i < args.Count; i++) - { - cnArgs.Add((IClassNode)ClassHierarchy.GetNode(args[i])); - } - RegisterLegacyConstructor(cn, cnArgs); - } - - public void RegisterLegacyConstructor(IClassNode c, IList<IConstructorArg> args) - { - string[] cn = new string[args.Count]; - - for (int i = 0; i < args.Count; i++) - { - cn[i] = args[i].Gettype(); - } - RegisterLegacyConstructor(c.GetFullName(), cn); - } - - public IConfiguration Build() - { - return new ConfigurationImpl(new ConfigurationBuilderImpl(this)); - } - - public void Bind(string key, string value) - { - INode n = this.ClassHierarchy.GetNode(key); - if (n is INamedParameterNode) - { - BindParameter((INamedParameterNode)n, value); - } - else if (n is IClassNode) - { - INode m = this.ClassHierarchy.GetNode(value); - Bind((IClassNode)n, (IClassNode)m); - } - else - { - var ex = new IllegalStateException(string.Format("getNode() returned {0} which is neither a ClassNode nor a NamedParameterNode", n)); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - } - - public void Bind(Types.INode key, Types.INode value) - { - if (key is INamedParameterNode) - { - BindParameter((INamedParameterNode)key, value.GetFullName()); - } - else if (key is IClassNode) - { - IClassNode k = (IClassNode)key; - if (value is IClassNode) - { - IClassNode val = (IClassNode)value; - if (val.IsExternalConstructor() && !k.IsExternalConstructor()) - { - BindConstructor(k, (IClassNode) val); - } - else - { - BindImplementation(k, (IClassNode)val); - } - } - } - } - - 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); - } - } - - public void BindImplementation(IClassNode n, IClassNode m) - { - if (this.ClassHierarchy.IsImplementation(n, m)) - { - BoundImpls.Add(n, m); - } - else - { - var ex = new ArgumentException(string.Format("Class {0} does not extend {1}.", m, n)); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - } - - public void BindSetEntry(String iface, String impl) - { - BoundSetEntries.Add((INamedParameterNode)this.ClassHierarchy.GetNode(iface), impl); - } - - public void BindSetEntry(String iface, INode impl) - { - BoundSetEntries.Add((INamedParameterNode)ClassHierarchy.GetNode(iface), impl); - } - - public void BindSetEntry(INamedParameterNode iface, String impl) - { - BoundSetEntries.Add(iface, impl); - } - - public void BindSetEntry(INamedParameterNode iface, INode impl) - { - BoundSetEntries.Add(iface, impl); - } - - public void BindList(INamedParameterNode iface, IList<INode> impl) - { - IList<object> l = new List<object>(); - foreach (var n in impl) - { - l.Add((object)n); - } - BoundLists.Add(iface, l); - } - - public void BindList(INamedParameterNode iface, IList<string> impl) - { - IList<object> l = new List<object>(); - foreach (var n in impl) - { - l.Add((object)n); - } - BoundLists.Add(iface, l); - } - - public void BindList(string iface, IList<INode> impl) - { - BindList((INamedParameterNode)ClassHierarchy.GetNode(iface), impl); - } - - public void BindList(string iface, IList<string> impl) - { - BindList((INamedParameterNode)ClassHierarchy.GetNode(iface), impl); - } - - public void BindConstructor(Types.IClassNode k, Types.IClassNode v) - { - BoundConstructors.Add(k, v); - } - - public string ClassPrettyDefaultString(string longName) - { - INamedParameterNode param = (INamedParameterNode) this.ClassHierarchy.GetNode(longName); - return param.GetSimpleArgName() + "=" + Join(",", param.GetDefaultInstanceAsStrings()); - } - - private String Join(string sep, string[] s) - { - if (s.Length == 0) - { - return null; - } - else - { - StringBuilder sb = new StringBuilder(s[0]); - for (int i = 1; i < s.Length; i++) - { - sb.Append(sep); - sb.Append(s[i]); - } - return sb.ToString(); - } - } - - public string ClassPrettyDescriptionString(string fullName) - { - INamedParameterNode param = (INamedParameterNode) this.ClassHierarchy.GetNode(fullName); - return param.GetDocumentation() + "\n" + param.GetFullName(); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Implementations/Configuration/ConfigurationImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Implementations/Configuration/ConfigurationImpl.cs b/lang/cs/Source/TANG/Tang/Implementations/Configuration/ConfigurationImpl.cs deleted file mode 100644 index 17739e8..0000000 --- a/lang/cs/Source/TANG/Tang/Implementations/Configuration/ConfigurationImpl.cs +++ /dev/null @@ -1,122 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -using System; -using System.Collections.Generic; -using Org.Apache.Reef.Tang.Interface; -using Org.Apache.Reef.Tang.Types; - -namespace Org.Apache.Reef.Tang.Implementations -{ - public class ConfigurationImpl : IConfiguration - { - public readonly ConfigurationBuilderImpl Builder; - - public ConfigurationImpl(ConfigurationBuilderImpl builder) - { - Builder = builder; - } - - public IClassHierarchy GetClassHierarchy() - { - return Builder.ClassHierarchy; - } - - public IConfigurationBuilder newBuilder() - { - return ((ConfigurationImpl)Builder.Build()).Builder; - } - - public ICollection<IClassNode> GetBoundImplementations() - { - return Builder.BoundImpls.Keys; - } - - public IClassNode GetBoundImplementation(IClassNode cn) - { - IClassNode v; - - Builder.BoundImpls.TryGetValue(cn, out v); - - return v; - } - - public ICollection<IClassNode> GetBoundConstructors() - { - return Builder.BoundConstructors.Keys; - } - - public IClassNode GetBoundConstructor(IClassNode cn) - { - IClassNode v; - - Builder.BoundConstructors.TryGetValue(cn, out v); - - return v; - } - - public ICollection<INamedParameterNode> GetNamedParameters() - { - return Builder.NamedParameters.Keys; - } - - public string GetNamedParameter(INamedParameterNode np) - { - string v = null; - Builder.NamedParameters.TryGetValue(np, out v); - - return v; - } - - public IConstructorDef GetLegacyConstructor(IClassNode cn) - { - IConstructorDef v; - - Builder.LegacyConstructors.TryGetValue(cn, out v); - - return v; - } - - public ICollection<IClassNode> GetLegacyConstructors() - { - return Builder.LegacyConstructors.Keys; - } - - public ISet<Object> GetBoundSet(INamedParameterNode np) - { - return Builder.BoundSetEntries.GetValuesForKey(np); - } - - public IEnumerator<KeyValuePair<INamedParameterNode, object>> GetBoundSets() - { - return Builder.BoundSetEntries.GetEnumerator(); - } - - public IDictionary<INamedParameterNode, IList<object>> GetBoundList() - { - return Builder.BoundLists; - } - - public IList<object> GetBoundList(INamedParameterNode np) - { - IList<object> list; - Builder.BoundLists.TryGetValue(np, out list); - return list; - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Implementations/Configuration/Configurations.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Implementations/Configuration/Configurations.cs b/lang/cs/Source/TANG/Tang/Implementations/Configuration/Configurations.cs deleted file mode 100644 index c61fbd7..0000000 --- a/lang/cs/Source/TANG/Tang/Implementations/Configuration/Configurations.cs +++ /dev/null @@ -1,55 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -using Org.Apache.Reef.Tang.Interface; -using Org.Apache.Reef.Tang.Protobuf; - -namespace Org.Apache.Reef.Tang.Implementations.Configuration -{ - public class Configurations - { - public static IConfiguration Merge(params IConfiguration[] configurations) - { - return TangFactory.GetTang().NewConfigurationBuilder(configurations).Build(); - } - - public static IConfiguration MergeDeserializedConfs(params IConfiguration[] configurations) - { - IClassHierarchy ch; - - if (configurations != null && configurations.Length > 0) - { - ch = configurations[0].GetClassHierarchy(); - } - else - { - ch = new ProtocolBufferClassHierarchy(); - } - - IConfigurationBuilder cb = TangFactory.GetTang().NewConfigurationBuilder(ch); - - foreach (IConfiguration tc in configurations) - { - cb.AddConfiguration(((ConfigurationImpl)tc)); - } - - return cb.Build(); - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Implementations/Configuration/CsConfigurationBuilderImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Implementations/Configuration/CsConfigurationBuilderImpl.cs b/lang/cs/Source/TANG/Tang/Implementations/Configuration/CsConfigurationBuilderImpl.cs deleted file mode 100644 index 70ff1b7..0000000 --- a/lang/cs/Source/TANG/Tang/Implementations/Configuration/CsConfigurationBuilderImpl.cs +++ /dev/null @@ -1,489 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Globalization; -using Org.Apache.Reef.Utilities.Logging; -using Org.Apache.Reef.Tang.Annotations; -using Org.Apache.Reef.Tang.Exceptions; -using Org.Apache.Reef.Tang.Interface; -using Org.Apache.Reef.Tang.Types; -using Org.Apache.Reef.Tang.Util; - -namespace Org.Apache.Reef.Tang.Implementations -{ - public class CsConfigurationBuilderImpl : ConfigurationBuilderImpl, ICsInternalConfigurationBuilder - { - private static readonly Logger LOGGER = Logger.GetLogger(typeof(CsConfigurationBuilderImpl)); - - #region Constructors - public CsConfigurationBuilderImpl(string[] assemblies, IConfiguration[] confs, Type[] parsers) : base(assemblies,confs,parsers) - { - } - - public CsConfigurationBuilderImpl(IConfiguration[] confs) : base(confs) - { - } - - public CsConfigurationBuilderImpl(CsConfigurationBuilderImpl impl) : base(impl) - { - } - - public CsConfigurationBuilderImpl(ICsClassHierarchy classHierarchy) - : base(classHierarchy) - { - } - - public CsConfigurationBuilderImpl(string[] assemblies) - : base(assemblies) - { - } - #endregion Constructors - - #region ICsConfigurationBuilder - /// <summary> - /// Builds this instance. - /// </summary> - /// <returns></returns> - public CsConfigurationImpl build() - { - return new CsConfigurationImpl(new CsConfigurationBuilderImpl(this)); - } - - /// <summary> - /// Binds a string to a named parameter. - /// </summary> - /// <param name="name">The name.</param> - /// <param name="value">The value.</param> - /// <returns></returns> - /// <exception cref="BindException">Detected type mismatch when setting named parameter + name - /// + Expected NamedParameterNode, but namespace contains a + np</exception> - public ICsConfigurationBuilder BindNamedParameter(Type name, string value) - { - if (value == null) - { - var ex = new IllegalStateException(string.Format(CultureInfo.CurrentCulture, "The value null set to the named parameter {0} is illegel.", name)); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - INode np = GetNode(name); - if (np is INamedParameterNode) - { - BindParameter((INamedParameterNode)np, value); - } - else - { - var ex = new BindException( - "Detected type mismatch when setting named parameter " + name - + " Expected NamedParameterNode, but namespace contains a " + np); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - return this; - } - - /// <summary> - /// Binds the class impl as the implementation of the interface iface - /// </summary> - /// <param name="iface">The iface.</param> - /// <param name="impl">The impl.</param> - /// <returns></returns> - public ICsConfigurationBuilder BindImplementation(Type iface, Type impl) - { - INode cn = GetNode(iface); - INode dn = GetNode(impl); - if (!(cn is IClassNode)) - { - var ex = new BindException( - "bindImplementation passed interface that resolved to " + cn - + " expected a ClassNode<?>"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - if (!(dn is IClassNode)) - { - var ex = new BindException( - "bindImplementation passed implementation that resolved to " + dn - + " expected a ClassNode<?>"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - BindImplementation((IClassNode)cn, (IClassNode)dn); - return this; - } - - /// <summary> - /// Binds the implementation to the interface - /// </summary> - /// <typeparam name="U"></typeparam> - /// <typeparam name="T"></typeparam> - /// <param name="iface">The iface.</param> - /// <param name="impl">The impl.</param> - /// <returns></returns> - public ICsConfigurationBuilder BindImplementation<U, T>(GenericType<U> iface, GenericType<T> impl) - where T : U - { - return BindImplementation(typeof(U), typeof(T)); - } - - /// <summary> - /// Binds a value to a named parameter. - /// </summary> - /// <typeparam name="U"></typeparam> - /// <typeparam name="T"></typeparam> - /// <param name="name">The name.</param> - /// <param name="value">The value.</param> - /// <returns></returns> - public ICsConfigurationBuilder BindNamedParameter<U, T>(GenericType<U> name, string value) - where U : Name<T> - { - return BindNamedParameter(typeof(U), value); - } - - /// <summary> - /// Binds an implementaion to a named parameter. - /// </summary> - /// <typeparam name="U"></typeparam> - /// <typeparam name="V"></typeparam> - /// <typeparam name="T"></typeparam> - /// <param name="iface">The iface.</param> - /// <param name="impl">The impl.</param> - /// <returns></returns> - public ICsConfigurationBuilder BindNamedParameter<U, V, T>(GenericType<U> iface, GenericType<V> impl) - where U : Name<T> - where V : T - { - return ((ICsInternalConfigurationBuilder)this).BindNamedParameter(typeof(U), typeof(V)); - } - - /// <summary> - /// Binds an external constructor. - /// </summary> - /// <typeparam name="T"></typeparam> - /// <typeparam name="U"></typeparam> - /// <param name="c">The c.</param> - /// <param name="v">The v.</param> - /// <returns></returns> - public ICsConfigurationBuilder BindConstructor<T, U>(GenericType<T> c, GenericType<U> v) - where U : IExternalConstructor<T> - { - return ((ICsInternalConfigurationBuilder)this).BindConstructor(typeof(T), typeof(U)); - } - - //public <T> void bindSetEntry(Class<? extends Name<Set<T>>> iface, String value) throws BindException; - /// <summary> - /// Binds a string value to a named parameter of ISet. - /// </summary> - /// <typeparam name="U"></typeparam> - /// <typeparam name="T"></typeparam> - /// <param name="iface">The iface.</param> - /// <param name="value">The value.</param> - /// <returns></returns> - public ICsConfigurationBuilder BindSetEntry<U, T>(GenericType<U> iface, string value) - where U : Name<ISet<T>> - { - return ((ICsInternalConfigurationBuilder)this).BindSetEntry(typeof(U), value); - } - - //public <T> void bindSetEntry(Class<? extends Name<Set<T>>> iface, Class<? extends T> impl) throws BindException; - /// <summary> - /// Binds an implementaion of T to a named parameter of ISet of T. - /// </summary> - /// <typeparam name="U"></typeparam> - /// <typeparam name="V"></typeparam> - /// <typeparam name="T"></typeparam> - /// <param name="iface">The iface.</param> - /// <param name="impl">The impl.</param> - /// <returns></returns> - public ICsConfigurationBuilder BindSetEntry<U, V, T>(GenericType<U> iface, GenericType<V> impl) - where U : Name<ISet<T>> - where V : T - { - return ((ICsInternalConfigurationBuilder)this).BindSetEntry(typeof(U), typeof(V)); - } - - public ICsConfigurationBuilder BindList<U, V, T>(GenericType<U> iface, IList<GenericType<V>> impl) - where U : Name<IList<T>> - where V : T - { - IList<Type> implTypes = new List<Type>(); - foreach (var item in impl) - { - implTypes.Add(item.TypeT); - } - return ((ICsInternalConfigurationBuilder)this).BindList(typeof(U), implTypes); - } - - public ICsConfigurationBuilder BindList<U, T>(GenericType<U> iface, IList<string> impl) - where U : Name<IList<T>> - { - return ((ICsInternalConfigurationBuilder)this).BindList(typeof(U), impl); - } - - public ICsConfigurationBuilder BindList(Type iface, IList<Type> implList) - { - INode n = GetNode(iface); - IList<INode> result = new List<INode>(); - - if (!(n is INamedParameterNode)) - { - var ex = new BindException("BindList got an interface that resolved to " + n + "; expected a NamedParameter"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - - Type listType = ReflectionUtilities.GetInterfaceTarget(typeof(Name<>), iface); - if (!ReflectionUtilities.IsGenericTypeof(typeof(IList<>), listType)) - { - var ex = new BindException("BindList got a NamedParameter that takes a " + listType + "; expected List<...>"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - - if (implList != null && implList.Count > 0) - { - Type valType = ReflectionUtilities.GetInterfaceTarget(typeof(IList<>), listType); - - foreach (var item in implList) - { - if (!valType.IsAssignableFrom((Type)item)) - { - var ex = - new BindException("BindList got implementation " + item + - " that is incompatible with expected type " + valType); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - result.Add(GetNode(item)); - } - } - - BindList((INamedParameterNode)n, result); - return this; - } - #endregion ICsConfigurationBuilder - - #region ICsInternalConfigurationBuilder - /// <summary> - /// Bind named parameters, implementations or external constructors, depending - /// on the types of the classes passed in. - /// </summary> - /// <param name="iface">The iface.</param> - /// <param name="impl">The impl.</param> - /// <returns></returns> - ICsInternalConfigurationBuilder ICsInternalConfigurationBuilder.Bind(Type iface, Type impl) - { - Bind(GetNode(iface), GetNode(impl)); - return this; - } - - /// <summary> - /// Binds an implementation for a named parameter. - /// </summary> - /// <param name="iface">The iface.</param> - /// <param name="impl">The impl.</param> - /// <returns></returns> - /// <exception cref="BindException">Type mismatch when setting named parameter + ifaceN - /// + Expected NamedParameterNode</exception> - ICsInternalConfigurationBuilder ICsInternalConfigurationBuilder.BindNamedParameter(Type iface, Type impl) - { - INode ifaceN = GetNode(iface); - INode implN = GetNode(impl); - if (!(ifaceN is INamedParameterNode)) - { - var ex = new BindException("Type mismatch when setting named parameter " + ifaceN - + " Expected NamedParameterNode"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - Bind(ifaceN, implN); - return this; - } - - //public <T> void bindSetEntry(Class<? extends Name<Set<T>>> iface, String value) throws BindException; - /// <summary> - /// Binds a string value to to a named parameter of ISet entry - /// </summary> - /// <param name="iface">The iface.</param> - /// <param name="value">The value.</param> - /// <returns></returns> - /// <exception cref="BindException">BindSetEntry got an interface that resolved to + n + ; expected a NamedParameter</exception> - ICsInternalConfigurationBuilder ICsInternalConfigurationBuilder.BindSetEntry(Type iface, string value) - { - INode n = GetNode(iface); - - if (!(n is INamedParameterNode)) - { - var ex = new BindException("BindSetEntry got an interface that resolved to " + n + "; expected a NamedParameter"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - Type setType = ReflectionUtilities.GetInterfaceTarget(typeof(Name<>), iface); - - //check if setType is ISet - if (ReflectionUtilities.GetInterfaceTarget(typeof(ISet<>), setType) == null) - { - var ex = new BindException("BindSetEntry got a NamedParameter that takes a " + setType + "; expected Set<...>"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - // Type valType = ReflectionUtilities.getInterfaceTarget(Set.class, setType); - BindSetEntry((INamedParameterNode)n, value); - return this; - } - - //public <T> void bindSetEntry(Class<? extends Name<Set<T>>> iface, Class<? extends T> impl) throws BindException; - /// <summary> - /// Binds an implementaion to a named parameter of ISset entry. - /// </summary> - /// <param name="iface">The iface.</param> - /// <param name="impl">The impl.</param> - /// <returns></returns> - /// <exception cref="BindException">BindSetEntry got an interface that resolved to + n + ; expected a NamedParameter</exception> - ICsInternalConfigurationBuilder ICsInternalConfigurationBuilder.BindSetEntry(Type iface, Type impl) - { - INode n = GetNode(iface); - INode m = GetNode(impl); - - if (!(n is INamedParameterNode)) - { - var ex = new BindException("BindSetEntry got an interface that resolved to " + n + "; expected a NamedParameter"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - Type setType = ReflectionUtilities.GetInterfaceTarget(typeof(Name<>), iface); - - //if (!ReflectionUtilities.GetRawClass(setType).Equals(typeof(ISet<>))) - if (!ReflectionUtilities.IsGenericTypeof(typeof(ISet<>), setType)) - { - var ex = new BindException("BindSetEntry got a NamedParameter that takes a " + setType + "; expected Set<...>"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - - Type valType = ReflectionUtilities.GetInterfaceTarget(typeof(ISet<>), setType); - - if (!valType.IsAssignableFrom(impl)) - //if (!ReflectionUtilities.GetRawClass(valType).IsAssignableFrom(impl)) - { - var ex = new BindException("BindSetEntry got implementation " + impl + " that is incompatible with expected type " + valType); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - - BindSetEntry((INamedParameterNode)n, m); - return this; - } - - ICsInternalConfigurationBuilder ICsInternalConfigurationBuilder.BindList(Type iface, IList<string> implList) - { - INode n = GetNode(iface); - - if (!(n is INamedParameterNode)) - { - var ex = new BindException("BindList got an interface that resolved to " + n + "; expected a NamedParameter"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - - Type listType = ReflectionUtilities.GetInterfaceTarget(typeof(Name<>), iface); - if (!ReflectionUtilities.IsGenericTypeof(typeof(IList<>), listType)) - { - var ex = new BindException("BindList got a NamedParameter that takes a " + listType + "; expected List<...>"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - - BindList((INamedParameterNode)n, implList); - return this; - } - - //public <T> void bindConstructor(Class<T> c, Class<? extends ExternalConstructor<? extends T>> v) throws BindException; - /// <summary> - /// Binds an external constructor. - /// </summary> - /// <param name="c">The c.</param> - /// <param name="v">The v.</param> - /// <returns></returns> - /// <exception cref="BindException">BindConstructor got class that resolved to + n + ; expected ClassNode</exception> - ICsInternalConfigurationBuilder ICsInternalConfigurationBuilder.BindConstructor(Type c, Type v) - { - INode n = GetNode(c); - INode m = GetNode(v); - - if (!(n is IClassNode)) - { - var ex = new BindException("BindConstructor got class that resolved to " + n + "; expected ClassNode"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - if (!(m is IClassNode)) - { - var ex = new BindException("BindConstructor got class that resolved to " + m + "; expected ClassNode"); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - BindConstructor((IClassNode)n, (IClassNode)m); - return this; - } - #endregion ICsInternalConfigurationBuilder - - #region extension methods - - public ICsConfigurationBuilder BindNamedParam<TName, TType>(string str) where TName : Name<TType> - { - return BindNamedParameter<TName, TType>(GenericType<TName>.Class, str); - } - - public ICsConfigurationBuilder BindStringNamedParam<T>(string str) where T : Name<string> - { - return BindNamedParameter<T, string>(GenericType<T>.Class, str); - } - - public ICsConfigurationBuilder BindIntNamedParam<T>(string str) where T : Name<int> - { - return BindNamedParameter<T, int>(GenericType<T>.Class, str); - } - - public ICsConfigurationBuilder BindNamedParameter<U, V, T>() - where U : Name<T> - where V : T - { - return BindNamedParameter<U, V, T>(GenericType<U>.Class, GenericType<V>.Class); - } - - public ICsConfigurationBuilder BindSetEntry<T1, T2, T3>() - where T1 : Name<ISet<T3>> - where T2 : T3 - { - return BindSetEntry<T1, T2, T3>(GenericType<T1>.Class, GenericType<T2>.Class); - } - - public ICsConfigurationBuilder BindSetEntry<U, T>(string value) where U : Name<ISet<T>> - { - return BindSetEntry<U, T>(GenericType<U>.Class, value); - } - - public ICsConfigurationBuilder BindImplementation<T1, T2>() where T2 : T1 - { - return BindImplementation(GenericType<T1>.Class, GenericType<T2>.Class); - } - - public ICsConfigurationBuilder BindList<U, T>(IList<string> impl) where U : Name<IList<T>> - { - return BindList<U, T>(GenericType<U>.Class, impl); - } - - public ICsConfigurationBuilder BindConstructor<T, U>() where U : IExternalConstructor<T> - { - return BindConstructor<T, U>(GenericType<T>.Class, GenericType<U>.Class); - } - - #endregion extension methods - - private INode GetNode(Type c) - { - return ((ICsClassHierarchy)ClassHierarchy).GetNode(c); - } - } -} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Implementations/Configuration/CsConfigurationImpl.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Implementations/Configuration/CsConfigurationImpl.cs b/lang/cs/Source/TANG/Tang/Implementations/Configuration/CsConfigurationImpl.cs deleted file mode 100644 index 1964792..0000000 --- a/lang/cs/Source/TANG/Tang/Implementations/Configuration/CsConfigurationImpl.cs +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Org.Apache.Reef.Tang.Interface; - -namespace Org.Apache.Reef.Tang.Implementations -{ - public class CsConfigurationImpl : ConfigurationImpl - { - public CsConfigurationImpl(CsConfigurationBuilderImpl builder) : base(builder) - { - } - } -} http://git-wip-us.apache.org/repos/asf/incubator-reef/blob/c1b5200f/lang/cs/Source/TANG/Tang/Implementations/InjectionPlan/Constructor.cs ---------------------------------------------------------------------- diff --git a/lang/cs/Source/TANG/Tang/Implementations/InjectionPlan/Constructor.cs b/lang/cs/Source/TANG/Tang/Implementations/InjectionPlan/Constructor.cs deleted file mode 100644 index 77845e8..0000000 --- a/lang/cs/Source/TANG/Tang/Implementations/InjectionPlan/Constructor.cs +++ /dev/null @@ -1,217 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Collections.ObjectModel; -using System.Linq; -using System.Text; -using Org.Apache.Reef.Utilities.Logging; -using Org.Apache.Reef.Tang.Types; - -namespace Org.Apache.Reef.Tang.Implementations -{ - //Base case for an injection plan. A plan for a class. - public class Constructor : InjectionPlan - { - private static readonly Logger LOGGER = Logger.GetLogger(typeof(Constructor)); - - IConstructorDef constructor; //which constructor to use - InjectionPlan[] args; //constructor arguments in which we already got injectionPlan for each (nested cases) - int numAlternatives; - bool isAmbiguous; - bool isInjectable; - - public InjectionPlan[] GetArgs() - { - return args; - } - - public new ICollection<InjectionPlan> GetChildren() - { - return new ReadOnlyCollection<InjectionPlan>(this.args.OfType<InjectionPlan>().ToList()); - } - - public IConstructorDef GetConstructorDef() - { - return constructor; - } - - public Constructor(IClassNode classNode, - IConstructorDef constructor, InjectionPlan[] args) : base(classNode) - { - this.constructor = constructor; - this.args = args; - int curAlternatives = 1; - bool curAmbiguous = false; - bool curInjectable = true; - foreach (InjectionPlan plan in args) - { - curAlternatives *= plan.GetNumAlternatives(); - curAmbiguous |= plan.IsAmbiguous(); - curInjectable &= plan.IsInjectable(); - } - this.numAlternatives = curAlternatives; - this.isAmbiguous = curAmbiguous; - this.isInjectable = curInjectable; - } - - public new IClassNode GetNode() - { - return (IClassNode) node; - } - - public override int GetNumAlternatives() - { - return numAlternatives; - } - - public override bool IsAmbiguous() - { - return isAmbiguous; - } - - public override bool IsInjectable() - { - return isInjectable; - } - - public override string ToString() - { - StringBuilder sb = new StringBuilder("new " + GetNode().GetName() + '('); - if (args.Length > 0) - { - sb.Append(args[0]); - for (int i = 1; i < args.Length; i++) - { - sb.Append(", " + args[i]); - } - } - sb.Append(')'); - return sb.ToString(); - } - - private String ShallowArgString(InjectionPlan arg) - { - if (arg is Constructor || arg is Subplan) - { - return arg.GetType().Name + ": " + arg.GetNode().GetName(); - } - else - { - return arg.ToShallowString(); - } - } - - public override string ToShallowString() - { - StringBuilder sb = new StringBuilder("new " + GetNode().GetName() + '('); - if (args.Length > 0) - { - sb.Append(ShallowArgString(args[0])); - for (int i = 1; i < args.Length; i++) - { - sb.Append(", " + ShallowArgString(args[i])); - } - } - sb.Append(')'); - return sb.ToString(); - } - - public override string ToAmbiguousInjectString() - { - - if (!isAmbiguous) - { - var ex = new ArgumentException(GetNode().GetFullName() + " is NOT ambiguous."); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - - StringBuilder sb = new StringBuilder(GetNode().GetFullName() + " has ambiguous arguments: [ "); - - foreach (InjectionPlan plan in args) - { - if (plan.IsAmbiguous()) - { - sb.Append(plan.ToAmbiguousInjectString()); - } - } - - sb.Append(']'); - return sb.ToString(); - } - - public override string ToInfeasibleInjectString() - { - IList<InjectionPlan> leaves = new List<InjectionPlan>(); - - foreach (InjectionPlan ip in args) - { - if (!ip.IsFeasible()) - { - if (ip.IsInfeasibleLeaf()) - { - leaves.Add(ip); - } else - { - return ip.ToInfeasibleInjectString(); - } - } - } - - if (leaves.Count == 0) - { - var ex = new ArgumentException(GetNode().GetFullName() + " has NO infeasible leaves."); - Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); - } - - if (leaves.Count == 1) - { - return GetNode().GetFullName() + " missing argument " + leaves[0].GetNode().GetFullName(); - } - else - { - StringBuilder sb = new StringBuilder(GetNode().GetFullName() + " missing arguments: [ "); - foreach (InjectionPlan leaf in leaves) - { - sb.Append(leaf.GetNode().GetFullName() + ' '); - } - sb.Append(']'); - return sb.ToString(); - } - } - - public override bool IsInfeasibleLeaf() - { - return false; - } - - //public override bool HasFutureDependency() - //{ - // foreach (InjectionPlan p in args) - // { - // if(p.HasFutureDependency()) - // { - // return true; - // } - // } - // return false; - //} - } -}
