<commit_before>﻿#region Weavisor
// Arx One Aspects
// A simple post build weaving package
// https://github.com/ArxOne/Weavisor
// Release under MIT license http://opensource.org/licenses/mit-license.php
#endregion
namespace ArxOne.Weavisor.Weaver
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using Mono.Cecil;
    using Mono.Cecil.Cil;
    using Mono.Cecil.Rocks;
    using Utility;
    using ICustomAttributeProvider = Mono.Cecil.ICustomAttributeProvider;
    using MethodAttributes = Mono.Cecil.MethodAttributes;

    /// <summary>
    /// Aspect weaver core
    /// Pointcuts are identified here
    /// Advices are injected here
    /// </summary>
    internal class AspectWeaver
    {
        public Logger Logger { get; set; }
        public TypeResolver TypeResolver { get; set; }

        /// <summary>
        /// Weaves the specified module definition.
        /// </summary>
        /// <param name="moduleDefinition">The module definition.</param>
        public void Weave(ModuleDefinition moduleDefinition)
        {
            var adviceInterface = TypeResolver.Resolve(moduleDefinition, Binding.AdviceInterfaceName);
            if (adviceInterface == null)
            {
                Logger.WriteWarning("IAdvice interface not found here, exiting");
                return;
            }
            var weavableMethods = GetMethods(moduleDefinition, adviceInterface).ToArray();
            foreach (var method in weavableMethods)
                Weave(method);

            var runtimeInitializerInterface = TypeResolver.Resolve(moduleDefinition, Binding.RuntimeInitializerInterfaceName);
            if (GetMethods(moduleDefinition, runtimeInitializerInterface).Any())
                WeaveRuntimeInitializer(moduleDefinition);
        }

        private void WeaveRuntimeInitializer(ModuleDefinition moduleDefinition)
        {
            var moduleType = moduleDefinition.Types.Single(t => t.Name == "<Module>");

            const string cctorMethodName = ".cctor";
            var staticCtor = moduleType.Methods.SingleOrDefault(m => m.Name == cctorMethodName);
            if (staticCtor == null)
            {
                staticCtor = new MethodDefinition(cctorMethodName,
                           MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName |
                           MethodAttributes.RTSpecialName,
                           moduleDefinition.Import(typeof(void)));
                moduleType.Methods.Add(staticCtor);
            }

            var instructions = new Instructions(staticCtor.Body.Instructions);

            var invocationType = TypeResolver.Resolve(moduleDefinition, Binding.InvocationProceedTypeName);
            if (invocationType == null)
                return;
            var proceedRuntimeInitializersReference = invocationType.GetMethods().SingleOrDefault(m => m.IsStatic && m.Name == Binding.InvocationProcessRuntimeInitializersMethodName);
            if (proceedRuntimeInitializersReference == null)
                return;
            var proceedMethod = moduleDefinition.Import(proceedRuntimeInitializersReference);

            instructions.Emit(OpCodes.Call, moduleDefinition.Import(ReflectionUtility.GetMethodInfo(() => Assembly.GetExecutingAssembly())));
            instructions.Emit(OpCodes.Call, proceedMethod);
            instructions.Emit(OpCodes.Ret);
        }

        /// <summary>
        /// Weaves the specified method.
        /// </summary>
        /// <param name="method">The method.</param>
        private void Weave(MethodDefinition method)
        {
            Logger.WriteDebug("Weaving method '{0}'", method.FullName);

            var moduleDefinition = method.DeclaringType.Module;

            // create inner method
            Logger.WriteDebug("> attributes '{0}'", method.Attributes);
            const MethodAttributes attributesToKeep = MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.PInvokeImpl |
                                                      MethodAttributes.UnmanagedExport | MethodAttributes.HasSecurity |
                                                      MethodAttributes.RequireSecObject;
            var innerMethodAttributes = method.Attributes & attributesToKeep | MethodAttributes.Private;
            // first is fun, second is nice: hard to decide which naming style to pick
            //var innerMethodName = string.Format("<{0}>b", method.Name );
            string innerMethodName;
            innerMethodName = string.Format("{0}\u200B", method.Name);
            var innerMethod = new MethodDefinition(innerMethodName, innerMethodAttributes, method.ReturnType);
            innerMethod.GenericParameters.AddRange(method.GenericParameters.Select(p => p.Clone(innerMethod)));
            innerMethod.ImplAttributes = method.ImplAttributes;
            innerMethod.SemanticsAttributes = method.SemanticsAttributes;
            innerMethod.Body.InitLocals = false;//method.Body.InitLocals;
            innerMethod.Parameters.AddRange(method.Parameters);
            innerMethod.Body.Instructions.AddRange(method.Body.Instructions);
            innerMethod.Body.Variables.AddRange(method.Body.Variables);

            // now empty the old one and make it call the inner method...
            method.Body.Instructions.Clear();
            method.Body.Variables.Clear();
            var instructions = new Instructions(method.Body.Instructions);

            var isStatic = method.Attributes.HasFlag(MethodAttributes.Static);
            var firstParameter = isStatic ? 0 : 1;
<commit_msg>Special names for property getter and setter<commit_after>﻿#region Weavisor
// Arx One Aspects
// A simple post build weaving package
// https://github.com/ArxOne/Weavisor
// Release under MIT license http://opensource.org/licenses/mit-license.php
#endregion
namespace ArxOne.Weavisor.Weaver
{
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using Mono.Cecil;
    using Mono.Cecil.Cil;
    using Mono.Cecil.Rocks;
    using Utility;
    using ICustomAttributeProvider = Mono.Cecil.ICustomAttributeProvider;
    using MethodAttributes = Mono.Cecil.MethodAttributes;

    /// <summary>
    /// Aspect weaver core
    /// Pointcuts are identified here
    /// Advices are injected here
    /// </summary>
    internal class AspectWeaver
    {
        public Logger Logger { get; set; }
        public TypeResolver TypeResolver { get; set; }

        /// <summary>
        /// Weaves the specified module definition.
        /// </summary>
        /// <param name="moduleDefinition">The module definition.</param>
        public void Weave(ModuleDefinition moduleDefinition)
        {
            var adviceInterface = TypeResolver.Resolve(moduleDefinition, Binding.AdviceInterfaceName);
            if (adviceInterface == null)
            {
                Logger.WriteWarning("IAdvice interface not found here, exiting");
                return;
            }
            var weavableMethods = GetMethods(moduleDefinition, adviceInterface).ToArray();
            foreach (var method in weavableMethods)
                Weave(method);

            var runtimeInitializerInterface = TypeResolver.Resolve(moduleDefinition, Binding.RuntimeInitializerInterfaceName);
            if (GetMethods(moduleDefinition, runtimeInitializerInterface).Any())
                WeaveRuntimeInitializer(moduleDefinition);
        }

        private void WeaveRuntimeInitializer(ModuleDefinition moduleDefinition)
        {
            var moduleType = moduleDefinition.Types.Single(t => t.Name == "<Module>");

            const string cctorMethodName = ".cctor";
            var staticCtor = moduleType.Methods.SingleOrDefault(m => m.Name == cctorMethodName);
            if (staticCtor == null)
            {
                staticCtor = new MethodDefinition(cctorMethodName,
                           MethodAttributes.Private | MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.SpecialName |
                           MethodAttributes.RTSpecialName,
                           moduleDefinition.Import(typeof(void)));
                moduleType.Methods.Add(staticCtor);
            }

            var instructions = new Instructions(staticCtor.Body.Instructions);

            var invocationType = TypeResolver.Resolve(moduleDefinition, Binding.InvocationProceedTypeName);
            if (invocationType == null)
                return;
            var proceedRuntimeInitializersReference = invocationType.GetMethods().SingleOrDefault(m => m.IsStatic && m.Name == Binding.InvocationProcessRuntimeInitializersMethodName);
            if (proceedRuntimeInitializersReference == null)
                return;
            var proceedMethod = moduleDefinition.Import(proceedRuntimeInitializersReference);

            instructions.Emit(OpCodes.Call, moduleDefinition.Import(ReflectionUtility.GetMethodInfo(() => Assembly.GetExecutingAssembly())));
            instructions.Emit(OpCodes.Call, proceedMethod);
            instructions.Emit(OpCodes.Ret);
        }

        /// <summary>
        /// Weaves the specified method.
        /// </summary>
        /// <param name="method">The method.</param>
        private void Weave(MethodDefinition method)
        {
            Logger.WriteDebug("Weaving method '{0}'", method.FullName);

            var moduleDefinition = method.DeclaringType.Module;

            // create inner method
            Logger.WriteDebug("> attributes '{0}'", method.Attributes);
            const MethodAttributes attributesToKeep = MethodAttributes.Static | MethodAttributes.HideBySig | MethodAttributes.PInvokeImpl |
                                                      MethodAttributes.UnmanagedExport | MethodAttributes.HasSecurity |
                                                      MethodAttributes.RequireSecObject;
            var innerMethodAttributes = method.Attributes & attributesToKeep | MethodAttributes.Private;
            string innerMethodName;
            if (method.IsGetter)
                innerMethodName = string.Format("\u200B{0}.get", method.Name.Substring(4));
            else if(method.IsSetter)
                innerMethodName = string.Format("\u200B{0}.set", method.Name.Substring(4));
            else
            innerMethodName = string.Format("{0}\u200B", method.Name);
            var innerMethod = new MethodDefinition(innerMethodName, innerMethodAttributes, method.ReturnType);
            innerMethod.GenericParameters.AddRange(method.GenericParameters.Select(p => p.Clone(innerMethod)));
            innerMethod.ImplAttributes = method.ImplAttributes;
            innerMethod.SemanticsAttributes = method.SemanticsAttributes;
            innerMethod.Body.InitLocals = false;//method.Body.InitLocals;
            innerMethod.Parameters.AddRange(method.Parameters);
            innerMethod.Body.Instructions.AddRange(method.Body.Instructions);
            innerMethod.Body.Variables.AddRange(method.Body.Variables);

            // now empty the old one and make it call the inner method...
            method.Body.Instructions.Clear();
            method.Body.Variables.Clear();
            var instructions = new Instructions(method.Body.Instructions);

            var isStatic = method.Attributes.HasFlag(MethodAttributes.Static);
            var firstParameter = isStatic ? 0 : 1;

{'fraction_non_alphanumeric': 0.09126569037656904, 'fraction_numerical': 0.0023535564853556486, 'mean_word_length': 2.3953832494820952, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 38, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3731801', 'n_tokens_mistral': 2723, 'n_tokens_neox': 2671, 'n_words': 563}