示例#1
0
        /// <summary>
        /// Creates a MethodSpecifier.
        /// </summary>
        /// <param name="name">Name of the method without any prefixes.</param>
        /// <param name="arguments">Specifiers for the arguments of the method.</param>
        /// <param name="returnTypes">Specifiers for the return types of the method.</param>
        /// <param name="modifiers">Modifiers of the method.</param>
        /// <param name="declaringType">Specifier for the type this method is contained in.</param>
        /// <param name="genericArguments">Generic arguments this method takes.</param>
        public MethodSpecifier
        (
            string name,
            IEnumerable <MethodParameter> arguments,
            IEnumerable <BaseType> returnTypes,
            MethodModifiers modifiers,
            MemberVisibility visibility,
            TypeSpecifier declaringType,
            IEnumerable <BaseType> genericArguments
        )
        {
            this.Name             = name;
            this.DeclaringType    = declaringType;
            this.Parameters       = arguments.ToList();
            this.ReturnTypes      = returnTypes.ToList();
            this.Modifiers        = modifiers;
            this.Visibility       = visibility;
            this.GenericArguments = genericArguments.ToList();

            this.HashCodeCache = HashCode.Combine
                                 (
                this.Name,
                this.Modifiers,
                string.Join(",", this.GenericArguments),
                string.Join(",", this.ReturnTypes),
                string.Join(",", this.Parameters),
                this.Visibility,
                this.DeclaringType
                                 );
        }
        private MethodInfo ResolveMethodInternal(Type clazz, String methodName, MethodModifiers methodModifiers)
        {
            MethodInfo[] methods      = clazz.GetMethods();
            MethodInfo   methodByName = null;

            // check each method by name
            foreach (MethodInfo method in methods.Where(m => m.Name == methodName))
            {
                if (methodByName != null)
                {
                    throw new EngineImportException(string.Format("Ambiguous method name: method by name '{0}' is overloaded in class '{1}'", methodName, clazz.FullName));
                }

                var isPublic = method.IsPublic;
                var isStatic = method.IsStatic;
                if (methodModifiers.AcceptsPublicFlag(isPublic) && methodModifiers.AcceptsStaticFlag(isStatic))
                {
                    methodByName = method;
                }
            }

            if (methodByName == null)
            {
                throw new EngineImportException("Could not find " + methodModifiers.Text + " method named '" + methodName + "' in class '" + clazz.FullName + "'");
            }
            return(methodByName);
        }
        private MethodInfo ResolveMethodInternalCheckOverloads(
            Type clazz,
            string methodName,
            MethodModifiers methodModifiers)
        {
            MethodInfo[]      methods      = clazz.GetMethods();
            ISet <MethodInfo> overloadeds  = null;
            MethodInfo        methodByName = null;

            // check each method by name, add to overloads when multiple methods for the same name
            foreach (var method in methods)
            {
                if (method.Name.Equals(methodName))
                {
                    bool isPublic = method.IsPublic;
                    bool isStatic = method.IsStatic;
                    if (methodModifiers.AcceptsPublicFlag(isPublic) && methodModifiers.AcceptsStaticFlag(isStatic))
                    {
                        if (methodByName != null)
                        {
                            if (overloadeds == null)
                            {
                                overloadeds = new HashSet <MethodInfo>();
                            }

                            overloadeds.Add(method);
                        }
                        else
                        {
                            methodByName = method;
                        }
                    }
                }
            }

            if (methodByName == null)
            {
                throw new ImportException("Could not find " + methodModifiers.Text + " method named '" + methodName + "' in class '" + clazz.Name + "'");
            }

            if (overloadeds == null)
            {
                return(methodByName);
            }

            // determine that all overloads have the same result type
            foreach (var overloaded in overloadeds)
            {
                if (overloaded.ReturnType != methodByName.ReturnType)
                {
                    throw new ImportException(
                              "Method by name '" + methodName + "' is overloaded in class '" + clazz.Name + "' and overloaded methods do not return the same type");
                }
            }

            return(methodByName);
        }
示例#4
0
 private MethodMetadata(MethodBase method)
 {
     Name             = method.Name;
     Extension        = EmitExtension(method);
     ReturnType       = EmitReturnType(method);
     GenericArguments = !method.IsGenericMethodDefinition ? null : EmitGenericArguments(method);
     Parameters       = EmitParameters(method);
     Modifiers        = EmitModifiers(method);
 }
 public MethodModifiers Except(MethodModifiers modifiersToRemoveIfEqual)
 {
     return(new MethodModifiers(
                Visibility == modifiersToRemoveIfEqual.Visibility ? 0 : Visibility,
                Static & !modifiersToRemoveIfEqual.Static,
                Abstract & !modifiersToRemoveIfEqual.Abstract,
                Virtual & !modifiersToRemoveIfEqual.Virtual,
                Final & !modifiersToRemoveIfEqual.Final,
                Override & !modifiersToRemoveIfEqual.Override));
 }
