Exemplo n.º 1
0
        private void BindUsages(ICollection <MethodInvocation> usages, IDictionary <IMethod, ITypeParameter> map,
                                IProgressIndicator pi)
        {
            if (usages.Count == 0)
            {
                return;
            }

            pi.Start(usages.Count);
            foreach (MethodInvocation usage in usages)
            {
                if (usage.IsValid())
                {
                    ITypeParameter typeParameter;
                    if (map.TryGetValue(usage.Method, out typeParameter))
                    {
                        // one more call to language specific code...
                        Exec[usage.Reference.GetTreeNode().Language].BindUsage(usage, typeParameter);
                    }
                    else
                    {
                        Exec[usage.Reference.GetTreeNode().Language].BindUsage(usage, null);
                    }
                }
                pi.Advance(1);
            }
        }
        public override void Rename(IRenameRefactoring executer, IProgressIndicator pi, bool hasConflictsWithDeclarations,
                                    IRefactoringDriver driver)
        {
            // Rename the "declaration"
            var declaredElement  = myPointer.FindDeclaredElement();
            var originalTreeNode = declaredElement?.GetTreeNode();

            if (originalTreeNode == null)
            {
                return;
            }

            var originalRange     = originalTreeNode.GetDocumentRange();
            var factory           = JavaScriptElementFactory.GetInstance(originalTreeNode);
            var literalExpression = factory.CreateExpression("\"$0\"", NewName);
            var newExpression     = originalTreeNode.ReplaceBy(literalExpression);

            RemoveFromTextualOccurrences(executer, originalRange);

            var references = executer.Workflow.GetElementReferences(declaredElement);

            if (!Enumerable.Any(references))
            {
                return;
            }

            pi.Start(references.Count);

            // Create a new declared element (other implementations don't appear to cache this, either)
            var element = new AsmDefNameDeclaredElement(declaredElement.GetJsServices(), NewName,
                                                        declaredElement.SourceFile, newExpression.GetTreeStartOffset().Offset);

            // Rename/bind the references
            foreach (var pair in LanguageUtil.SortReferences(references.Where(r => r.IsValid())))
            {
                foreach (var sortedReference in LanguageUtil.GetSortedReferences(pair.Value))
                {
                    InterruptableActivityCookie.CheckAndThrow(pi);

                    var referenceRange = sortedReference.GetDocumentRange();

                    if (sortedReference.IsValid())
                    {
                        sortedReference.BindTo(element);
                    }

                    RemoveFromTextualOccurrences(executer, referenceRange);

                    pi.Advance();
                }
            }

            element.GetPsiServices().Caches.Update();
            myNewPointer = element.CreateElementPointer();
        }
        private FileSystemPath Deobfuscate(IObfuscatedFile obfuscatedFile, IProgressIndicator progressIndicator)
        {
            try
            {
                progressIndicator.Start(5);
                progressIndicator.TaskName = "Deobfuscating...";
                progressIndicator.CurrentItemText = string.Format("Saving to {0}", obfuscatedFile.NewFilename);

                progressIndicator.Advance(1);
                obfuscatedFile.deobfuscateBegin();

                progressIndicator.Advance(1);
                obfuscatedFile.deobfuscate();

                progressIndicator.Advance(1);
                obfuscatedFile.deobfuscateEnd();

                // turn all flags on
                RenamerFlags flags = RenamerFlags.DontCreateNewParamDefs | RenamerFlags.DontRenameDelegateFields |
                                     RenamerFlags.RenameEvents |
                                     RenamerFlags.RenameFields | RenamerFlags.RenameGenericParams |
                                     RenamerFlags.RenameMethodArgs |
                                     RenamerFlags.RenameMethods | RenamerFlags.RenameNamespaces |
                                     RenamerFlags.RenameProperties |
                                     RenamerFlags.RenameTypes | RenamerFlags.RestoreEvents |
                                     RenamerFlags.RestoreEventsFromNames |
                                     RenamerFlags.RestoreProperties | RenamerFlags.RestorePropertiesFromNames;
                var renamer = new Renamer(obfuscatedFile.DeobfuscatorContext, new[] { obfuscatedFile }, flags);
                progressIndicator.Advance(1);
                renamer.rename();

                progressIndicator.Advance(1);
                obfuscatedFile.save();
            }
            finally
            {
                progressIndicator.Stop();
            }

            return new FileSystemPath(obfuscatedFile.NewFilename);
        }
