private bool ShouldUseWrapper(MethodDefDeclaration targetMethod)
                {
                    if (targetMethod.Name == "ToString")
                    {
                        MethodDefDeclaration parent = targetMethod.GetParentDefinition(true);
                        if (parent != null)
                        {
                            if (parent.MatchesReference(
                                    this.transformationInstance.parent.assets.ToStringMethodSignature))
                            {
                                return(true);
                            }
                        }
                    }

                    for (int i = 0; i < targetMethod.Parameters.Count; i++)
                    {
                        ITypeSignature parameterType = targetMethod.Parameters[i].ParameterType;

                        if (!parameterType.BelongsToClassification(TypeClassifications.Intrinsic) ||
                            IntrinsicTypeSignature.Is(parameterType, IntrinsicType.Object))
                        {
                            return(true);
                        }
                    }

                    return(false);
                }
Exemple #2
0
        public void Initialize(ModuleDeclaration module)
        {
            this.loggingImplementation = new LoggingImplementationTypeBuilder(module);
            this.formatWriter          = new StringFormatWriter(module);

            this.loggerType = module.FindType(typeof(Logger));

            Predicate <MethodDefDeclaration> singleMessagePredicate =
                method => method.Parameters.Count == 1 &&
                IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String);

            this.categoryInitializerMethod = module.FindMethod(module.FindType(typeof(LogManager)), "GetLogger", singleMessagePredicate);

            this.writeDebugMethod = module.FindMethod(this.loggerType, "Trace", singleMessagePredicate);
            this.writeInfoMethod  = module.FindMethod(this.loggerType, "Info", singleMessagePredicate);
            this.writeWarnMethod  = module.FindMethod(this.loggerType, "Warn", singleMessagePredicate);
            this.writeErrorMethod = module.FindMethod(this.loggerType, "Error", singleMessagePredicate);
            this.writeFatalMethod = module.FindMethod(this.loggerType, "Fatal", singleMessagePredicate);

            this.getIsTraceEnabledMethod = module.FindMethod(this.loggerType, "get_IsTraceEnabled");
            this.getIsInfoEnabledMethod  = module.FindMethod(this.loggerType, "get_IsInfoEnabled");
            this.getIsWarnEnabledMethod  = module.FindMethod(this.loggerType, "get_IsWarnEnabled");
            this.getIsErrorEnabledMethod = module.FindMethod(this.loggerType, "get_IsErrorEnabled");
            this.getIsFatalEnabledMethod = module.FindMethod(this.loggerType, "get_IsFatalEnabled");
        }
        public StringFormatWriter(ModuleDeclaration module)
        {
            this.module = module;
            ITypeSignature stringType = module.Cache.GetType(typeof(string));

            this.format1Method = module.FindMethod(stringType, "Format",
                                                   method => method.Parameters.Count == 2 &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object));

            this.format2Method = module.FindMethod(stringType, "Format",
                                                   method => method.Parameters.Count == 3 &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object));

            this.format3Method = module.FindMethod(stringType, "Format",
                                                   method => method.Parameters.Count == 4 &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object) &&
                                                   IntrinsicTypeSignature.Is(method.Parameters[3].ParameterType, IntrinsicType.Object));

            this.formatArrayMethod = module.FindMethod(stringType, "Format",
                                                       method => method.Parameters.Count == 2 &&
                                                       IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                                       method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));
        }
                private string CreateMessageFormatString(LogOptions logOption, MethodDefDeclaration targetMethod)
                {
                    StringBuilder formatBuilder = new StringBuilder();

                    formatBuilder.AppendFormat("{0}.{1}", targetMethod.DeclaringType, targetMethod.Name);
                    formatBuilder.Append("(");

                    int parameterCount = Context.MethodMapping.MethodSignature.ParameterCount;

                    for (int i = 0; i < parameterCount; i++)
                    {
                        if (i > 0)
                        {
                            formatBuilder.Append(", ");
                        }

                        ITypeSignature parameterType = Context.MethodMapping.MethodSignature.GetParameterType(i);
                        if ((logOption & LogOptions.IncludeParameterType) != 0)
                        {
                            formatBuilder.Append(parameterType.ToString());
                            formatBuilder.Append(' ');
                        }

                        if ((logOption & LogOptions.IncludeParameterName) != 0)
                        {
                            formatBuilder.Append(Context.MethodMapping.MethodMappingInformation.GetParameterName(i));
                            formatBuilder.Append(' ');
                        }

                        if ((logOption & LogOptions.IncludeParameterValue) != 0)
                        {
                            formatBuilder.AppendFormat("= ");

                            if (IntrinsicTypeSignature.Is(parameterType, IntrinsicType.String))
                            {
                                formatBuilder.AppendFormat("\"" + "{{{0}}}" + "\"", i);
                            }
                            else
                            {
                                formatBuilder.AppendFormat("{{{0}}}", i);
                            }
                        }
                    }

                    formatBuilder.Append(")");

                    return(formatBuilder.ToString());
                }