示例#6
0
        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (parameter is MethodModifiers mask)
            {
                modifiers ^= mask;
                return(modifiers);
            }

            return(MethodModifiers.None);
        }
示例#7
0
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            if (value is MethodModifiers m && parameter is MethodModifiers mask)
            {
                modifiers = m;
                return((mask & m) != 0);
            }

            return(MethodModifiers.None);
        }
示例#8
0
 public MethodSpecifier(string name, IEnumerable <BaseType> arguments,
                        IEnumerable <BaseType> returnTypes, MethodModifiers modifiers, TypeSpecifier declaringType,
                        IList <BaseType> genericArguments)
 {
     Name             = name;
     DeclaringType    = declaringType;
     Arguments        = arguments.ToList();
     ReturnTypes      = returnTypes.ToList();
     Modifiers        = modifiers;
     GenericArguments = genericArguments.ToList();
 }
示例#9
0
        public async Task ModifiersReturnsExpectedValue(string modifiers, MethodModifiers expected)
        {
            var declaringType = Substitute.For <IClassDefinition>();

            var node = await TestNode
                       .FindNode <MethodDeclarationSyntax>(
                MethodDefinitionCode.ClassWithMethod.Replace("public string", "public " + modifiers + " string"))
                       .ConfigureAwait(false);

            var sut = new MethodDefinition(declaringType, node);

            sut.Modifiers.Should().Be(expected);
        }
示例#10
0
        public static MethodSpecifier MethodSpecifierFromSymbol(IMethodSymbol method)
        {
            MethodModifiers modifiers = MethodModifiers.Private;

            if (method.IsPublic())
            {
                modifiers |= MethodModifiers.Public;
            }

            if (method.IsVirtual)
            {
                modifiers |= MethodModifiers.Virtual;
            }

            if (method.IsSealed)
            {
                modifiers |= MethodModifiers.Sealed;
            }

            if (method.IsAbstract)
            {
                modifiers |= MethodModifiers.Abstract;
            }

            if (method.IsStatic)
            {
                modifiers |= MethodModifiers.Static;
            }

            // TODO: Protected / Internal

            BaseType[] returnTypes = method.ReturnsVoid ?
                new BaseType[] { } :
                new BaseType[] { BaseTypeSpecifierFromSymbol(method.ReturnType) };

            Named<BaseType>[] parameterTypes = method.Parameters.Select(
                p => NamedBaseTypeSpecifierFromSymbol(p)).ToArray();

            BaseType[] genericArgs = method.TypeParameters.Select(
                p => BaseTypeSpecifierFromSymbol(p)).ToArray();

            return new MethodSpecifier(
                method.Name,
                parameterTypes,
                returnTypes,
                modifiers,
                TypeSpecifierFromSymbol(method.ContainingType),
                genericArgs);
        }
        public void CalculateChangesReturnsResultsForAddedElementDefinitions(MethodModifiers modifiers,
                                                                             SemVerChangeType expected)
        {
            var oldItems = Array.Empty <IMethodDefinition>();
            var newItem  = new TestMethodDefinition().Set(x =>
            {
                x.IsVisible = true;
                x.Modifiers = modifiers;
            });
            var newItems = new List <IMethodDefinition>
            {
                newItem
            };
            var options      = ComparerOptions.Default;
            var matchResults = new MatchResults <IMethodDefinition>(Array.Empty <IMethodDefinition>(),
                                                                    newItems);

            Service <IMethodEvaluator>().FindMatches(oldItems, newItems).Returns(matchResults);

            var actual = SUT.CalculateChanges(oldItems, newItems, options).ToList();

            actual.Should().HaveCount(1);
            actual[0].ChangeType.Should().Be(expected);
        }
示例#12
0
 /// <summary>
 /// <paramref name="access"/> <paramref name="modifiers"/> <paramref name="retType"/> <paramref name="methodName"/> (<paramref name="parameters"/>)
 /// {
 /// </summary>
 /// <param name="access">Access modifiers</param>
 /// <param name="modifiers">Method modifiers</param>
 /// <param name="retType">Return typename</param>
 /// <param name="methodName">Name of method</param>
 /// <param name="parameters">Optional parameters.</param>
 public void StartMethod(AccessModifiers access, MethodModifiers modifiers, string retType, string methodName, params (string name, string type)[]? parameters)
示例#13
0
 public static string ToCode(this MethodModifiers modifiers)
 => modifiers == MethodModifiers.None ? String.Empty : modifiers.ToString().ToLowerInvariant();