static void AnalyzeParameterList(SyntaxNodeAnalysisContext nodeContext) { var node = nodeContext.Node as TypeParameterListSyntax; var member = node.Parent; if (member == null) return; var memberSymbol = nodeContext.SemanticModel.GetDeclaredSymbol(member); if (memberSymbol.IsAbstract || memberSymbol.IsVirtual || memberSymbol.IsOverride) return; if (memberSymbol.ExplicitInterfaceImplementations().Length > 0) return; var walker = new ReferenceFinder(nodeContext); walker.Visit(member); foreach (var param in node.Parameters) { var sym = nodeContext.SemanticModel.GetDeclaredSymbol(param); if (sym == null) continue; if (!walker.UsedTypeParameters.Contains(sym)) { var diagnostic = Diagnostic.Create(descriptor, param.Identifier.GetLocation(), sym.Name); nodeContext.ReportDiagnostic(diagnostic); } } }
public static bool DerivesFrom(this TypeDefinition typeDefinition, Type type) { if (!type.IsClass) { throw new InvalidOperationException("The <type> argument (" + type.Name + ") must be a class type."); } var referenceFinder = new ReferenceFinder(typeDefinition.Module); var baseTypeDefinition = referenceFinder.GetTypeReference(type); return typeDefinition.DerivesFrom(baseTypeDefinition); }
public void Execute() { var msCoreReferenceFinder = new ReferenceFinder { ModuleDefinition = ModuleDefinition, AssemblyResolver = AssemblyResolver }; msCoreReferenceFinder.Execute(); var interceptorFinder = new InterceptorFinder { ModuleDefinition = ModuleDefinition }; interceptorFinder.Execute(); var methodProcessor = new MethodProcessor { referenceFinder = msCoreReferenceFinder, typeSystem = ModuleDefinition.TypeSystem, InterceptorFinder = interceptorFinder, }; foreach (var typeDefinition in ModuleDefinition.GetTypes()) { if (typeDefinition.ContainsTimeAttribute()) { methodProcessor.Process(typeDefinition.Methods.Where(x => !x.IsAbstract)); continue; } foreach (var method in typeDefinition.Methods) { if (method.IsAbstract) { continue; } if (!method.ContainsTimeAttribute()) { continue; } methodProcessor.Process(method); } } }
public MethodDecorator(ModuleDefinition moduleDefinition) { this._referenceFinder = new ReferenceFinder(moduleDefinition); }
public void ChangeSavedOneToOneReferenceTest() { Key person1Key, person2Key; Key license1Key, license2Key; using (var populateSession = Domain.OpenSession()) using (var transaction = populateSession.OpenTransaction()) { var person = new Person() { DriverLicense = new DriverLicense() }; person1Key = person.Key; license1Key = person.DriverLicense.Key; person = new Person() { DriverLicense = new DriverLicense() }; person2Key = person.Key; license2Key = person.DriverLicense.Key; transaction.Complete(); } using (var session = Domain.OpenSession(sessionConfiguration)) { var person1 = session.Query.Single <Person>(person1Key); var person2 = session.Query.Single <Person>(person2Key); var license1 = person1.DriverLicense; var license2 = person2.DriverLicense; Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(person1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(person2).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(license1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(license2).Count(), Is.EqualTo(1)); var newlicense = new DriverLicense(); person1.DriverLicense = newlicense; Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(person1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(person2).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(license1).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(license2).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(newlicense).Count(), Is.EqualTo(1)); var newPerson = new Person(); license2.Person = newPerson; Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(person1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(person2).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(newPerson).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(license1).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(license2).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(newlicense).Count(), Is.EqualTo(1)); person1.DriverLicense = null; Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(person1).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(person2).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(newPerson).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(license1).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(license2).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(newlicense).Count(), Is.EqualTo(0)); license2.Person = null; Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(person1).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(person2).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(newPerson).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(license1).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(license2).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(newlicense).Count(), Is.EqualTo(0)); } }
public override void Run(RefactoringOptions options) { if (options.SelectedItem is LocalVariable || options.SelectedItem is IParameter) { var col = ReferenceFinder.FindReferences(options.SelectedItem); if (col == null) { return; } TextEditorData data = options.GetTextEditorData(); Mono.TextEditor.TextEditor editor = data.Parent; if (editor == null) { MessageService.ShowCustomDialog(new RenameItemDialog(options, this)); return; } List <TextLink> links = new List <TextLink> (); TextLink link = new TextLink("name"); int baseOffset = Int32.MaxValue; foreach (MemberReference r in col) { baseOffset = Math.Min(baseOffset, data.Document.LocationToOffset(r.Line, r.Column)); } foreach (MemberReference r in col) { Segment segment = new Segment(data.Document.LocationToOffset(r.Line, r.Column) - baseOffset, r.Name.Length); if (segment.Offset <= data.Caret.Offset - baseOffset && data.Caret.Offset - baseOffset <= segment.EndOffset) { link.Links.Insert(0, segment); } else { link.AddLink(segment); } } links.Add(link); if (editor.CurrentMode is TextLinkEditMode) { ((TextLinkEditMode)editor.CurrentMode).ExitTextLinkMode(); } TextLinkEditMode tle = new TextLinkEditMode(editor, baseOffset, links); tle.SetCaretPosition = false; tle.SelectPrimaryLink = true; if (tle.ShouldStartTextLinkMode) { ModeHelpWindow helpWindow = new ModeHelpWindow(); helpWindow.TransientFor = IdeApp.Workbench.RootWindow; helpWindow.TitleText = options.SelectedItem is LocalVariable?GettextCatalog.GetString("<b>Local Variable -- Renaming</b>") : GettextCatalog.GetString("<b>Parameter -- Renaming</b>"); helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Key</b>"), GettextCatalog.GetString("<b>Behavior</b>"))); helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Return</b>"), GettextCatalog.GetString("<b>Accept</b> this refactoring."))); helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Esc</b>"), GettextCatalog.GetString("<b>Cancel</b> this refactoring."))); tle.HelpWindow = helpWindow; tle.Cancel += delegate { editor.Document.Undo(); }; tle.OldMode = data.CurrentMode; tle.StartMode(); data.CurrentMode = tle; } } else { MessageService.ShowCustomDialog(new RenameItemDialog(options, this)); } }
string GetReferences() { var referenceFinder = new ReferenceFinder(assemblyPath, projectPath); var builder = new StringBuilder(); var assemblyNameReferences = ModuleDefinition.ReadModule(assemblyPath).AssemblyReferences; foreach (var assemblyNameReference in assemblyNameReferences) { builder.Append(referenceFinder.Resolve(assemblyNameReference)); builder.Append(";"); } builder.Append(referenceFinder.Resolve("System")); builder.Append(";"); builder.Append(referenceFinder.Resolve("System.Core")); builder.Append(";"); return builder.ToString(); }
public MethodDecorator(ModuleDefinition moduleDefinition) { this.referenceFinder = new ReferenceFinder(moduleDefinition); }
public static void AddDebuggerDisplayAttributes(ModuleDefinition moduleDefinition, TypeDefinition type, ReferenceFinder referenceFinder) { if (type.IsEnum || type.CustomAttributes.Any(c => c.AttributeType.Name == "CompilerGeneratedAttribute" || c.AttributeType.Name == "DebuggerDisplayAttribute")) { return; } var fields = type.Fields .Where(f => f.IsPublic && !f.HasConstant && !f.IsStatic && CanPrint(f.FieldType)) .Cast <MemberReference>(); var props = type.Properties .Where(p => p.GetMethod != null && p.GetMethod.IsPublic && !p.GetMethod.IsStatic && !p.GetMethod.HasParameters && CanPrint(p.PropertyType)) .Cast <MemberReference>(); var displayBits = fields.Concat(props) .OrderBy(m => m, new DisplayAttributeOrderComparer()) .ToList(); if (!displayBits.Any()) { return; } AddSimpleDebuggerDisplayAttribute(moduleDefinition, type, referenceFinder); if (type.Methods.Any(m => m.Name == "DebuggerDisplay" && m.Parameters.Count == 0)) { return; } var debuggerDisplayMethod = new MethodDefinition("DebuggerDisplay", MethodAttributes.Private | MethodAttributes.HideBySig | MethodAttributes.SpecialName, moduleDefinition.TypeSystem.String); var body = debuggerDisplayMethod.Body; var arrayVar = new VariableDefinition(moduleDefinition.TypeSystem.Object.MakeArrayType()); body.Variables.Add(arrayVar); body.SimplifyMacros(); var instructions = body.Instructions; instructions.Add(Instruction.Create(OpCodes.Ldstr, string.Join(" | ", displayBits.Select((m, i) => $"{DisplayName(m)} = \"{{{i}}}\"")))); instructions.Add(Instruction.Create(OpCodes.Ldc_I4, displayBits.Count)); instructions.Add(Instruction.Create(OpCodes.Newarr, moduleDefinition.TypeSystem.Object)); instructions.Add(Instruction.Create(OpCodes.Stloc, arrayVar)); for (var i = 0; i < displayBits.Count; i++) { instructions.Add(Instruction.Create(OpCodes.Ldloc, arrayVar)); instructions.Add(Instruction.Create(OpCodes.Ldc_I4, i)); instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); if (displayBits[i] is FieldDefinition field) { instructions.Add(Instruction.Create(OpCodes.Ldfld, field)); if (!field.FieldType.IsRefType()) { instructions.Add(Instruction.Create(OpCodes.Box, field.FieldType)); } } if (displayBits[i] is PropertyDefinition property) { instructions.Add(Instruction.Create(OpCodes.Call, property.GetMethod)); if (!property.PropertyType.IsRefType()) { instructions.Add(Instruction.Create(OpCodes.Box, property.PropertyType)); } } instructions.Add(Instruction.Create(OpCodes.Stelem_Ref)); } instructions.Add(Instruction.Create(OpCodes.Ldloc, arrayVar)); instructions.Add(Instruction.Create(OpCodes.Call, referenceFinder.StringFormat)); instructions.Add(Instruction.Create(OpCodes.Ret)); body.InitLocals = true; body.OptimizeMacros(); type.Methods.Add(debuggerDisplayMethod); }
public override void Run(RefactoringOptions options) { if (options.SelectedItem is IVariable) { var field = options.SelectedItem as IField; if (field != null && (field.Accessibility != Accessibility.Private || field.DeclaringTypeDefinition != null && field.DeclaringTypeDefinition.Parts.Count > 1)) { MessageService.ShowCustomDialog(new RenameItemDialog(options, this)); return; } var par = options.SelectedItem as IParameter; if (par != null && (par.Owner.Accessibility != Accessibility.Private || par.Owner.DeclaringTypeDefinition != null && par.Owner.DeclaringTypeDefinition.Parts.Count > 1)) { MessageService.ShowCustomDialog(new RenameItemDialog(options, this)); return; } var col = ReferenceFinder.FindReferences(options.SelectedItem, true); if (col == null) { return; } var data = options.Document != null?options.GetTextEditorData() : IdeApp.Workbench.ActiveDocument.Editor; var editor = data.Parent; if (editor == null) { MessageService.ShowCustomDialog(new RenameItemDialog(options, this)); return; } var links = new List <TextLink> (); var link = new TextLink("name"); int baseOffset = Int32.MaxValue; foreach (var r in col) { baseOffset = Math.Min(baseOffset, r.Offset); } foreach (MemberReference r in col) { var segment = new TextSegment(r.Offset - baseOffset, r.Length); if (segment.Offset <= data.Caret.Offset - baseOffset && data.Caret.Offset - baseOffset <= segment.EndOffset) { link.Links.Insert(0, segment); } else { link.AddLink(segment); } } links.Add(link); if (editor.CurrentMode is TextLinkEditMode) { ((TextLinkEditMode)editor.CurrentMode).ExitTextLinkMode(); } TextLinkEditMode tle = new TextLinkEditMode(editor, baseOffset, links); tle.SetCaretPosition = false; tle.SelectPrimaryLink = true; if (tle.ShouldStartTextLinkMode) { var helpWindow = new TableLayoutModeHelpWindow(); helpWindow.TitleText = options.SelectedItem is IVariable?GettextCatalog.GetString("<b>Local Variable -- Renaming</b>") : GettextCatalog.GetString("<b>Parameter -- Renaming</b>"); helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Key</b>"), GettextCatalog.GetString("<b>Behavior</b>"))); helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Return</b>"), GettextCatalog.GetString("<b>Accept</b> this refactoring."))); helpWindow.Items.Add(new KeyValuePair <string, string> (GettextCatalog.GetString("<b>Esc</b>"), GettextCatalog.GetString("<b>Cancel</b> this refactoring."))); tle.HelpWindow = helpWindow; tle.Cancel += delegate { if (tle.HasChangedText) { editor.Document.Undo(); } }; tle.OldMode = data.CurrentMode; tle.StartMode(); data.CurrentMode = tle; } } else { MessageService.ShowCustomDialog(new RenameItemDialog(options, this)); } }
static void AddIEnumerableTProxy(ModuleDefinition moduleDefinition, TypeDefinition type, GenericInstanceType enumerableT, ReferenceFinder referenceFinder) { var itemType = enumerableT.GenericArguments[0]; var itemArray = itemType.MakeArrayType(); var proxyType = CreateProxy(moduleDefinition, type, referenceFinder); TypeReference proxyTypeRef = proxyType; if (type.HasGenericParameters) { proxyTypeRef = proxyType.MakeGenericInstanceType(type.GenericParameters.Select(CloneGenericParameter).ToArray()); } var field = proxyType.Fields[0]; var fieldRef = new FieldReference(field.Name, field.FieldType, proxyTypeRef); var listCtor = referenceFinder.ListCtor.MakeHostInstanceGeneric(itemType); var listToArray = referenceFinder.ListToArray.MakeHostInstanceGeneric(itemType); var getMethod = new MethodDefinition("get_Items", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName, itemArray); getMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); getMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ldfld, fieldRef)); getMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Newobj, listCtor)); getMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Call, listToArray)); getMethod.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); proxyType.Methods.Add(getMethod); var property = new PropertyDefinition("Items", PropertyAttributes.None, itemArray) { GetMethod = getMethod }; var debuggerBrowsableAttribute = new CustomAttribute(referenceFinder.DebuggerBrowsableAttributeCtor); debuggerBrowsableAttribute.ConstructorArguments.Add(new CustomAttributeArgument(referenceFinder.DebuggerBrowsableStateType, (int)DebuggerBrowsableState.RootHidden)); property.CustomAttributes.Add(debuggerBrowsableAttribute); proxyType.Properties.Add(property); AddDebuggerTypeProxyAttribute(type, proxyType, referenceFinder); }
static TypeDefinition CreateProxy(ModuleDefinition moduleDefinition, TypeDefinition type, ReferenceFinder referenceFinder) { var proxyType = new TypeDefinition( null, $"<{type.Name.Split('`')[0]}>Proxy", TypeAttributes.NestedPrivate | TypeAttributes.Sealed | TypeAttributes.BeforeFieldInit, moduleDefinition.TypeSystem.Object); TypeReference proxyTypeRef = proxyType; type.NestedTypes.Add(proxyType); proxyType.CustomAttributes.Add(new CustomAttribute(referenceFinder.CompilerGeneratedAttributeCtor)); if (type.HasGenericParameters) { foreach (var genericParameter in type.GenericParameters) { proxyType.GenericParameters.Add(new GenericParameter(genericParameter.Name, proxyType)); } proxyTypeRef = proxyType.MakeGenericInstanceType(type.GenericParameters.Select(CloneGenericParameter).ToArray()); } var originalType = moduleDefinition.ImportReference(type); if (type.HasGenericParameters) { originalType = originalType.MakeGenericInstanceType(proxyType.GenericParameters.ToArray()); } var field = new FieldDefinition("original", FieldAttributes.Private | FieldAttributes.InitOnly, originalType); proxyType.Fields.Add(field); var fieldRef = new FieldReference(field.Name, field.FieldType, proxyTypeRef); var method = new MethodDefinition( ".ctor", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.RTSpecialName, moduleDefinition.TypeSystem.Void); method.Parameters.Add(new ParameterDefinition("original", ParameterAttributes.None, originalType)); method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); method.Body.Instructions.Add(Instruction.Create(OpCodes.Call, moduleDefinition.ImportReference(moduleDefinition.TypeSystem.Object.Resolve().Constructors().First()))); method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); method.Body.Instructions.Add(Instruction.Create(OpCodes.Ldarg_1)); method.Body.Instructions.Add(Instruction.Create(OpCodes.Stfld, fieldRef)); method.Body.Instructions.Add(Instruction.Create(OpCodes.Ret)); proxyType.Methods.Add(method); return(proxyType); }
public void ClearEntitySetTest() { Key firstReferencingEntityKey, secondReferencingEntityKey; Key firstReferencedEntityKey, secondReferencedEntityKey; Key sharedReferencedEntityKey; using (var populateSession = Domain.OpenSession()) using (var transaction = populateSession.OpenTransaction()) { var firstReferencingEntity = new EntityWithManyReferences(); firstReferencingEntityKey = firstReferencingEntity.Key; var secondReferencingEntity = new EntityWithManyReferences(); secondReferencingEntityKey = secondReferencingEntity.Key; var firstReferencedEntity = new ReferencedEntity(); firstReferencedEntityKey = firstReferencedEntity.Key; firstReferencingEntity.Entities.Add(firstReferencedEntity); var secondReferencedEntity = new ReferencedEntity(); secondReferencedEntityKey = secondReferencedEntity.Key; secondReferencingEntity.Entities.Add(secondReferencedEntity); var sharedReferencedEntity = new ReferencedEntity(); sharedReferencedEntityKey = sharedReferencedEntity.Key; firstReferencingEntity.Entities.Add(sharedReferencedEntity); secondReferencingEntity.Entities.Add(sharedReferencedEntity); transaction.Complete(); } using (var session = Domain.OpenSession(sessionConfiguration)) { var firstReferencingEntity = session.Query.Single <EntityWithManyReferences>(firstReferencingEntityKey); var firstReferencedEntity = session.Query.Single <ReferencedEntity>(firstReferencedEntityKey); var secondReferencingEntity = session.Query.Single <EntityWithManyReferences>(secondReferencingEntityKey); var secondReferencedEntity = session.Query.Single <ReferencedEntity>(secondReferencedEntityKey); var sharedReferencedEntity = session.Query.Single <ReferencedEntity>(sharedReferencedEntityKey); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); var expectedAssociation = firstReferencingEntity.TypeInfo.Fields["Entities"].Associations.First(); Assert.That(expectedAssociation.IsPaired, Is.False); Assert.That(expectedAssociation.TargetType, Is.EqualTo(firstReferencedEntity.TypeInfo)); var references = ReferenceFinder.GetReferencesTo(firstReferencedEntity).ToList(); Assert.That(references.Count, Is.EqualTo(1)); Assert.That(references.Any(r => r.Association == expectedAssociation && r.ReferencedEntity == firstReferencedEntity && r.ReferencingEntity == firstReferencingEntity), Is.True); references = ReferenceFinder.GetReferencesTo(secondReferencedEntity).ToList(); Assert.That(references.Count, Is.EqualTo(1)); Assert.That(references.Any(r => r.Association == expectedAssociation && r.ReferencedEntity == secondReferencedEntity && r.ReferencingEntity == secondReferencingEntity), Is.True); references = ReferenceFinder.GetReferencesTo(sharedReferencedEntity).ToList(); Assert.That(references.Count, Is.EqualTo(2)); Assert.That(references.Any(r => r.Association == expectedAssociation && r.ReferencedEntity == sharedReferencedEntity && r.ReferencingEntity == firstReferencingEntity), Is.True); Assert.That(references.Any(r => r.Association == expectedAssociation && r.ReferencedEntity == sharedReferencedEntity && r.ReferencingEntity == secondReferencingEntity), Is.True); firstReferencingEntity.Entities.Clear(); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(2)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); references = ReferenceFinder.GetReferencesTo(firstReferencedEntity).ToList(); Assert.That(references.Count, Is.EqualTo(0)); references = ReferenceFinder.GetReferencesTo(secondReferencedEntity).ToList(); Assert.That(references.Count, Is.EqualTo(1)); Assert.That(references.Any(r => r.Association == expectedAssociation && r.ReferencedEntity == secondReferencedEntity && r.ReferencingEntity == secondReferencingEntity), Is.True); references = ReferenceFinder.GetReferencesTo(sharedReferencedEntity).ToList(); Assert.That(references.Count, Is.EqualTo(1)); Assert.That(references.Any(r => r.Association == expectedAssociation && r.ReferencedEntity == sharedReferencedEntity && r.ReferencingEntity == firstReferencingEntity), Is.False); Assert.That(references.Any(r => r.Association == expectedAssociation && r.ReferencedEntity == sharedReferencedEntity && r.ReferencingEntity == secondReferencingEntity), Is.True); secondReferencingEntity.Entities.Clear(); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(4)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); references = ReferenceFinder.GetReferencesTo(firstReferencedEntity).ToList(); Assert.That(references.Count, Is.EqualTo(0)); references = ReferenceFinder.GetReferencesTo(secondReferencedEntity).ToList(); Assert.That(references.Count, Is.EqualTo(0)); references = ReferenceFinder.GetReferencesTo(sharedReferencedEntity).ToList(); Assert.That(references.Count, Is.EqualTo(0)); } }
public override List <Change> PerformChanges(RefactoringOptions options, object prop) { List <Change> result = new List <Change> (); TextEditorData data = options.GetTextEditorData(); MemberResolveResult resolveResult = options.ResolveResult as MemberResolveResult; IProperty property = resolveResult.ResolvedMember as IProperty; INRefactoryASTProvider astProvider = options.GetASTProvider(); string backingStoreName = RetrieveBackingStore(options, astProvider, property); int backinStoreStart; int backinStoreEnd; IField backingStore = GetBackingStoreField(options, backingStoreName, out backinStoreStart, out backinStoreEnd); if (backingStore != null) { using (var monitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor(true, true)) { foreach (MemberReference memberRef in ReferenceFinder.FindReferences(backingStore, monitor)) { result.Add(new TextReplaceChange() { FileName = memberRef.FileName, Offset = memberRef.Position, RemovedChars = memberRef.Name.Length, InsertedText = property.Name }); } } result.RemoveAll(c => backinStoreStart <= ((TextReplaceChange)c).Offset && ((TextReplaceChange)c).Offset <= backinStoreEnd); result.Add(new TextReplaceChange() { FileName = options.Document.FileName, Offset = backinStoreStart, RemovedChars = backinStoreEnd - backinStoreStart }); } if (property.HasGet) { int startOffset = data.Document.LocationToOffset(property.GetRegion.Start.ToDocumentLocation(data.Document)); int endOffset = data.Document.LocationToOffset(property.GetRegion.End.ToDocumentLocation(data.Document)); string text = astProvider.OutputNode(options.Dom, new PropertyGetRegion(null, null), options.GetIndent(property) + "\t").Trim(); result.RemoveAll(c => startOffset <= ((TextReplaceChange)c).Offset && ((TextReplaceChange)c).Offset <= endOffset); result.Add(new TextReplaceChange() { FileName = options.Document.FileName, Offset = startOffset, RemovedChars = endOffset - startOffset, InsertedText = text }); } int setStartOffset; int setEndOffset; PropertySetRegion setRegion = new PropertySetRegion(null, null); string setText; if (property.HasSet) { setStartOffset = data.Document.LocationToOffset(property.SetRegion.Start.ToDocumentLocation(data.Document)); setEndOffset = data.Document.LocationToOffset(property.SetRegion.End.ToDocumentLocation(data.Document)); setText = astProvider.OutputNode(options.Dom, setRegion, options.GetIndent(property) + "\t").Trim(); } else { setEndOffset = setStartOffset = data.Document.LocationToOffset(property.GetRegion.End.ToDocumentLocation(data.Document)); setRegion.Modifier = ICSharpCode.NRefactory.Ast.Modifiers.Private; setText = Environment.NewLine + astProvider.OutputNode(options.Dom, setRegion, options.GetIndent(property) + "\t").TrimEnd(); } result.RemoveAll(c => setStartOffset <= ((TextReplaceChange)c).Offset && ((TextReplaceChange)c).Offset <= setEndOffset); result.Add(new TextReplaceChange() { FileName = options.Document.FileName, Offset = setStartOffset, RemovedChars = setEndOffset - setStartOffset, InsertedText = setText }); return(result); }
public void AddEntityToEntitySetTest() { Key firstReferencingKey; Key secondReferencingKey; var firstList = new List <Key>(); var secondList = new List <Key>(); using (var populateSession = Domain.OpenSession()) using (var transaction = populateSession.OpenTransaction()) { var referencedEntities = new List <ReferencedEntity>(); for (int i = 0; i < 10; i++) { referencedEntities.Add(new ReferencedEntity()); } var referencingEntity = new EntityWithManyReferences(); firstReferencingKey = referencingEntity.Key; foreach (var referencedEntity in referencedEntities.Take(5)) { referencingEntity.Entities.Add(referencedEntity); firstList.Add(referencedEntity.Key); } referencingEntity = new EntityWithManyReferences(); secondReferencingKey = referencingEntity.Key; foreach (var referencedEntity in referencedEntities.Skip(5)) { referencingEntity.Entities.Add(referencedEntity); secondList.Add(referencedEntity.Key); } transaction.Complete(); } using (var session = Domain.OpenSession(sessionConfiguration)) { var firstReferencingEntity = session.Query.Single <EntityWithManyReferences>(firstReferencingKey); var secondReferencingEntity = session.Query.Single <EntityWithManyReferences>(secondReferencingKey); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); var expectedAssociation = firstReferencingEntity.TypeInfo.Fields["Entities"].Associations.First(); Assert.That(expectedAssociation.IsPaired, Is.False); Assert.That(expectedAssociation.TargetType, Is.EqualTo(Domain.Model.Types[typeof(ReferencedEntity)])); List <ReferenceInfo> references; foreach (var key in firstList) { var referencedEntity = session.Query.Single <ReferencedEntity>(key); references = ReferenceFinder.GetReferencesTo(referencedEntity).ToList(); Assert.That(references.Count, Is.EqualTo(1)); Assert.That(references.Any(r => r.Association == expectedAssociation && r.ReferencedEntity == referencedEntity && r.ReferencingEntity == firstReferencingEntity), Is.True); } foreach (var key in secondList) { var referencedEntity = session.Query.Single <ReferencedEntity>(key); references = ReferenceFinder.GetReferencesTo(referencedEntity).ToList(); Assert.That(references.Count, Is.EqualTo(1)); Assert.That(references.Any(r => r.Association == expectedAssociation && r.ReferencedEntity == referencedEntity && r.ReferencingEntity == secondReferencingEntity), Is.True); } var newReferencedEntity1 = new ReferencedEntity(); var newReferencedEntity2 = new ReferencedEntity(); var newReferencedEntity3 = new ReferencedEntity(); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(newReferencedEntity1).Count(), Is.EqualTo(0)); firstReferencingEntity.Entities.Add(newReferencedEntity1); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(1)); references = ReferenceFinder.GetReferencesTo(newReferencedEntity1).ToList(); Assert.That(references.Count, Is.EqualTo(1)); Assert.That(references.Any(r => r.Association == expectedAssociation && r.ReferencedEntity == newReferencedEntity1 && r.ReferencingEntity == firstReferencingEntity), Is.True); secondReferencingEntity.Entities.Add(newReferencedEntity2); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(2)); references = ReferenceFinder.GetReferencesTo(newReferencedEntity2).ToList(); Assert.That(references.Count, Is.EqualTo(1)); Assert.That(references.Any(r => r.Association == expectedAssociation && r.ReferencedEntity == newReferencedEntity2 && r.ReferencingEntity == secondReferencingEntity), Is.True); firstReferencingEntity.Entities.Add(newReferencedEntity3); secondReferencingEntity.Entities.Add(newReferencedEntity3); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(4)); references = ReferenceFinder.GetReferencesTo(newReferencedEntity3).ToList(); Assert.That(references.Count, Is.EqualTo(2)); Assert.That(references.Any(r => r.Association == expectedAssociation && r.ReferencedEntity == newReferencedEntity3 && r.ReferencingEntity == firstReferencingEntity), Is.True); Assert.That(references.Any(r => r.Association == expectedAssociation && r.ReferencedEntity == newReferencedEntity3 && r.ReferencingEntity == secondReferencingEntity), Is.True); var entityFromFirstList = session.Query.Single <ReferencedEntity>(firstList[0]); references = ReferenceFinder.GetReferencesTo(entityFromFirstList).ToList(); Assert.That(references.Count, Is.EqualTo(1)); Assert.That(references.Any(r => r.Association == expectedAssociation && r.ReferencedEntity == entityFromFirstList && r.ReferencingEntity == firstReferencingEntity), Is.True); secondReferencingEntity.Entities.Add(entityFromFirstList); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(5)); references = ReferenceFinder.GetReferencesTo(entityFromFirstList).ToList(); Assert.That(references.Count, Is.EqualTo(2)); Assert.That(references.Any(r => r.Association == expectedAssociation && r.ReferencedEntity == entityFromFirstList && r.ReferencingEntity == firstReferencingEntity), Is.True); Assert.That(references.Any(r => r.Association == expectedAssociation && r.ReferencedEntity == entityFromFirstList && r.ReferencingEntity == secondReferencingEntity), Is.True); } }
public void RemoveEntityToEntitySetTest() { Key tableWithItemsKey; Key item1Key, item2Key; Key book1Key, book2Key; Key author1Key, author2Key; using (var populateSession = Domain.OpenSession()) using (var transaction = populateSession.OpenTransaction()) { var tableWithItems = new TableOfContent(); tableWithItemsKey = tableWithItems.Key; var item1 = new TableOfContentItem(); var item2 = new TableOfContentItem(); item1Key = item1.Key; item2Key = item2.Key; tableWithItems.Items.Add(item1); tableWithItems.Items.Add(item2); var book1 = new Book(); book1Key = book1.Key; var book2 = new Book(); book2Key = book2.Key; var author1 = new Author(); author1Key = author1.Key; var author2 = new Author(); author2Key = author2.Key; author1.Books.Add(book1); author1.Books.Add(book2); author2.Books.Add(book1); author2.Books.Add(book2); transaction.Complete(); } using (var session = Domain.OpenSession(sessionConfiguration)) { var tableWithItems = session.Query.Single <TableOfContent>(tableWithItemsKey); var item1 = session.Query.Single <TableOfContentItem>(item1Key); var item2 = session.Query.Single <TableOfContentItem>(item2Key); var item3 = new TableOfContentItem(); tableWithItems.Items.Add(item3); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(item1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(item2).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(item3).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(tableWithItems).Count(), Is.EqualTo(3)); tableWithItems.Items.Remove(item3); tableWithItems.Items.Remove(item2); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(item1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(item2).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(item3).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(tableWithItems).Count(), Is.EqualTo(1)); var oldBook1 = session.Query.Single <Book>(book1Key); var oldBook2 = session.Query.Single <Book>(book2Key); var newBook = new Book(); var oldAutor1 = session.Query.Single <Author>(author1Key); var oldAutor2 = session.Query.Single <Author>(author2Key); var newAuthor = new Author(); newAuthor.Books.Add(oldBook1); newAuthor.Books.Add(oldBook2); newAuthor.Books.Add(newBook); newBook.Authors.Add(oldAutor1); newBook.Authors.Add(oldAutor2); Assert.That(ReferenceFinder.GetReferencesTo(oldBook1).Count(), Is.EqualTo(3)); Assert.That(ReferenceFinder.GetReferencesTo(oldBook2).Count(), Is.EqualTo(3)); Assert.That(ReferenceFinder.GetReferencesTo(oldAutor1).Count(), Is.EqualTo(3)); Assert.That(ReferenceFinder.GetReferencesTo(oldAutor2).Count(), Is.EqualTo(3)); Assert.That(ReferenceFinder.GetReferencesTo(newBook).Count(), Is.EqualTo(3)); Assert.That(ReferenceFinder.GetReferencesTo(newAuthor).Count(), Is.EqualTo(3)); oldAutor1.Books.Remove(oldBook1); Assert.That(ReferenceFinder.GetReferencesTo(oldBook1).Count(), Is.EqualTo(2)); Assert.That(ReferenceFinder.GetReferencesTo(oldBook2).Count(), Is.EqualTo(3)); Assert.That(ReferenceFinder.GetReferencesTo(oldAutor1).Count(), Is.EqualTo(2)); Assert.That(ReferenceFinder.GetReferencesTo(oldAutor2).Count(), Is.EqualTo(3)); Assert.That(ReferenceFinder.GetReferencesTo(newBook).Count(), Is.EqualTo(3)); Assert.That(ReferenceFinder.GetReferencesTo(newAuthor).Count(), Is.EqualTo(3)); oldBook2.Authors.Remove(oldAutor2); Assert.That(ReferenceFinder.GetReferencesTo(oldBook1).Count(), Is.EqualTo(2)); Assert.That(ReferenceFinder.GetReferencesTo(oldBook2).Count(), Is.EqualTo(2)); Assert.That(ReferenceFinder.GetReferencesTo(oldAutor1).Count(), Is.EqualTo(2)); Assert.That(ReferenceFinder.GetReferencesTo(oldAutor2).Count(), Is.EqualTo(2)); Assert.That(ReferenceFinder.GetReferencesTo(newBook).Count(), Is.EqualTo(3)); Assert.That(ReferenceFinder.GetReferencesTo(newAuthor).Count(), Is.EqualTo(3)); oldAutor1.Books.Remove(newBook); newBook.Authors.Remove(oldAutor2); Assert.That(ReferenceFinder.GetReferencesTo(oldBook1).Count(), Is.EqualTo(2)); Assert.That(ReferenceFinder.GetReferencesTo(oldBook2).Count(), Is.EqualTo(2)); Assert.That(ReferenceFinder.GetReferencesTo(oldAutor1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(oldAutor2).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(newBook).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(newAuthor).Count(), Is.EqualTo(3)); newBook.Authors.Remove(newAuthor); Assert.That(ReferenceFinder.GetReferencesTo(oldBook1).Count(), Is.EqualTo(2)); Assert.That(ReferenceFinder.GetReferencesTo(oldBook2).Count(), Is.EqualTo(2)); Assert.That(ReferenceFinder.GetReferencesTo(oldAutor1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(oldAutor2).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(newBook).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(newAuthor).Count(), Is.EqualTo(2)); } }
public void ChangeReferenceToSameObjectTest() { Key person1Key, person2Key; Key license1Key, license2Key; Key tableOfContentKey, item1Key, item2Key; using (var populateSession = Domain.OpenSession()) using (var transaction = populateSession.OpenTransaction()) { var person1 = new Person { DriverLicense = new DriverLicense() }; var person2 = new Person { DriverLicense = new DriverLicense() }; person1Key = person1.Key; person2Key = person2.Key; license1Key = person1.DriverLicense.Key; license2Key = person2.DriverLicense.Key; var tableOfContent = new TableOfContent(); tableOfContentKey = tableOfContent.Key; var item1 = new TableOfContentItem(); item1Key = item1.Key; tableOfContent.Items.Add(item1); var item2 = new TableOfContentItem(); item2Key = item2.Key; tableOfContent.Items.Add(item2); transaction.Complete(); } using (var session = Domain.OpenSession(sessionConfiguration)) { var person = session.Query.Single <Person>(person1Key); var license = session.Query.Single <DriverLicense>(license1Key); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(person).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(license).Count(), Is.EqualTo(1)); person.DriverLicense = license; Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(person).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(license).Count(), Is.EqualTo(1)); person = session.Query.Single <Person>(person2Key); license = session.Query.Single <DriverLicense>(license2Key); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(person).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(license).Count(), Is.EqualTo(1)); license.Person = person; Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(person).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(license).Count(), Is.EqualTo(1)); var item1 = session.Query.Single <TableOfContentItem>(item1Key); var item2 = session.Query.Single <TableOfContentItem>(item2Key); var tableOfContent = session.Query.Single <TableOfContent>(tableOfContentKey); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(item1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(item2).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContent).Count(), Is.EqualTo(2)); item1.TableOfContent = tableOfContent; Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(item1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(item2).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContent).Count(), Is.EqualTo(2)); item2.TableOfContent = tableOfContent; Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(item1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(item2).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContent).Count(), Is.EqualTo(2)); } }
public static void FindDerivedClasses(ITypeDefinition cls) { var solution = IdeApp.ProjectOperations.CurrentSelectedSolution; if (solution == null) { return; } var sourceProject = TypeSystemService.GetProject(cls); if (sourceProject == null) { return; } var projects = ReferenceFinder.GetAllReferencingProjects(solution, sourceProject); ThreadPool.QueueUserWorkItem(delegate { using (var monitor = IdeApp.Workbench.ProgressMonitors.GetSearchProgressMonitor(true, true)) { var cache = new Dictionary <string, TextEditorData> (); Parallel.ForEach(projects, p => { var comp = TypeSystemService.GetCompilation(p); if (comp == null) { return; } var importedType = comp.Import(cls); if (importedType == null) { return; } foreach (var type in comp.MainAssembly.GetAllTypeDefinitions()) { if (!type.IsDerivedFrom(importedType)) { continue; } TextEditorData textFile; if (!cache.TryGetValue(type.Region.FileName, out textFile)) { cache [type.Region.FileName] = textFile = TextFileProvider.Instance.GetTextEditorData(type.Region.FileName); } int position = textFile.LocationToOffset(type.Region.Begin); string keyword; switch (type.Kind) { case TypeKind.Interface: keyword = "interface"; break; case TypeKind.Struct: keyword = "struct"; break; case TypeKind.Delegate: keyword = "delegate"; break; case TypeKind.Enum: keyword = "enum"; break; default: keyword = "class"; break; } while (position < textFile.Length - keyword.Length) { if (textFile.GetTextAt(position, keyword.Length) == keyword) { position += keyword.Length; while (position < textFile.Length && textFile.GetCharAt(position) == ' ' || textFile.GetCharAt(position) == '\t') { position++; } break; } position++; } monitor.ReportResult(new MonoDevelop.Ide.FindInFiles.SearchResult(new FileProvider(type.Region.FileName, p), position, 0)); } }); foreach (var tf in cache.Values) { if (tf.Parent == null) { tf.Dispose(); } } } }); }
static void AddDebuggerTypeProxyAttribute(TypeDefinition type, TypeDefinition proxyType, ReferenceFinder referenceFinder) { var debuggerTypeProxyAttribute = new CustomAttribute(referenceFinder.DebuggerTypeProxyAttributeCtor); debuggerTypeProxyAttribute.ConstructorArguments.Add(new CustomAttributeArgument(referenceFinder.SystemType, proxyType)); type.CustomAttributes.Add(debuggerTypeProxyAttribute); }
private void ProcessItems(IList <Entity> entities) { if (entities.Count == 0) { return; } var entityType = entities[0].TypeInfo; var sequence = entityType.GetRemovalAssociationSequence(); if (sequence == null || sequence.Count == 0) { return; } ExecutePrefetchAction(entities); var referenceDescriptors = new List <ReferenceDescriptor>(); foreach (var association in sequence) { if (association.OwnerType.UnderlyingType.IsAssignableFrom(entityType.UnderlyingType)) { foreach (var entity in entities) { var descriptor = new ReferenceDescriptor { RemovingEntity = entity, Processor = GetProcessor(association.OnOwnerRemove.Value), Association = association, References = ReferenceFinder.GetReferencesFrom(entity, association), IsOutgoing = true }; referenceDescriptors.Add(descriptor); } } if (association.TargetType.UnderlyingType.IsAssignableFrom(entityType.UnderlyingType)) { foreach (var entity in entities) { var descriptor = new ReferenceDescriptor { RemovingEntity = entity, Processor = GetProcessor(association.OnTargetRemove.Value), Association = association, References = ReferenceFinder.GetReferencesTo(entity, association), IsOutgoing = false }; referenceDescriptors.Add(descriptor); } } } if (Session.Handler.ExecutePrefetchTasks() == null) { Session.ExecuteUserDefinedDelayedQueries(false); } foreach (var container in referenceDescriptors) { var processor = container.Processor; var association = container.Association; var removingEntity = container.RemovingEntity; if (container.IsOutgoing) { foreach (var reference in container.References.ToList()) { processor.Process(Context, association, removingEntity, reference.ReferencedEntity, removingEntity, reference.ReferencedEntity); } } else { foreach (var reference in container.References.ToList()) { processor.Process(Context, association, removingEntity, reference.ReferencingEntity, reference.ReferencingEntity, removingEntity); } } } }
public static void AddDebuggerTypeProxyAttributes(ModuleDefinition moduleDefinition, TypeDefinition type, ReferenceFinder referenceFinder) { if (type.CustomAttributes.Any(c => c.AttributeType.Name == "CompilerGeneratedAttribute" || c.AttributeType.Name == "DebuggerTypeProxyAttribute")) { return; } if (type.CustomAttributes.All(c => c.AttributeType.Name != "DebuggerEnumerableTypeAttribute")) { return; } var collectionT = type.Interfaces.FirstOrDefault(i => i.InterfaceType.Name == "ICollection`1"); if (collectionT != null) { AddICollectionTProxy(moduleDefinition, type, (GenericInstanceType)collectionT.InterfaceType, referenceFinder); return; } var enumerableT = type.Interfaces.FirstOrDefault(i => i.InterfaceType.Name == "IEnumerable`1"); if (enumerableT != null) { AddIEnumerableTProxy(moduleDefinition, type, (GenericInstanceType)enumerableT.InterfaceType, referenceFinder); } }
static void AddSimpleDebuggerDisplayAttribute(ModuleDefinition moduleDefinition, TypeDefinition type, ReferenceFinder referenceFinder) { var debuggerDisplay = new CustomAttribute(referenceFinder.DebuggerDisplayAttributeCtor); debuggerDisplay.ConstructorArguments.Add(new CustomAttributeArgument(moduleDefinition.TypeSystem.String, "{DebuggerDisplay(),nq}")); type.CustomAttributes.Add(debuggerDisplay); }
public override void Run(RefactoringOptions options) { if (options.SelectedItem is IVariable) { var field = options.SelectedItem as IField; if (field != null && (field.Accessibility != Accessibility.Private || field.DeclaringTypeDefinition != null && field.DeclaringTypeDefinition.Parts.Count > 1)) { using (var dlg = new RenameItemDialog(options, this)) MessageService.ShowCustomDialog(dlg); return; } var par = options.SelectedItem as IParameter; if (par != null && par.Owner != null && (par.Owner.Accessibility != Accessibility.Private || par.Owner.DeclaringTypeDefinition != null && par.Owner.DeclaringTypeDefinition.Parts.Count > 1)) { using (var dlg = new RenameItemDialog(options, this)) MessageService.ShowCustomDialog(dlg); return; } var col = ReferenceFinder.FindReferences(options.SelectedItem, true); if (col == null) { return; } var data = options.Document != null?options.GetTextEditorData() : IdeApp.Workbench.ActiveDocument.Editor; var editor = data.Parent; if (editor == null) { using (var dlg = new RenameItemDialog(options, this)) MessageService.ShowCustomDialog(dlg); return; } var links = new List <TextLink> (); var link = new TextLink("name"); int baseOffset = Int32.MaxValue; foreach (var r in col) { baseOffset = Math.Min(baseOffset, r.Offset); } foreach (MemberReference r in col) { var segment = new TextSegment(r.Offset - baseOffset, r.Length); if (segment.Offset <= data.Caret.Offset - baseOffset && data.Caret.Offset - baseOffset <= segment.EndOffset) { link.Links.Insert(0, segment); } else { link.AddLink(segment); } } links.Add(link); if (editor.CurrentMode is TextLinkEditMode) { ((TextLinkEditMode)editor.CurrentMode).ExitTextLinkMode(); } TextLinkEditMode tle = new TextLinkEditMode(editor, baseOffset, links); tle.SetCaretPosition = false; tle.SelectPrimaryLink = true; if (tle.ShouldStartTextLinkMode) { tle.Cancel += delegate { if (tle.HasChangedText) { editor.Document.Undo(); } }; tle.OldMode = data.CurrentMode; tle.StartMode(); data.CurrentMode = tle; } } else { using (var dlg = new RenameItemDialog(options, this)) MessageService.ShowCustomDialog(dlg); } }
public override List <Change> PerformChanges(RefactoringOptions options, object prop) { RenameProperties properties = (RenameProperties)prop; List <Change> result = new List <Change> (); IEnumerable <MemberReference> col = null; using (var monitor = new MessageDialogProgressMonitor(true, false, false, true)) { col = ReferenceFinder.FindReferences(options.SelectedItem, properties.IncludeOverloads, monitor); if (col == null) { return(result); } if (properties.RenameFile && options.SelectedItem is IType) { var cls = ((IType)options.SelectedItem).GetDefinition(); int currentPart = 1; HashSet <string> alreadyRenamed = new HashSet <string> (); foreach (var part in cls.Parts) { if (alreadyRenamed.Contains(part.Region.FileName)) { continue; } alreadyRenamed.Add(part.Region.FileName); string oldFileName = System.IO.Path.GetFileNameWithoutExtension(part.Region.FileName); string newFileName; if (oldFileName.ToUpper() == properties.NewName.ToUpper() || oldFileName.ToUpper().EndsWith("." + properties.NewName.ToUpper())) { continue; } int idx = oldFileName.IndexOf(cls.Name); if (idx >= 0) { newFileName = oldFileName.Substring(0, idx) + properties.NewName + oldFileName.Substring(idx + cls.Name.Length); } else { newFileName = currentPart != 1 ? properties.NewName + currentPart : properties.NewName; currentPart++; } int t = 0; while (System.IO.File.Exists(GetFullFileName(newFileName, part.Region.FileName, t))) { t++; } result.Add(new RenameFileChange(part.Region.FileName, GetFullFileName(newFileName, part.Region.FileName, t))); } } foreach (var memberRef in col) { TextReplaceChange change = new TextReplaceChange(); change.FileName = memberRef.FileName; change.Offset = memberRef.Offset; change.RemovedChars = memberRef.Length; change.InsertedText = properties.NewName; change.Description = string.Format(GettextCatalog.GetString("Replace '{0}' with '{1}'"), memberRef.GetName(), properties.NewName); result.Add(change); } } return(result); }
private void DecorateByType(MethodDecorator.Fody.MethodDecorator decorator) { var referenceFinder = new ReferenceFinder(this.ModuleDefinition); var markerTypeDefinitions = this.FindMarkerTypes(); // Look for rules in the assembly and module. var assemblyRules = FindAspectRules(this.ModuleDefinition.Assembly.CustomAttributes); var moduleRules = FindAspectRules(this.ModuleDefinition.CustomAttributes); // Read the top-level and nested types from this module foreach (var type in this.ModuleDefinition.Types.SelectMany(x => GetAllTypes(x))) { // Look for rules on the type and marker attributes var classRules = FindByMarkerType(markerTypeDefinitions, type.CustomAttributes) .Concat(FindAspectRules(type.CustomAttributes, true)); // Loop through all methods in this type foreach (var method in type.Methods.Where(x => x.HasBody)) { // Find any rules applied to the method. var methodRules = FindByMarkerType(markerTypeDefinitions, method.CustomAttributes) .Concat(FindAspectRules(method.CustomAttributes, true)); // Join together all the rules and give them an ordering number starting at 0 for // the highest level (assembly) to N as a lowest level (last attribute on the method) var allRules = assemblyRules .Concat(moduleRules) .Concat(classRules) .Concat(methodRules) .Select((Rule, ScopeOrdering) => new { Rule, ScopeOrdering }); var orderedList = allRules .Where(x => x.Rule.Match(type, method)) .OrderByDescending(x => x.Rule.AspectPriority) .ThenByDescending(x => x.ScopeOrdering) .GroupBy(x => x.Rule.MethodDecoratorAttribute.AttributeType); // Group the rules by the aspect type foreach (var aspectSet in orderedList) { // Sort the rules in priority order (so that attributes applied to the // method take precedence over the type, module then assembly) // Then pick out the first rule - this tells us whether to include // or exclude. var rule = aspectSet .OrderBy(x => x.Rule.AttributePriority) .ThenByDescending(x => x.ScopeOrdering) .Select(x => x.Rule) .FirstOrDefault(); // If we have a rule and it isn't an exclusion, apply the method decoration. if (rule != null && !rule.AttributeExclude) { decorator.Decorate( type, method, rule.MethodDecoratorAttribute, rule.ExplicitMatch); } } } } }
private IEnumerable<string> GetCopyLocal() { var referenceFinder = new ReferenceFinder(assemblyPath, projectPath); var assemblyNameReferences = ModuleDefinition.ReadModule(assemblyPath).AssemblyReferences; foreach (var assemblyNameReference in assemblyNameReferences) { if (!assemblyNameReference.FullName.Contains("PreEmbed")) { yield return referenceFinder.Resolve(assemblyNameReference); } } }
public static MethodReference GetGetMethod(this PropertyDefinition property, TypeReference targetType) { MethodReference method = property.GetMethod; if (method.DeclaringType.HasGenericParameters) { var genericInstanceType = property.DeclaringType.GetGenericInstanceType(targetType); method = new MethodReference(method.Name, method.ReturnType.IsGenericParameter ? method.ReturnType : ReferenceFinder.ImportCustom(method.ReturnType)) { DeclaringType = ReferenceFinder.ImportCustom(genericInstanceType), HasThis = true }; } return(method); }
static void AddICollectionTProxy(ModuleDefinition moduleDefinition, TypeDefinition type, GenericInstanceType collectionT, ReferenceFinder referenceFinder) { var itemType = collectionT.GenericArguments[0]; var itemArray = itemType.MakeArrayType(); var proxyType = CreateProxy(moduleDefinition, type, referenceFinder); TypeReference proxyTypeRef = proxyType; if (type.HasGenericParameters) { proxyTypeRef = proxyType.MakeGenericInstanceType(type.GenericParameters.Select(CloneGenericParameter).ToArray()); } var field = proxyType.Fields[0]; var fieldRef = new FieldReference(field.Name, field.FieldType, proxyTypeRef); var countProperty = type.Properties.First(p => p.Name == "Count" || p.Name == "System.Collections.ICollection.Count"); MethodReference countMethod = countProperty.GetMethod; MethodReference copyToMethod = type.Methods.First(p => p.Name == "CopyTo" || p.Name == "System.Collections.ICollection.CopyTo"); if (type.HasGenericParameters) { countMethod = countMethod.MakeHostInstanceGeneric(type.GenericParameters.Select(CloneGenericParameter).ToArray()); copyToMethod = copyToMethod.MakeHostInstanceGeneric(type.GenericParameters.Select(CloneGenericParameter).ToArray()); } var getMethod = new MethodDefinition("get_Items", MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName, itemArray); var getMethodBody = getMethod.Body; var localItems = new VariableDefinition(itemArray); getMethodBody.Variables.Add(localItems); getMethodBody.SimplifyMacros(); getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldfld, fieldRef)); getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Callvirt, countMethod)); getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Newarr, itemType)); getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Stloc, localItems)); getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldarg_0)); getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldfld, fieldRef)); getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldloc, localItems)); getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldloc, localItems)); getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldlen)); getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Conv_I4)); getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Callvirt, copyToMethod)); getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ldloc, localItems)); getMethodBody.Instructions.Add(Instruction.Create(OpCodes.Ret)); getMethodBody.InitLocals = true; getMethodBody.OptimizeMacros(); proxyType.Methods.Add(getMethod); var property = new PropertyDefinition("Items", PropertyAttributes.None, itemArray) { GetMethod = getMethod }; var debuggerBrowsableAttribute = new CustomAttribute(referenceFinder.DebuggerBrowsableAttributeCtor); debuggerBrowsableAttribute.ConstructorArguments.Add(new CustomAttributeArgument(referenceFinder.DebuggerBrowsableStateType, (int)DebuggerBrowsableState.RootHidden)); property.CustomAttributes.Add(debuggerBrowsableAttribute); proxyType.Properties.Add(property); AddDebuggerTypeProxyAttribute(type, proxyType, referenceFinder); }
public void InitializeOneToManyReferenceFieldTest() { using (var session = Domain.OpenSession(sessionConfiguration)) { var tableOfContent = new TableOfContent(); var tableOfContentItem1 = new TableOfContentItem(); var tableOfContentItem2 = new TableOfContentItem(); var tableOfContentItem3 = new TableOfContentItem(); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContent).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem1).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem2).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem3).Count(), Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); tableOfContent.Items.Add(tableOfContentItem1); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContent).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem2).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem3).Count(), Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); tableOfContent.Items.Add(tableOfContentItem2); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContent).Count(), Is.EqualTo(2)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem2).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem3).Count(), Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); tableOfContent.Items.Add(tableOfContentItem3); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContent).Count(), Is.EqualTo(3)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem2).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem3).Count(), Is.EqualTo(1)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); tableOfContent = new TableOfContent(); tableOfContentItem1 = new TableOfContentItem(); tableOfContentItem2 = new TableOfContentItem(); tableOfContentItem3 = new TableOfContentItem(); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContent).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem1).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem2).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem3).Count(), Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); tableOfContentItem1.TableOfContent = tableOfContent; Assert.That(ReferenceFinder.GetReferencesTo(tableOfContent).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem2).Count(), Is.EqualTo(0)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem3).Count(), Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); tableOfContentItem2.TableOfContent = tableOfContent; Assert.That(ReferenceFinder.GetReferencesTo(tableOfContent).Count(), Is.EqualTo(2)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem2).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem3).Count(), Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); tableOfContentItem3.TableOfContent = tableOfContent; Assert.That(ReferenceFinder.GetReferencesTo(tableOfContent).Count(), Is.EqualTo(3)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem1).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem2).Count(), Is.EqualTo(1)); Assert.That(ReferenceFinder.GetReferencesTo(tableOfContentItem3).Count(), Is.EqualTo(1)); Assert.That(session.NonPairedReferencesRegistry.AddedReferencesCount, Is.EqualTo(0)); Assert.That(session.NonPairedReferencesRegistry.RemovedReferencesCount, Is.EqualTo(0)); } }