Exemplo n.º 4
0
        private FileSystemPath Deobfuscate(IObfuscatedFile obfuscatedFile, IProgressIndicator progressIndicator)
        {
            try
            {
                progressIndicator.Start(5);
                progressIndicator.TaskName        = "Deobfuscating...";
                progressIndicator.CurrentItemText = string.Format("Saving to {0}", obfuscatedFile.NewFilename);

                progressIndicator.Advance(1);
                obfuscatedFile.deobfuscateBegin();

                progressIndicator.Advance(1);
                obfuscatedFile.deobfuscate();

                progressIndicator.Advance(1);
                obfuscatedFile.deobfuscateEnd();

                // turn all flags on
                RenamerFlags flags = RenamerFlags.DontCreateNewParamDefs | RenamerFlags.DontRenameDelegateFields |
                                     RenamerFlags.RenameEvents |
                                     RenamerFlags.RenameFields | RenamerFlags.RenameGenericParams |
                                     RenamerFlags.RenameMethodArgs |
                                     RenamerFlags.RenameMethods | RenamerFlags.RenameNamespaces |
                                     RenamerFlags.RenameProperties |
                                     RenamerFlags.RenameTypes | RenamerFlags.RestoreEvents |
                                     RenamerFlags.RestoreEventsFromNames |
                                     RenamerFlags.RestoreProperties | RenamerFlags.RestorePropertiesFromNames;
                var renamer = new Renamer(obfuscatedFile.DeobfuscatorContext, new[] { obfuscatedFile }, flags);
                progressIndicator.Advance(1);
                renamer.rename();

                progressIndicator.Advance(1);
                obfuscatedFile.save();
            }
            finally
            {
                progressIndicator.Stop();
            }

            return(new FileSystemPath(obfuscatedFile.NewFilename));
        }
Exemplo n.º 5
0
        private IEnumerable <MethodInvocation> PreProcessMethodUsages(ICollection <IReference> references,
                                                                      IProgressIndicator pi)
        {
            if (references.Count == 0)
            {
                yield break;
            }

            pi.Start(references.Count);
            foreach (IReference reference in references)
            {
                MethodInvocation usage = Exec[reference.GetTreeNode().Language].ProcessUsage(reference);
                if (usage != null)
                {
                    yield return(usage);
                }
                pi.Advance(1);
            }
        }
