public static TypeDefinition TypeDefinition(Navigator nav, AdviceSpec spec) { TypeDefinition result = nav.Current as TypeDefinition; if (result == null) throw new AdviceException ("Expected a type definition, but got " + nav.Current + " : " + nav.Current.GetType(), spec); return result; }
public void Test() { Person p = new Person("Gil","Tom",29); Navigator root = new Navigator(p); Navigator nav = (Navigator) root.Clone(); AssertEqual(XPathNodeType.Root, nav.NodeType); AssertFalse(nav.MoveToNext()); // Only one root AssertEqual(XPathNodeType.Root, nav.NodeType); AssertTrue(nav.MoveToFirstChild()); // Now points to a Person AssertFalse(nav.MoveToFirstChild()); // Person has no child AssertEqual(XPathNodeType.Element, nav.NodeType); AssertEqual("Person", nav.LocalName); AssertEqual(typeof(Person).FullName, nav.Value); AssertSame(p, nav.RealObject); AssertEqual("Gil", nav.GetAttribute("Name", string.Empty)); AssertTrue(nav.MoveToAttribute("Name",string.Empty)); // Now points to the Name property AssertEqual(XPathNodeType.Attribute, nav.NodeType); AssertEqual("Name", nav.LocalName); AssertEqual("Gil", nav.Value); AssertTrue(nav.MoveToParent()); AssertEqual(XPathNodeType.Element, nav.NodeType); AssertTrue(nav.MoveToFirstAttribute()); AssertEqual("Age", nav.LocalName); AssertEqual("29", nav.Value); AssertTrue(nav.MoveToNextAttribute()); AssertEqual("FirstName", nav.LocalName); AssertEqual("Tom", nav.Value); }
public static TypeDefinition InterfaceDefinition(Navigator nav, AdviceSpec spec) { TypeDefinition result = TypeDefinition(nav, spec); if (!result.IsInterface) throw new AdviceException ("Expected an interface definition, but got " + nav.Current, spec); return result; }
// Navigate across attributes public bool MoveToFirstAttribute(Navigator n) { bool result = m_NbAttributes > 0; if (result) n.AttributesIndex = 0; return result; }
public static Instruction Instruction(Navigator nav, AdviceSpec spec) { Instruction result = nav.Current as Instruction; if (result == null) throw new AdviceException ("Expected an instruction, but got " + nav.Current + ":" + nav.Current.GetType(), spec); return result; }
public static Instruction CallInstruction(Navigator nav, AdviceSpec spec) { Instruction result = Instruction(nav, spec); string name = result.OpCode.Name; if (!name.StartsWith("call") && !name.StartsWith("newobj")) throw new AdviceException("Expected a call instruction, but got " + nav.Current, spec); return result; }
public override string Value(Navigator n) { switch (n.AttributesIndex) { case -1: case 0: return Cast(n).Constructor.ToString(); default: return string.Empty; } }
public override string Name(Navigator n) { switch (n.AttributesIndex) { case -1: return AttributeName; case 0: return "Constructor"; default: return string.Empty; } }
public static MethodDefinition ConcreteMethod(Navigator nav, AdviceSpec spec) { MethodDefinition result = Method(nav, spec); if (result.IsAbstract) throw new AdviceException ("Expected a concrete method, but got " + result, spec); return result; }
public override string Value(Navigator n) { switch (n.AttributesIndex) { case -1: case 0: return Cast(n).Name.Name; case 1: return Cast(n).Name.FullName; default: return string.Empty; } }
public override string Value(Navigator n) { switch (n.AttributesIndex) { case -1: case 0: return Cast(n).VariableType.ToString(); case 1: return Cast(n).Name; default: return string.Empty; } }
public override string Name(Navigator n) { switch (n.AttributesIndex) { case -1: return VariableName; case 0: return "Type"; case 1: return "Name"; default: return string.Empty; } }
public override string Name(Navigator n) { switch (n.AttributesIndex) { case -1: return AssemblyName; case 0: return "Name"; case 1: return "FullName"; default: return string.Empty; } }
public override string Value(Navigator n) { switch (n.AttributesIndex) { case -1: case 0: return Cast(n).ToString(); case 1: return Cast(n).PropertyType.ToString(); case 2: return Cast(n).Name; case 3: return Cast(n).Attributes.ToString(); default: return string.Empty; } }
public static MethodDefinition ConcreteMethodMatching(Navigator nav, string regex, AdviceSpec spec) { MethodDefinition result = Method(nav, spec); if (!SimpleRegex.IsMatch(result, regex)) { string errorMsg = string.Format ("Expected to match signature {0}, but got {1}", SimpleRegex.EscapePseudoRegex(regex), result); throw new AdviceException(errorMsg, spec); } return result; }
public override void Remove(Navigator n) { TypeDefinition type = (TypeDefinition)n.Current; TypeDefinition declaringType = (TypeDefinition)type.DeclaringType; if (declaringType != null) declaringType.NestedTypes.Remove(type); else type.Module.Types.Remove(type); }
public override bool MoveToNext(Navigator n) { bool result = false; InstructionDefinition instruction = Cast(n); if (instruction.Next != null) { n.Current = instruction.Next; result = true; } return result; }
public override string Name(Navigator n) { switch (n.AttributesIndex) { case -1: return PropertyName; case 0: return "Signature"; case 1: return "Type"; case 2: return "Name"; case 3: return "Attributes"; default: return string.Empty; } }
public override string Name(Navigator n) { switch (n.AttributesIndex) { case -1: return ConstructorName; case 0: return "Signature"; case 1: return "Name"; case 2: return "IsStatic"; case 3: return "Attributes"; default: return string.Empty; } }
public override string Value(Navigator n) { switch (n.AttributesIndex) { case -1: case 0: return Cast(n).ToString(); case 1: return Cast(n).Name; case 2: return Cast(n).ReturnType.ReturnType.FullName; case 3: return Cast(n).IsStatic.ToString(); case 4: return Cast(n).IsAbstract.ToString(); case 5: return Cast(n).Attributes.ToString(); default: return string.Empty; } }
public override string Name(Navigator n) { switch (n.AttributesIndex) { case -1: return TypeName; case 0: return "Namespace"; case 1: return "Name"; case 2: return "FullName"; case 3: return "BaseType"; case 4: return "Attributes"; default: return string.Empty; } }
public override string Value(Navigator n) { switch (n.AttributesIndex) { case -1: return n.Current.ToString(); case 0: return Cast(n).Namespace; case 1: return Cast(n).Name; case 2: return Cast(n).FullName; case 3: return Cast(n).BaseType != null ? Cast(n).BaseType.ToString() : string.Empty; case 4: return Cast(n).Attributes.ToString(); default: return string.Empty; } }
public override string Name(Navigator n) { switch (n.AttributesIndex) { case -1: return MethodName; case 0: return "Signature"; case 1: return "Name"; case 2: return "ReturnType"; case 3: return "IsStatic"; case 4: return "IsAbstract"; case 5: return "Attributes"; default: return string.Empty; } }
public override bool MoveToNext(Navigator n) { bool result = false; object parent = n.Parent; if (parent is FieldDefinition) { // Next attribute in field FieldDefinition field = (FieldDefinition)parent; result = GoToNext(n, field.CustomAttributes); } else if (parent is PropertyDefinition) { // Next attribute in property PropertyDefinition property = (PropertyDefinition)parent; result = GoToNext(n, property.CustomAttributes); } else if (parent is AssemblyDefinition) { // Next attribute in assembly or first type AssemblyDefinition assembly = (AssemblyDefinition)parent; result = GoToNext(n, assembly.CustomAttributes, assembly.MainModule.Types); } else if (parent is TypeDefinition) {// Next attribute in type or first field / property / constructor / method TypeDefinition type = (TypeDefinition)parent; result = GoToNext(n, type.CustomAttributes, type.Fields, type.Properties, type.Constructors, type.Methods); } else if (parent is MethodDefinition) { // Next attribute in operation or first variable / instruction MethodDefinition operation = (MethodDefinition)parent; result = (operation.Body == null) ? GoToNext(n, operation.CustomAttributes) : GoToNext(n, operation.CustomAttributes, operation.Body.Variables, operation.Body.Instructions); } else throw new NotSupportedException("this kind of parent should not occur here"); return result; }
public override string Name(Navigator n) { InstructionDefinition instruction = Cast(n); string name; if (instruction.OpCode == OpCodes.Call || instruction.OpCode == OpCodes.Callvirt || instruction.OpCode == OpCodes.Newobj || instruction.OpCode == OpCodes.Calli) name = CallName; //else if (instruction.OpCode == OpCodes.Ldfld) // name = LdFldName; //else if (instruction.OpCode == OpCodes.Stfld) // name = StFldName; else if (instruction.OpCode == OpCodes.Ldfld || instruction.OpCode == OpCodes.Ldsfld) name = LdFldName; else if (instruction.OpCode == OpCodes.Stfld || instruction.OpCode == OpCodes.Stsfld) name = StFldName; else name = InstructionName; switch (n.AttributesIndex) { case -1: return name; case 0: return "OpCode"; case 1: return "Operand"; default: return string.Empty; } }
private static MethodDefinition Cast(Navigator n) { return (MethodDefinition)n.Current; }
public override void Remove(Navigator n) { ((TypeDefinition)n.Parent).Methods.Remove(Cast(n)); }
public override bool MoveToNext(Navigator n) { TypeDefinition type = Cast(n).DeclaringType as TypeDefinition; return GoToNext(n, type.Methods); }
public override bool MoveToFirstChild(Navigator n) { return Cast(n).Body == null ? GoToFirstChild(n, Cast(n).CustomAttributes) : GoToFirstChild(n, Cast(n).CustomAttributes, Cast(n).Body.Variables, Cast(n).Body.Instructions); }
public static void TestSuite() { TestSimpleRegex(); Navigator nav = new Navigator(AssemblyFactory.GetAssembly(typeof(AutoTest).Assembly.Location)); nav.MoveToRoot(); AssertEqual("/", nav.Name); nav.MoveToFirstChild(); AssertEqual("Assembly", nav.Name); nav.MoveToFirstAttribute(); AssertEqual("Name", nav.Name); AssertEqual("aspectdng", nav.Value); nav.MoveToNextAttribute(); AssertEqual("FullName", nav.Name); AssertFalse(nav.MoveToNextAttribute(), "only be 2 attributes on an assembly"); AssertFalse(nav.MoveToFirstChild(), "attributes don't have firstChild"); nav.MoveToParent(); // To assembly AssertEqual("Assembly", nav.Name); nav.MoveToFirstChild(); AssertEqual("Attribute", nav.Name); nav.MoveToNext(); AssertEqual("Attribute", nav.Name); while (nav.Name != "Type") nav.MoveToNext(); nav.MoveToNext(); // Second type AssertEqual(1, nav.ChildIndex); Navigator otherNav = (Navigator) nav.Clone(); AssertTrue(nav.MoveToFirstChild()); // Type children while (nav.Name != "Method") nav.MoveToNext(); // First Method nav.MoveToNext(); // Second Method AssertEqual(1, nav.ChildIndex); AssertTrue(nav.MoveToFirstChild()); // Method children nav.MoveToFirstAttribute(); AssertTrue(nav.MoveToParent()); // Back to method children AssertTrue(nav.MoveToParent()); // Back to Method AssertTrue(nav.MoveToParent()); // Back to Type otherNav.MoveToFirstChild(); otherNav.MoveToParent(); AssertTrue(nav.IsSamePosition(otherNav)); nav.MoveToRoot(); AssertEqual(1, nav.SelectList("/").Count); AssertEqual(1, nav.SelectList("/Assembly").Count); AssertFalse(nav.SelectList("//Type").Count == 0); AssertFalse(nav.SelectList("//Type[starts-with(@Name, 'A')]").Count == 0); AssertTrue(nav.SelectList("//Type/*/*/*").Count == 0); AssertTrue(nav.SelectList("//Type").Count > nav.SelectList("//Type[starts-with(@Name, 'A')]").Count); AssertTrue(nav.SelectList("//Type").Count > nav.SelectList("//Type[.//Attribute]").Count); AssertFalse(nav.SelectList("//Type[.//Attribute]").Count == 0); DateTime start = DateTime.Now; int nb = nav.SelectList("//*[name(following::*[1]) = 'Type']").Count; Console.WriteLine("Time elapsed : " + (DateTime.Now - start).Milliseconds); }