public object Parse(INamedParameterNode np, string value) { IClassNode iface; try { iface = (IClassNode)GetNode(np.GetFullArgName()); } catch (NameResolutionException e) { throw new IllegalStateException("Could not parse validated named parameter argument type. NamedParameter is " + np.GetFullName() + " argument type is " + np.GetFullArgName()); } Type clazz; String fullName; try { clazz = (Type)ClassForName(iface.GetFullName()); fullName = null; } catch (TypeLoadException e) { clazz = null; fullName = iface.GetFullName(); } try { if (clazz != null) { return(parameterParser.Parse(clazz, value)); } else { return(parameterParser.Parse(fullName, value)); } } catch (UnsupportedOperationException e) { try { INode impl = GetNode(value); if (impl is IClassNode) { if (IsImplementation(iface, (IClassNode)impl)) { return(impl); } } throw new ParseException("Name<" + iface.GetFullName() + "> " + np.GetFullName() + " cannot take non-subclass " + impl.GetFullName(), e); } catch (NameResolutionException e2) { throw new ParseException("Name<" + iface.GetFullName() + "> " + np.GetFullName() + " cannot take non-class " + value, e); } } }
private static ClassHierarchyProto.Node SerializeNode(INode n) { IList <ClassHierarchyProto.Node> children = new List <ClassHierarchyProto.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 <ClassHierarchyProto.ConstructorDef> injectableConstructors = new List <ClassHierarchyProto.ConstructorDef>(); foreach (IConstructorDef inj in injectable) { injectableConstructors.Add(SerializeConstructorDef(inj)); } IList <ClassHierarchyProto.ConstructorDef> otherConstructors = new List <ClassHierarchyProto.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()); } return(NewClassNode(cn.GetName(), cn.GetFullName(), cn.IsInjectionCandidate(), cn.IsExternalConstructor(), cn.IsUnit(), injectableConstructors, otherConstructors, implFullNames, children)); } else if (n is INamedParameterNode) { INamedParameterNode np = (INamedParameterNode)n; return(NewNamedParameterNode(np.GetName(), np.GetFullName(), np.GetSimpleArgName(), np.GetFullArgName(), np.IsSet(), np.GetDocumentation(), np.GetShortName(), np.GetDefaultInstanceAsStrings(), children)); } else if (n is IPackageNode) { return(NewPackageNode(n.GetName(), n.GetFullName(), children)); } else { throw new IllegalStateException("Encountered unknown type of Node: " + n); } }
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); }
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); } } string shortName = np.GetShortName(); if (shortName != null && !shortName.Equals("")) { 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); }
public INode BuildPathToNode(Type type) { INode parent = GetParentNode(type); Type argType = GetNamedParameterTargetOrNull(type); if (argType == null) { return(NodeFactory.CreateClassNode(parent, type)); } else { INamedParameterNode np = NodeFactory.CreateNamedParameterNode(parent, type, argType); //TODO //if(parameterParser.canParse(ReflectionUtilities.getFullName(argType))) { // if(clazz.getAnnotation(NamedParameter.class).default_class() != Void.class) { // throw new ClassHierarchyException("Named parameter " + ReflectionUtilities.getFullName(clazz) + " defines default implementation for parsable type " + ReflectionUtilities.getFullName(argType)); // } //} string shortName = np.GetShortName(); if (shortName != null && !shortName.Equals("")) { INamedParameterNode oldNode = null; shortNames.TryGetValue(shortName, out oldNode); if (oldNode != null) { if (oldNode.GetFullName().Equals(np.GetFullName())) { throw new IllegalStateException("Tried to double bind " + oldNode.GetFullName() + " to short name " + shortName); } throw new ClassHierarchyException("Named parameters " + oldNode.GetFullName() + " and " + np.GetFullName() + " have the same short name: " + shortName); } shortNames.Add(shortName, np); } return(np); } }
public IClassHierarchy Merge(IClassHierarchy ch) { if (this == ch) { return(this); } if (!(ch is ProtocolBufferClassHierarchy)) { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new NotSupportedException( "Cannot merge ExternalClassHierarchies yet!"), LOGGER); } ProtocolBufferClassHierarchy pch = (ProtocolBufferClassHierarchy)ch; foreach (var pair in pch.lookupTable) { if (!this.lookupTable.ContainsKey(pair.Key)) { this.lookupTable.Add(pair); } } foreach (INode n in ch.GetNamespace().GetChildren()) { if (!rootNode.Contains(n.GetFullName())) { if (n is INamedParameterNode) { INamedParameterNode np = (INamedParameterNode)n; new NamedParameterNodeImpl(this.rootNode, np.GetName(), np.GetFullName(), np.GetFullArgName(), np.GetSimpleArgName(), np.IsSet(), np.IsList(), np.GetDocumentation(), np.GetShortName(), np.GetDefaultInstanceAsStrings().ToArray()); } else if (n is IClassNode) { IClassNode cn = (IClassNode)n; new ClassNodeImpl(rootNode, cn.GetName(), cn.GetFullName(), cn.IsUnit(), cn.IsInjectionCandidate(), cn.IsExternalConstructor(), cn.GetInjectableConstructors(), cn.GetAllConstructors(), cn.GetDefaultImplementation()); } } } return(this); }
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); } } }
public object Parse(INamedParameterNode np, string value) { IClassNode iface = null; try { iface = (IClassNode)GetNode(np.GetFullArgName()); } catch (NameResolutionException e) { Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); var ex = new IllegalStateException("Could not parse validated named parameter argument type. NamedParameter is " + np.GetFullName() + " argument type is " + np.GetFullArgName(), e); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } Type clazz; string fullName; try { clazz = (Type)ClassForName(iface.GetFullName()); fullName = null; } catch (TypeLoadException e) { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(e, Level.Warning, LOGGER); clazz = null; fullName = iface.GetFullName(); } object result = null; if (clazz != null) { result = Parameterparser.Parse(clazz, value); } else { result = Parameterparser.Parse(fullName, value); } if (result == null) { try { INode impl = GetNode(value); if (impl is IClassNode) { if (IsImplementation(iface, (IClassNode)impl)) { return(impl); } } var ex = new ParseException( "Name<" + iface.GetFullName() + "> " + np.GetFullName() + " cannot take non-subclass " + impl.GetFullName()); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } catch (NameResolutionException ec) { Utilities.Diagnostics.Exceptions.Caught(ec, Level.Error, LOGGER); var ex = new ParseException( "Name<" + iface.GetFullName() + "> " + np.GetFullName() + " cannot take non-class " + value, ec); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } } return(result); }
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); } } }
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) { throw new ClassHierarchyException("Could not parse default value", e); } } if (name.IsSet()) { return new HashSet<object>(ret.ToList<object>()); } else { if (ret.Length == 0) { return null; } else if (ret.Length == 1) { return ret[0]; } else { throw new IllegalStateException("Multiple defaults for non-set named parameter! " + name.GetFullName()); } } }
public object Parse(INamedParameterNode np, string value) { IClassNode iface; try { iface = (IClassNode)GetNode(np.GetFullArgName()); } catch(NameResolutionException e) { throw new IllegalStateException("Could not parse validated named parameter argument type. NamedParameter is " + np.GetFullName() + " argument type is " + np.GetFullArgName()); } Type clazz; String fullName; try { clazz = (Type)ClassForName(iface.GetFullName()); fullName = null; } catch(TypeLoadException e) { clazz = null; fullName = iface.GetFullName(); } try { if(clazz != null) { return parameterParser.Parse(clazz, value); } else { return parameterParser.Parse(fullName, value); } } catch (UnsupportedOperationException e) { try { INode impl = GetNode(value); if (impl is IClassNode) { if (IsImplementation(iface, (IClassNode)impl)) { return impl; } } throw new ParseException("Name<" + iface.GetFullName() + "> " + np.GetFullName() + " cannot take non-subclass " + impl.GetFullName(), e); } catch(NameResolutionException e2) { throw new ParseException("Name<" + iface.GetFullName() + "> " + np.GetFullName() + " cannot take non-class " + value, e); } } }
public string ClassPrettyDescriptionString(string fullName) { INamedParameterNode param = (INamedParameterNode)this.ClassHierarchy.GetNode(fullName); return(param.GetDocumentation() + "\n" + param.GetFullName()); }
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 { try { NamedParameters.Add(name, value); } catch (ArgumentException e) { var msg = string.Format(CultureInfo.InvariantCulture, DuplicatedEntryForNamedParamater + "try to bind [{0}] to [{1}], but the configuration has been set for it.", value, name.GetFullName()); Utilities.Diagnostics.Exceptions.Throw(new ArgumentException(msg + e.Message), LOGGER); } } }
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 }
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); }
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 }
public object Parse(INamedParameterNode np, string value) { IClassNode iface = null; try { iface = (IClassNode)GetNode(np.GetFullArgName()); } catch (NameResolutionException e) { Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); var ex = new IllegalStateException("Could not parse validated named parameter argument type. NamedParameter is " + np.GetFullName() + " argument type is " + np.GetFullArgName(), e); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } Type clazz; string fullName; try { clazz = (Type)ClassForName(iface.GetFullName()); fullName = null; } catch (TypeLoadException e) { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(e, Level.Warning, LOGGER); clazz = null; fullName = iface.GetFullName(); } object result = null; if (clazz != null) { result = Parameterparser.Parse(clazz, value); } else { result = Parameterparser.Parse(fullName, value); } if (result == null) { try { INode impl = GetNode(value); if (impl is IClassNode) { if (IsImplementation(iface, (IClassNode)impl)) { return impl; } } var ex = new ParseException( "Name<" + iface.GetFullName() + "> " + np.GetFullName() + " cannot take non-subclass " + impl.GetFullName()); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } catch (NameResolutionException ec) { Utilities.Diagnostics.Exceptions.Caught(ec, Level.Error, LOGGER); var ex = new ParseException( "Name<" + iface.GetFullName() + "> " + np.GetFullName() + " cannot take non-class " + value, ec); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } } return result; }
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) { throw new ClassHierarchyException("Could not parse default value", e); } } if (name.IsSet()) { return(new HashSet <object>(ret.ToList <object>())); } else { if (ret.Length == 0) { return(null); } else if (ret.Length == 1) { return(ret[0]); } else { throw new IllegalStateException("Multiple defaults for non-set named parameter! " + name.GetFullName()); } } }