Example #1
0
 private void RefactorMethodNamesInStatement(CodeStatement statement, string newName)
 {
     if (typeof(CodeVariableDeclarationStatement) == statement.GetType())
     {
         CodeVariableDeclarationStatement vdeclStatement = (CodeVariableDeclarationStatement)statement;
         if (vdeclStatement.InitExpression != null)
         {
             if (typeof(CodeCastExpression) == vdeclStatement.InitExpression.GetType())
             {
                 CodeCastExpression castExp = (CodeCastExpression)vdeclStatement.InitExpression;
                 if (typeof(CodeMethodInvokeExpression) == castExp.Expression.GetType())
                 {
                     CodeMethodInvokeExpression miExp = (CodeMethodInvokeExpression)castExp.Expression;
                     miExp.Method.MethodName = PascalCaseConverterHelper.GetPascalCaseMethodName(miExp.Method.MethodName);
                 }
             }
             else if (typeof(CodeMethodInvokeExpression) == vdeclStatement.InitExpression.GetType())
             {
                 CodeMethodInvokeExpression miExp = (CodeMethodInvokeExpression)vdeclStatement.InitExpression;
                 miExp.Method.MethodName = PascalCaseConverterHelper.GetPascalCaseMethodName(miExp.Method.MethodName);
             }
         }
     }
     else if (typeof(CodeExpressionStatement) == statement.GetType())
     {
         CodeExpressionStatement ceStatement = (CodeExpressionStatement)statement;
         if (typeof(CodeMethodInvokeExpression) == ceStatement.Expression.GetType())
         {
             CodeMethodInvokeExpression miExp = (CodeMethodInvokeExpression)ceStatement.Expression;
             miExp.Method.MethodName = PascalCaseConverterHelper.GetPascalCaseMethodName(miExp.Method.MethodName);
         }
     }
     else if (typeof(CodeAssignStatement) == statement.GetType())
     {
         CodeAssignStatement asnStatement = (CodeAssignStatement)statement;
         if (typeof(CodeCastExpression) == asnStatement.Right.GetType())
         {
             CodeCastExpression castExp = (CodeCastExpression)asnStatement.Right;
             if (typeof(CodeMethodInvokeExpression) == castExp.Expression.GetType())
             {
                 CodeMethodInvokeExpression miExp = (CodeMethodInvokeExpression)castExp.Expression;
                 miExp.Method.MethodName = PascalCaseConverterHelper.GetPascalCaseMethodName(miExp.Method.MethodName);
             }
         }
         else if (typeof(CodeMethodInvokeExpression) == asnStatement.Right.GetType())
         {
             CodeMethodInvokeExpression miExp = (CodeMethodInvokeExpression)asnStatement.Right;
             miExp.Method.MethodName = PascalCaseConverterHelper.GetPascalCaseMethodName(miExp.Method.MethodName);
         }
     }
     else if (typeof(CodeMethodReturnStatement) == statement.GetType())
     {
         CodeMethodReturnStatement retStatement = (CodeMethodReturnStatement)statement;
         if (typeof(CodeMethodInvokeExpression) == retStatement.Expression.GetType())
         {
             CodeMethodInvokeExpression miExp = (CodeMethodInvokeExpression)retStatement.Expression;
             miExp.Method.MethodName = PascalCaseConverterHelper.GetPascalCaseMethodName(miExp.Method.MethodName);
         }
     }
 }
Example #2
0
        private void RefactorFieldNamesInFieldReferences(CodeExpression expression, string newName)
        {
            // Do we have a field reference in the expression we are given?
            if (typeof(CodeFieldReferenceExpression) != expression.GetType())
            {
                return;
            }

            CodeFieldReferenceExpression frefStatement = (CodeFieldReferenceExpression)expression;

            // Is target object of field reference referring to a variable declaration?
            if (typeof(CodeVariableReferenceExpression) == frefStatement.TargetObject.GetType())
            {
                CodeVariableReferenceExpression vrefStatement = (CodeVariableReferenceExpression)frefStatement.TargetObject;
                // Get a reference to the variable declaration statement.
                CodeVariableDeclarationStatement vdeclStatement = stackVariables[vrefStatement.VariableName];

                // Is this a variable of type we are modifying now?
                if (vdeclStatement.Type.BaseType == newName)
                {
                    // Then we can convert the field names to PascalCase.
                    frefStatement.FieldName = PascalCaseConverterHelper.GetPascalCaseName(frefStatement.FieldName);
                }
            }
        }