Exemplo n.º 6
0
        private void ProcessParameterUsages(ICollection <IReference> references, IProgressIndicator pi)
        {
            // can not start progress indicator with zero count.
            if (references.Count == 0)
            {
                return;
            }

            pi.Start(references.Count);
            foreach (IReference reference in references)
            {
                // reference can be invalid if previous changes affected it's element. It is unlikely to be occured...
                if (reference.IsValid())
                {
                    // process reference with language specific implementation...
                    Exec[reference.GetTreeNode().Language].ProcessParameterReference(reference);
                }
                pi.Advance(1);
            }
        }
    public override ICollection<IOccurence> Search(IProgressIndicator progressIndicator)
    {
      // Create and configure utility class, which will perform search
      var searcher = new StringSearcher(mySearchString, myCaseSensitive);

      // Fetch all projects for solution, start progress 
      ICollection<IProject> projects = Solution.GetAllProjects();
      progressIndicator.Start(projects.Count);
      var items = new List<IOccurence>();

      // visit each project and collect occurences
      var visitor = new ProjectTextSearcher(searcher, items, mySearchFlags, myDocumentManager, mySolution);
      foreach (IProject project in projects)
      {
        progressIndicator.CurrentItemText = string.Format("Scanning project '{0}'", project.Name);
        project.Accept(visitor);
        progressIndicator.Advance(1);
      }

      return items;
    }
        public override ICollection <IOccurence> Search(IProgressIndicator progressIndicator)
        {
            // Create and configure utility class, which will perform search
            var searcher = new StringSearcher(mySearchString, myCaseSensitive);

            // Fetch all projects for solution, start progress
            ICollection <IProject> projects = Solution.GetAllProjects();

            progressIndicator.Start(projects.Count);
            var items = new List <IOccurence>();

            // visit each project and collect occurences
            var visitor = new ProjectTextSearcher(searcher, items, mySearchFlags, myDocumentManager, mySolution);

            foreach (IProject project in projects)
            {
                progressIndicator.CurrentItemText = string.Format("Scanning project '{0}'", project.Name);
                project.Accept(visitor);
                progressIndicator.Advance(1);
            }

            return(items);
        }
    public override void Rename(RenameRefactoring executer, IProgressIndicator pi, bool hasConflictsWithDeclarations, IRefactoringDriver driver)
    {
      BuildDeclarations();

      //Logger.Assert(myDeclarations.Count > 0, "myDeclarations.Count > 0");

      IDeclaredElement declaredElement = myOriginalElementPointer.FindDeclaredElement();
      if (declaredElement == null)
      {
        return;
      }

      IPsiServices psiServices = declaredElement.GetPsiServices();

      IList<IReference> primaryReferences = executer.Workflow.GetElementReferences(PrimaryDeclaredElement);
      List<Pair<IDeclaredElement, IList<IReference>>> secondaryElementWithReferences = SecondaryDeclaredElements.Select(x => Pair.Of(x, executer.Workflow.GetElementReferences(x))).ToList();
      pi.Start(myDeclarations.Count + primaryReferences.Count);

      foreach (IDeclaration declaration in myDeclarations)
      {
        InterruptableActivityCookie.CheckAndThrow(pi);
        declaration.SetName(myNewName);
        pi.Advance();
      }

      psiServices.PsiManager.UpdateCaches();

      IDeclaredElement newDeclaredElement = null;
      if (myDeclarations.Count > 0)
      {
        newDeclaredElement = myDeclarations[0].DeclaredElement;
      }

      Assertion.Assert(newDeclaredElement != null, "The condition (newDeclaredElement != null) is false.");

      myNewElementPointer = newDeclaredElement.CreateElementPointer();
      Assertion.Assert(newDeclaredElement.IsValid(), "myNewDeclaredElement.IsValid()");


      myNewReferences.Clear();
      OneToSetMap<PsiLanguageType, IReference> references = LanguageUtil.SortReferences(primaryReferences.Where(x => x.IsValid()));
      IList<IReference> referencesToRename = new List<IReference>();
      foreach (var pair in references)
      {
        List<IReference> sortedReferences = LanguageUtil.GetSortedReferences(pair.Value);
        foreach (IReference reference in sortedReferences)
        {
          IReference oldReferenceForConflict = reference;
          InterruptableActivityCookie.CheckAndThrow(pi);
          if (reference.IsValid()) // reference may invalidate during additional reference processing
          {
            RenameHelperBase rename = executer.Workflow.LanguageSpecific[reference.GetTreeNode().Language];
            IReference reference1 = rename.TransformProjectedInitializer(reference);
            DeclaredElementInstance subst = GetSubst(newDeclaredElement, executer);
            IReference newReference;
            if (subst != null)
            {
              if (subst.Substitution.Domain.IsEmpty())
              {
                newReference = reference1.BindTo(subst.Element);
              }
              else
              {
                newReference = reference1.BindTo(subst.Element, subst.Substitution);
              }
            }
            else
            {
              newReference = reference1.BindTo(newDeclaredElement);
            }
            if (!(newReference is IImplicitReference))
            {
              IDeclaredElement element = newReference.Resolve().DeclaredElement;
              if (!hasConflictsWithDeclarations && !myDoNotShowBindingConflicts && (element == null || !element.Equals(newDeclaredElement)) && !rename.IsAlias(newDeclaredElement))
              {
                driver.AddLateConflict(() => new Conflict(newReference.GetTreeNode().GetSolution(), "Usage {0} can not be updated correctly.", ConflictSeverity.Error, oldReferenceForConflict), "not bound");
              }
              referencesToRename.Add(newReference);
            }
            myNewReferences.Insert(0, newReference);
            rename.AdditionalReferenceProcessing(newDeclaredElement, newReference, myNewReferences);
          }

          pi.Advance();
        }
      }

      foreach (var pair in secondaryElementWithReferences)
      {
        IDeclaredElement element = pair.First;
        IList<IReference> secondaryReferences = pair.Second;
        foreach (IReference reference in secondaryReferences)
        {
          InterruptableActivityCookie.CheckAndThrow(pi);
          if (reference.IsValid())
          {
            reference.BindTo(element);
          }
        }
      }
    }
        public override void Rename(IRenameRefactoring executer, IProgressIndicator pi, bool hasConflictsWithDeclarations, IRefactoringDriver driver)
        {
            BuildDeclarations();

            //Logger.Assert(myDeclarations.Count > 0, "myDeclarations.Count > 0");

            IDeclaredElement declaredElement = myOriginalElementPointer.FindDeclaredElement();

            if (declaredElement == null)
            {
                return;
            }

            IPsiServices psiServices = declaredElement.GetPsiServices();

            IList <IReference> primaryReferences = executer.Workflow.GetElementReferences(PrimaryDeclaredElement);
            List <Pair <IDeclaredElement, IList <IReference> > > secondaryElementWithReferences = SecondaryDeclaredElements.Select(x => Pair.Of(x, executer.Workflow.GetElementReferences(x))).ToList();

            pi.Start(myDeclarations.Count + primaryReferences.Count);

            foreach (IDeclaration declaration in myDeclarations)
            {
                InterruptableActivityCookie.CheckAndThrow(pi);
                declaration.SetName(myNewName);
                pi.Advance();
            }

            psiServices.Caches.Update();

            IDeclaredElement newDeclaredElement;

            if (myDeclarations.Count > 0)
            {
                newDeclaredElement = myDeclarations[0].DeclaredElement;
            }
            else
            {
                if (myElement is RoleDeclaredElement)
                {
                    newDeclaredElement = myElement;
                    ((RoleDeclaredElement)myElement).ChangeName = true;
                    ((RoleDeclaredElement)myElement).NewName    = NewName;
                    ((RoleDeclaredElement)newDeclaredElement).SetName(NewName);
                }
                else
                {
                    newDeclaredElement = null;
                }
            }
            Assertion.Assert(newDeclaredElement != null, "The condition (newDeclaredElement != null) is false.");

            myNewElementPointer = newDeclaredElement.CreateElementPointer();
            Assertion.Assert(newDeclaredElement.IsValid(), "myNewDeclaredElement.IsValid()");


            myNewReferences.Clear();
            OneToSetMap <PsiLanguageType, IReference> references = LanguageUtil.SortReferences(primaryReferences.Where(x => x.IsValid()));
            IList <IReference> referencesToRename = new List <IReference>();

            foreach (var pair in references)
            {
                List <IReference> sortedReferences = pair.Value.ToList();//LanguageUtil.GetSortedReferences(pair.Value);
                foreach (IReference reference in sortedReferences)
                {
                    IReference oldReferenceForConflict = reference;
                    InterruptableActivityCookie.CheckAndThrow(pi);
                    if (reference.IsValid()) // reference may invalidate during additional reference processing
                    {
                        RenameHelperBase        rename     = executer.Workflow.LanguageSpecific[reference.GetTreeNode().Language];
                        IReference              reference1 = rename.TransformProjectedInitializer(reference);
                        DeclaredElementInstance subst      = GetSubst(newDeclaredElement, executer);
                        IReference              newReference;
                        if (subst != null)
                        {
                            if (subst.Substitution.Domain.IsEmpty())
                            {
                                newReference = reference1.BindTo(subst.Element);
                            }
                            else
                            {
                                newReference = reference1.BindTo(subst.Element, subst.Substitution);
                            }
                        }
                        else
                        {
                            newReference = reference1.BindTo(newDeclaredElement);
                        }
                        if (!(newReference is IImplicitReference))
                        {
                            IDeclaredElement element = newReference.Resolve().DeclaredElement;
                            if (!hasConflictsWithDeclarations && !myDoNotShowBindingConflicts && (element == null || !element.Equals(newDeclaredElement)) && !rename.IsAlias(newDeclaredElement))
                            {
                                driver.AddLateConflict(() => new Conflict(newReference.GetTreeNode().GetSolution(), "Usage {0} can not be updated correctly.", ConflictSeverity.Error, oldReferenceForConflict), "not bound");
                            }
                            referencesToRename.Add(newReference);
                        }
                        myNewReferences.Insert(0, newReference);
                        rename.AdditionalReferenceProcessing(newDeclaredElement, newReference, myNewReferences);
                    }

                    pi.Advance();
                }
            }

            foreach (var pair in secondaryElementWithReferences)
            {
                IDeclaredElement   element             = pair.First;
                IList <IReference> secondaryReferences = pair.Second;
                foreach (IReference reference in secondaryReferences)
                {
                    InterruptableActivityCookie.CheckAndThrow(pi);
                    if (reference.IsValid())
                    {
                        reference.BindTo(element);
                    }
                }
            }

            if (myElement is RoleDeclaredElement)
            {
                ((RoleDeclaredElement)myElement).ChangeName = false;
                ((RoleDeclaredElement)myElement).SetName(NewName);
                foreach (IReference reference in referencesToRename)
                {
                    ((PsiRoleReference)reference).SetName(NewName);
                    reference.CurrentResolveResult = null;
                    ((PsiFile)((RoleDeclaredElement)myElement).File).ClearTables();
                }
            }
        }