/// <summary>
        /// if the child class already has a constructor, 
        /// a new parameter will be added to the constructor
        /// </summary>
        /// <param name="oldConstructor">constructor to which
        /// we are going to add the new parameter</param>
        /// <returns>the constructor with the additional new parameter</returns>
        public ConstructorDeclarationSyntax ExtendExistingConstructor(
            ConstructorDeclarationSyntax oldConstructor)
        {
            if (oldConstructor.IsStatic())
                return oldConstructor;

            var parameterName = _mixin.Name.ConvertFieldNameToParameterName();
            // if there is already a parameter with the same name, skip further processing
            var alreadyHasParameter = oldConstructor.ParameterList.Parameters.Any(x => x.Identifier.Text == parameterName);
            if (alreadyHasParameter)
                return oldConstructor;

            // first rule: extend the constructors parameter list
            var parameter = CreateConstructorParameterForMixin(parameterName);
            var newConstructor = oldConstructor.AddParameterListParameters(parameter);
            // second rule: check for initializer
            // if we have no initializer or a base initializer, do the assignment in this constructor
            // but do not delegate the parameter to further constructors
            var initializer = oldConstructor.Initializer;
            if(initializer == null || initializer.IsKind(SyntaxKind.BaseConstructorInitializer))
            {
                newConstructor = newConstructor
                    .AddBodyStatements(CreateAssigmentStatementForConstructorBody(parameterName));
            }
            return newConstructor;
        }  
 public override SyntaxNode VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
 {
     node = (ConstructorDeclarationSyntax)base.VisitConstructorDeclaration(node);
     if(!node.IsStatic())  // ignore static constructors
         node = _injectMixinIntoConstructor.ExtendExistingConstructor(node);
     // remember that class already has a constructor,
     // so no need to create a new one
     _SourceClassHasConstructor = true;
     return node;
 }
Exemplo n.º 3
0
        public override void VisitConstructorDeclaration(ConstructorDeclarationSyntax node)
        {
            // ignore static constructors
            if (node.IsStatic())
                return;

            var constructor = new Constructor();
            // read parameters
            var parameterSyntaxReader = new ParameterSyntaxReader(constructor, _semantic);
            parameterSyntaxReader.Visit(node);
            _constructors.AddConstructor(constructor);
            base.VisitConstructorDeclaration(node);
        }