Exemple #1
0
        private EventDefinition FindEvent(MethodDefinition eventMethod, TypeDefinition type)
        {
            if (type == null)
            {
                throw new Exception("Can't find event named \"" + GetNameOfPropertyOrEvent(eventMethod.Name) + "\" in null type");
            }

            foreach (EventDefinition @event in type.Events)
            {
                if (@event.AddMethod == eventMethod || @event.RemoveMethod == eventMethod)
                {
                    return(@event);
                }
            }
            string eventName = "";

            if (AnalysisUtils.IsMethodExplicitlyImplemented(eventMethod))
            {
                eventName += eventMethod.Name.Substring(0, eventMethod.Name.LastIndexOf('.') + 1);
            }
            eventName += GetNameOfPropertyOrEvent(eventMethod.Name);
            EventDefinition newEvent = new EventDefinition(eventName, EventAttributes.None, eventMethod.Parameters[0].ParameterType);

            if (eventMethod.Name.Contains("add_"))
            {
                newEvent.AddMethod    = eventMethod;
                newEvent.RemoveMethod = GetSecondMethodOfPropertyOrEvent(eventMethod, type);
            }
            else
            {
                newEvent.AddMethod    = GetSecondMethodOfPropertyOrEvent(eventMethod, type);
                newEvent.RemoveMethod = eventMethod;
            }
            return(newEvent);
        }
Exemple #2
0
        /// <summary>
        /// Check if the method is an acual method, or a member of a property or event.
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        private MethodType IsMethodOrPropertyOrEvent(MethodDefinition method)
        {
            string methodName;

            if (AnalysisUtils.IsMethodExplicitlyImplemented(method))
            {
                methodName = method.Name.Substring(method.Name.LastIndexOf('.') + 1);
            }
            else
            {
                methodName = method.Name;
            }
            if (methodName.StartsWith("get_") || methodName.StartsWith("set_"))
            {
                return(MethodType.PROPERTY);
            }
            else if (methodName.StartsWith("add_") || methodName.StartsWith("remove_"))
            {
                return(MethodType.EVENT);
            }
            else
            {
                return(MethodType.METHOD);
            }
        }
Exemple #3
0
        private MethodSignature GetSignatureOfSecondMethodOfPropertyFromFirstMethod(MethodDefinition method)
        {
            bool          isExplicitlyImplemented = AnalysisUtils.IsMethodExplicitlyImplemented(method);
            string        name          = GetNameOfPropertyOrEvent(method.Name);
            string        fromInterface = "";
            string        returnType;
            bool          hasParameters;
            List <string> parameters = new List <string>();

            if (isExplicitlyImplemented)
            {
                fromInterface = method.Name.Substring(0, method.Name.LastIndexOf('.') + 1);
            }
            string newPrefix;

            if (method.Name.Contains("get_"))
            {
                newPrefix     = "set_";
                returnType    = "System.Void";
                hasParameters = true;
                if (method.HasParameters)
                {
                    foreach (ParameterDefinition param in method.Parameters)
                    {
                        parameters.Add(param.ParameterType.FullName);
                    }
                }
                parameters.Add(method.ReturnType.FullName);
            }
            else
            {
                newPrefix     = "get_";
                returnType    = method.Parameters[method.Parameters.Count - 1].ParameterType.FullName;
                hasParameters = (method.Parameters.Count > 1);
                if (hasParameters)
                {
                    for (int i = 0; i < method.Parameters.Count - 1; i++)
                    {
                        parameters.Add(method.Parameters[i].ParameterType.FullName);
                    }
                }
            }
            string fullName = fromInterface + newPrefix + name;

            return(new MethodSignature(fullName, returnType, hasParameters, parameters));
        }
