Exemple #1
0
        private static IDeclaredElement TryFindDeclaration([NotNull] IInvocationExpression expression, [NotNull] string propertyName,
                                                           [NotNull] string requiredTypeName, [NotNull] string requiredMethodName, out string name)
        {
            name = null;
            var baseClass = expression.GetContainingNode <IClassLikeDeclaration>();

            if (baseClass == null)
            {
                return(null);
            }
            var result = TryFindIdDeclarationInClassLikeDeclaration(baseClass, propertyName, requiredTypeName, requiredMethodName, out name);

            if (result != null)
            {
                return(result);
            }

            var next = ClassLikeDeclarationNavigator.GetByNestedTypeDeclaration(baseClass);

            while (next != null)
            {
                baseClass = next;
                result    = TryFindIdDeclarationInClassLikeDeclaration(baseClass, propertyName, requiredTypeName, requiredMethodName, out name);
                if (result != null)
                {
                    return(result);
                }

                next = ClassLikeDeclarationNavigator.GetByNestedTypeDeclaration(baseClass);
            }

            return(null);
        }
Exemple #2
0
        /// <summary>
        /// Swap base to this unless local implementation.
        /// </summary>
        /// <param name="invocationExpression">
        /// The invocation expression.
        /// </param>
        public static void SwapBaseToThisUnlessLocalImplementation(IInvocationExpression invocationExpression)
        {
            bool isOverride = false;

            bool isNew = false;

            IPrimaryExpression invokedExpression = invocationExpression.InvokedExpression;

            if (invokedExpression != null)
            {
                IReferenceExpression referenceExpressionNode = invokedExpression as IReferenceExpression;

                if (referenceExpressionNode != null)
                {
                    IReferenceExpression referenceExpression = invokedExpression as IReferenceExpression;
                    if (referenceExpression != null)
                    {
                        ICSharpExpression qualifierExpression = referenceExpression.QualifierExpression;
                        if (qualifierExpression is IBaseExpression)
                        {
                            string methodName = referenceExpressionNode.NameIdentifier.Name;

                            ICSharpTypeDeclaration typeDeclaration = invocationExpression.GetContainingNode <ICSharpTypeDeclaration>(true);

                            if (typeDeclaration != null)
                            {
                                foreach (ICSharpTypeMemberDeclaration memberDeclaration in typeDeclaration.MemberDeclarations)
                                {
                                    if (memberDeclaration.DeclaredName == methodName)
                                    {
                                        IMethodDeclaration methodDeclaration = memberDeclaration as IMethodDeclaration;
                                        if (methodDeclaration != null)
                                        {
                                            isOverride = methodDeclaration.IsOverride;
                                            isNew      = methodDeclaration.IsNew();
                                            break;
                                        }
                                    }
                                }

                                if (isOverride || isNew)
                                {
                                    return;
                                }

                                using (WriteLockCookie.Create(true))
                                {
                                    // swap the base to this
                                    ICSharpExpression expression = CSharpElementFactory.GetInstance(invocationExpression.GetPsiModule()).CreateExpression("this");

                                    referenceExpression.SetQualifierExpression(expression);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #3
0
        private static void InsertDeclarations(
            [NotNull] IInvocationExpression invocation, ref LocalList <IDeclarationStatement> declarations)
        {
            IBlock           block  = invocation.GetContainingNode <IBlock>(true).NotNull();
            ICSharpStatement anchor = invocation.GetContainingStatement();

            for (var i = declarations.Count - 1; i >= 0; i--)
            {
                declarations[i] = block.AddStatementBefore(declarations[i], anchor);
                anchor          = declarations[i];
            }
        }
Exemple #4
0
        private static IClassLikeDeclaration GetTopLevelClassLikeDeclaration([NotNull] IInvocationExpression expression)
        {
            var baseClass = expression.GetContainingNode <IClassLikeDeclaration>();

            if (baseClass == null)
            {
                return(null);
            }

            var next = ClassLikeDeclarationNavigator.GetByNestedTypeDeclaration(baseClass);

            while (next != null)
            {
                baseClass = next;
                next      = ClassLikeDeclarationNavigator.GetByNestedTypeDeclaration(baseClass);
            }

            return(baseClass);
        }
        /// <summary>
        /// Swap base to this unless local implementation.
        /// </summary>
        /// <param name="invocationExpression">
        /// The invocation expression.
        /// </param>
        public static void SwapBaseToThisUnlessLocalImplementation(IInvocationExpression invocationExpression)
        {
            bool isOverride = false;

            bool isNew = false;

            IPrimaryExpression invokedExpression = invocationExpression.InvokedExpression;

            if (invokedExpression != null)
            {
                IReferenceExpression referenceExpressionNode = invokedExpression as IReferenceExpression;

                if (referenceExpressionNode != null)
                {
                    IReferenceExpression referenceExpression = invokedExpression as IReferenceExpression;
                    if (referenceExpression != null)
                    {
                        ICSharpExpression qualifierExpression = referenceExpression.QualifierExpression;
                        if (qualifierExpression is IBaseExpression)
                        {
                            string methodName = referenceExpressionNode.NameIdentifier.Name;

                            ICSharpTypeDeclaration typeDeclaration = invocationExpression.GetContainingNode<ICSharpTypeDeclaration>(true);

                            if (typeDeclaration != null)
                            {
                                foreach (ICSharpTypeMemberDeclaration memberDeclaration in typeDeclaration.MemberDeclarations)
                                {
                                    if (memberDeclaration.DeclaredName == methodName)
                                    {
                                        IMethodDeclaration methodDeclaration = memberDeclaration as IMethodDeclaration;
                                        if (methodDeclaration != null)
                                        {
                                            isOverride = methodDeclaration.IsOverride;
                                            isNew = methodDeclaration.IsNew();
                                            break;
                                        }
                                    }
                                }

                                if (isOverride || isNew || methodName.Equals("Equals", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    return;
                                }

                                using (WriteLockCookie.Create(true))
                                {
                                    // swap the base to this
                                    ICSharpExpression expression = CSharpElementFactory.GetInstance(invocationExpression.GetPsiModule()).CreateExpression("this");

                                    referenceExpression.SetQualifierExpression(expression);
                                }
                            }
                        }
                    }
                }
            }
        }
Exemple #6
0
        /// <summary>
        /// Swap base to this unless local implementation.
        /// </summary>
        /// <param name="invocationExpression">
        /// The invocation expression.
        /// </param>
        public static void SwapBaseToThisUnlessLocalImplementation(IInvocationExpression invocationExpression)
        {
            bool isOverride = false;

            bool isNew = false;

            IPrimaryExpression invokedExpression = invocationExpression.InvokedExpression;

            if (invokedExpression == null)
            {
                return;
            }

            IReferenceExpression referenceExpression = invokedExpression as IReferenceExpression;

            if (referenceExpression == null)
            {
                return;
            }

            ICSharpExpression qualifierExpression = referenceExpression.QualifierExpression;

            if (!(qualifierExpression is IBaseExpression))
            {
                return;
            }

            ICSharpTypeDeclaration typeDeclaration = invocationExpression.GetContainingNode <ICSharpTypeDeclaration>(true);

            if (typeDeclaration == null)
            {
                return;
            }

            ITypeElement typeDeclaredElement = typeDeclaration.DeclaredElement;

            if (typeDeclaredElement == null)
            {
                return;
            }

            IDeclaredElement referenceDeclaredElement = referenceExpression.Reference.Resolve().DeclaredElement;

            if (referenceDeclaredElement == null)
            {
                return;
            }

            foreach (var member in typeDeclaredElement.GetAllClassMembers(referenceDeclaredElement.ShortName))
            {
                if (!member.Equals(referenceDeclaredElement))
                {
                    continue;
                }

                using (WriteLockCookie.Create(true))
                {
                    // swap the base to this
                    ICSharpExpression expression = CSharpElementFactory.GetInstance(invocationExpression.GetPsiModule()).CreateExpression("this");

                    referenceExpression.SetQualifierExpression(expression);
                }
            }
        }
Exemple #7
0
 public bool CanReplace(IInvocationExpression element)
 => element.GetContainingNode <ILockStatement>() == null;