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); } }
public static InjectionPlan Deserialize(IClassHierarchy ch, InjectionPlanProto.InjectionPlan ip) { string fullName = ip.name; if (ip.constructor != null) { InjectionPlanProto.Constructor cons = ip.constructor; IClassNode cn = (IClassNode)ch.GetNode(fullName); InjectionPlanProto.InjectionPlan[] protoBufArgs = cons.args.ToArray(); IClassNode[] cnArgs = new IClassNode[protoBufArgs.Length]; for (int i = 0; i < protoBufArgs.Length; i++) { INode no = ch.GetNode(protoBufArgs[i].name); if (no is IClassNode) { cnArgs[i] = (IClassNode)no; } else if (no is INamedParameterNode) { INamedParameterNode np = (INamedParameterNode)no; cnArgs[i] = (IClassNode)ch.GetNode(np.GetFullArgName()); } } InjectionPlan[] ipArgs = new InjectionPlan[protoBufArgs.Length]; for (int i = 0; i < protoBufArgs.Length; i++) { ipArgs[i] = (InjectionPlan)Deserialize(ch, protoBufArgs[i]); } IConstructorDef constructor = cn.GetConstructorDef(cnArgs); return(new Constructor(cn, constructor, ipArgs)); } if (ip.instance != null) { InjectionPlanProto.Instance ins = ip.instance; object instance = Parse(ip.name, ins.value); return(new CsInstance(ch.GetNode(ip.name), instance)); } if (ip.subplan != null) { InjectionPlanProto.Subplan subplan = ip.subplan; InjectionPlanProto.InjectionPlan[] protoBufPlans = subplan.plans.ToArray(); InjectionPlan[] subPlans = new InjectionPlan[protoBufPlans.Length]; for (int i = 0; i < protoBufPlans.Length; i++) { subPlans[i] = (InjectionPlan)Deserialize(ch, protoBufPlans[i]); } INode n = ch.GetNode(fullName); return(new Subplan(n, subPlans)); } Org.Apache.Reef.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException("Encountered unknown type of InjectionPlan: " + ip), LOGGER); return(null); }
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 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 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); }
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 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 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 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 INode RegisterType(Type type) { try { INode n = GetAlreadyBoundNode(type); return(n); } catch (NameResolutionException e) { } if (type.BaseType != null) { RegisterType(type.BaseType); } foreach (Type interf in type.GetInterfaces()) { RegisterType(interf); } Type enclosingClass = this.GetIEnclosingClass(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) { throw new ArgumentException("not type in arg"); } RegisterType(constructorArg.Gettype()); //Gettype returns param's Type.fullname if (constructorArg.GetNamedParameterName() != null) { INamedParameterNode np = (INamedParameterNode)RegisterType(constructorArg.GetNamedParameterName()); if (np.IsSet()) { throw new NotImplementedException(); } else { //check is not isCoercable, then throw ClassHierarchyException } } } } } else { INamedParameterNode npNode = node as INamedParameterNode; if (npNode != null) { RegisterType(npNode.GetFullArgName()); } } return(node); }
public object InjectFromPlan(InjectionPlan plan) { if (!plan.IsFeasible()) { var ex = new InjectionException("Cannot inject " + plan.GetNode().GetFullName() + ": " + plan.ToCantInjectString()); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } if (plan.IsAmbiguous()) { var ex = new InjectionException("Cannot inject " + plan.GetNode().GetFullName() + " " + plan.ToCantInjectString()); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } if (plan is InjectionFuturePlan) { InjectionFuturePlan fut = (InjectionFuturePlan)plan; INode node = fut.GetNode(); string key = node.GetFullName(); try { Type t = null; Type nodeType = classHierarchy.ClassForName(node.GetFullName()); if (node is IClassNode) { t = nodeType; } else if (node is INamedParameterNode) { var nn = (INamedParameterNode)node; t = classHierarchy.ClassForName(nn.GetFullArgName()); if (nn.IsSet()) { t = typeof(ISet <>).MakeGenericType(new Type[] { t }); } } else { var ex = new ApplicationException("Unexpected node type. Wanted ClassNode or NamedParameterNode. Got: " + node); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(ex, LOGGER); } // Java - InjectionFuture<?> ret = new InjectionFuture<>(this, javaNamespace.classForName(fut.getNode().getFullName())); // C# - InjectionFuture<object> ret = new InjectionFutureImpl<object>(this, classHierarchy.ClassForName(fut.GetNode().GetFullName())); // We cannot simply create an object from generic with object as <T> // typeof(InjectionFutureImpl<>).MakeGenericType(t) will get the InjectionFutureImpl generic Type with <T> as t // for ClassNode, t is the Type of the class, for NamedParameterNode, t is the Type of the argument // we then use reflection to invoke the constructor // To retain generic argument information?? Type injectionFuture = typeof(InjectionFutureImpl <>).MakeGenericType(t); var constructor = injectionFuture.GetConstructor(new Type[] { typeof(IInjector), typeof(Type) }); IInjectionFuture <object> ret = (IInjectionFuture <object>)constructor.Invoke(new object[] { this, nodeType }); pendingFutures.Add(ret); return(ret); } catch (TypeLoadException e) { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new InjectionException("Could not get class for " + key), LOGGER); } } else if (plan.GetNode() is IClassNode && null != GetCachedInstance((IClassNode)plan.GetNode())) { return(GetCachedInstance((IClassNode)plan.GetNode())); } else if (plan is CsInstance) { // TODO: Must be named parameter node. Check. // throw new IllegalStateException("Instance from plan not in Injector's set of instances?!?"); return(((CsInstance)plan).instance); } else if (plan is Constructor) { Constructor constructor = (Constructor)plan; object[] args = new object[constructor.GetArgs().Length]; InjectionPlan[] argPlans = constructor.GetArgs(); for (int i = 0; i < argPlans.Length; i++) { args[i] = InjectFromPlan(argPlans[i]); } try { concurrentModificationGuard = true; object ret = null; try { IConstructorDef def = (IConstructorDef)constructor.GetConstructorDef(); ConstructorInfo c = GetConstructor(def); if (aspect != null) { ret = aspect.Inject(def, c, args); } else { ret = c.Invoke(args); } } catch (ArgumentException e) { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); StringBuilder sb = new StringBuilder("Internal Tang error? Could not call constructor " + constructor.GetConstructorDef() + " with arguments ["); foreach (object o in args) { sb.Append("\n\t" + o); } sb.Append("]"); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException(sb.ToString(), e), LOGGER); } if (ret is IExternalConstructor <object> ) { ret = ((IExternalConstructor <object>)ret).NewInstance(); } instances.Add(constructor.GetNode(), ret); return(ret); } catch (TargetInvocationException e) { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Caught(e, Level.Error, LOGGER); Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new InjectionException("Could not invoke constructor: " + plan, e), LOGGER); } finally { concurrentModificationGuard = false; } } else if (plan is Subplan) { Subplan ambiguous = (Subplan)plan; return(InjectFromPlan(ambiguous.GetDelegatedPlan())); } else if (plan is SetInjectionPlan) { SetInjectionPlan setPlan = (SetInjectionPlan)plan; INode n = setPlan.GetNode(); string typeOfSet = null; // TODO: This doesn't work for sets of generics (e.g., Set<Foo<int>> // because GetFullName and GetFullArgName strip generic info). if (n is INamedParameterNode) { INamedParameterNode np = (INamedParameterNode)n; typeOfSet = np.GetFullArgName(); } else if (n is IClassNode) { typeOfSet = n.GetFullName(); } else { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new ApplicationException("Unknown node type :" + n.ToString()), LOGGER); } Type t = classHierarchy.ClassForName(typeOfSet); // MakeGenericType(t = int: MonotonicHashSet<> -> MonotonicHashSet<int> // Get constructor: MonotonicHashSet<int> -> public MonotonicHashSet<int>() { ... } // Invoke: public MonotonicHashSet<int> -> new MonotonicHashSet<int>() object ret = typeof(MonotonicHashSet <>).MakeGenericType(t).GetConstructor(new Type[] { }).Invoke(new object[] { }); // (this, classHierarchy.ClassForName(fut.GetNode().GetFullName())); MethodInfo mf = ret.GetType().GetMethod("Add"); foreach (InjectionPlan subplan in setPlan.GetEntryPlans()) { // ret.Add(InjectFromPlan(subplan)); mf.Invoke(ret, new object[] { InjectFromPlan(subplan) }); } return(ret); } else if (plan is ListInjectionPlan) { ListInjectionPlan listPlan = (ListInjectionPlan)plan; INode n = listPlan.GetNode(); string typeOfList = null; if (n is INamedParameterNode) { INamedParameterNode np = (INamedParameterNode)n; typeOfList = np.GetFullArgName(); } else if (n is IClassNode) { typeOfList = n.GetFullName(); } else { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new ApplicationException("Unknown node type :" + n.ToString()), LOGGER); } Type t = classHierarchy.ClassForName(typeOfList); object ret = typeof(List <>).MakeGenericType(t).GetConstructor(new Type[] { }).Invoke(new object[] { }); MethodInfo mf = ret.GetType().GetMethod("Add"); foreach (InjectionPlan subplan in listPlan.GetEntryPlans()) { mf.Invoke(ret, new object[] { InjectFromPlan(subplan) }); } return(ret); } else { Org.Apache.REEF.Utilities.Diagnostics.Exceptions.Throw(new IllegalStateException("Unknown plan type: " + plan), LOGGER); } return(null); // should never reach here }