Exemple #5
0
            public ConsoleCategoryBuilder(ConsoleBackend parent, ModuleDeclaration module)
            {
                this.parent = parent;
                this.module = module;

                this.writeLineMessage = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 1 &&
                    IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

                this.writeLineFormat1 = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 2 &&
                    IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                    IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object));

                this.writeLineFormat2 = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 3 &&
                    IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                    IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                    IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object));

                this.writeLineFormat3 = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 4 &&
                    IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                    IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                    IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object) &&
                    IntrinsicTypeSignature.Is(method.Parameters[3].ParameterType, IntrinsicType.Object));

                this.writeLineFormat4 = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 5 &&
                    IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                    IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                    IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object) &&
                    IntrinsicTypeSignature.Is(method.Parameters[3].ParameterType, IntrinsicType.Object) &&
                    IntrinsicTypeSignature.Is(method.Parameters[4].ParameterType, IntrinsicType.Object));

                this.writeLineFormatArray = module.FindMethod(
                    module.Cache.GetType(typeof(System.Console)), "WriteLine",
                    method => method.Parameters.Count == 2 &&
                    IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                    method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));
            }
        public void Initialize(ModuleDeclaration module)
        {
            this.module = module;
            this.loggingImplementation = new LoggingImplementationTypeBuilder(module);

            this.loggerType = module.FindType(typeof(ILog));
            LoggerMethodsBuilder builder = new LoggerMethodsBuilder(module, this.loggerType);

            this.categoryInitializerMethod = module.FindMethod(module.FindType(typeof(LogManager)), "GetLogger",
                                                               method => method.Parameters.Count == 1 &&
                                                               IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

            this.loggerMethods[LogLevel.Debug]   = builder.CreateLoggerMethods("Debug");
            this.loggerMethods[LogLevel.Info]    = builder.CreateLoggerMethods("Info");
            this.loggerMethods[LogLevel.Warning] = builder.CreateLoggerMethods("Warn");
            this.loggerMethods[LogLevel.Error]   = builder.CreateLoggerMethods("Error");
            this.loggerMethods[LogLevel.Fatal]   = builder.CreateLoggerMethods("Fatal");
        }
 private static void AppendFormatPlaceholder(int i, StringBuilder formatBuilder, ITypeSignature parameterType)
 {
     if (IntrinsicTypeSignature.Is(parameterType, IntrinsicType.String))
     {
         formatBuilder.AppendFormat("\"" + "{{{0}}}" + "\"", i);
     }
     else if (!parameterType.BelongsToClassification(TypeClassifications.Intrinsic) ||
              IntrinsicTypeSignature.Is(parameterType, IntrinsicType.Object))
     {
         formatBuilder.Append("{{");
         formatBuilder.AppendFormat("{{{0}}}", i);
         formatBuilder.Append("}}");
     }
     else
     {
         formatBuilder.AppendFormat("{{{0}}}", i);
     }
 }
        public LoggingImplementationTypeBuilder(ModuleDeclaration module)
        {
            this.module         = module;
            this.categoryFields = new Dictionary <string, FieldDefDeclaration>();
            this.wrapperMethods = new Dictionary <IMethod, MethodDefDeclaration>();

            this.stringFormatArrayMethod = module.FindMethod(module.Cache.GetIntrinsic(IntrinsicType.String), "Format",
                                                             method => method.Parameters.Count == 2 &&
                                                             IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                                             method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));

            this.traceWriteLineMethod = module.FindMethod(module.Cache.GetType(typeof(System.Diagnostics.Trace)), "WriteLine",
                                                          method => method.Parameters.Count == 1 &&
                                                          IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

            this.weavingHelper      = new WeavingHelper(module);
            this.implementationType = this.CreateContainingType();
            this.isLoggingField     = this.CreateIsLoggingField();
        }
        public void Initialize(ModuleDeclaration module)
        {
            this.module       = module;
            this.formatWriter = new StringFormatWriter(module);

            ITypeSignature traceTypeSignature = module.Cache.GetType(typeof(System.Diagnostics.Trace));

            this.writeLineString = module.FindMethod(traceTypeSignature, "WriteLine",
                                                     method => method.Parameters.Count == 1 &&
                                                     IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

            this.traceInfoString = module.FindMethod(traceTypeSignature, "TraceInformation",
                                                     method => method.Parameters.Count == 1 &&
                                                     IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

            this.traceInfoFormat = module.FindMethod(traceTypeSignature, "TraceInformation",
                                                     method => method.Parameters.Count == 2 &&
                                                     IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                                     method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));

            this.traceWarningString = module.FindMethod(traceTypeSignature, "TraceWarning",
                                                        method => method.Parameters.Count == 1 &&
                                                        IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

            this.traceWarningFormat = module.FindMethod(traceTypeSignature, "TraceWarning",
                                                        method => method.Parameters.Count == 2 &&
                                                        IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                                        method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));

            this.traceErrorString = module.FindMethod(traceTypeSignature, "TraceError",
                                                      method => method.Parameters.Count == 1 &&
                                                      IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String));

            this.traceErrorFormat = module.FindMethod(traceTypeSignature, "TraceError",
                                                      method => method.Parameters.Count == 2 &&
                                                      IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                                      method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array));
        }
        public EqualsInjection(Project project)
        {
            this.objectType  = project.Module.Cache.GetIntrinsic(IntrinsicType.Object);
            this.booleanType = project.Module.Cache.GetIntrinsic(IntrinsicType.Boolean);

            this.objectTypeDef = this.objectType.GetTypeDefinition();

            this.referenceEqualsMethod = project.Module.FindMethod(objectTypeDef, "ReferenceEquals");
            this.instanceEqualsMethod  = project.Module.FindMethod(objectTypeDef, "Equals", 1);
            this.staticEqualsMethod    = project.Module.FindMethod(objectTypeDef, "Equals", 2);
            this.getTypeMethod         = project.Module.FindMethod(objectTypeDef, "GetType");

            var collectionHelperTypeDef = project.Module.Cache.GetType(typeof(CollectionHelper)).GetTypeDefinition();

            this.collectionEqualsMethod = project.Module.FindMethod(collectionHelperTypeDef, "Equals",
                                                                    declaration => declaration.IsStatic);

            var typeTypeDef = project.Module.Cache.GetType(typeof(Type)).GetTypeDefinition();

            this.getTypeFromHandleMethod = project.Module.FindMethod(typeTypeDef, "GetTypeFromHandle");

            this.equatableInterface = project.Module.Cache.GetType(typeof(IEquatable <>));
        }
