<commit_before>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ArgumentWeaver.cs" company="Catel development team">
//   Copyright (c) 2008 - 2013 Catel development team. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------


namespace Catel.Fody.Weaving.Argument
{
    using Mono.Cecil;

    public class ArgumentWeaver
    {
        #region Fields
        private readonly TypeDefinition _typeDefinition;
        #endregion

        #region Constructors
        public ArgumentWeaver(TypeDefinition typeDefinition)
        {
            _typeDefinition = typeDefinition;
        }
        #endregion

        #region Methods
        public void Execute()
        {
            foreach (var method in _typeDefinition.Methods)
            {
                ProcessMethod(method);
            }
        }

        private void ProcessMethod(MethodDefinition method)
        {
            // TODO: Implement
        }
        #endregion
    }
}<commit_msg>Very first draft of ArgumentWeaver<commit_after>﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ArgumentWeaver.cs" company="Catel development team">
//   Copyright (c) 2008 - 2013 Catel development team. All rights reserved.
// </copyright>
// --------------------------------------------------------------------------------------------------------------------

namespace Catel.Fody.Weaving.Argument
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    using Mono.Cecil;
    using Mono.Cecil.Cil;
    using Mono.Cecil.Rocks;
    using Mono.Collections.Generic;

    public class ArgumentWeaver
    {
        #region Fields
        private static readonly Dictionary<string, Action<CustomAttribute, MethodBody, ParameterDefinition, int>> ArgumentWaveActions = new Dictionary<string, Action<CustomAttribute, MethodBody, ParameterDefinition, int>> { { "Catel.Fody.NotNullAttribute", NotNullWeave } };
 
        private readonly TypeDefinition _typeDefinition;
        #endregion

        #region Constructors
        public ArgumentWeaver(TypeDefinition typeDefinition)
        {
            _typeDefinition = typeDefinition;
        }
        #endregion

        #region Methods
        /// <summary>
        /// TODO: Create a weaver class instaed this and register it into the dictionary.
        /// </summary>
        /// <param name="customAttribute"></param>
        /// <param name="body"></param>
        /// <param name="parameter"></param>
        /// <param name="parameterIdx"></param>
        private static void NotNullWeave(CustomAttribute customAttribute, MethodBody body, ParameterDefinition parameter, int parameterIdx)
        {
            TypeDefinition argumentTypeDefinition = FodyEnvironment.ModuleDefinition.GetType("Catel.Argument");
            MethodDefinition methodDefinition = argumentTypeDefinition.Methods.FirstOrDefault(definition => definition.Name == "IsNotNull" && definition.Parameters.Count == 2);

            body.SimplifyMacros();

            Collection<Instruction> instructions = body.Instructions;
            instructions.Insert(0, Instruction.Create(OpCodes.Nop), Instruction.Create(OpCodes.Ldstr, parameter.Name), Instruction.Create(OpCodes.Ldarg_S, parameterIdx), Instruction.Create(OpCodes.Call, methodDefinition));

            body.OptimizeMacros();
        }

        public void Execute()
        {
            foreach (var method in _typeDefinition.Methods)
            {
                ProcessMethod(method);
            }
        }

        private void ProcessMethod(MethodDefinition method)
        {
            int parameterIdx = 0;
            foreach (ParameterDefinition parameter in method.Parameters)
            {
                foreach (CustomAttribute customAttribute in parameter.CustomAttributes)
                {
                    string attributeFullName = customAttribute.AttributeType.FullName;
                    if (ArgumentWaveActions.ContainsKey(attributeFullName))
                    {
                        ArgumentWaveActions[attributeFullName].Invoke(customAttribute, method.Body, parameter, parameterIdx);
                        parameter.RemoveAttribute(attributeFullName);
                    }
                }

                parameterIdx++;
            }
        }

        #endregion
    }
}
{'fraction_non_alphanumeric': 0.1801269424381703, 'fraction_numerical': 0.0041584591814401405, 'mean_word_length': 1.6204128440366972, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1783807', 'n_tokens_mistral': 1010, 'n_tokens_neox': 960, 'n_words': 226}