Example #3
0
        protected override void OnTypeNameChanged(CodeTypeExtension typeExtension, string oldName, string newName)
        {
            // Prepare the MessageContractAttribute attribute to specify the type name on the wire.
            CodeAttributeDeclaration messageContractAttribute = typeExtension.FindAttribute("System.ServiceModel.MessageContractAttribute");

            if (messageContractAttribute != null)
            {
                CodeAttributeArgument wrapperNameArgument = messageContractAttribute.Arguments
                                                            .OfType <CodeAttributeArgument>()
                                                            .FirstOrDefault(arg => arg.Name.Equals("WrapperName", StringComparison.OrdinalIgnoreCase));

                if (wrapperNameArgument == null)
                {
                    return;
                }

                CodePrimitiveExpression wrapperNameValue = wrapperNameArgument.Value as CodePrimitiveExpression;
                if (wrapperNameValue != null && !string.IsNullOrEmpty((string)wrapperNameValue.Value))
                {
                    string newWrapperNameValue = PascalCaseConverterHelper.GetPascalCaseName((string)wrapperNameValue.Value);
                    wrapperNameArgument.Value = new CodePrimitiveExpression(newWrapperNameValue);
                }
            }
            else
            {
                CodeAttributeDeclaration xmlType =
                    new CodeAttributeDeclaration("System.ServiceModel.MessageContractAttribute",
                                                 new CodeAttributeArgumentExtended("WrapperName",
                                                                                   new CodePrimitiveExpression(oldName), true));

                // Add the XmlTypeAttribute attribute to ctd.
                typeExtension.AddAttribute(xmlType);
            }
        }
Example #4
0
        /// <summary>
        /// Contains the core logic for converting a method name to Pascal case.
        /// </summary>
        private void ConvertMethod(CodeTypeMemberExtension memberExtension)
        {
            // Get a copy of the original name.
            string oldName = memberExtension.ExtendedObject.Name;
            // Change the method name to Pascal case.
            string newName = PascalCaseConverterHelper.GetPascalCaseMethodName(oldName);

            memberExtension.ExtendedObject.Name = newName;
            OnMethodNameChanged(memberExtension, oldName, newName);
        }
Example #5
0
        private void ConvertEnumMemberName(CodeTypeMemberExtension typeMemberExtension)
        {
            // Get a copy of the original type name.
            string oldName = typeMemberExtension.ExtendedObject.Name;
            // Change the field/property name to Pascal case.
            string newName = PascalCaseConverterHelper.GetPascalCaseName(oldName);

            typeMemberExtension.ExtendedObject.Name = newName;
            OnEnumMemberChanged(typeMemberExtension, oldName, newName);
        }
Example #6
0
        /// <summary>
        /// Changes the given type's name to Pascal case.
        /// </summary>
        private string ConvertTypeName(out string oldName)
        {
            // Holds the original type name.
            oldName = this.typeExtension.ExtendedObject.Name;
            // Change the type name to Pascal case.
            string newName = PascalCaseConverterHelper.GetPascalCaseName(oldName);

            this.typeExtension.ExtendedObject.Name = newName;
            OnTypeNameChanged(this.typeExtension, oldName, newName);
            return(newName);
        }