Exemple #11
0
        public LoggerMethodsBuilder(ModuleDeclaration module, ITypeSignature loggerType)
        {
            this.module     = module;
            this.loggerType = loggerType;

            // matches XXX(string)
            this.objectPredicate = method => method.Parameters.Count == 1 &&
                                   IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.Object);

            // matches XXX(string format, object arg)
            this.format1Predicate = method => method.Parameters.Count == 2 &&
                                    IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                    IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object);

            // matches XXX(string format, object arg0, object arg1)
            this.format2Predicate = method => method.Parameters.Count == 3 &&
                                    IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                    IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                                    IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object);

            // matches XXX(string format, object arg0, object arg1, object arg2)
            this.format3Predicate = method => method.Parameters.Count == 4 &&
                                    IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                    IntrinsicTypeSignature.Is(method.Parameters[1].ParameterType, IntrinsicType.Object) &&
                                    IntrinsicTypeSignature.Is(method.Parameters[2].ParameterType, IntrinsicType.Object) &&
                                    IntrinsicTypeSignature.Is(method.Parameters[3].ParameterType, IntrinsicType.Object);

            // matches XXX(string format, params object[] args)
            this.formatArrayPredicate = method => method.Parameters.Count == 2 &&
                                        IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.String) &&
                                        method.Parameters[1].ParameterType.BelongsToClassification(TypeClassifications.Array);

            this.objectExceptionPredicate = method => method.Parameters.Count == 2 &&
                                            IntrinsicTypeSignature.Is(method.Parameters[0].ParameterType, IntrinsicType.Object) &&
                                            method.Parameters[1].ParameterType.MatchesReference(module.Cache.GetType(typeof(Exception)));
        }
 private bool ShouldLogReturnType(LogOptions logOptions)
 {
     return((logOptions & LogOptions.IncludeReturnValue) != 0 &&
            !IntrinsicTypeSignature.Is(this.context.MethodMapping.MethodSignature.ReturnType, IntrinsicType.Void));
 }