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 InjectionPlan BuildClassNodeInjectionPlan(IClassNode cn, object cachedInstance, IClassNode externalConstructor, IClassNode boundImpl, IClassNode defaultImpl, IDictionary <INode, InjectionPlan> memo) { if (cachedInstance != null) { return(new CsInstance(cn, cachedInstance)); } else if (externalConstructor != null) { BuildInjectionPlan(externalConstructor, memo); InjectionPlan ip = null; memo.TryGetValue(externalConstructor, out ip); return(new Subplan(cn, 0, new InjectionPlan[] { ip })); } else if (boundImpl != null && !cn.Equals(boundImpl)) { // We need to delegate to boundImpl, so recurse. BuildInjectionPlan(boundImpl, memo); InjectionPlan ip = null; memo.TryGetValue(boundImpl, out ip); return(new Subplan(cn, 0, new InjectionPlan[] { ip })); } else if (defaultImpl != null && !cn.Equals(defaultImpl)) { BuildInjectionPlan(defaultImpl, memo); InjectionPlan ip = null; memo.TryGetValue(defaultImpl, out ip); return(new Subplan(cn, 0, new InjectionPlan[] { ip })); } else { // if we're here and there is a bound impl or a default impl, // then we're bound / defaulted to ourselves, so don't add // other impls to the list of things to consider. List <IClassNode> candidateImplementations = new List <IClassNode>(); if (boundImpl == null && defaultImpl == null) { foreach (var ki in cn.GetKnownImplementations()) { candidateImplementations.Add(ki); } } candidateImplementations.Add(cn); List <InjectionPlan> sub_ips = FilterCandidateConstructors(candidateImplementations, memo); if (candidateImplementations.Count == 1 && candidateImplementations[0].GetFullName().Equals(cn.GetFullName())) { return(WrapInjectionPlans(cn, sub_ips, false, -1)); } else { return(WrapInjectionPlans(cn, sub_ips, true, -1)); } } }
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 bool IsImplementationOf(IClassNode inter) { List <IClassNode> worklist = new List <IClassNode>(); if (this.Equals(inter)) { return(true); } worklist.Add(inter); while (worklist.Count != 0) { IClassNode cn = worklist[worklist.Count - 1]; worklist.RemoveAt(worklist.Count - 1); ISet <IClassNode> impls = cn.GetKnownImplementations(); if (impls.Contains(this)) { return(true); } worklist.AddRange(impls); } return(false); }
private InjectionPlan BuildClassNodeInjectionPlan(IClassNode cn, object cachedInstance, IClassNode externalConstructor, IClassNode boundImpl, IClassNode defaultImpl, IDictionary<INode, InjectionPlan> memo) { if (cachedInstance != null) { return new CsInstance(cn, cachedInstance); } else if (externalConstructor != null) { BuildInjectionPlan(externalConstructor, memo); InjectionPlan ip = null; memo.TryGetValue(externalConstructor, out ip); return new Subplan(cn, 0, new InjectionPlan[] { ip }); } else if (boundImpl != null && !cn.Equals(boundImpl)) { // We need to delegate to boundImpl, so recurse. BuildInjectionPlan(boundImpl, memo); InjectionPlan ip = null; memo.TryGetValue(boundImpl, out ip); return new Subplan(cn, 0, new InjectionPlan[] { ip }); } else if (defaultImpl != null && !cn.Equals(defaultImpl)) { BuildInjectionPlan(defaultImpl, memo); InjectionPlan ip = null; memo.TryGetValue(defaultImpl, out ip); return new Subplan(cn, 0, new InjectionPlan[] { ip }); } else { // if we're here and there is a bound impl or a default impl, // then we're bound / defaulted to ourselves, so don't add // other impls to the list of things to consider. List<IClassNode> candidateImplementations = new List<IClassNode>(); if (boundImpl == null && defaultImpl == null) { foreach (var ki in cn.GetKnownImplementations()) candidateImplementations.Add(ki); } candidateImplementations.Add(cn); List<InjectionPlan> sub_ips = FilterCandidateConstructors(candidateImplementations, memo); if (candidateImplementations.Count == 1 && candidateImplementations[0].GetFullName().Equals(cn.GetFullName())) { return WrapInjectionPlans(cn, sub_ips, false, -1); } else { return WrapInjectionPlans(cn, sub_ips, true, -1); } } }