Exemple #4
0
        //todo: change return type.
        private Tuple <AccessModifierEnum, AccessModifierEnum> AddMethodToImplementedMethodsSet(MethodDefinition method)
        {
            MethodType type          = IsMethodOrPropertyOrEvent(method);
            string     fromInterface = "";

            if (AnalysisUtils.IsMethodExplicitlyImplemented(method))
            {
                fromInterface = method.Name.Substring(0, method.Name.LastIndexOf('.') + 1);
            }
            if (_implementedMethods.ContainsKey(_parentClassAnalyzer._assemblyName))
            {
                if (_implementedMethods[_parentClassAnalyzer._assemblyName].ContainsKey(_parentClassAnalyzer.Element.FullName))
                {
                    if (type == MethodType.METHOD)
                    {
                        _implementedMethods[_parentClassAnalyzer._assemblyName][_parentClassAnalyzer.Element.FullName].Add(new MethodSignature(method));
                        return(new Tuple <AccessModifierEnum, AccessModifierEnum>(AccessModifierEnum.PUBLIC, AccessModifierEnum.PUBLIC));
                    }
                    else if (type == MethodType.PROPERTY)
                    {
                        string          propertyName      = GetNameOfPropertyOrEvent(method.Name);
                        MethodSignature methodToImplement = new MethodSignature(method);
                        MethodSignature secondMethod      = GetSignatureOfSecondMethodOfPropertyFromFirstMethod(method);
                        _implementedMethods[_parentClassAnalyzer._assemblyName][_parentClassAnalyzer.Element.FullName].Add(methodToImplement);
                        AccessModifierEnum secondMethodAccessModifierIfAny;
                        if ((secondMethodAccessModifierIfAny = IsPropertyMethodDefined(secondMethod, _parentClassAnalyzer.Element)) != AccessModifierEnum.NONE)
                        {
                            _implementedMethods[_parentClassAnalyzer._assemblyName][_parentClassAnalyzer.Element.FullName].Add(secondMethod);
                        }
                        if (method.Name.Contains("get_"))
                        {
                            return(new Tuple <AccessModifierEnum, AccessModifierEnum>(AccessModifierEnum.PUBLIC, secondMethodAccessModifierIfAny));
                        }
                        else if (method.Name.Contains("set_"))
                        {
                            return(new Tuple <AccessModifierEnum, AccessModifierEnum>(secondMethodAccessModifierIfAny, AccessModifierEnum.PUBLIC));
                        }
                    }
                    else if (type == MethodType.EVENT)
                    {
                        string eventName  = GetNameOfPropertyOrEvent(method.Name);
                        string returnType = method.Parameters[0].ParameterType.FullName;
                        _implementedMethods[_parentClassAnalyzer._assemblyName][_parentClassAnalyzer.Element.FullName].Add(new MethodSignature(fromInterface + "add_" + eventName, "System.Void", true, new List <string>()
                        {
                            returnType
                        }));
                        _implementedMethods[_parentClassAnalyzer._assemblyName][_parentClassAnalyzer.Element.FullName].Add(new MethodSignature(fromInterface + "remove_" + eventName, "System.Void", true, new List <string>()
                        {
                            returnType
                        }));
                    }
                }
                else
                {
                    if (type == MethodType.METHOD)
                    {
                        _implementedMethods[_parentClassAnalyzer._assemblyName].Add(_parentClassAnalyzer.Element.FullName, new HashSet <MethodSignature>()
                        {
                            new MethodSignature(method)
                        });
                    }
                    else if (type == MethodType.PROPERTY)
                    {
                        string          propertyName      = GetNameOfPropertyOrEvent(method.Name);
                        MethodSignature methodToImplement = new MethodSignature(method);
                        MethodSignature secondMethod      = GetSignatureOfSecondMethodOfPropertyFromFirstMethod(method);
                        _implementedMethods[_parentClassAnalyzer._assemblyName].Add(_parentClassAnalyzer.Element.FullName, new HashSet <MethodSignature>()
                        {
                            methodToImplement
                        });
                        AccessModifierEnum secondMethodAccessModifierIfAny;
                        if ((secondMethodAccessModifierIfAny = IsPropertyMethodDefined(secondMethod, _parentClassAnalyzer.Element)) != AccessModifierEnum.NONE)
                        {
                            _implementedMethods[_parentClassAnalyzer._assemblyName][_parentClassAnalyzer.Element.FullName].Add(secondMethod);
                        }
                        if (method.Name.Contains("get_"))
                        {
                            return(new Tuple <AccessModifierEnum, AccessModifierEnum>(AccessModifierEnum.PUBLIC, secondMethodAccessModifierIfAny));
                        }
                        else if (method.Name.Contains("set_"))
                        {
                            return(new Tuple <AccessModifierEnum, AccessModifierEnum>(secondMethodAccessModifierIfAny, AccessModifierEnum.PUBLIC));
                        }
                    }
                    else if (type == MethodType.EVENT)
                    {
                        string eventName  = GetNameOfPropertyOrEvent(method.Name);
                        string returnType = method.Parameters[0].ParameterType.FullName;
                        _implementedMethods[_parentClassAnalyzer._assemblyName].Add(_parentClassAnalyzer.Element.FullName, new HashSet <MethodSignature>()
                        {
                            new MethodSignature(fromInterface + "add_" + eventName, "System.Void", true, new List <string>()
                            {
                                returnType
                            }),
                            new MethodSignature(fromInterface + "remove_" + eventName, "System.Void", true, new List <string>()
                            {
                                returnType
                            })
                        });
                    }
                }
            }
            else
            {
                Dictionary <string, HashSet <MethodSignature> > typeDict = new Dictionary <string, HashSet <MethodSignature> >
                {
                    { _parentClassAnalyzer.Element.FullName, new HashSet <MethodSignature>() }
                };
                if (type == MethodType.METHOD)
                {
                    typeDict[_parentClassAnalyzer.Element.FullName].Add(new MethodSignature(method));
                    _implementedMethods.Add(_parentClassAnalyzer._assemblyName, typeDict);
                }
                else if (type == MethodType.PROPERTY)
                {
                    string          propertyName      = GetNameOfPropertyOrEvent(method.Name);
                    MethodSignature methodToImplement = new MethodSignature(method);
                    MethodSignature secondMethod      = GetSignatureOfSecondMethodOfPropertyFromFirstMethod(method);
                    typeDict[_parentClassAnalyzer.Element.FullName].Add(methodToImplement);
                    AccessModifierEnum secondMethodAccessModifierIfAny;
                    if ((secondMethodAccessModifierIfAny = IsPropertyMethodDefined(secondMethod, _parentClassAnalyzer.Element)) != AccessModifierEnum.NONE)
                    {
                        typeDict[_parentClassAnalyzer.Element.FullName].Add(secondMethod);
                    }
                    _implementedMethods.Add(_parentClassAnalyzer._assemblyName, typeDict);
                    if (method.Name.Contains("get_"))
                    {
                        return(new Tuple <AccessModifierEnum, AccessModifierEnum>(AccessModifierEnum.PUBLIC, secondMethodAccessModifierIfAny));
                    }
                    else if (method.Name.Contains("set_"))
                    {
                        return(new Tuple <AccessModifierEnum, AccessModifierEnum>(secondMethodAccessModifierIfAny, AccessModifierEnum.PUBLIC));
                    }
                }
                else if (type == MethodType.EVENT)
                {
                    string eventName  = GetNameOfPropertyOrEvent(method.Name);
                    string returnType = method.Parameters[0].ParameterType.FullName;
                    typeDict[_parentClassAnalyzer.Element.FullName].Add(new MethodSignature(fromInterface + "add_" + eventName, "System.Void", true, new List <string>()
                    {
                        returnType
                    }));
                    typeDict[_parentClassAnalyzer.Element.FullName].Add(new MethodSignature(fromInterface + "remove_" + eventName, "System.Void", true, new List <string>()
                    {
                        returnType
                    }));
                    _implementedMethods.Add(_parentClassAnalyzer._assemblyName, typeDict);
                }
            }
            return(new Tuple <AccessModifierEnum, AccessModifierEnum>(AccessModifierEnum.PUBLIC, AccessModifierEnum.PUBLIC));
        }