Example #7
0
        private void OnFieldOrPropertyNameChanged(CodeTypeMemberExtension memberExtension, string oldName, string newName)
        {
            // Here we basically have two cases. Array and non-array.
            // If it's an non-array type, we have to decorate it with either
            // XmlAttributeAttribute attribute or XmlElementAttribute attribute.
            // If it's an array type we have to decorate it with XmlArrayAttribute
            // attribute.

            // There is one quickie we can try before anything nevertheless.
            // Regardless of whether the member type is an array type or not
            // member can already have an XmlElementAttribute attribute.
            // If this is the case we can simply add the XML type name information to that.
            CodeAttributeDeclaration xmlElementAttribute = memberExtension.FindAttribute("System.Xml.Serialization.XmlElementAttribute");

            if (xmlElementAttribute != null)
            {
                // Create a new CodeAttributeDeclaration with required arguments
                xmlElementAttribute = new CodeAttributeDeclaration("System.Xml.Serialization.XmlElementAttribute",
                                                                   new CodeAttributeArgumentExtended(
                                                                       "ElementName", new CodePrimitiveExpression(oldName), true));

                // Add the newly created attribute to CodeTypeMember.
                memberExtension.AddAttribute(xmlElementAttribute);
                // No need to proceed, so simply return.
                return;
            }

            // Let's first handle the non-array case.
            // And then handl the array case.
            if (!PascalCaseConverterHelper.IsArray(memberExtension))
            {
                // See if we can spot the XmlAttributeAttribute attribute.
                CodeAttributeDeclaration xmlAttribute = memberExtension.FindAttribute("System.Xml.Serialization.XmlAttributeAttribute");
                // If we could, then let's add the AttributeName argument to it.
                if (xmlAttribute != null)
                {
                    // Create a new CodeAttributeDeclaration with required arguments.
                    CodeAttributeDeclaration xmlAttributeAttribute =
                        new CodeAttributeDeclaration("System.Xml.Serialization.XmlAttributeAttribute",
                                                     new CodeAttributeArgumentExtended(
                                                         "AttributeName", new CodePrimitiveExpression(oldName), true));

                    // Add the newly created attribute to CodeTypeMember.
                    memberExtension.AddAttribute(xmlAttributeAttribute);
                }
                else
                {
                    // We arrive here if we could not spot the XmlAttributeAttribute attribute.
                    // Therefore we can add the XmlElementAttribute attribute.
                    // However, before we proceed we have to check whether any of the following attributes
                    // already exists.
                    if (memberExtension.FindAttribute("System.Xml.Serialization.XmlTextAttribute") != null ||
                        memberExtension.FindAttribute("System.Xml.Serialization.XmlIgnoreAttribute") != null ||
                        memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyElementAttribute") != null ||
                        memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyAttributeAttribute") != null)
                    {
                        // We cannot add XmlElementAttribute attribute here.
                        return;
                    }

                    // Create a new CodeAttributeDeclaration with required arguments
                    xmlElementAttribute = new CodeAttributeDeclaration("System.Xml.Serialization.XmlElementAttribute",
                                                                       new CodeAttributeArgumentExtended(
                                                                           "ElementName", new CodePrimitiveExpression(oldName), true));

                    // Add the newly created attribute to CodeTypeMember.
                    memberExtension.AddAttribute(xmlElementAttribute);
                }
            }
            else
            {
                // We arrive here if we have an array type.
                // We can proceed to adding XmlArrayAttribue attribute if following attributes are
                // not present.
                if (memberExtension.FindAttribute("System.Xml.Serialization.XmlTextAttribute") != null ||
                    memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyElementAttribute") != null ||
                    memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyAttributeAttribute") != null)
                {
                    // We cannot add XmlElementAttribute attribute here.
                    return;
                }

                // Create a new CodeAttributeDeclaration for XmlArrayAttribute with required arguments.
                CodeAttributeDeclaration xmlArrayAttribute =
                    new CodeAttributeDeclaration("System.Xml.Serialization.XmlArrayAttribute",
                                                 new CodeAttributeArgumentExtended(
                                                     "ElementName", new CodePrimitiveExpression(oldName), true));

                // Add the newly created CodeAttributeDeclaration to the attributes collection of
                // CodeTypeMemeber.
                memberExtension.AddAttribute(xmlArrayAttribute);
            }
        }
Example #8
0
        protected override void OnEnumMemberChanged(CodeTypeMemberExtension memberExtension, string oldName, string newName)
        {
            // Fix references found in DefaultValue attributes.
            foreach (CodeTypeExtension type in Code.DataContracts)
            {
                foreach (CodeTypeMemberExtension member in type.Fields)
                {
                    CodeAttributeDeclaration attribute = member.FindAttribute("System.ComponentModel.DefaultValueAttribute");
                    if (attribute == null)
                    {
                        continue;
                    }

                    CodeAttributeArgument        argument      = attribute.Arguments[0];
                    CodeFieldReferenceExpression argumentValue = argument.Value as CodeFieldReferenceExpression;
                    if (argumentValue == null)
                    {
                        continue;
                    }

                    string baseTypeName           = ((CodeTypeReferenceExpression)argumentValue.TargetObject).Type.BaseType;
                    string nameOfTypeInAttribute  = PascalCaseConverterHelper.GetPascalCaseName(baseTypeName);
                    string nameOfTypeBeingChanged = memberExtension.Parent.ExtendedObject.Name;

                    if (argumentValue.FieldName == oldName && nameOfTypeInAttribute == nameOfTypeBeingChanged)
                    {
                        argumentValue.FieldName = newName;
                    }
                }

                // Fix references found in constructor where default values are set.
                // This is required for fixed references to enum values.
                // e.g. <xs:attribute ref="xlink:type" fixed="simple"/>
                foreach (CodeTypeMemberExtension ctorExtension in type.Constructors)
                {
                    // Get a reference to the actual constructor object.
                    CodeConstructor constructor = (CodeConstructor)ctorExtension.ExtendedObject;

                    // Do this for all statements we have in the constructor.
                    foreach (CodeStatement statement in constructor.Statements)
                    {
                        // Is this an assign statement?
                        CodeAssignStatement assignStatement = statement as CodeAssignStatement;
                        if (assignStatement != null)
                        {
                            // Do we have a field reference on the right side of the assignment statement?
                            CodeFieldReferenceExpression fieldRef = assignStatement.Right as CodeFieldReferenceExpression;
                            if (fieldRef != null)
                            {
                                // Does the referenced field belong to a type reference?
                                if (typeof(CodeTypeReferenceExpression) == fieldRef.TargetObject.GetType())
                                {
                                    string baseTypeName           = ((CodeTypeReferenceExpression)fieldRef.TargetObject).Type.BaseType;
                                    string nameOfTypeForField     = PascalCaseConverterHelper.GetPascalCaseName(baseTypeName);
                                    string nameOfTypeBeingChanged = memberExtension.Parent.ExtendedObject.Name;

                                    // Change the field name if it's changed.
                                    if (fieldRef.FieldName == oldName && nameOfTypeForField == nameOfTypeBeingChanged)
                                    {
                                        // Fix the field name first.
                                        fieldRef.FieldName = newName;

                                        // Also fix the name in the type reference.
                                        ((CodeTypeReferenceExpression)fieldRef.TargetObject).Type.BaseType = nameOfTypeForField;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            // Before adding the XmlEnumAttribute attribute to the CodeTypeMember
            // we have to make sure that the following attributes are not present.
            // If the 'XmlEnumAttribute' is already present the original value was not a valid name
            // and there should be no attempt to perform a rename.
            if (memberExtension.FindAttribute("System.Xml.Serialization.XmlAttributeAttribute") != null ||
                memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyElementAttribute") != null ||
                memberExtension.FindAttribute("System.Xml.Serialization.XmlAnyAttributeAttribute") != null ||
                memberExtension.FindAttribute("System.Xml.Serialization.XmlEnumAttribute") != null)
            {
                // We cannot proceed.
                return;
            }
            // Create a CodeAttributeDeclaration for XmlEnumAttribute attribute and
            // add it to the attributes collection.
            CodeAttributeDeclaration xmlEnum = new CodeAttributeDeclaration
                                                   ("System.Xml.Serialization.XmlEnumAttribute");

            xmlEnum.Arguments.Add(new CodeAttributeArgumentExtended("Name",
                                                                    new CodePrimitiveExpression(oldName), true));

            // Finally add it to the custom attributes collection.
            memberExtension.AddAttribute(xmlEnum);
        }