/// <summary> /// Gets the named parameter target or null. /// </summary> /// <param name="type">The type.</param> /// <returns></returns> /// <exception cref="ClassHierarchyException">Named parameter + GetName(type) + implements /// + multiple interfaces. It is only allowed to implement Name</exception> public static Type GetNamedParameterTargetOrNull(Type type) { var npAnnotation = type.GetCustomAttribute <NamedParameterAttribute>(); if (npAnnotation != null) { Type[] intfs = type.GetInterfaces(); if (intfs.Length > 1) { var ex = new ClassHierarchyException("Named parameter " + GetName(type) + " implements " + "multiple interfaces. It is only allowed to implement Name<T>"); Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } else if (intfs.Length == 0 || !IsName(intfs[0])) { var ex = new ClassHierarchyException("Found illegal [NamedParameter " + GetName(type) + " does not implement Name<T>"); Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } Type[] args = intfs[0].GetGenericArguments(); if (args.Length > 1) { var ex = new ClassHierarchyException("Found illegal [NamedParameter " + GetName(type) + " that has more than one arguments"); Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } if (args.Length == 0) { var ex = new ClassHierarchyException("Found illegal [NamedParameter " + GetName(type) + " that has no argument"); Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } if (HasConstructor(type) || HasInjectableConstructor(type)) { var ex = new ClassHierarchyException("Named parameter " + GetName(type) + " has " + (HasInjectableConstructor(type) ? "an injectable" : "a") + " constructor. " + " Named parameters must not declare any constructors."); Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } return(args[0]); } if (ImplementName(type)) //Implement Name<> but no [NamedParameter] attribute { var ex = new ClassHierarchyException("Named parameter " + GetName(type) + " is missing its [NamedParameter] attribute."); Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } return(null); }
private void ProcessSet(object impl) { FieldInfo f; Builder.Map.TryGetValue(impl, out f); if (f == null) { var e = new ClassHierarchyException("Unknown Impl/Param when setting " + impl.GetType().Name + ". Did you pass in a field from some other module?"); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } if (!reqSet.Contains(f)) { reqSet.Add(f); } }
public ConfigurationModule Build() { ConfigurationModuleBuilder c = DeepCopy(); if (!(c.reqUsed.ContainsAll(c.ReqDecl) && c.optUsed.ContainsAll(c.OptDecl))) { ISet <FieldInfo> fset = new MonotonicHashSet <FieldInfo>(); foreach (FieldInfo f in c.ReqDecl) { if (!c.reqUsed.Contains(f)) { fset.Add(f); } } foreach (FieldInfo f in c.OptDecl) { if (!c.optUsed.Contains(f)) { fset.Add(f); } } var e = new ClassHierarchyException( "Found declared options that were not used in binds: " + ToString(fset)); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } foreach (Type clz in c.lateBindClazz.Keys) { try { c.B.Bind(ReflectionUtilities.GetAssemblyQualifiedName(clz), c.lateBindClazz.Get(clz)); } catch (NameResolutionException e) { Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); var ex = new ClassHierarchyException("ConfigurationModule refers to unknown class: " + c.lateBindClazz.Get(clz), e); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } catch (BindException e) { Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); var ex = new ClassHierarchyException("bind failed while initializing ConfigurationModuleBuilder", e); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } } return(new ConfigurationModule(c)); }
public void AssertStaticClean() { if (!( setImpls.IsEmpty() && setParams.IsEmpty()) && setImplSets.IsEmpty() && setLateImplSets.IsEmpty() && setParamSets.IsEmpty() && setImplLists.IsEmpty() && setLateImplLists.IsEmpty() && setParamLists.IsEmpty() && setLateImpls.IsEmpty()) { var e = new ClassHierarchyException("Detected statically set ConfigurationModule Parameter / Implementation. set() should only be used dynamically. Use bind...() instead."); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } }
public ConfigurationModuleBuilder BindSetEntry <U, T>(GenericType <U> iface, string impl) where U : Name <ISet <T> > { ConfigurationModuleBuilder c = DeepCopy(); try { ICsInternalConfigurationBuilder b = (ICsInternalConfigurationBuilder)c.B; b.BindSetEntry(typeof(U), impl); } catch (BindException ex) { Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER); var e = new ClassHierarchyException("Error in BindSetEntry: " + ex); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } return(c); }
public ConfigurationModuleBuilder BindSetEntry <U, V, T>(GenericType <U> iface, GenericType <V> impl) where U : Name <ISet <T> > where V : T { ConfigurationModuleBuilder c = DeepCopy(); try { c.B.BindSetEntry <U, V, T>(iface, impl); } catch (BindException ex) { Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER); var e = new ClassHierarchyException("Error in BindSetEntry: " + ex); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } return(c); }
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); }
// 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); } }
private void ProcessUse(object impl) { FieldInfo f; Map.TryGetValue(impl, out f); if (f == null) { var e = new ClassHierarchyException("Unknown Impl/Param when binding " + impl.GetType().Name + ". Did you pass in a field from some other module?"); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } if (!reqUsed.Contains(f)) { reqUsed.Add(f); } if (!optUsed.Contains(f)) { optUsed.Add(f); } }
public ConfigurationModuleBuilder Merge(ConfigurationModule d) { if (d == null) { var e = new NullReferenceException("If merge() was passed a static final field that is initialized to non-null, then this is almost certainly caused by a circular class dependency."); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } try { d.AssertStaticClean(); } catch (ClassHierarchyException ex) { Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER); var e = new ClassHierarchyException(ReflectionUtilities.GetAssemblyQualifiedName(GetType()) + ": detected attempt to merge with ConfigurationModule that has had set() called on it", ex); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } ConfigurationModuleBuilder c = DeepCopy(); try { c.B.AddConfiguration(d.Builder.B.Build()); } catch (BindException ex) { Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER); var e = new ClassHierarchyException("Error in AddConfiguration in Merge: " + ex); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } c.ReqDecl.AddAll(d.Builder.ReqDecl); c.OptDecl.AddAll(d.Builder.OptDecl); c.reqUsed.AddAll(d.Builder.reqUsed); c.optUsed.AddAll(d.Builder.optUsed); c.SetOpts.AddAll(d.Builder.SetOpts); //// c.ListOpts.AddAll(d.Builder.ListOpts); c.Map.AddAll(d.Builder.Map); c.FreeImpls.AddAll(d.Builder.FreeImpls); c.FreeParams.AddAll(d.Builder.FreeParams); c.lateBindClazz.AddAll(d.Builder.lateBindClazz); return(c); }
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); } } }
internal static void ProcessConfigData(IConfigurationBuilder conf, IList <KeyValuePair <string, string> > settings) { foreach (KeyValuePair <string, string> kv in settings) { try { conf.Bind(kv.Key, kv.Value); } catch (BindException ex) { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER); var e = new BindException("Failed to process configuration tuple: [" + kv.Key + "=" + kv.Value + "]", ex); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } catch (ClassHierarchyException ex) { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(ex, Level.Error, LOGGER); var e = new ClassHierarchyException("Failed to process configuration tuple: [" + kv.Key + "=" + kv.Value + "]", ex); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } } }
private ConfigurationModuleBuilder(ConfigurationModuleBuilder c) { try { B.AddConfiguration(c.B.Build()); } catch (BindException e) { Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); var ex = new ClassHierarchyException("Build error in ConfigurationModuleBuilder: " + e); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } ReqDecl.UnionWith(c.ReqDecl); OptDecl.UnionWith(c.OptDecl); reqUsed.UnionWith(c.reqUsed); optUsed.UnionWith(c.optUsed); SetOpts.UnionWith(c.SetOpts); Map.AddAll(c.Map); FreeImpls.AddAll(c.FreeImpls); FreeParams.AddAll(c.FreeParams); lateBindClazz.AddAll(c.lateBindClazz); }
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 ConfigurationModuleBuilder() { foreach (FieldInfo f in GetType().GetFields(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Static)) { Type t = f.FieldType; if (ParamBlacklist.Contains(t)) { var e = new ClassHierarchyException( "Found a field of type " + t + " which should be a Required/Optional Parameter/Implementation instead"); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } if (ParamTypes.Contains(t.Name)) { if (!f.IsPublic) { var e = new ClassHierarchyException("Found a non-public configuration option in " + GetType() + ": " + f); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } if (!f.IsStatic) { var e = new ClassHierarchyException("Found a non-static configuration option in " + GetType() + ": " + f); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } if (!f.IsInitOnly) { var e = new ClassHierarchyException("Found a non-readonly configuration option in " + GetType() + ": " + f); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } object o = null; try { o = f.GetValue(null); } catch (ArgumentException e) { Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); var ex = new ClassHierarchyException("Could not look up field instance in " + GetType() + " field: " + f, e); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } catch (FieldAccessException e) { Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); var ex = new ClassHierarchyException("Could not look up field instance in " + GetType() + " field: " + f, e); Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } if (Map.ContainsKey(o)) { FieldInfo fi; Map.TryGetValue(o, out fi); var e = new ClassHierarchyException("Detected aliased instances in class " + GetType() + " for fields " + fi + " and " + f); Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } if (ReflectionUtilities.IsGenericTypeof(typeof(RequiredImpl <>), t) || ReflectionUtilities.IsGenericTypeof(typeof(RequiredParameter <>), t)) { ReqDecl.Add(f); } else { OptDecl.Add(f); } Map.Add(o, f); } } }
private void BuildInjectionPlan(INode n, IDictionary <INode, InjectionPlan> memo) { if (memo.ContainsKey(n)) { InjectionPlan p = null; memo.TryGetValue(n, out p); if (BUILDING == p) { StringBuilder loopyList = new StringBuilder("["); foreach (INode node in memo.Keys) { InjectionPlan p1 = null; memo.TryGetValue(node, out p1); if (p1 == BUILDING) { loopyList.Append(" " + node.GetFullName()); } } loopyList.Append(" ]"); var ex = new ClassHierarchyException("Detected loopy constructor involving " + loopyList.ToString()); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } else { return; } } memo.Add(n, BUILDING); InjectionPlan ip = null; if (n is INamedParameterNode) { INamedParameterNode np = (INamedParameterNode)n; object boundInstance = ParseBoundNamedParameter(np); object defaultInstance = this.classHierarchy.ParseDefaultValue(np); object instance = boundInstance != null ? boundInstance : defaultInstance; if (instance is INode) { BuildInjectionPlan((INode)instance, memo); InjectionPlan sp; memo.TryGetValue((INode)instance, out sp); ip = new Subplan(n, 0, new InjectionPlan[] { sp }); } else if (instance is ISet <object> ) { ISet <object> entries = (ISet <object>)instance; ip = CreateSetInjectionPlan(n, memo, entries); } else if (instance is ISet <INode> ) { ISet <INode> entries = (ISet <INode>)instance; ip = CreateSetInjectionPlan(n, memo, entries); } else if (instance is IList <object> ) { IList <object> entries = (IList <object>)instance; ip = CreateListInjectionPlan(n, memo, entries); } else if (instance is IList <INode> ) { IList <INode> entries = (IList <INode>)instance; ip = CreateListInjectionPlan(n, memo, entries); } else { ip = new CsInstance(np, instance); } } else if (n is IClassNode) { IClassNode cn = (IClassNode)n; // Any (or all) of the next four values might be null; that's fine. object cached = GetCachedInstance(cn); IClassNode boundImpl = this.configuration.GetBoundImplementation(cn); IClassNode defaultImpl = ParseDefaultImplementation(cn); IClassNode ec = this.configuration.GetBoundConstructor(cn); ip = BuildClassNodeInjectionPlan(cn, cached, ec, boundImpl, defaultImpl, memo); } else if (n is IPackageNode) { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new ArgumentException( "Request to instantiate Java package as object"), LOGGER); } else { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException( "Type hierarchy contained unknown node type!:" + n), LOGGER); } memo[n] = ip; }
public INode RegisterType(Type type) { if (ReflectionUtilities.IsAnonymousType(type)) { // DevNote: Kinda hacky way to indicate the no-op case. return(rootNode); } INode n = GetAlreadyBoundNode(type); if (n != null) { return(n); } if (type.BaseType != null) { RegisterType(type.BaseType); } foreach (Type interf in type.GetInterfaces()) { RegisterType(ReflectionUtilities.EnsureInterfaceType(interf)); } Type enclosingClass = type.DeclaringType; // this.GetEnclosingClass(type); if (enclosingClass != null) { RegisterType(enclosingClass); } INode node = RegisterClass(type); foreach (Type inner in type.GetNestedTypes()) { RegisterType(inner); } IClassNode classNode = node as ClassNodeImpl; if (classNode != null) { foreach (IConstructorDef constructorDef in classNode.GetInjectableConstructors()) { foreach (IConstructorArg constructorArg in constructorDef.GetArgs()) { if (constructorArg.Gettype() == null) { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new ArgumentException("not type in arg"), LOGGER); } RegisterType(constructorArg.Gettype()); // Gettype returns param's Type.fullname if (constructorArg.GetNamedParameterName() != null) { var pn = RegisterType(constructorArg.GetNamedParameterName()); if (!(pn is INamedParameterNode)) { string message = string.Format(CultureInfo.CurrentCulture, "The class {0}, used in the constructor of {1}, should not be defined as a NamedParameter.", constructorArg.GetNamedParameterName(), constructorDef.GetClassName()); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new ArgumentException(message), LOGGER); } INamedParameterNode np = (INamedParameterNode)RegisterType(constructorArg.GetNamedParameterName()); try { if (np.IsSet() || np.IsList()) { // throw new NotImplementedException(); } else { if (!ReflectionUtilities.IsCoercable(ClassForName(constructorArg.Gettype()), ClassForName(np.GetFullArgName()))) { var e = new ClassHierarchyException( "Named parameter type mismatch in " + classNode.GetFullName() + ". Constructor expects a " + constructorArg.Gettype() + " but " + np.GetName() + " is a " + np.GetFullArgName()); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } } } catch (TypeLoadException e) { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); var ex = new ClassHierarchyException("Constructor refers to unknown class " + constructorArg.GetType(), e); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } } } } } else { INamedParameterNode npNode = node as INamedParameterNode; if (npNode != null) { RegisterType(npNode.GetFullArgName()); } } return(node); }
public static INode CreateClassNode(INode parent, Type clazz) { // var namedParameter = clazz.GetCustomAttribute<NamedParameterAttribute>(); var unit = clazz.GetCustomAttribute <UnitAttribute>() != null; string simpleName = ReflectionUtilities.GetName(clazz); string fullName = ReflectionUtilities.GetAssemblyQualifiedName(clazz); //// bool isStatic = true; // clazz.IsSealed && clazz.IsAbstract; always true in C# for Java static class //// bool injectable = true; // always true in C# bool isAssignableFromExternalConstructor = ReflectionUtilities.IsAssignableFromIgnoreGeneric(typeof(IExternalConstructor <>), clazz); // bool parentIsUnit = false; ////No such thing in C#, should be false ////bool foundNonStaticInnerClass = false; ////foreach (Type c in clazz.getNestedTypes()) { //// if (!Modifier.isStatic(c.getModifiers())) { //// foundNonStaticInnerClass = true; //// } ////} var injectableConstructors = new List <IConstructorDef>(); var allConstructors = new List <IConstructorDef>(); foreach (ConstructorInfo c in clazz.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance)) { var constructorAnnotatedInjectable = c.GetCustomAttribute <InjectAttribute>() != null; bool constructorInjectable = constructorAnnotatedInjectable; ConstructorDefImpl constructorDef = CreateConstructorDef(c, constructorAnnotatedInjectable); if (constructorInjectable) { // if (injectableConstructors.Contains(constructorDef)) if (constructorDef.IsInList(injectableConstructors)) { var e = new ClassHierarchyException( "Ambiguous boundConstructors detected in class " + clazz + ": " + constructorDef + " differs from some other" + " constructor only " + "by parameter order."); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } else { injectableConstructors.Add(constructorDef); } } allConstructors.Add(constructorDef); } string defaultImplementation = null; DefaultImplementationAttribute defaultImpl = clazz.GetCustomAttribute <DefaultImplementationAttribute>(); if (defaultImpl != null) { Type defaultImplementationClazz = defaultImpl.Value; if (defaultImplementationClazz == null) { defaultImplementation = defaultImpl.Name; } else { if (!ReflectionUtilities.IsAssignableFromIgnoreGeneric(clazz, defaultImplementationClazz)) { var e = new ClassHierarchyException(clazz + " declares its default implementation to be non-subclass " + defaultImplementationClazz); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(e, LOGGER); } defaultImplementation = ReflectionUtilities.GetAssemblyQualifiedName(defaultImplementationClazz); } } else { defaultImplementation = null; } return(new ClassNodeImpl(parent, simpleName, fullName, unit, true, isAssignableFromExternalConstructor, injectableConstructors, allConstructors, defaultImplementation)); }
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 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.DefaultValue == null || namedParameter.DefaultValue.Equals(NamedParameterAttribute.ReefUninitializedValue)) { hasStringDefault = false; } else { hasStringDefault = true; default_count++; } 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, namedParameter.Alias, namedParameter.AliasLanguage)); }