Exemple #5
0
 /// <summary>
 /// Check if we can use the current method.
 /// </summary>
 /// <returns></returns>
 public bool CanWorkOnElement()
 {
     try
     {
         if (Element == null)
         {
             return(false);
         }
         bool   isExplicitlyImplemented;
         string methodName;
         if (AnalysisUtils.IsMethodExplicitlyImplemented(Element))
         {
             int lastDotIndex = Element.Name.LastIndexOf('.');
             methodName = Element.Name.Substring(lastDotIndex + 1);
             string        interfaceFullName             = Element.Name.Substring(0, lastDotIndex);
             TypeReference interfaceWhereMethodIsDefined = GetInterfaceImplementedByParentFromFullName(AnalysisUtils.GetGenericTypeNameFromTypeName(interfaceFullName));
             if (interfaceWhereMethodIsDefined == null)
             {
                 isExplicitlyImplemented = false;
             }
             else
             {
                 string           interfaceAssemblyName = interfaceWhereMethodIsDefined.Scope.Name.Replace(".dll", "");
                 HashSet <string> interfaceMethods;
                 if (ClassAnalyzer.analyzeHelpher._coreSupportedMethods.ContainsType(interfaceWhereMethodIsDefined.Name, interfaceWhereMethodIsDefined.Namespace))
                 {
                     isExplicitlyImplemented = true;
                 }
                 else if (ClassAnalyzer.analyzeHelpher.IsTypeSupported(interfaceWhereMethodIsDefined))
                 {
                     //TODO : check if method is supported
                     isExplicitlyImplemented = true;
                 }
                 else if (_unsupportedMethods.ContainsKey(interfaceAssemblyName))
                 {
                     if (_unsupportedMethods[interfaceAssemblyName].TryGetValue(interfaceWhereMethodIsDefined.Name, out interfaceMethods))
                     {
                         isExplicitlyImplemented = interfaceMethods.Contains(methodName);
                     }
                     else
                     {
                         if (_parentClassAnalyzer._additionalTypesToImplement.TryGetValue(interfaceWhereMethodIsDefined, out interfaceMethods))
                         {
                             isExplicitlyImplemented = interfaceMethods.Contains(methodName);
                         }
                         else
                         {
                             isExplicitlyImplemented = false;
                         }
                     }
                 }
                 else
                 {
                     if (_parentClassAnalyzer._additionalTypesToImplement.TryGetValue(interfaceWhereMethodIsDefined, out interfaceMethods))
                     {
                         isExplicitlyImplemented = interfaceMethods.Contains(methodName);
                     }
                     else
                     {
                         isExplicitlyImplemented = false;
                     }
                 }
             }
         }
         else
         {
             methodName = Element.Name;
             isExplicitlyImplemented = false;
         }
         bool isNotAlreadyImplemented = !IsMethodAlreadyImplemented(Element);
         bool isUnsupported           = _unsupportedMethodsInCurrentType.Contains(methodName);
         bool isAccessible            = (_outputOptions.OutputOnlyPublicAndProtectedMembers && (Element.IsPublic || Element.IsFamily)) ||
                                        !_outputOptions.OutputOnlyPublicAndProtectedMembers;
         return(isNotAlreadyImplemented && isUnsupported && (isAccessible || isExplicitlyImplemented));
     }
     catch (Exception e)
     {
         System.IO.File.AppendAllText(
             System.IO.Path.Combine(Configuration.PathOfDirectoryWhereFileAreGenerated, "error.txt"),
             String.Format("Cannot generate {0}.{1}{2}", Element.DeclaringType.FullName, Element.Name, Environment.NewLine));
         return(false);
     }
 }